2008/10/14 Peter Rabbitson <[EMAIL PROTECTED]>:
> Not acceptable without tests. Please write some, and it will be applied.

New patch with tests in attachment.
Feel free to correct my bad English in POD.

-- 
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)
Index: t/92storage.t
===================================================================
--- t/92storage.t	(revision 4997)
+++ t/92storage.t	(working copy)
@@ -32,7 +32,7 @@
     }
 }
 
-plan tests => 6;
+plan tests => 9;
 
 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
 
@@ -64,10 +64,42 @@
 is(1, $schema->resultset('Artist')->search({name => "Exploding Sheep" })->count,
   "And the STH was retired");
 
-my $info = { on_connect_do => [] };
+my $info = { on_connect_do => [qw/a b c/] };
 
 $storage->connect_info(['foo','bar','baz',$info]);
 
 ok(exists($info->{on_connect_do}), q{Didn't kill key passed to storage});
 
+$storage->connect_info(
+  [
+    {
+      dsn         => 'foo',
+      #user       => 'bar',
+      password    => 'baz',
+      AutoCommit  => 1,
+      quote_char  => q{"},
+      name_sep    => q{.},
+    }, 
+    $info
+  ]
+);
+is_deeply
+  $storage->{_connect_info},
+  [ 
+    'foo',
+    undef,
+    'baz',
+    {
+      AutoCommit  => 1,
+      quote_char  => q{"},
+      name_sep    => q{.},
+    },
+  ],
+  'parse_connect_info() base check';
+is_deeply
+  $storage->{on_connect_do},
+  [ qw/a b c/ ],
+  'parse_connect_info() extra arg check';
+ok(exists($info->{on_connect_do}), q{parse_connect_info() didn't kill passed key});
+
 1;
Index: lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- lib/DBIx/Class/Storage/DBI.pm	(revision 4997)
+++ lib/DBIx/Class/Storage/DBI.pm	(working copy)
@@ -345,9 +345,12 @@
 encapsulates its argument list in an arrayref before calling
 C<connect_info> here.
 
-The arrayref can either contain the same set of arguments one would
+Before using argument processed via C<parse_connect_info>. After processing,
+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.
+
+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>
@@ -493,6 +496,19 @@
     ]
   );
 
+  # Same, but with hashref as argument
+  # See C<parse_connect_info> for explanation
+  ->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(
     [
@@ -513,6 +529,8 @@
 
   return $self->_connect_info if !$info_arg;
 
+  $info_arg = $self->parse_connect_info($info_arg);
+
   # Kill sql_maker/_sql_maker_opts, so we get a fresh one with only
   #  the new set of options
   $self->_sql_maker(undef);
@@ -553,6 +571,52 @@
 
 This method is deprecated in favor of setting via L</connect_info>.
 
+=head2 parse_connect_info
+
+This method should parse refarray argument, passed to C<connect_info>,
+and return refarray that can be directly used by C<connect_info>.
+
+If first element in input refarray is 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.
+All other arguments from input will be added to output without
+modification. After all, resulted array will contain the same
+set of arguments one would normally pass to L<DBI/connect>.
+
+If first element in input refarray isn't hashref, then input
+will be returned unmodified.
+
+Derived classes can redefine this method to parse arguments in
+some other way.
+
+=cut
+
+sub parse_connect_info {
+  my ($self, $info_arg) = @_;
+
+  if(ref $info_arg->[0] eq 'HASH') {
+    my @info_new = ();
+    my @info_arg = @$info_arg;
+
+    # parse pre-defined options
+    my $hash = shift @info_arg;
+    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 rest of input
+    push(@info_new, @info_arg) if @info_arg > 1;
+
+    $info_arg = [EMAIL PROTECTED];
+  }
+
+  return $info_arg;
+}
+
 =head2 dbh_do
 
 Arguments: ($subref | $method_name), @extra_coderef_args?
_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/[email protected]

Reply via email to