[Bast-commits] r3704 - in DBIx-Class/0.08/branches/cdbicompat_integration: . lib/DBIx/Class/Storage lib/DBIx/Class/Storage/DBI/Oracle t t/cdbi-t t/lib

matthewt at dev.catalyst.perl.org matthewt at dev.catalyst.perl.org
Tue Aug 21 20:56:43 GMT 2007


Author: matthewt
Date: 2007-08-21 20:56:43 +0100 (Tue, 21 Aug 2007)
New Revision: 3704

Added:
   DBIx-Class/0.08/branches/cdbicompat_integration/t/73oracle_inflate.t
   DBIx-Class/0.08/branches/cdbicompat_integration/t/92storage_on_connect_do.t
Modified:
   DBIx-Class/0.08/branches/cdbicompat_integration/
   DBIx-Class/0.08/branches/cdbicompat_integration/Changes
   DBIx-Class/0.08/branches/cdbicompat_integration/Makefile.PL
   DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI.pm
   DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
   DBIx-Class/0.08/branches/cdbicompat_integration/t/cdbi-t/11-triggers.t
   DBIx-Class/0.08/branches/cdbicompat_integration/t/lib/DBICTest.pm
Log:
 r11194 at jules (orig r3695):  matthewt | 2007-08-21 19:24:53 +0100
  r10460 at jules (orig r3640):  tomboh | 2007-08-01 12:27:38 +0100
  Add an 'on_disconnect_do' argument to
  DBIx::Class::Storage::DBI::connect_info that, on disconnection, do what
  'on_connect_do' does on connection.  Currently, this only works if the
  code explicitly calls disconnect() on the Storage object.
  
  While I'm here, make both 'on_connect_do' and 'on_disconnect_do' accept
  code references as well as strings containing SQL statements.
  
  Finally, remove code to call compose_connection() from DBICTest.pm that
  never gets called any more.
  
 
 r11195 at jules (orig r3696):  matthewt | 2007-08-21 19:24:57 +0100
  r11152 at jules (orig r3678):  tomboh | 2007-08-15 09:24:32 +0100
  Restore code that I removed in revision 3640 that tests still need
  
 
 r11196 at jules (orig r3697):  matthewt | 2007-08-21 19:25:00 +0100
  r11153 at jules (orig r3679):  tomboh | 2007-08-15 09:25:47 +0100
  Let on_connect_do() and on_disconnect_do() take code references.
  Provide tests and documentation for this.
  
 
 r11197 at jules (orig r3698):  matthewt | 2007-08-21 19:25:33 +0100
 
 r11199 at jules (orig r3700):  matthewt | 2007-08-21 19:50:00 +0100
 arrayrefs for on_connect_do
 r11200 at jules (orig r3701):  matthewt | 2007-08-21 19:53:44 +0100
 note on_connect_do changes
 r11201 at jules (orig r3702):  matthewt | 2007-08-21 20:17:22 +0100
 oracle datetime inflator patch
 r11202 at jules (orig r3703):  matthewt | 2007-08-21 20:52:09 +0100
 force CDBICompat deps for developers, fix tests to work with latest Class::Trigger



Property changes on: DBIx-Class/0.08/branches/cdbicompat_integration
___________________________________________________________________
Name: svk:merge
   - 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
9c88509d-e914-0410-b01c-b9530614cbfe:/local/DBIx-Class:32260
9c88509d-e914-0410-b01c-b9530614cbfe:/local/DBIx-Class-CDBICompat:32708
9c88509d-e914-0410-b01c-b9530614cbfe:/vendor/DBIx-Class:31122
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/DBIx-Class/0.08/trunk:3684
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-current:2222
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3606
   + 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
9c88509d-e914-0410-b01c-b9530614cbfe:/local/DBIx-Class:32260
9c88509d-e914-0410-b01c-b9530614cbfe:/local/DBIx-Class-CDBICompat:32708
9c88509d-e914-0410-b01c-b9530614cbfe:/vendor/DBIx-Class:31122
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/DBIx-Class/0.08/branches/on_disconnect_do:3694
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/DBIx-Class/0.08/trunk:3703
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-current:2222
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3606

