From 45adf1e23bcfc8dde127c0c1c57472da8d2b76ad Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 9 Jul 2010 11:10:54 -0700 Subject: [PATCH] prepared statements can pull times --- ext/mysql2/statement.c | 16 ++++++++++++++++ spec/mysql2/statement_spec.rb | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/ext/mysql2/statement.c b/ext/mysql2/statement.c index 1d102fb..456c97d 100644 --- a/ext/mysql2/statement.c +++ b/ext/mysql2/statement.c @@ -106,6 +106,7 @@ static VALUE each(VALUE self) my_bool * error; unsigned long * length; int int_data; + MYSQL_TIME ts; unsigned int field_count; unsigned int i; @@ -129,6 +130,10 @@ static VALUE each(VALUE self) binds[i].buffer_type = MYSQL_TYPE_LONG; binds[i].buffer = (char *)&int_data; break; + case MYSQL_TYPE_DATETIME: + binds[i].buffer_type = MYSQL_TYPE_DATETIME; + binds[i].buffer = (char *)&ts; + break; default: rb_raise(cMysql2Error, "unhandled mysql type: %d", fields[i].type); } @@ -151,6 +156,17 @@ static VALUE each(VALUE self) case MYSQL_TYPE_LONG: column = INT2NUM(int_data); 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: rb_raise(cMysql2Error, "unhandled buffer type: %d", binds[i].buffer_type); diff --git a/spec/mysql2/statement_spec.rb b/spec/mysql2/statement_spec.rb index d2a0be0..14628b3 100644 --- a/spec/mysql2/statement_spec.rb +++ b/spec/mysql2/statement_spec.rb @@ -72,6 +72,15 @@ describe Mysql2::Statement do rows.should == [[1]] 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 stmt = @client.create_statement stmt.prepare 'SELECT 1 as foo, 2'