From: Ron "Quinn" Straight Date: Mon, 4 May 2009 01:58:03 +0000 (+0000) Subject: Proper support for timestamp inflation. Added last_updated_at to DBICTest::Schema... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=abc914bdc9b48f7295ed1d27380f0b14c971db1c;p=dbsrgits%2FDBIx-Class-Historic.git Proper support for timestamp inflation. Added last_updated_at to DBICTest::Schema::Track as a date by default, initialized in sqlite loader, redefined to timestamp for Oracle tests. --- diff --git a/lib/DBIx/Class/InflateColumn/DateTime.pm b/lib/DBIx/Class/InflateColumn/DateTime.pm index afd118d..3053035 100644 --- a/lib/DBIx/Class/InflateColumn/DateTime.pm +++ b/lib/DBIx/Class/InflateColumn/DateTime.pm @@ -94,7 +94,7 @@ sub register_column { my $type; - for (qw/date datetime/) { + for (qw/date datetime timestamp/) { my $key = "inflate_${_}"; next unless exists $info->{$key}; @@ -106,7 +106,6 @@ sub register_column { unless ($type) { $type = lc($info->{data_type}); - $type = 'datetime' if ($type =~ /^timestamp/); } my $timezone; @@ -128,7 +127,7 @@ sub register_column { my $undef_if_invalid = $info->{datetime_undef_if_invalid}; - if ($type eq 'datetime' || $type eq 'date') { + if ($type eq 'datetime' || $type eq 'date' || $type eq 'timestamp') { my ($parse, $format) = ("parse_${type}", "format_${type}"); # This assignment must happen here, otherwise Devel::Cycle treats @@ -157,7 +156,9 @@ sub register_column { { inflate => sub { my ($value, $obj) = @_; - my $dt = eval { $obj->_datetime_parser->$parse($value); }; + my $parser = $obj->_datetime_parser; + my $parser_method = $parser->can($parse) ? $parse : "parse_datetime"; + my $dt = eval { $parser->$parser_method($value); }; die "Error while inflating ${value} for ${column} on ${self}: $@" if $@ and not $undef_if_invalid; $dt->set_time_zone($timezone) if $timezone; @@ -166,6 +167,8 @@ sub register_column { }, deflate => sub { my ($value, $obj) = @_; + my $parser = $obj->_datetime_parser; + my $parser_method = $parser->can($format) ? $format : "format_datetime"; if ($timezone) { warn "You're using a floating timezone, please see the documentation of" . " DBIx::Class::InflateColumn::DateTime for an explanation" @@ -175,13 +178,14 @@ sub register_column { $value->set_time_zone($timezone); $value->set_locale($locale) if $locale; } - $obj->_datetime_parser->$format($value); + $parser->$parser_method($value); }, } ); } } + sub _datetime_parser { my $self = shift; if (my $parser = $self->__datetime_parser) { diff --git a/t/73oracle_inflate.t b/t/73oracle_inflate.t index 20e7089..ded8bb4 100644 --- a/t/73oracle_inflate.t +++ b/t/73oracle_inflate.t @@ -17,12 +17,13 @@ else { plan skip_all => 'needs DateTime and DateTime::Format::Oracle for testing'; } else { - plan tests => 4; + plan tests => 7; } } # DateTime::Format::Oracle needs this set $ENV{NLS_DATE_FORMAT} = 'DD-MON-YY'; +$ENV{NLS_TIMESTAMP_FORMAT} = 'YYYY-MM-DD HH24:MI:SSXFF'; my $schema = DBICTest::Schema->connect($dsn, $user, $pass); @@ -30,16 +31,20 @@ my $schema = DBICTest::Schema->connect($dsn, $user, $pass); my $col_metadata = $schema->class('Track')->column_info('last_updated_on'); $schema->class('Track')->add_column( 'last_updated_on' => { data_type => 'date' }); +$schema->class('Track')->add_column( 'last_updated_at' => { + data_type => 'timestamp' }); my $dbh = $schema->storage->dbh; +#$dbh->do("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SSXFF'"); + eval { $dbh->do("DROP TABLE track"); }; -$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)"); +$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at TIMESTAMP)"); # insert a row to play with -my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07' }); +my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07', last_updated_at => '2009-05-03 21:17:18.5' }); is($new->trackid, 1, "insert sucessful"); my $track = $schema->resultset('Track')->find( 1 ); @@ -48,11 +53,18 @@ is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok"); is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column"); +#note '$track->last_updated_at => ', $track->last_updated_at; +is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok"); + +is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision"); + my $dt = DateTime->now(); $track->last_updated_on($dt); +$track->last_updated_at($dt); $track->update; is( $track->last_updated_on->month, $dt->month, "deflate ok"); +is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision"); # clean up our mess END { diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm index e3f731e..ee07f5e 100644 --- a/t/lib/DBICTest/Schema/Track.pm +++ b/t/lib/DBICTest/Schema/Track.pm @@ -26,6 +26,10 @@ __PACKAGE__->add_columns( accessor => 'updated_date', is_nullable => 1 }, + last_updated_at => { + data_type => 'datetime', + is_nullable => 1 + }, ); __PACKAGE__->set_primary_key('trackid'); diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql index 9e8aaa6..0eef182 100644 --- a/t/lib/sqlite.sql +++ b/t/lib/sqlite.sql @@ -375,7 +375,8 @@ CREATE TABLE track ( cd integer NOT NULL, position integer NOT NULL, title varchar(100) NOT NULL, - last_updated_on datetime + last_updated_on datetime, + last_updated_at datetime ); CREATE INDEX track_idx_cd_track ON track (cd);