Subject: | Passing arguments to DBIx::Class::Storage::DBI::connect_info as single hashref |
This is not really bug, but extension proposal/request with patch.
Passing one hashref to connect_info is more clean and self-documented.
With this patch, for example, we can say in Catalyst-based application
config something like this:
<Model::DB>
schema_class App::DB
<connect_info>
dsn dbi:mysql:database=test
user testuser
password TestPass
AutoCommit 1
</connect_info>
</Model::DB>
Subject: | DBIx-Class-Storage-DBI-connect_info_with_hash.patch |
diff -Ewur DBIx-Class-0.08010_/lib/DBIx/Class/Storage/DBI.pm DBIx-Class-0.08010/lib/DBIx/Class/Storage/DBI.pm
--- DBIx-Class-0.08010_/lib/DBIx/Class/Storage/DBI.pm 2008-02-27 15:49:09.000000000 +0200
+++ DBIx-Class-0.08010/lib/DBIx/Class/Storage/DBI.pm 2008-08-26 03:10:58.000000000 +0300
@@ -343,7 +343,15 @@
The arrayref can either contain the same set of arguments one would
normally pass to L<DBI/connect>, or a lone code reference which returns
-a connected database handle. Please note that the L<DBI> docs
+a connected database handle, or a lone hash reference.
+
+In case with hashref, values with keys 'dsn', 'user' and 'password'
+from hash will be converted into array, in this order. If there
+other keys/values present - hashref to they will be added as last
+element of resulted array. After all, resulted array will contain
+the same set of arguments one would normally pass to L<DBI/connect>.
+
+Please note that the L<DBI> docs
recommend that you always explicitly set C<AutoCommit> to either
C<0> or C<1>. L<DBIx::Class> further recommends that it be set
to C<1>, and that you perform transactions via our L</txn_do>
@@ -483,6 +491,18 @@
]
);
+ # Same, but with hashref as argument
+ ->connect_info(
+ [{
+ dsn => 'dbi:Pg:dbname=foo',
+ user => 'postgres',
+ password => 'my_pg_password',
+ AutoCommit => 1,
+ quote_char => q{"},
+ name_sep => q{.},
+ }]
+ );
+
# Subref + DBIC-specific connection options
->connect_info(
[
@@ -503,6 +523,24 @@
return $self->_connect_info if !$info_arg;
+ if(ref $info_arg->[0] eq 'HASH') {
+ my @info_new = ();
+
+ # parse pre-defined options
+ my $hash = { %{$info_arg->[0]} };
+ for my $opt (qw/dsn user password/) {
+ push(@info_new, delete $hash->{$opt});
+ }
+
+ # parse rest of options
+ push(@info_new, $hash) if keys %$hash;
+
+ # save optional connect_info options
+ push(@info_new, $info_arg->[1]) if @$info_arg > 1;
+
+ $info_arg = \@info_new;
+ }
+
# Kill sql_maker/_sql_maker_opts, so we get a fresh one with only
# the new set of options
$self->_sql_maker(undef);
diff -Ewur DBIx-Class-0.08010_/t/92storage.t DBIx-Class-0.08010/t/92storage.t
--- DBIx-Class-0.08010_/t/92storage.t 2007-08-12 00:07:59.000000000 +0300
+++ DBIx-Class-0.08010/t/92storage.t 2008-08-26 03:21:13.000000000 +0300
@@ -32,7 +32,7 @@
}
}
-plan tests => 6;
+plan tests => 7;
my $schema = DBICTest->init_schema();
@@ -70,4 +70,21 @@
ok(exists($info->{on_connect_do}), q{Didn't kill key passed to storage});
+$storage->connect_info([{
+ dsn => 'foo',
+ user => 'bar',
+ password => 'baz',
+ on_connect_do => [],
+}]);
+
+my $connect_info = $storage->connect_info;
+
+ok( $connect_info->[0] eq 'foo'
+ && $connect_info->[1] eq 'bar'
+ && $connect_info->[2] eq 'baz'
+ && ref $connect_info->[3] eq 'HASH'
+ && exists($connect_info->[3]->{on_connect_do}),
+q{All args as on hashref passed successfully}
+);
+
1;