adding Stmt#fields
This commit is contained in:
parent
27cd68f456
commit
9b1789fbfc
|
@ -57,6 +57,66 @@ static VALUE execute(VALUE self)
|
|||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.fields -> array
|
||||
*
|
||||
* Returns a list of fields that will be returned by this statement.
|
||||
*/
|
||||
static VALUE fields(VALUE self)
|
||||
{
|
||||
MYSQL_STMT * stmt;
|
||||
MYSQL_FIELD * fields;
|
||||
MYSQL_RES * metadata;
|
||||
unsigned int field_count;
|
||||
unsigned int i;
|
||||
VALUE field_list;
|
||||
VALUE cMysql2Field;
|
||||
|
||||
Data_Get_Struct(self, MYSQL_STMT, stmt);
|
||||
metadata = mysql_stmt_result_metadata(stmt);
|
||||
fields = mysql_fetch_fields(metadata);
|
||||
field_count = mysql_stmt_field_count(stmt);
|
||||
field_list = rb_ary_new2((long)field_count);
|
||||
|
||||
cMysql2Field = rb_const_get(mMysql2, rb_intern("Field"));
|
||||
|
||||
for(i = 0; i < field_count; i++) {
|
||||
VALUE argv[2];
|
||||
VALUE field;
|
||||
|
||||
/* FIXME: encoding. Also, can this return null? */
|
||||
argv[0] = rb_str_new2(fields[i].name);
|
||||
argv[1] = INT2NUM(fields[i].type);
|
||||
|
||||
field = rb_class_new_instance(2, argv, cMysql2Field);
|
||||
|
||||
rb_ary_store(field_list, (long)i, field);
|
||||
}
|
||||
|
||||
return field_list;
|
||||
}
|
||||
|
||||
static VALUE each(VALUE self)
|
||||
{
|
||||
MYSQL_STMT * stmt;
|
||||
MYSQL_FIELD * fields;
|
||||
MYSQL_RES * metadata;
|
||||
unsigned int field_count;
|
||||
unsigned int i;
|
||||
VALUE block;
|
||||
|
||||
Data_Get_Struct(self, MYSQL_STMT, stmt);
|
||||
|
||||
block = rb_block_proc();
|
||||
metadata = mysql_stmt_result_metadata(stmt);
|
||||
fields = mysql_fetch_fields(metadata);
|
||||
field_count = mysql_stmt_field_count(stmt);
|
||||
|
||||
for(i = 0; i < field_count; i++) {
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
void init_mysql2_statement()
|
||||
{
|
||||
cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject);
|
||||
|
@ -65,4 +125,6 @@ void init_mysql2_statement()
|
|||
rb_define_method(cMysql2Statement, "param_count", param_count, 0);
|
||||
rb_define_method(cMysql2Statement, "field_count", field_count, 0);
|
||||
rb_define_method(cMysql2Statement, "execute", execute, 0);
|
||||
rb_define_method(cMysql2Statement, "each", each, 0);
|
||||
rb_define_method(cMysql2Statement, "fields", fields, 0);
|
||||
}
|
||||
|
|
|
@ -55,4 +55,31 @@ describe Mysql2::Statement do
|
|||
stmt = @client.create_statement
|
||||
lambda { stmt.execute }.should raise_error(Mysql2::Error)
|
||||
end
|
||||
|
||||
it "should raise an exception without a block" do
|
||||
stmt = @client.create_statement
|
||||
stmt.prepare 'SELECT 1'
|
||||
stmt.execute
|
||||
lambda { stmt.each }.should raise_error
|
||||
end
|
||||
|
||||
it "should let us iterate over results" do
|
||||
stmt = @client.create_statement
|
||||
stmt.prepare 'SELECT 1'
|
||||
stmt.execute
|
||||
rows = []
|
||||
stmt.each { |row| rows << row }
|
||||
pending "not working yet"
|
||||
rows.should == [[1]]
|
||||
end
|
||||
|
||||
it "should tell us about the fields" do
|
||||
stmt = @client.create_statement
|
||||
stmt.prepare 'SELECT 1 as foo, 2'
|
||||
stmt.execute
|
||||
list = stmt.fields
|
||||
list.length.should == 2
|
||||
list.first.name.should == 'foo'
|
||||
list[1].name.should == '2'
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue