prepared statements can pull times

This commit is contained in:
Aaron Patterson 2010-07-09 11:10:54 -07:00
parent d510ff675e
commit 45adf1e23b
2 changed files with 25 additions and 0 deletions

View File

@ -106,6 +106,7 @@ static VALUE each(VALUE self)
my_bool * error; my_bool * error;
unsigned long * length; unsigned long * length;
int int_data; int int_data;
MYSQL_TIME ts;
unsigned int field_count; unsigned int field_count;
unsigned int i; unsigned int i;
@ -129,6 +130,10 @@ static VALUE each(VALUE self)
binds[i].buffer_type = MYSQL_TYPE_LONG; binds[i].buffer_type = MYSQL_TYPE_LONG;
binds[i].buffer = (char *)&int_data; binds[i].buffer = (char *)&int_data;
break; break;
case MYSQL_TYPE_DATETIME:
binds[i].buffer_type = MYSQL_TYPE_DATETIME;
binds[i].buffer = (char *)&ts;
break;
default: default:
rb_raise(cMysql2Error, "unhandled mysql type: %d", fields[i].type); rb_raise(cMysql2Error, "unhandled mysql type: %d", fields[i].type);
} }
@ -151,6 +156,17 @@ static VALUE each(VALUE self)
case MYSQL_TYPE_LONG: case MYSQL_TYPE_LONG:
column = INT2NUM(int_data); column = INT2NUM(int_data);
break; break;
/* FIXME: maybe we want to return a datetime in this case? */
case MYSQL_TYPE_DATETIME:
column = rb_funcall(rb_cTime,
rb_intern("mktime"), 6,
UINT2NUM(ts.year),
UINT2NUM(ts.month),
UINT2NUM(ts.day),
UINT2NUM(ts.hour),
UINT2NUM(ts.minute),
UINT2NUM(ts.second));
break;
default: default:
rb_raise(cMysql2Error, "unhandled buffer type: %d", rb_raise(cMysql2Error, "unhandled buffer type: %d",
binds[i].buffer_type); binds[i].buffer_type);

View File

@ -72,6 +72,15 @@ describe Mysql2::Statement do
rows.should == [[1]] rows.should == [[1]]
end end
it "should select dates" do
stmt = @client.create_statement
stmt.prepare 'SELECT NOW()'
stmt.execute
rows = []
stmt.each { |row| rows << row }
rows.first.first.should be_kind_of Time
end
it "should tell us about the fields" do it "should tell us about the fields" do
stmt = @client.create_statement stmt = @client.create_statement
stmt.prepare 'SELECT 1 as foo, 2' stmt.prepare 'SELECT 1 as foo, 2'