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 element reference operator #[] to Mysql2::Result using mysql_data_seek #345

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

Conversation

sodabrew
Copy link
Collaborator

For #130 mysql_data_seek is wrapped behind the element reference operator #[]. You can jump directly to a particular result row:

  result = @client.query "SELECT 1 UNION SELECT 2"
  p result[1]

  {"1" => 2}

Open for comment!

@sodabrew
Copy link
Collaborator Author

The fourth changeset builds on the #[] changeset, adding support for:

  result = @client.query "SELECT 1 AS col UNION SELECT 2 AS col"
  p result[1, 0]
  []

  p result[1, 1]
  [{"col" => 2}]

  p result[-2, 2]
  [{"col" => 1}, {"col" => 2}]

  p result[1, {:symbolize_keys => true}]
  {:col => 2}

  p result[1, 1, {:symbolize_keys => true}]
  [{:col => 2}]

Now that I'm returning an array, I should probably support cacheRows, which means the code in result#[] is going to look a lot like result#each. More refactoring!

@sodabrew
Copy link
Collaborator Author

Two years later - rebased yo!

@@ -440,14 +444,121 @@ static VALUE rb_mysql_result_fetch_fields(VALUE self) {
return wrapper->fields;
}

static void rb_mysql_row_query_options(VALUE opts, ID *db_timezone, ID *app_timezone, int *symbolizeKeys, int *asArray, int *castBool, int *cast, int *cacheRows) {
ID dbTz, appTz;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

underindented by one?

@sodabrew sodabrew force-pushed the mysql_data_seek branch 2 times, most recently from 8262aa4 to 1eea3e2 Compare June 11, 2015 22:08
}

if (!NIL_P(opts)) {
opts = rb_funcall(defaults, intern_merge, 1, opts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this preamble could be implemented in ruby and would be much easier to read - why not do that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually moved this further down the chain so that the same code is used by the variants of [] and each.

@sodabrew sodabrew force-pushed the mysql_data_seek branch 2 times, most recently from c1dcead to b77f237 Compare June 12, 2015 03:56
@sodabrew sodabrew modified the milestones: 0.4.0, 0.5.0 Aug 5, 2015
@sodabrew sodabrew modified the milestones: 0.5.0, 0.6.0 Mar 19, 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.

2 participants