Skip to content

Commit b273935

Browse files
committed
Further move query options into a separate function
1 parent b128660 commit b273935

File tree

1 file changed

+35
-56
lines changed

1 file changed

+35
-56
lines changed

ext/mysql2/result.c

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -755,50 +755,60 @@ static VALUE rb_mysql_result_fetch_fields(VALUE self) {
755755
return wrapper->fields;
756756
}
757757

758-
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) {
758+
static result_each_args rb_mysql_row_query_options(VALUE self, VALUE opts) {
759759
ID dbTz, appTz;
760+
VALUE defaults;
761+
result_each_args args;
762+
763+
defaults = rb_iv_get(self, "@query_options");
764+
Check_Type(defaults, T_HASH);
765+
if (!NIL_P(opts)) {
766+
opts = rb_funcall(defaults, intern_merge, 1, opts);
767+
} else {
768+
opts = defaults;
769+
}
760770

761-
*symbolizeKeys = RTEST(rb_hash_aref(opts, sym_symbolize_keys));
762-
*asArray = rb_hash_aref(opts, sym_as) == sym_array;
763-
*castBool = RTEST(rb_hash_aref(opts, sym_cast_booleans));
764-
*cacheRows = RTEST(rb_hash_aref(opts, sym_cache_rows));
765-
*cast = RTEST(rb_hash_aref(opts, sym_cast));
771+
args.symbolizeKeys = RTEST(rb_hash_aref(opts, sym_symbolize_keys));
772+
args.asArray = rb_hash_aref(opts, sym_as) == sym_array;
773+
args.castBool = RTEST(rb_hash_aref(opts, sym_cast_booleans));
774+
args.cacheRows = RTEST(rb_hash_aref(opts, sym_cache_rows));
775+
args.cast = RTEST(rb_hash_aref(opts, sym_cast));
776+
args.block_given = Qnil;
766777

767778
dbTz = rb_hash_aref(opts, sym_database_timezone);
768779
if (dbTz == sym_local) {
769-
*db_timezone = intern_local;
780+
args.db_timezone = intern_local;
770781
} else if (dbTz == sym_utc) {
771-
*db_timezone = intern_utc;
782+
args.db_timezone = intern_utc;
772783
} else {
773784
if (!NIL_P(dbTz)) {
774785
rb_warn(":database_timezone option must be :utc or :local - defaulting to :local");
775786
}
776-
*db_timezone = intern_local;
787+
args.db_timezone = intern_local;
777788
}
778789

779790
appTz = rb_hash_aref(opts, sym_application_timezone);
780791
if (appTz == sym_local) {
781-
*app_timezone = intern_local;
792+
args.app_timezone = intern_local;
782793
} else if (appTz == sym_utc) {
783-
*app_timezone = intern_utc;
794+
args.app_timezone = intern_utc;
784795
} else {
785-
*app_timezone = Qnil;
796+
args.app_timezone = Qnil;
786797
}
798+
799+
return args;
787800
}
788801

789802
static VALUE rb_mysql_result_element(int argc, VALUE * argv, VALUE self) {
790803
result_each_args args;
791804
MYSQL_FIELD *fields = NULL;
792-
ID db_timezone, app_timezone;
793805
VALUE seek, count, row, rows;
794806
long i, c_seek, c_count = 0;
795-
int symbolizeKeys, asArray, castBool, cacheRows, cast;
796-
VALUE defaults, block, opts, (*fetch_row_func)(VALUE, MYSQL_FIELD *fields, const result_each_args *args);
807+
VALUE (*fetch_row_func)(VALUE, MYSQL_FIELD *fields, const result_each_args *args);
808+
VALUE block, opts;
797809

798810
GET_RESULT(self);
799811

800-
defaults = rb_iv_get(self, "@query_options");
801-
Check_Type(defaults, T_HASH);
802812
rb_scan_args(argc, argv, "12&", &seek, &count, &opts, &block);
803813

804814
/* If the second arg is a hash, it's the opts and there's no count */
@@ -814,13 +824,8 @@ static VALUE rb_mysql_result_element(int argc, VALUE * argv, VALUE self) {
814824
if (!c_count) return rb_ary_new();
815825
}
816826

817-
if (!NIL_P(opts)) {
818-
opts = rb_funcall(defaults, intern_merge, 1, opts);
819-
} else {
820-
opts = defaults;
821-
}
822-
823-
rb_mysql_row_query_options(opts, &db_timezone, &app_timezone, &symbolizeKeys, &asArray, &castBool, &cast, &cacheRows);
827+
args = rb_mysql_row_query_options(self, opts);
828+
args.block_given = block;
824829

825830
if (wrapper->is_streaming) {
826831
rb_raise(cMysql2Error, "Element reference operator #[] cannot be used in streaming mode.");
@@ -848,16 +853,7 @@ static VALUE rb_mysql_result_element(int argc, VALUE * argv, VALUE self) {
848853
}
849854

850855
mysql_data_seek(wrapper->result, c_seek);
851-
852-
// Backward compat
853-
args.symbolizeKeys = symbolizeKeys;
854-
args.asArray = asArray;
855-
args.castBool = castBool;
856-
args.cacheRows = cacheRows;
857-
args.cast = cast;
858-
args.db_timezone = db_timezone;
859-
args.app_timezone = app_timezone;
860-
args.block_given = block;
856+
fields = mysql_fetch_fields(wrapper->result);
861857

862858
if (wrapper->stmt) {
863859
fetch_row_func = rb_mysql_result_fetch_row_stmt;
@@ -974,21 +970,14 @@ static VALUE rb_mysql_result_each_(VALUE self,
974970

975971
static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
976972
result_each_args args;
977-
VALUE defaults, opts, block, (*fetch_row_func)(VALUE, MYSQL_FIELD *fields, const result_each_args *args);
978-
ID db_timezone, app_timezone;
979-
int symbolizeKeys, asArray, castBool, cacheRows, cast;
973+
VALUE (*fetch_row_func)(VALUE, MYSQL_FIELD *fields, const result_each_args *args);
974+
VALUE opts, block;
980975

981976
GET_RESULT(self);
982977

983-
defaults = rb_iv_get(self, "@query_options");
984-
Check_Type(defaults, T_HASH);
985-
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
986-
opts = rb_funcall(defaults, intern_merge, 1, opts);
987-
} else {
988-
opts = defaults;
989-
}
990-
991-
rb_mysql_row_query_options(opts, &db_timezone, &app_timezone, &symbolizeKeys, &asArray, &castBool, &cast, &cacheRows);
978+
rb_scan_args(argc, argv, "01&", &opts, &block);
979+
args = rb_mysql_row_query_options(self, opts);
980+
args.block_given = block;
992981

993982
if (wrapper->lastRowProcessed == 0 && !wrapper->is_streaming) {
994983
wrapper->numberOfRows = wrapper->stmt ? mysql_stmt_num_rows(wrapper->stmt) : mysql_num_rows(wrapper->result);
@@ -999,16 +988,6 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
999988
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
1000989
}
1001990

1002-
// Backward compat
1003-
args.symbolizeKeys = symbolizeKeys;
1004-
args.asArray = asArray;
1005-
args.castBool = castBool;
1006-
args.cacheRows = cacheRows;
1007-
args.cast = cast;
1008-
args.db_timezone = db_timezone;
1009-
args.app_timezone = app_timezone;
1010-
args.block_given = block;
1011-
1012991
if (wrapper->stmt) {
1013992
fetch_row_func = rb_mysql_result_fetch_row_stmt;
1014993
} else {

0 commit comments

Comments
 (0)