Modified: DBIx-Class/0.08/branches/cdbicompat_integration/Changes
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/Changes	2007-08-21 19:52:09 UTC (rev 3703)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/Changes	2007-08-21 19:56:43 UTC (rev 3704)
@@ -1,5 +1,9 @@
 Revision history for DBIx::Class
 
+        - patch for Oracle datetime inflation (abram at arin.net)
+        - added on_disconnect_do
+        - on_connect_do and on_disconnect_do take coderefs and arrayrefs
+
 0.08006 2007-08-12 15:12:00
         - Move to using Class::C3::Componentised
         - Remove warn statement from DBIx::Class::Row

Modified: DBIx-Class/0.08/branches/cdbicompat_integration/Makefile.PL
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/Makefile.PL	2007-08-21 19:52:09 UTC (rev 3703)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/Makefile.PL	2007-08-21 19:56:43 UTC (rev 3704)
@@ -29,9 +29,11 @@
 
 tests "t/*.t t/*/*.t";
 
-# re-build README if we're in an svk checkout
-if( -e 'MANIFEST.SKIP' ) {
-    system('pod2text lib/DBIx/Class.pm > README');
+# re-build README and require CDBI modules for testing if we're in a checkout
+if( -e 'inc/.author' ) {
+  build_requires 'DBIx::ContextualFetch';
+  build_requires 'Class::Trigger';
+  system('pod2text lib/DBIx/Class.pm > README');
 }
 
 auto_provides;

Modified: DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm	2007-08-21 19:52:09 UTC (rev 3703)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm	2007-08-21 19:56:43 UTC (rev 3704)
@@ -84,6 +84,15 @@
   $self->next::method(uc($table));
 }
 
+=head2 datetime_parser_type
+
+This sets the proper DateTime::Format module for use with
+L<DBIx::Class::InflateColumn::DateTime>.
+
+=cut
+
+sub datetime_parser_type { return "DateTime::Format::Oracle"; }
+
 =head1 AUTHORS
 
 Andy Grundman <andy at hybridized.org>

Modified: DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI.pm	2007-08-21 19:52:09 UTC (rev 3703)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/lib/DBIx/Class/Storage/DBI.pm	2007-08-21 19:56:43 UTC (rev 3704)
@@ -14,7 +14,7 @@
 __PACKAGE__->mk_group_accessors('simple' =>
     qw/_connect_info _dbi_connect_info _dbh _sql_maker _sql_maker_opts
        _conn_pid _conn_tid disable_sth_caching on_connect_do
-       transaction_depth unsafe _dbh_autocommit/
+       on_disconnect_do transaction_depth unsafe _dbh_autocommit/
 );
 
 __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::Cursor');
@@ -347,10 +347,31 @@
 
 =item on_connect_do
 
-This can be set to an arrayref of literal sql statements, which will
-be executed immediately after making the connection to the database
-every time we [re-]connect.
+Specifies things to do immediately after connecting or re-connecting to
+the database.  Its value may contain:
 
+=over
+
+=item an array reference
+
+This contains SQL statements to execute in order.  Each element contains
+a string or a code reference that returns a string.
+
+=item a code reference
+
+This contains some code to execute.  Unlike code references within an
+array reference, its return value is ignored.
+
+=back
+
+=item on_disconnect_do
+
+Takes arguments in the same for as L<on_connect_do> and executes them
+immediately before disconnecting from the database.
+
+Note, this only runs if you explicitly call L<disconnect> on the
+storage object.
+
 =item disable_sth_caching
 
 If set to a true value, this option will disable the caching of
