From 2c86f9d72a762afcf3d243c108eff02123b9fa37 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 8 Jul 2010 15:55:16 -0700 Subject: [PATCH] prepared statements will tell us the field count --- Rakefile | 1 + ext/mysql2/statement.c | 13 +++++++++++++ spec/mysql2/statement_spec.rb | 9 +++++++++ 3 files changed, 23 insertions(+) diff --git a/Rakefile b/Rakefile index edecf8d..79c32a9 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ # encoding: UTF-8 begin + require 'rubygems' require 'jeweler' JEWELER = Jeweler::Tasks.new do |gem| gem.name = "mysql2" diff --git a/ext/mysql2/statement.c b/ext/mysql2/statement.c index 2e78805..3eb1acf 100644 --- a/ext/mysql2/statement.c +++ b/ext/mysql2/statement.c @@ -30,10 +30,23 @@ static VALUE param_count(VALUE self) return ULL2NUM(mysql_stmt_param_count(stmt)); } +/* call-seq: stmt.field_count # => 2 + * + * Returns the number of fields the prepared statement returns. + */ +static VALUE field_count(VALUE self) +{ + MYSQL_STMT * stmt; + Data_Get_Struct(self, MYSQL_STMT, stmt); + + return UINT2NUM(mysql_stmt_field_count(stmt)); +} + void init_mysql2_statement() { cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject); rb_define_method(cMysql2Statement, "prepare", prepare, 1); rb_define_method(cMysql2Statement, "param_count", param_count, 0); + rb_define_method(cMysql2Statement, "field_count", field_count, 0); } diff --git a/spec/mysql2/statement_spec.rb b/spec/mysql2/statement_spec.rb index 8306f0b..f66f424 100644 --- a/spec/mysql2/statement_spec.rb +++ b/spec/mysql2/statement_spec.rb @@ -35,4 +35,13 @@ describe Mysql2::Statement do stmt.prepare 'SELECT 1' stmt.param_count.should == 0 end + + it "should tell us the field count" do + stmt = @client.create_statement + stmt.prepare 'SELECT ?, ?' + stmt.field_count.should == 2 + + stmt.prepare 'SELECT 1' + stmt.field_count.should == 1 + end end