Permalink
Browse files

Handles the extra nil row for streaming queries.

Makes sure the extra nil row is accounted for on #count after a streaming query,
ideally the nil should not be yielded but that'd be a separate patch.
  • Loading branch information...
1 parent 4a5664a commit d3f5ddcbe3ffc478a8b9d737ad4a51283011898b @camilo camilo committed Jun 12, 2012
Showing with 15 additions and 2 deletions.
  1. +6 −1 ext/mysql2/result.c
  2. +9 −1 spec/mysql2/result_spec.rb
View
@@ -541,7 +541,12 @@ static VALUE rb_mysql_result_count(VALUE self) {
GetMysql2Result(self, wrapper);
if(wrapper->resultFreed) {
if (wrapper->streamingComplete){
- return LONG2NUM(wrapper->numberOfRows);
+ if(wrapper->numberOfRows > 0){
+ // -1 is necessary because the final nil row is yielded
+ return LONG2NUM(wrapper->numberOfRows - 1);
+ }else{
+ return LONG2NUM(0);
+ }
} else {
return LONG2NUM(RARRAY_LEN(wrapper->rows));
}
View
@@ -23,7 +23,15 @@
result = @client.query("SELECT * FROM mysql2_test", :stream => true, :cache_rows => false)
result.count.should eql(0)
result.each {|r| }
- result.count.should eql(1)
+ result.count.should eql(1)
+ end
+
+ it "#count should be zero for rows after streaming when there were no results " do
+ @client.query "USE test"
+ result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
+ result.count.should eql(0)
+ result.each {|r| }
+ result.count.should eql(0)
end
it "should have included Enumerable" do

0 comments on commit d3f5ddc

Please sign in to comment.