@@ -481,9 +502,10 @@
   my $last_info = $dbi_info->[-1];
   if(ref $last_info eq 'HASH') {
     $last_info = { %$last_info }; # so delete is non-destructive
-    for my $storage_opt (
-        qw/on_connect_do disable_sth_caching unsafe cursor_class/
-      ) {
+    my @storage_option = qw(
+      on_connect_do on_disconnect_do disable_sth_caching unsafe cursor_class
+    );
+    for my $storage_opt (@storage_option) {
       if(my $value = delete $last_info->{$storage_opt}) {
         $self->$storage_opt($value);
       }
@@ -650,6 +672,9 @@
   my ($self) = @_;
 
   if( $self->connected ) {
+    my $connection_do = $self->on_disconnect_do;
+    $self->_do_connection_actions($connection_do) if ref($connection_do);
+
     $self->_dbh->rollback unless $self->_dbh_autocommit;
     $self->_dbh->disconnect;
     $self->_dbh(undef);
@@ -742,17 +767,43 @@
     }
   }
 
-  # if on-connect sql statements are given execute them
-  foreach my $sql_statement (@{$self->on_connect_do || []}) {
-    $self->_query_start($sql_statement);
-    $self->_dbh->do($sql_statement);
-    $self->_query_end($sql_statement);
-  }
+  my $connection_do = $self->on_connect_do;
+  $self->_do_connection_actions($connection_do) if ref($connection_do);
 
   $self->_conn_pid($$);
   $self->_conn_tid(threads->tid) if $INC{'threads.pm'};
 }
 
+sub _do_connection_actions {
+  my $self = shift;
+  my $connection_do = shift;
+
+  if (ref $connection_do eq 'ARRAY') {
+    $self->_do_query($_) foreach @$connection_do;
+  }
+  elsif (ref $connection_do eq 'CODE') {
+    $connection_do->();
+  }
+
+  return $self;
+}
+
+sub _do_query {
+  my ($self, $action) = @_;
+
+  if (ref $action eq 'CODE') {
+    $action->($self);
+  }
+  else {
+    my @to_run = (ref $action eq 'ARRAY') ? (@$action) : ($action);
+    $self->_query_start(@to_run);
+    $self->_dbh->do(@to_run);
+    $self->_query_end(@to_run);
+  }
+
+  return $self;
+}
+
 sub _connect {
   my ($self, @info) = @_;
 

Added: DBIx-Class/0.08/branches/cdbicompat_integration/t/73oracle_inflate.t
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/t/73oracle_inflate.t	                        (rev 0)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/t/73oracle_inflate.t	2007-08-21 19:56:43 UTC (rev 3704)
@@ -0,0 +1,64 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
+
+eval "use DateTime; use DateTime::Format::Oracle;";
+if ($@) {
+    plan skip_all => 'needs DateTime and DateTime::Format::Oracle for testing';
+}
+elsif (not ($dsn && $user && $pass)) {
+    plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
+         'Warning: This test drops and creates a table called \'track\'';
+}
+else {
+    plan tests => 4;
+}
+
+# DateTime::Format::Oracle needs this set
+$ENV{NLS_DATE_FORMAT} = 'DD-MON-YY';
+
+my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
+
+# Need to redefine the last_updated_on column
+my $col_metadata = $schema->class('Track')->column_info('last_updated_on');
+$schema->class('Track')->add_column( 'last_updated_on' => {
+    data_type => 'date' });
+
+my $dbh = $schema->storage->dbh;
+
+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)");
+
+# 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' });
+is($new->trackid, 1, "insert sucessful");
+
+my $track = $schema->resultset('Track')->find( 1 );
+
+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");
+
+my $dt = DateTime->now();
+$track->last_updated_on($dt);
+$track->update;
+
+is( $track->last_updated_on->month, $dt->month, "deflate ok");
+
+# clean up our mess
+END {
+    # Set the metadata back for the last_updated_on column
+    $schema->class('Track')->add_column( 'last_updated_on' => $col_metadata );
+
+    if($dbh) {
+        $dbh->do("DROP TABLE track");
+    }
+}
+

Added: DBIx-Class/0.08/branches/cdbicompat_integration/t/92storage_on_connect_do.t
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/t/92storage_on_connect_do.t	                        (rev 0)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/t/92storage_on_connect_do.t	2007-08-21 19:56:43 UTC (rev 3704)
@@ -0,0 +1,59 @@
+use strict;
+use warnings;
+
+use Test::More tests => 9;
+
+use lib qw(t/lib);
+use base 'DBICTest';
+
+
+my $schema = DBICTest->init_schema(
+    no_connect  => 1,
+    no_deploy   => 1,
+);
+ok $schema->connection(
+    DBICTest->_database,
+    {
+        on_connect_do       => [
+            'CREATE TABLE TEST_empty (id INTEGER)',
+            [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ],
+        ],
+        on_disconnect_do    =>
+            [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
+    },
+), 'connection()';
+
+is_deeply
+  $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
+  [ [ 2 ] ],
+  'on_connect_do() worked';
+eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); };
+ok $@, 'Searching for nonexistent table dies';
+
+$schema->storage->disconnect();
+
+my($connected, $disconnected);
+ok $schema->connection(
+    DBICTest->_database,
+    {
+        on_connect_do       => sub { $connected = 1 },
+        on_disconnect_do    => sub { $disconnected = 1 },
+    },
+), 'second connection()';
+$schema->storage->dbh->do('SELECT 1');
+ok $connected, 'on_connect_do() called after connect()';
+ok ! $disconnected, 'on_disconnect_do() not called after connect()';
+$schema->storage->disconnect();
+ok $disconnected, 'on_disconnect_do() called after disconnect()';
+
+
+sub check_exists {
+    my $storage = shift;
+    ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
+}
+
+sub check_dropped {
+    my $storage = shift;
+    eval { $storage->dbh->do('SELECT 1 FROM TEST_empty'); };
+    ok $@, 'Reading from dropped table fails';
+}

