Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

joe07734
Copy link

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:

opts = Mysql2::Client.default_query_options
opts[:as] = :struct

And query results can be used like this:

band = query(<<-SQL
    SELECT band.name, bio.bio
    FROM bands AS band
    LEFT JOIN bios AS bio ON bio.band_id = band.id
    WHERE band.id = #{band_id}
    SQL
).first

puts "#{band.name} - #{band.bio}"

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.

@joe07734
Copy link
Author

joe07734 commented Nov 1, 2018

Fixed for those failed tests, now going to update the unit tests for statements, like in the other PR.

@@ -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;
Copy link
Collaborator

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)

Copy link
Author

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);
Copy link
Collaborator

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.

Copy link
Author

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.

Copy link
Author

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);
Copy link
Collaborator

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?)

Copy link
Author

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.

@sodabrew
Copy link
Collaborator

sodabrew commented Nov 1, 2018

This is also awesome. Few comments. Thank you for contributing upstream!

@sodabrew sodabrew added this to the 0.6.0 milestone Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants