Skip to content

Commit 8a14154

Browse files
nyaxtjustincase
authored andcommitted
streaming works!
1 parent c1dd35c commit 8a14154

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

ext/mysql2/statement.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void *nogvl_stmt_store_result(void *ptr) {
193193

194194
/* call-seq: stmt.execute
195195
*
196-
* Executes the current prepared statement, returns +stmt+.
196+
* Executes the current prepared statement, returns +result+.
197197
*/
198198
static VALUE execute(int argc, VALUE *argv, VALUE self) {
199199
MYSQL_BIND *bind_buffers = NULL;
@@ -345,9 +345,7 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
345345
current = rb_hash_dup(rb_iv_get(stmt_wrapper->client, "@query_options"));
346346
GET_CLIENT(stmt_wrapper->client);
347347

348-
if(is_streaming) {
349-
rb_raise(cMysql2Error, "TODO: streaming stmt execute not yet impl.");
350-
} else {
348+
if (!is_streaming) {
351349
// recieve the whole result set from the server
352350
if (rb_thread_call_without_gvl(nogvl_stmt_store_result, stmt, RUBY_UBF_IO, 0) == Qfalse) {
353351
rb_raise_mysql2_stmt_error(self);

spec/mysql2/result_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@
128128
end
129129

130130
it "#count should be zero for rows after streaming when there were no results" do
131+
@client.query "USE test"
131132
result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
132133
result.count.should eql(0)
133-
result.each.to_a
134+
result.each {|r| }
134135
result.count.should eql(0)
135136
end
136137

spec/mysql2/statement_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@
147147

148148
result.to_a.should == [{"整数"=>1}]
149149
end
150+
end
150151

152+
context "streaming result" do
153+
it "should be able to stream query result" do
154+
n = 1
155+
stmt = @client.prepare("SELECT 1 UNION SELECT 2")
156+
157+
@client.query_options.merge!({:stream => true, :cache_rows => false, :as => :array})
158+
159+
stmt.execute.each do |r|
160+
case n
161+
when 1
162+
r.should == [1]
163+
when 2
164+
r.should == [2]
165+
else
166+
violated "returned more than two rows"
167+
end
168+
n += 1
169+
end
170+
end
151171
end
152172
end

0 commit comments

Comments
 (0)