From: Tom Hukins Date: Wed, 15 Aug 2007 08:25:47 +0000 (+0000) Subject: Let on_connect_do() and on_disconnect_do() take code references. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6d2e7a96f2b9f1e9f5718a421831344dcdb400a3;p=dbsrgits%2FDBIx-Class-Historic.git Let on_connect_do() and on_disconnect_do() take code references. Provide tests and documentation for this. --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 3245620..bc63abf 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -346,15 +346,27 @@ connection-specific options: =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, 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 and executes them +immediately before disconnecting from the database. Note, this only runs if you explicitly call L on the storage object. @@ -658,9 +670,9 @@ sub disconnect { 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 @@ sub _populate_dbh { } } - 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 { diff --git a/t/92storage_on_connect_do.t b/t/92storage_on_connect_do.t index 038e4d7..a736272 100644 --- a/t/92storage_on_connect_do.t +++ b/t/92storage_on_connect_do.t @@ -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 @@ 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';