From: Rafael Kitover Date: Sat, 13 Jun 2009 11:03:36 +0000 (+0000) Subject: fixup _setup_connect_do, other minor cleanups X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9900b5695787f3292eb51df9a0a0f86ea42210c4;p=dbsrgits%2FDBIx-Class-Historic.git fixup _setup_connect_do, other minor cleanups --- diff --git a/Makefile.PL b/Makefile.PL index 940d78d..7f935b1 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -81,7 +81,7 @@ my %force_requires_if_author = ( 'DateTime::Format::MySQL' => 0, 'DateTime::Format::Pg' => 0, - # t/73oracle.t + # t/73oracle_inflate.t 'DateTime::Format::Oracle' => 0, # t/96_is_deteministic_value.t diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 5ebbab5..fd30265 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -241,12 +241,15 @@ above. =back -=item set_datetime_format +=item datetime_setup Execute any statements necessary to initialize the database session to return and accept datetime/timestamp values used with L. +Only necessary for some databases, see your specific storage driver for +implementation details. + =back =item on_disconnect_call @@ -474,26 +477,22 @@ sub _setup_connect_do { my $val = shift; - (my $call = $opt) =~ s/_do\z/_call/; + $self->throw_exception("The value of $opt cannot be 'undef'") + unless defined $val; - if (ref($self->$call) ne 'ARRAY') { - $self->$call([ - defined $self->$call ? $self->$call : () - ]); - } + my @store; if (not ref($val)) { - push @{ $self->$call }, [ 'do_sql', $val ]; + push @store, [ 'do_sql', $val ]; } elsif (ref($val) eq 'CODE') { - push @{ $self->$call }, $val; + push @store, $val; } elsif (ref($val) eq 'ARRAY') { - push @{ $self->$call }, - map [ 'do_sql', $_ ], @$val; + push @store, map [ 'do_sql', $_ ], @$val; } else { $self->throw_exception("Invalid type for $opt ".ref($val)); } - $self->$accessor($val); + $self->$accessor(\@store); } =head2 dbh_do @@ -642,9 +641,12 @@ sub disconnect { my ($self) = @_; if( $self->connected ) { - my $connection_call = $self->on_disconnect_call; - $self->_do_connection_actions(disconnect_call_ => $connection_call) - if $connection_call; + if (my $connection_call = $self->on_disconnect_call) { + $self->_do_connection_actions(disconnect_call_ => $connection_call) + } + if (my $connection_do = $self->_on_disconnect_do) { + $self->_do_connection_actions(disconnect_call_ => $connection_do) + } $self->_dbh->rollback unless $self->_dbh_autocommit; $self->_dbh->disconnect; @@ -761,9 +763,12 @@ sub _populate_dbh { # there is no transaction in progress by definition $self->{transaction_depth} = $self->_dbh_autocommit ? 0 : 1; - my $connection_call = $self->on_connect_call; - $self->_do_connection_actions(connect_call_ => $connection_call) - if $connection_call; + if (my $connection_call = $self->on_connect_call) { + $self->_do_connection_actions(connect_call_ => $connection_call) + } + if (my $connection_do = $self->_on_connect_do) { + $self->_do_connection_actions(connect_call_ => $connection_do) + } } sub _determine_driver { @@ -821,7 +826,7 @@ sub disconnect_call_do_sql { } # override in db-specific backend when necessary -sub connect_call_set_datetime_format { 1 } +sub connect_call_datetime_setup { 1 } sub _do_query { my ($self, $action) = @_; diff --git a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm index e1f6f29..4e733a2 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm @@ -183,11 +183,11 @@ L. sub datetime_parser_type { return "DateTime::Format::Oracle"; } -=head2 connect_call_set_datetime_format +=head2 connect_call_datetime_setup Used as: - on_connect_call => 'set_datetime_format' + on_connect_call => 'datetime_setup' In L to set the session nls date, and timestamp values for use with L and the @@ -200,9 +200,15 @@ already been set. C is also initialized but is not currently used by L. +These are the defaults used: + + $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS'; + $ENV{NLS_TIMESTAMP_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF'; + $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM'; + =cut -sub connect_call_set_datetime_format { +sub connect_call_datetime_setup { my $self = shift; my $dbh = $self->dbh; diff --git a/t/73oracle.t b/t/73oracle.t index 0e250c2..59f93fb 100644 --- a/t/73oracle.t +++ b/t/73oracle.t @@ -41,12 +41,10 @@ plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\'' unless ($dsn && $user && $pass); -plan tests => 36; +plan tests => 35; DBICTest::Schema->load_classes('ArtistFQN'); -my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { - on_connect_call => 'set_datetime_format' -}); +my $schema = DBICTest::Schema->connect($dsn, $user, $pass); my $dbh = $schema->storage->dbh; @@ -115,13 +113,11 @@ is($new->artistid, 1, "Oracle Auto-PK worked"); $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } ); is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" ); -# test join with row count ambiguity, and DateTime inflation - -my $dt = DateTime->now; +# test join with row count ambiguity my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' }); my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1, - position => 1, title => 'Track1', last_updated_on => $dt }); + position => 1, title => 'Track1' }); my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, { join => 'cd', rows => 2 } @@ -130,7 +126,6 @@ my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, ok(my $row = $tjoin->next); is($row->title, 'Track1', "ambiguous column ok"); -is($row->updated_date, $dt, "DateTime inflation/deflation ok"); # check count distinct with multiple columns my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' }); diff --git a/t/73oracle_inflate.t b/t/73oracle_inflate.t index 0f2fc23..949a6c3 100644 --- a/t/73oracle_inflate.t +++ b/t/73oracle_inflate.t @@ -17,7 +17,7 @@ else { plan skip_all => 'needs DateTime and DateTime::Format::Oracle for testing'; } else { - plan tests => 7; + plan tests => 9; } } @@ -67,9 +67,33 @@ $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"); +# test datetime_setup + +$schema->storage->disconnect; + +delete $ENV{NLS_DATE_FORMAT}; +delete $ENV{NLS_TIMESTAMP_FORMAT}; + +$schema->connection($dsn, $user, $pass, { + on_connect_call => 'datetime_setup' +}); + +$dt = DateTime->now(); + +my $timestamp = $dt->clone; +$timestamp->millisecond( 80 ); + +$track = $schema->resultset('Track')->find( 1 ); +$track->update({ last_updated_on => $dt, last_updated_at => $timestamp }); + +$track = $schema->resultset('Track')->find(1); + +is( $track->last_updated_on, $dt, 'DateTime round-trip as DATE' ); +is( $track->last_updated_at, $timestamp, 'DateTime round-trip as TIMESTAMP' ); + # clean up our mess END { - if($dbh) { + if($schema && ($dbh = $schema->storage->dbh)) { $dbh->do("DROP TABLE track"); } } diff --git a/t/92storage.t b/t/92storage.t index 94bdfd3..d8bba98 100644 --- a/t/92storage.t +++ b/t/92storage.t @@ -162,7 +162,7 @@ for my $type (keys %$invocations) { is_deeply ( [$storage->on_connect_do, $storage->on_disconnect_do ], - [ [qw/a b c/], [qw/d e f/] ], + [ [ map [ do_sql => $_ ], qw/a b c/ ], [ map [ do_sql => $_ ], qw/d e f/ ] ], "$type correctly parsed DBIC specific on_[dis]connect_do", ); } diff --git a/t/92storage_on_connect_call.t b/t/92storage_on_connect_call.t index d0d937b..09befcd 100644 --- a/t/92storage_on_connect_call.t +++ b/t/92storage_on_connect_call.t @@ -3,7 +3,7 @@ use warnings; no warnings qw/once redefine/; use lib qw(t/lib); -require DBICTest; +use DBICTest; use Test::More tests => 9; @@ -31,6 +31,7 @@ ok $schema->connection( [ do_sql => [ 'insert into test1 values (?)', {}, 1 ] ], [ do_sql => sub { ['insert into test1 values (2)'] } ], [ sub { $_[0]->dbh->do($_[1]) }, 'insert into test1 values (3)' ], + # this invokes $storage->connect_call_foo('bar') (above) [ foo => 'bar' ], ], on_connect_do => 'insert into test1 values (4)',