Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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
from

Conversation

Projects
None yet
2 participants
Collaborator

sodabrew commented Dec 30, 2012

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!

Collaborator

sodabrew commented Dec 31, 2012

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!

Collaborator

sodabrew commented Feb 26, 2015

Two years later - rebased yo!

@tamird tamird commented on an outdated diff Mar 14, 2015

ext/mysql2/result.c
@@ -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;
@tamird

tamird Mar 14, 2015

Contributor

underindented by one?

@tamird tamird and 1 other commented on an outdated diff Jun 12, 2015

ext/mysql2/result.c
+
+ /* If the second arg is a hash, it's the opts and there's no count */
+ if (TYPE(count) == T_HASH) {
+ opts = count;
+ count = Qnil;
+ }
+
+ c_seek = NUM2LONG(seek);
+ if (!NIL_P(count)) {
+ c_count = NUM2LONG(count);
+ /* Special case: ary[x, 0] returns []*/
+ if (!c_count) return rb_ary_new();
+ }
+
+ if (!NIL_P(opts)) {
+ opts = rb_funcall(defaults, intern_merge, 1, opts);
@tamird

tamird Jun 12, 2015

Contributor

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

@sodabrew

sodabrew Jun 12, 2015

Collaborator

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

@sodabrew sodabrew modified the milestones: 0.4.0, 0.5.0 Aug 5, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment