Author: timbo
Date: Mon May 24 14:04:31 2004
New Revision: 360

Modified:
   dbi/trunk/Changes
   dbi/trunk/DBI.pm
   dbi/trunk/t/10examp.t
Log:
Add DBI->parse_dsn()
Fixup t/10examp.t tests (prompted by Jeff Urlwin)
Use pattern for split of DBI_TRACE thanks to H.Merijn Brand


Modified: dbi/trunk/Changes
==============================================================================
--- dbi/trunk/Changes   (original)
+++ dbi/trunk/Changes   Mon May 24 14:04:31 2004
@@ -30,6 +30,7 @@
     to Stevan Little and Andy Lester. See http://qa.perl.org/phalanx/
   Changed Test::More minimum prerequisite version to 0.40 (2001).
 
+  Added DBI->parse_dsn($dsn) method.
   Added warning if build directory path contains whitespace.
   Removed "may change" warnings from the docs for table_info(),
     primary_key_info(), and foreign_key_info() methods.

Modified: dbi/trunk/DBI.pm
==============================================================================
--- dbi/trunk/DBI.pm    (original)
+++ dbi/trunk/DBI.pm    Mon May 24 14:04:31 2004
@@ -265,7 +265,7 @@
 
 use strict;
 
-DBI->trace(split '=', $ENV{DBI_TRACE}, 2) if $ENV{DBI_TRACE};
+DBI->trace(split /=/, $ENV{DBI_TRACE}, 2) if $ENV{DBI_TRACE};
 
 $DBI::connect_via = "connect";
 
@@ -483,7 +483,16 @@
     }
     %DBI::installed_drh = ();  # clear loaded drivers so they have a chance to 
reinitialize
 }
-       
+
+sub parse_dsn {
+    my ($class, $dsn) = @_;
+    $dsn =~ s/^(dbi):(\w*?)(?:\((.*?)\))?://i or return;
+    my ($scheme, $driver, $attr, $attr_hash) = (lc($1), $2, $3);
+    $driver ||= $ENV{DBI_DRIVER} || '';
+    $attr_hash = { split /\s*=>?\s*|\s*,\s*/, $attr, -1 } if $attr;
+    return ($scheme, $driver, $attr, $attr_hash, $dsn);
+}
+
 
 # --- The DBI->connect Front Door methods
 
@@ -491,6 +500,7 @@
     # For library code using connect_cached() with mod_perl
     # we redirect those calls to Apache::DBI::connect() as well
     my ($class, $dsn, $user, $pass, $attr) = @_;
+    # XXX modifies callers data!
     ($attr ||= {})->{dbi_connect_method} =
        ($DBI::connect_via eq "Apache::DBI::connect")
            ? 'Apache::DBI::connect' : 'connect_cached';
@@ -2264,6 +2274,23 @@
 
 =over 4
 
+=item C<parse_dsn>
+
+  ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn($dsn)
+      or die "Can't parse DBI DSN '$dsn'";
+
+Breaks apart a DBI Data Source Name (DSN) and returns the individual
+parts. If $dsn doesn't contain a valid DSN then parse_dsn() returns
+an empty list.
+
+$scheme is the first part of the DSN and is currently always 'dbi'.
+$driver is the driver name, possibly defaulted to $ENV{DBI_DRIVER},
+and may be undefined.  $attr_string is the optional attribute string,
+which may be undefined.  If $attr_string is true then $attr_hash
+is a reference to a hash containing the parsed attribute names and
+values. $driver_dsn is the last part of the DBI DSN string.
+
+
 =item C<connect>
 
   $dbh = DBI->connect($data_source, $username, $password)
@@ -6293,7 +6320,7 @@
 trace settings for the DBI at startup. Can also be used to direct
 trace output to a file. When the DBI is loaded it does:
 
-  DBI->trace(split '=', $ENV{DBI_TRACE}, 2) if $ENV{DBI_TRACE};
+  DBI->trace(split /=/, $ENV{DBI_TRACE}, 2) if $ENV{DBI_TRACE};
 
 So if C<DBI_TRACE> contains an "C<=>" character then what follows
 it is used as the name of the file to append the trace to.

Modified: dbi/trunk/t/10examp.t
==============================================================================
--- dbi/trunk/t/10examp.t       (original)
+++ dbi/trunk/t/10examp.t       Mon May 24 14:04:31 2004
@@ -460,10 +460,10 @@
 
 print "selectall_hashref\n";
 $r = $dbh->selectall_hashref($std_sql, 'NAME', undef, $dir);
-ok($r, $r);
-ok(ref $r eq 'HASH', ref $r);
-ok(keys %$r == $rows);
-ok($r->{ $row_a[2] }{SIZE} eq $row_a[1], qq{$r->{ $row_a[2] }{SIZE} eq $row_a[1]});
+ok($r, "selectall_hashref result");
+is(ref $r, 'HASH', "selectall_hashref HASH: ".ref $r);
+is(scalar keys %$r, $rows);
+is($r->{ $row_a[2] }{SIZE}, $row_a[1], qq{$r->{ $row_a[2] }{SIZE} eq $row_a[1]});
 
 print "selectall_hashref by column number\n";
 $r = $dbh->selectall_hashref($std_sql, 3, undef, $dir);

Reply via email to