Skip to content

Commit 7138a9b

Browse files
nyaxtjustincase
authored andcommitted
test pass
1 parent 9199d11 commit 7138a9b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

ext/mysql2/statement.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
VALUE cMysql2Statement;
44
extern VALUE mMysql2, cMysql2Error, cBigDecimal, cDateTime, cDate;
5-
static VALUE sym_stream, intern_error_number_eql, intern_sql_state_eql;
5+
static VALUE sym_stream, intern_error_number_eql, intern_sql_state_eql, intern_each;
66

77
#define GET_STATEMENT(self) \
88
mysql_stmt_wrapper *stmt_wrapper; \
@@ -365,6 +365,11 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
365365
}
366366
#endif
367367

368+
if (!is_streaming) {
369+
// cache all result
370+
rb_funcall(resultObj, intern_each, 0);
371+
}
372+
368373
return resultObj;
369374
}
370375

@@ -428,4 +433,5 @@ void init_mysql2_statement() {
428433

429434
intern_error_number_eql = rb_intern("error_number=");
430435
intern_sql_state_eql = rb_intern("sql_state=");
436+
intern_each = rb_intern("each");
431437
}

spec/mysql2/statement_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@
5858
rows.should == [{"1"=>1}]
5959
end
6060

61+
it "should keep its result after other query" do
62+
@client.query 'USE test'
63+
@client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'
64+
@client.query 'INSERT INTO mysql2_stmt_q (a) VALUES (1), (2)'
65+
stmt = @client.prepare('SELECT a FROM mysql2_stmt_q WHERE a = ?')
66+
result1 = stmt.execute(1)
67+
result2 = stmt.execute(2)
68+
result2.first.should == {"a"=>2}
69+
result1.first.should == {"a"=>1}
70+
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_q'
71+
end
72+
6173
it "should select dates" do
6274
statement = @client.prepare 'SELECT NOW()'
6375
result = statement.execute

0 commit comments

Comments
 (0)