From 423e51b081231278ee29dc157f0c19bfd874c275 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Sat, 17 May 2008 16:05:53 +0100 Subject: be more granular with time/data translation --- lib/sqlite3/translator.rb | 9 +++++---- test/tc_integration.rb | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/sqlite3/translator.rb b/lib/sqlite3/translator.rb index d91671e..3590f6c 100644 --- a/lib/sqlite3/translator.rb +++ b/lib/sqlite3/translator.rb @@ -61,9 +61,11 @@ module SQLite3 # Register the default translators for the current Translator instance. # This includes translators for most major SQL data types. def register_default_translators - [ "date", - "datetime", - "time" ].each { |type| add_translator( type ) { |t,v| Time.parse( v ) } } + [ "time", + "timestamp" ].each { |type| add_translator( type ) { |t, v| Time.parse( v ) } } + + add_translator( "date" ) { |t,v| time = Time.parse(v); Date.new(time.year, time.month, time.day) } + add_translator( "datetime" ) { |t,v| DateTime.parse(v) } [ "decimal", "float", @@ -91,7 +93,6 @@ module SQLite3 end end - add_translator( "timestamp" ) { |type, value| Time.at( value.to_i ) } add_translator( "tinyint" ) do |type, value| if type =~ /\(\s*1\s*\)/ value.to_i == 1 diff --git a/test/tc_integration.rb b/test/tc_integration.rb index 6c5789f..85fb27a 100644 --- a/test/tc_integration.rb +++ b/test/tc_integration.rb @@ -960,7 +960,7 @@ module Integration define_method( "test_type_translation_with_null_column" ) do @db.type_translation = true - @db.execute "create table bar ( a integer, b datetime, c string )" + @db.execute "create table bar ( a integer, b time, c string )" @db.execute "insert into bar (a, b, c) values (NULL, '1974-07-25 14:39:00', 'hello')" @db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')" @db.execute "insert into bar (a, b, c) values (2, '1974-07-25 14:39:00', NULL)" @@ -971,6 +971,19 @@ module Integration end end + define_method( "test_date_and_time_translation" ) do + @db.type_translation = true + @db.execute "create table bar ( a date, b datetime, c time, d timestamp )" + @db.execute "insert into bar (a, b, c, d) values ('1999-01-08', '1997-12-17 07:37:16', '07:37:16', '2004-10-19 10:23:54')" + @db.query( "select * from bar" ) do |result| + result = result.next + assert result[0].is_a?(Date) + assert result[1].is_a?(DateTime) + assert result[2].is_a?(Time) + assert result[3].is_a?(Time) + end + end + define_method( "test_next_results_as_hash" ) do @db.results_as_hash = true @result.reset( 1 ) -- cgit