From: Rafael Kitover Date: Sun, 30 Aug 2009 01:14:36 +0000 (+0000) Subject: add dbh_maker option to connect_info hash X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8fd069b9fbf5814f8186323a85e2e1bad6d6fc91;p=dbsrgits%2FDBIx-Class-Historic.git add dbh_maker option to connect_info hash --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 5fb3f82..abb2533 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -125,6 +125,9 @@ following config (L style): +The C/C/C combination can be substituted by the +C key whose value is a coderef that returns the C<$dbh>. + =back Please note that the L docs recommend that you always explicitly @@ -337,6 +340,12 @@ L # Connect via subref ->connect_info([ sub { DBI->connect(...) } ]); + # Connect via subref in hashref + ->connect_info([{ + dbh_maker => sub { DBI->connect(...) }, + on_connect_do => 'alter session ...', + }]); + # A bit more complicated ->connect_info( [ @@ -407,8 +416,15 @@ sub connect_info { elsif (ref $args[0] eq 'HASH') { # single hashref (i.e. Catalyst config) %attrs = %{$args[0]}; @args = (); - for (qw/password user dsn/) { - unshift @args, delete $attrs{$_}; + if (my $code = delete $attrs{dbh_maker}) { + @args = $code; + if (delete @attrs{qw/dsn user password/}) { + warn 'dsn/user/password ignored when dbh_maker coderef used in ' . + 'connect_info'; + } + } + else { + @args = delete @attrs{qw/dsn user password/}; } } else { # otherwise assume dsn/user/password + \%attrs + \%extra_attrs diff --git a/t/storage/base.t b/t/storage/base.t index c8a0bba..138465d 100644 --- a/t/storage/base.t +++ b/t/storage/base.t @@ -33,7 +33,7 @@ use Data::Dumper; } } -plan tests => 17; +plan tests => 21; my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); @@ -145,6 +145,16 @@ my $invocations = { }, ], }, + 'connect_info ([ \%attr_with_coderef ])' => { + args => [ { + dbh_maker => $coderef, + on_connect_do => [qw/a b c/], + on_disconnect_do => [qw/d e f/], + } ], + dbi_connect_info => [ + $coderef + ], + }, }; for my $type (keys %$invocations) {