Modified: DBIx-Class/0.08/branches/cdbicompat_integration/t/cdbi-t/11-triggers.t
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/t/cdbi-t/11-triggers.t	2007-08-21 19:52:09 UTC (rev 3703)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/t/cdbi-t/11-triggers.t	2007-08-21 19:56:43 UTC (rev 3704)
@@ -25,13 +25,11 @@
 
 sub default_rating { $_[0]->Rating(15); }
 
-Film->add_trigger(
-	before_create => \&default_rating,
-	after_create  => \&create_trigger2,
-	after_delete  => \&delete_trigger,
-	before_update => \&pre_up_trigger,
-	after_update  => \&pst_up_trigger,
-);
+Film->add_trigger(before_create => \&default_rating);
+Film->add_trigger(after_create  => \&create_trigger2);
+Film->add_trigger(after_delete  => \&delete_trigger);
+Film->add_trigger(before_update => \&pre_up_trigger);
+Film->add_trigger(after_update  => \&pst_up_trigger);
 
 ok(
 	my $ver = Film->create({

Modified: DBIx-Class/0.08/branches/cdbicompat_integration/t/lib/DBICTest.pm
===================================================================
--- DBIx-Class/0.08/branches/cdbicompat_integration/t/lib/DBICTest.pm	2007-08-21 19:52:09 UTC (rev 3703)
+++ DBIx-Class/0.08/branches/cdbicompat_integration/t/lib/DBICTest.pm	2007-08-21 19:56:43 UTC (rev 3704)
@@ -42,9 +42,8 @@
 
 =cut
 
-sub init_schema {
+sub _database {
     my $self = shift;
-    my %args = @_;
     my $db_file = "t/var/DBIxClass.db";
 
     unlink($db_file) if -e $db_file;
@@ -55,19 +54,28 @@
     my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
     my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
 
+    my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
+
+    return @connect_info;
+}
+
+sub init_schema {
+    my $self = shift;
+    my %args = @_;
+
     my $schema;
 
-    my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
-
     if ($args{compose_connection}) {
       $schema = DBICTest::Schema->compose_connection(
-                  'DBICTest', @connect_info
+                  'DBICTest', $self->_database
                 );
     } else {
-      $schema = DBICTest::Schema->compose_namespace('DBICTest')
-                                ->connect(@connect_info);
+      $schema = DBICTest::Schema->compose_namespace('DBICTest');
     }
-    $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
+    if ( !$args{no_connect} ) {
+      $schema = $schema->connect($self->_database);
+      $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
+    }
     if ( !$args{no_deploy} ) {
         __PACKAGE__->deploy_schema( $schema );
         __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );




More information about the Bast-commits mailing list