[Bast-commits] r3679 - in DBIx-Class/0.08/branches/on_disconnect_do: lib/DBIx/Class/Storage t

tomboh at dev.catalyst.perl.org tomboh at dev.catalyst.perl.org
Wed Aug 15 09:25:48 GMT 2007


Author: tomboh
Date: 2007-08-15 09:25:47 +0100 (Wed, 15 Aug 2007)
New Revision: 3679

Modified:
   DBIx-Class/0.08/branches/on_disconnect_do/lib/DBIx/Class/Storage/DBI.pm
   DBIx-Class/0.08/branches/on_disconnect_do/t/92storage_on_connect_do.t
Log:
Let on_connect_do() and on_disconnect_do() take code references.
Provide tests and documentation for this.


Modified: DBIx-Class/0.08/branches/on_disconnect_do/lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- DBIx-Class/0.08/branches/on_disconnect_do/lib/DBIx/Class/Storage/DBI.pm	2007-08-15 08:24:32 UTC (rev 3678)
+++ DBIx-Class/0.08/branches/on_disconnect_do/lib/DBIx/Class/Storage/DBI.pm	2007-08-15 08:25:47 UTC (rev 3679)
@@ -346,15 +346,27 @@
 
 =item on_connect_do
 
-This can be set to an arrayref containing literal sql statements and
-code references, 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
 
-As with L<on_connect_do>, this takes an arrayref of literal sql
-statements and code references, but these statements execute immediately
-before disconnecting from the database.
+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.
@@ -658,9 +670,9 @@
   my ($self) = @_;
 
   if( $self->connected ) {
-    foreach (@{$self->on_disconnect_do || []}) {
-      $self->_do_query($_);
-    }
+    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);
@@ -752,19 +764,31 @@
     }
   }
 
-  foreach (@{$self->on_connect_do || []}) {
-    $self->_do_query($_);
-  }
+  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) = @_;
 
-  # $action contains either an SQL string or a code ref
-  if (ref $action) {
+  if (ref $action eq 'CODE') {
     $action->($self);
   }
   else {

Modified: DBIx-Class/0.08/branches/on_disconnect_do/t/92storage_on_connect_do.t
===================================================================
--- DBIx-Class/0.08/branches/on_disconnect_do/t/92storage_on_connect_do.t	2007-08-15 08:24:32 UTC (rev 3678)
+++ DBIx-Class/0.08/branches/on_disconnect_do/t/92storage_on_connect_do.t	2007-08-15 08:25:47 UTC (rev 3679)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 5;
+use Test::More tests => 9;
 
 use lib qw(t/lib);
 use base 'DBICTest';
@@ -26,6 +26,21 @@
 
 $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';




More information about the Bast-commits mailing list