6 use DBIx::Class::Optional::Dependencies ();
10 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
11 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
15 if (not ($dsn && $user && $pass)) {
16 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
17 'Warning: This test drops and creates a table called \'track\'';
20 # DateTime::Format::Oracle needs this set
21 $ENV{NLS_DATE_FORMAT} = 'DD-MON-YY';
22 $ENV{NLS_TIMESTAMP_FORMAT} = 'YYYY-MM-DD HH24:MI:SSXFF';
23 $ENV{NLS_LANG} = 'AMERICAN_AMERICA.WE8ISO8859P1';
24 $ENV{NLS_SORT} = "BINARY";
25 $ENV{NLS_COMP} = "BINARY";
27 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
29 # older oracles do not support a TIMESTAMP datatype
30 my $timestamp_datatype = ($schema->storage->_server_info->{normalized_dbms_version}||0) < 9
35 # Need to redefine the last_updated_on column
36 my $col_metadata = $schema->class('Track')->column_info('last_updated_on');
37 $schema->class('Track')->add_column( 'last_updated_on' => {
38 data_type => 'date' });
39 $schema->class('Track')->add_column( 'last_updated_at' => {
40 data_type => $timestamp_datatype });
42 my $dbh = $schema->storage->dbh;
44 #$dbh->do("alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SSXFF'");
47 $dbh->do("DROP TABLE track");
49 $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_datatype)");
51 # TODO is in effect for the rest of the tests
52 local $TODO = 'FIXME - something odd is going on with Oracle < 9 datetime support'
53 if ($schema->storage->_server_info->{normalized_dbms_version}||0) < 9;
57 # insert a row to play with
58 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' });
59 is($new->trackid, 1, "insert sucessful");
61 my $track = $schema->resultset('Track')->find( 1 );
63 is( ref($track->last_updated_on), 'DateTime', "last_updated_on inflated ok");
65 is( $track->last_updated_on->month, 5, "DateTime methods work on inflated column");
67 #note '$track->last_updated_at => ', $track->last_updated_at;
68 is( ref($track->last_updated_at), 'DateTime', "last_updated_at inflated ok");
70 is( $track->last_updated_at->nanosecond, 500_000_000, "DateTime methods work with nanosecond precision");
72 my $dt = DateTime->now();
73 $track->last_updated_on($dt);
74 $track->last_updated_at($dt);
77 is( $track->last_updated_on->month, $dt->month, "deflate ok");
78 is( int $track->last_updated_at->nanosecond, int $dt->nanosecond, "deflate ok with nanosecond precision");
82 $schema->storage->disconnect;
84 delete $ENV{NLS_DATE_FORMAT};
85 delete $ENV{NLS_TIMESTAMP_FORMAT};
87 $schema->connection($dsn, $user, $pass, {
88 on_connect_call => 'datetime_setup'
91 $dt = DateTime->now();
93 my $timestamp = $dt->clone;
94 $timestamp->set_nanosecond( int 500_000_000 );
96 $track = $schema->resultset('Track')->find( 1 );
97 $track->update({ last_updated_on => $dt, last_updated_at => $timestamp });
99 $track = $schema->resultset('Track')->find(1);
101 is( $track->last_updated_on, $dt, 'DateTime round-trip as DATE' );
102 is( $track->last_updated_at, $timestamp, 'DateTime round-trip as TIMESTAMP' );
104 is( int $track->last_updated_at->nanosecond, int 500_000_000,
105 'TIMESTAMP nanoseconds survived' );
107 } 'dateteime operations executed correctly';
113 if($schema && (my $dbh = $schema->storage->dbh)) {
114 $dbh->do("DROP TABLE track");