Skip to content

Commit 6f2fa06

Browse files
nyaxtjustincase
authored andcommitted
cast is forced for prepared statements
1 parent 62a8145 commit 6f2fa06

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

ext/mysql2/result.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static void rb_mysql_result_alloc_result_buffers(VALUE self, MYSQL_FIELD *fields
294294
}
295295
}
296296

297-
static VALUE rb_mysql_result_stmt_fetch_row(VALUE self, ID db_timezone, ID app_timezone, int symbolizeKeys, int asArray, int castBool, int cast, MYSQL_FIELD *fields) {
297+
static VALUE rb_mysql_result_stmt_fetch_row(VALUE self, ID db_timezone, ID app_timezone, int symbolizeKeys, int asArray, int castBool, MYSQL_FIELD * fields) {
298298
VALUE rowVal;
299299
mysql2_result_wrapper *wrapper;
300300
unsigned int i = 0;
@@ -780,12 +780,13 @@ static VALUE rb_mysql_result_each_nonstmt(VALUE self, const result_each_args* ar
780780
wrapper->numberOfRows++;
781781
if (args->block_given != Qnil) {
782782
rb_yield(row);
783+
wrapper->lastRowProcessed++;
783784
}
784785
}
785786
} while(row != Qnil);
786787

787788
rb_mysql_result_free_result(wrapper);
788-
// wrapper->numberOfRows = wrapper->lastRowProcessed;
789+
wrapper->numberOfRows = wrapper->lastRowProcessed;
789790
wrapper->streamingComplete = 1;
790791

791792
// Check for errors, the connection might have gone out from under us
@@ -864,17 +865,18 @@ static VALUE rb_mysql_result_each_stmt(VALUE self, const result_each_args* args)
864865
fields = mysql_fetch_fields(wrapper->result);
865866

866867
do {
867-
row = rb_mysql_result_stmt_fetch_row(self, args->db_timezone, args->app_timezone, args->symbolizeKeys, args->asArray, args->castBool, args->cast, fields);
868+
row = rb_mysql_result_stmt_fetch_row(self, args->db_timezone, args->app_timezone, args->symbolizeKeys, args->asArray, args->castBool, fields);
868869
if (row != Qnil) {
869870
wrapper->numberOfRows++;
870871
if (args->block_given != Qnil) {
871872
rb_yield(row);
873+
wrapper->lastRowProcessed++;
872874
}
873875
}
874876
} while(row != Qnil);
875877

876878
rb_mysql_result_free_result(wrapper);
877-
// wrapper->numberOfRows = wrapper->lastRowProcessed;
879+
wrapper->numberOfRows = wrapper->lastRowProcessed;
878880
wrapper->streamingComplete = 1;
879881

880882
// Check for errors, the connection might have gone out from under us
@@ -903,7 +905,7 @@ static VALUE rb_mysql_result_each_stmt(VALUE self, const result_each_args* args)
903905
if (args->cacheRows && i < rowsProcessed) {
904906
row = rb_ary_entry(wrapper->rows, i);
905907
} else {
906-
row = rb_mysql_result_stmt_fetch_row(self, args->db_timezone, args->app_timezone, args->symbolizeKeys, args->asArray, args->castBool, args->cast, fields);
908+
row = rb_mysql_result_stmt_fetch_row(self, args->db_timezone, args->app_timezone, args->symbolizeKeys, args->asArray, args->castBool, fields);
907909
if (args->cacheRows) {
908910
rb_ary_store(wrapper->rows, i, row);
909911
}
@@ -962,10 +964,14 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
962964
rb_warn("cacheRows is ignored if streaming is true");
963965
}
964966

965-
if(wrapper->stmt && !args.cacheRows && !args.streaming) {
967+
if (wrapper->stmt && !args.cacheRows && !args.streaming) {
966968
rb_warn("cacheRows is forced for prepared statements (if not streaming)");
967969
}
968970

971+
if (wrapper->stmt && !args.cast) {
972+
rb_warn("cast is forced for prepared statements");
973+
}
974+
969975
dbTz = rb_hash_aref(opts, sym_database_timezone);
970976
if (dbTz == sym_local) {
971977
db_timezone = intern_local;

spec/mysql2/statement_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,6 @@
239239
@test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute.first
240240
end
241241

242-
it "should return nil values for NULL and strings for everything else when :cast is false" do
243-
result = @client.query('SELECT null_test, tiny_int_test, bool_cast_test, int_test, date_test, enum_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast => false).first
244-
result["null_test"].should be_nil
245-
result["tiny_int_test"].should == "1"
246-
result["bool_cast_test"].should == "1"
247-
result["int_test"].should == "10"
248-
result["date_test"].should == "2010-04-04"
249-
result["enum_test"].should == "val1"
250-
end
251-
252242
it "should return nil for a NULL value" do
253243
@test_result['null_test'].class.should eql(NilClass)
254244
@test_result['null_test'].should eql(nil)

0 commit comments

Comments
 (0)