[Bast-commits] r6117 - in DBIx-Class/0.08/branches/oracle-tweaks: lib/DBIx/Class/InflateColumn t t/lib t/lib/DBICTest/Schema

nniuq at dev.catalyst.perl.org nniuq at dev.catalyst.perl.org
Mon May 4 01:58:04 GMT 2009


Author: nniuq
Date: 2009-05-04 01:58:03 +0000 (Mon, 04 May 2009)
New Revision: 6117

Modified:
   DBIx-Class/0.08/branches/oracle-tweaks/lib/DBIx/Class/InflateColumn/DateTime.pm
   DBIx-Class/0.08/branches/oracle-tweaks/t/73oracle.t
   DBIx-Class/0.08/branches/oracle-tweaks/t/73oracle_inflate.t
   DBIx-Class/0.08/branches/oracle-tweaks/t/lib/DBICTest/Schema/Track.pm
   DBIx-Class/0.08/branches/oracle-tweaks/t/lib/sqlite.sql
Log:
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.


Modified: DBIx-Class/0.08/branches/oracle-tweaks/lib/DBIx/Class/InflateColumn/DateTime.pm
===================================================================
--- DBIx-Class/0.08/branches/oracle-tweaks/lib/DBIx/Class/InflateColumn/DateTime.pm	2009-05-03 09:13:48 UTC (rev 6116)
+++ DBIx-Class/0.08/branches/oracle-tweaks/lib/DBIx/Class/InflateColumn/DateTime.pm	2009-05-04 01:58:03 UTC (rev 6117)
@@ -94,7 +94,7 @@
 
   my $type;
 
-  for (qw/date datetime/) {
+  for (qw/date datetime timestamp/) {
     my $key = "inflate_${_}";
 
     next unless exists $info->{$key};
@@ -106,7 +106,6 @@
 
   unless ($type) {
     $type = lc($info->{data_type});
-    $type = 'datetime' if ($type =~ /^timestamp/);
   }
 
   my $timezone;
@@ -128,7 +127,7 @@
 
   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 @@
         {
           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 @@
           },
           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 @@
                 $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) {

Modified: DBIx-Class/0.08/branches/oracle-tweaks/t/73oracle.t
===================================================================

Modified: DBIx-Class/0.08/branches/oracle-tweaks/t/73oracle_inflate.t
===================================================================
--- DBIx-Class/0.08/branches/oracle-tweaks/t/73oracle_inflate.t	2009-05-03 09:13:48 UTC (rev 6116)
+++ DBIx-Class/0.08/branches/oracle-tweaks/t/73oracle_inflate.t	2009-05-04 01:58:03 UTC (rev 6117)
@@ -17,12 +17,13 @@
         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 $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( $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 {

Modified: DBIx-Class/0.08/branches/oracle-tweaks/t/lib/DBICTest/Schema/Track.pm
===================================================================
--- DBIx-Class/0.08/branches/oracle-tweaks/t/lib/DBICTest/Schema/Track.pm	2009-05-03 09:13:48 UTC (rev 6116)
+++ DBIx-Class/0.08/branches/oracle-tweaks/t/lib/DBICTest/Schema/Track.pm	2009-05-04 01:58:03 UTC (rev 6117)
@@ -26,6 +26,10 @@
     accessor => 'updated_date',
     is_nullable => 1
   },
+  last_updated_at => {
+    data_type => 'datetime',
+    is_nullable => 1
+  },
 );
 __PACKAGE__->set_primary_key('trackid');
 

Modified: DBIx-Class/0.08/branches/oracle-tweaks/t/lib/sqlite.sql
===================================================================
--- DBIx-Class/0.08/branches/oracle-tweaks/t/lib/sqlite.sql	2009-05-03 09:13:48 UTC (rev 6116)
+++ DBIx-Class/0.08/branches/oracle-tweaks/t/lib/sqlite.sql	2009-05-04 01:58:03 UTC (rev 6117)
@@ -375,7 +375,8 @@
   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);




More information about the Bast-commits mailing list