Skip to content

Commit f253a57

Browse files
committed
Merge branch '0.2.x'
2 parents ee00a49 + 5de5755 commit f253a57

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

ext/mysql2/result.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,14 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
464464
return wrapper->rows;
465465
}
466466

467+
static VALUE rb_mysql_result_count(VALUE self) {
468+
mysql2_result_wrapper *wrapper;
469+
470+
GetMysql2Result(self, wrapper);
471+
472+
return INT2FIX(mysql_num_rows(wrapper->result));
473+
}
474+
467475
/* Mysql2::Result */
468476
VALUE rb_mysql_result_to_obj(MYSQL_RES * r) {
469477
VALUE obj;
@@ -489,6 +497,8 @@ void init_mysql2_result() {
489497
cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject);
490498
rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1);
491499
rb_define_method(cMysql2Result, "fields", rb_mysql_result_fetch_fields, 0);
500+
rb_define_method(cMysql2Result, "count", rb_mysql_result_count, 0);
501+
rb_define_alias(cMysql2Result, "size", "count");
492502

493503
intern_encoding_from_charset = rb_intern("encoding_from_charset");
494504
intern_encoding_from_charset_code = rb_intern("encoding_from_charset_code");

lib/mysql2.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
require 'mysql2/version' unless defined? Mysql2::VERSION
77
require 'mysql2/error'
8+
require 'mysql2/result'
89
require 'mysql2/mysql2'
910
require 'mysql2/client'
10-
require 'mysql2/result'
1111

1212
# = Mysql2
1313
#

spec/mysql2/result_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
}.should_not raise_error(Mysql2::Error)
2929
end
3030

31+
it "should respond to #count, which is aliased as #size" do
32+
r = @client.query "SELECT 1"
33+
r.should respond_to :count
34+
r.should respond_to :size
35+
end
36+
37+
it "should be able to return the number of rows in the result set" do
38+
r = @client.query "SELECT 1"
39+
r.count.should eql(1)
40+
r.size.should eql(1)
41+
end
42+
3143
context "metadata queries" do
3244
it "should show tables" do
3345
@result = @client.query "SHOW TABLES"

0 commit comments

Comments
 (0)