prepared statements will tell us the field count

This commit is contained in:
Aaron Patterson 2010-07-08 15:55:16 -07:00
parent a5d8a087a7
commit 2c86f9d72a
3 changed files with 23 additions and 0 deletions

View File

@ -1,5 +1,6 @@
# encoding: UTF-8
begin
require 'rubygems'
require 'jeweler'
JEWELER = Jeweler::Tasks.new do |gem|
gem.name = "mysql2"

View File

@ -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);
}

View File

@ -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