-
Notifications
You must be signed in to change notification settings - Fork 550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for returning result rows as structs. (:as => :struct) #1011
base: master
Are you sure you want to change the base?
Conversation
…s struct is enabled
Fixed for those failed tests, now going to update the unit tests for statements, like in the other PR. |
ext/mysql2/result.c
Outdated
@@ -826,11 +861,19 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) { | |||
} | |||
|
|||
symbolizeKeys = RTEST(rb_hash_aref(opts, sym_symbolize_keys)); | |||
asArray = rb_hash_aref(opts, sym_as) == sym_array; | |||
rowsAs = AS_HASH; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: make this the else
case in the block below. (or the default
case, and convert to a switch
statement)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
} | ||
} | ||
|
||
if (args->rowsAs == AS_STRUCT) { | ||
rowVal = cast_row_as_struct(self, rowVal, wrapper); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still using an intermediary array in the row fetch loop above. Is that better/more efficient to create the struct once than to incrementally add members to the struct in the same way elements are pushed into the array or added into the hash? I think the latter would have more clarity / all three options being handled as cases in the same loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to avoid modifying the the main loop for clarity! It also allowed me to factor the "cast_row_as_struct" code with a simple parameter list. The statement support added a lot of duplicate code and I really wanted to not contribute to that. Also, the struct.new native call has a nice array form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to rethink this if you think it's critical.
rowVal = rb_hash_new(); | ||
} else /* array or struct */ { | ||
/* struct uses array as an intermediary */ | ||
rowVal = rb_ary_new2(wrapper->numberOfFields); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These don't get GC marked, why does the struct variant need to be GC marked? (w->rowStruct
above) (Or conversely, should these also get GC marked?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I admit I'm hazy on when something needs to be gc-marked and when it doesn't. The "prototype" struct needs to be marked so that it'll stick around while control hops between the gem and ruby, while enumerating the results. Each row result struct, tho', is an instance of this prototype struct and is made right here, just like the hash and array, and returned to the client asap and will be gc marked there. That's my understanding anyway.
This is also awesome. Few comments. Thank you for contributing upstream! |
This patch adds a new query option to return result rows as structs.
At Bandcamp we've been using this patch (and the other) in our fork of the mysql2 gem since the beginning. We think dot notation is cleaner and more object-oriented-looking, and wanted to use it in our code that processes query results. I believe it can also make the code faster if it's doing a lot of column accesses.
This is a default for all our queries:
And query results can be used like this:
I've been meaning to make these two pull requests since forever. We've had years of experience with them and think they're useful. If you have any questions about how Bandcamp's Linux/MySQL/Ruby stack works, just ask.