@@ -30,14 +30,15 @@ typedef struct {
3030 int streaming ;
3131 ID db_timezone ;
3232 ID app_timezone ;
33- VALUE block_given ;
33+ int block_given ; /* boolean */
3434} result_each_args ;
3535
3636extern VALUE mMysql2 , cMysql2Client , cMysql2Error ;
3737static VALUE cMysql2Result , cDateTime , cDate ;
3838static VALUE opt_decimal_zero , opt_float_zero , opt_time_year , opt_time_month , opt_utc_offset ;
3939static ID intern_new , intern_utc , intern_local , intern_localtime , intern_local_offset ,
40- intern_civil , intern_new_offset , intern_merge , intern_BigDecimal ;
40+ intern_civil , intern_new_offset , intern_merge , intern_BigDecimal ,
41+ intern_query_options ;
4142static VALUE sym_symbolize_keys , sym_as , sym_array , sym_database_timezone ,
4243 sym_application_timezone , sym_local , sym_utc , sym_cast_booleans ,
4344 sym_cache_rows , sym_cast , sym_stream , sym_name ;
@@ -695,7 +696,7 @@ static VALUE rb_mysql_result_fetch_fields(VALUE self) {
695696
696697 GET_RESULT (self );
697698
698- defaults = rb_iv_get (self , "@query_options" );
699+ defaults = rb_ivar_get (self , intern_query_options );
699700 Check_Type (defaults , T_HASH );
700701 if (rb_hash_aref (defaults , sym_symbolize_keys ) == Qtrue ) {
701702 symbolizeKeys = 1 ;
@@ -740,7 +741,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
740741 row = fetch_row_func (self , fields , args );
741742 if (row != Qnil ) {
742743 wrapper -> numberOfRows ++ ;
743- if (args -> block_given != Qnil ) {
744+ if (args -> block_given ) {
744745 rb_yield (row );
745746 }
746747 }
@@ -790,7 +791,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
790791 return Qnil ;
791792 }
792793
793- if (args -> block_given != Qnil ) {
794+ if (args -> block_given ) {
794795 rb_yield (row );
795796 }
796797 }
@@ -808,7 +809,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
808809
809810static VALUE rb_mysql_result_each (int argc , VALUE * argv , VALUE self ) {
810811 result_each_args args ;
811- VALUE defaults , opts , block , (* fetch_row_func )(VALUE , MYSQL_FIELD * fields , const result_each_args * args );
812+ VALUE defaults , opts , (* fetch_row_func )(VALUE , MYSQL_FIELD * fields , const result_each_args * args );
812813 ID db_timezone , app_timezone , dbTz , appTz ;
813814 int symbolizeKeys , asArray , castBool , cacheRows , cast ;
814815
@@ -818,9 +819,12 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
818819 rb_raise (cMysql2Error , "Statement handle already closed" );
819820 }
820821
821- defaults = rb_iv_get (self , "@query_options" );
822+ defaults = rb_ivar_get (self , intern_query_options );
822823 Check_Type (defaults , T_HASH );
823- if (rb_scan_args (argc , argv , "01&" , & opts , & block ) == 1 ) {
824+
825+ // A block can be passed to this method, but since we don't call the block directly from C,
826+ // we don't need to capture it into a variable here with the "&" scan arg.
827+ if (rb_scan_args (argc , argv , "01" , & opts ) == 1 ) {
824828 opts = rb_funcall (defaults , intern_merge , 1 , opts );
825829 } else {
826830 opts = defaults ;
@@ -886,7 +890,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
886890 args .cast = cast ;
887891 args .db_timezone = db_timezone ;
888892 args .app_timezone = app_timezone ;
889- args .block_given = block ;
893+ args .block_given = rb_block_given_p () ;
890894
891895 if (wrapper -> stmt_wrapper ) {
892896 fetch_row_func = rb_mysql_result_fetch_row_stmt ;
@@ -951,7 +955,7 @@ VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_
951955 }
952956
953957 rb_obj_call_init (obj , 0 , NULL );
954- rb_iv_set (obj , "@query_options" , options );
958+ rb_ivar_set (obj , intern_query_options , options );
955959
956960 /* Options that cannot be changed in results.each(...) { |row| }
957961 * should be processed here. */
@@ -980,6 +984,7 @@ void init_mysql2_result() {
980984 intern_civil = rb_intern ("civil" );
981985 intern_new_offset = rb_intern ("new_offset" );
982986 intern_BigDecimal = rb_intern ("BigDecimal" );
987+ intern_query_options = rb_intern ("@query_options" );
983988
984989 sym_symbolize_keys = ID2SYM (rb_intern ("symbolize_keys" ));
985990 sym_as = ID2SYM (rb_intern ("as" ));
0 commit comments