Skip to content

Commit 2eb4a8f

Browse files
committed
Factor out result @query_options into a separate function.
1 parent afaa514 commit 2eb4a8f

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

ext/mysql2/result.c

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -417,72 +417,76 @@ static VALUE rb_mysql_result_fetch_fields(VALUE self) {
417417
return wrapper->fields;
418418
}
419419

420-
static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
421-
VALUE defaults, opts, block;
422-
ID db_timezone, app_timezone, dbTz, appTz;
423-
mysql2_result_wrapper * wrapper;
424-
unsigned long i;
425-
int symbolizeKeys = 0, asArray = 0, castBool = 0, cacheRows = 1, cast = 1, streaming = 0;
426-
MYSQL_FIELD * fields = NULL;
427-
428-
GetMysql2Result(self, wrapper);
429-
430-
defaults = rb_iv_get(self, "@query_options");
431-
Check_Type(defaults, T_HASH);
432-
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
433-
opts = rb_funcall(defaults, intern_merge, 1, opts);
434-
} else {
435-
opts = defaults;
436-
}
420+
static void rb_mysql_row_query_options(VALUE opts, ID *db_timezone, ID *app_timezone, int *symbolizeKeys, int *asArray, int *castBool, int *cast, int *streaming, int *cacheRows) {
421+
ID dbTz, appTz;
437422

438423
if (rb_hash_aref(opts, sym_symbolize_keys) == Qtrue) {
439-
symbolizeKeys = 1;
424+
*symbolizeKeys = 1;
440425
}
441426

442427
if (rb_hash_aref(opts, sym_as) == sym_array) {
443-
asArray = 1;
428+
*asArray = 1;
444429
}
445430

446431
if (rb_hash_aref(opts, sym_cast_booleans) == Qtrue) {
447-
castBool = 1;
432+
*castBool = 1;
448433
}
449434

450-
if (rb_hash_aref(opts, sym_cache_rows) == Qfalse) {
451-
cacheRows = 0;
435+
if (rb_hash_aref(opts, sym_cast) == Qfalse) {
436+
*cast = 0;
452437
}
453438

454-
if (rb_hash_aref(opts, sym_cast) == Qfalse) {
455-
cast = 0;
439+
if (rb_hash_aref(opts, sym_cache_rows) == Qfalse) {
440+
*cacheRows = 0;
456441
}
457442

458443
if(rb_hash_aref(opts, sym_stream) == Qtrue) {
459-
streaming = 1;
444+
*streaming = 1;
460445
}
461446

462-
if(streaming && cacheRows) {
447+
if(*streaming && *cacheRows) {
463448
rb_warn("cacheRows is ignored if streaming is true");
464449
}
465450

466451
dbTz = rb_hash_aref(opts, sym_database_timezone);
467452
if (dbTz == sym_local) {
468-
db_timezone = intern_local;
453+
*db_timezone = intern_local;
469454
} else if (dbTz == sym_utc) {
470-
db_timezone = intern_utc;
455+
*db_timezone = intern_utc;
471456
} else {
472457
if (!NIL_P(dbTz)) {
473458
rb_warn(":database_timezone option must be :utc or :local - defaulting to :local");
474459
}
475-
db_timezone = intern_local;
460+
*db_timezone = intern_local;
476461
}
477462

478463
appTz = rb_hash_aref(opts, sym_application_timezone);
479464
if (appTz == sym_local) {
480-
app_timezone = intern_local;
465+
*app_timezone = intern_local;
481466
} else if (appTz == sym_utc) {
482-
app_timezone = intern_utc;
467+
*app_timezone = intern_utc;
468+
} else {
469+
*app_timezone = Qnil;
470+
}
471+
}
472+
473+
static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
474+
VALUE defaults, opts, block;
475+
ID db_timezone, app_timezone;
476+
mysql2_result_wrapper * wrapper;
477+
unsigned long i;
478+
int symbolizeKeys = 0, asArray = 0, castBool = 0, cacheRows = 1, cast = 1, streaming = 0;
479+
480+
GetMysql2Result(self, wrapper);
481+
482+
defaults = rb_iv_get(self, "@query_options");
483+
Check_Type(defaults, T_HASH);
484+
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
485+
opts = rb_funcall(defaults, intern_merge, 1, opts);
483486
} else {
484-
app_timezone = Qnil;
487+
opts = defaults;
485488
}
489+
rb_mysql_row_query_options(opts, &db_timezone, &app_timezone, &symbolizeKeys, &asArray, &castBool, &cast, &streaming, &cacheRows);
486490

487491
if (wrapper->lastRowProcessed == 0) {
488492
if(streaming) {

0 commit comments

Comments
 (0)