[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] Added multi handle support for CGI::Application::Plugin::DBH


I've attached a patch to this message that adds simple multiple handle
support to CGI::Application::Plugin::DBH.pm. I have done some simple
tests with it to make sure that it seems to work. It's far from
thoroughly tested though.

There shouldn't be any changes to the existing interface DBH.pm provides
now. There should only be added features.

I'd appreciate any feedback anyone has. If there is interest in using it
I would be happy to create proper tests for it to be included in the
main distribution.

-- 
Tony Fraser
suppressed
Sybaspace Internet Solutions                        System Administrator
phone: (250) 246-5368                                fax: (250) 246-5398
--- DBH.pm.orig	2004-09-26 15:25:05.000000000 -0700
+++ DBH.pm	2004-09-26 17:01:22.000000000 -0700
@@ -13,41 +13,49 @@
     dbh_config
 );
 
-$VERSION = '1.00';
+$VERSION = '1.01';
 
 sub dbh {
     my $self = shift;
+    my $name = shift || "cgi_application_plugin_dbh.$$";
 
-    die "must call dbh_config() before calling dbh()." unless $self->{__DBH_CONFIG};
-
-    if (!$self->{__DBH}) {
+    die "must call dbh_config() before calling dbh()." unless $self->{__DBH_CONFIG}{$name};
 
+    unless( defined($self->{__DBH}{$name}) && $self->{__DBH}{$name}->ping ) {
         # create DBH object
-        if ($self->{__DBH_CONFIG}) {
+        if( $self->{__DBH_CONFIG}{$name} ) {
             require DBI;
             # use the parameters the user supplied
-            $self->{__DBH} = DBI->connect(@{ $self->{__DBH_CONFIG} });
+            $self->{__DBH}{$name} = DBI->connect(@{ $self->{__DBH_CONFIG}{$name} });
         } else {
         }
     }
 
-    return $self->{__DBH};
+    return $self->{__DBH}{$name};
 }
 
-sub dbh_config  {
+sub dbh_config {
     my $self = shift;
-    die "Calling dbh_config after the dbh has already been created" if (defined $self->{__DBH});
 
-    # See if a handle is being passed in directly.
     require UNIVERSAL;
-    if ((ref $_[0]) and $_[0]->isa('DBI::db')) {
-        $self->{__DBH} = $_[0];
+    my $name = undef;
+    $name = shift if( ref($_[1]) );
+    $name ||= "cgi_application_plugin_dbh.$$";
+    
+    die "Calling dbh_config after the dbh has already been created" if( defined $self->{__DBH}{$name} );
+
+    # See if a handle is being passed in directly.
+    if( ref($_[0]) eq 'ARRAY' ) {
+	$self->{__DBH_CONFIG}{$name} = shift;
+    }
+    elsif( ref($_[0]) and $_[0]->isa('DBI::db') ) {
+        $self->{__DBH}{$name} = shift;
 
         # Set this to note that we have completed the 'config' stage.
-        $self->{__DBH_CONFIG} = 1;
+        $self->{__DBH_CONFIG}{$name} = 1;
     }
     else {
-        $self->{__DBH_CONFIG} = suppressed;
+        $self->{__DBH_CONFIG}{$name} = suppressed;
     }
 
 }
@@ -68,19 +76,27 @@
 
     # use the same args as DBI->connect();
     $self->dbh_config($data_source, $username, $auth, \%attr);
+
+    # or to use more than one dbh
+    $self->dbh_config('my_handle', 
+		    [ $data_source, $user, $auth, \%attr ]);
+    $self->dbh_config('my_other_handle', 
+		    [ $data_source, $user, $auth, \%attr ]);
  }
 
  sub my_run_mode {
     my $self = shift;
 
     my $date = $self->dbh->selectrow_array("SELECT CURRENT_DATE");
+    # again with a named handle
+    $date = $self->dbh('my_handle')->selectrow_array("SELECT CURRENT_DATE");
 
     # OR ...
 
     my $dbh = $self->dbh;
+    # again with a named handle
+    $dbh = $self->dbh('my_other_handle');
     my $date = $dbh->selectrow_array("SELECT CURRENT_DATE");
-
-
  } 
 
 
@@ -97,10 +113,14 @@
 =head2 dbh()
 
  my $date = $self->dbh->selectrow_array("SELECT CURRENT_DATE");
+ # again with a named handle
+ $date = $self->dbh('my_handle')->selectrow_array("SELECT CURRENT_DATE");
 
  # OR ...
 
  my $dbh = $self->dbh;
+ # again with a named handle
+ $dbh = $self->dbh('my_other_handle');
  my $date = $dbh->selectrow_array("SELECT CURRENT_DATE");
 
 This method will return the current L<DBI|DBI> database handle.  The database handle is created on
@@ -114,9 +134,15 @@
     # use the same args as DBI->connect();
     $self->dbh_config($data_source, $username, $auth, \%attr);
 
+    # or to use more than one dbh
+    $self->dbh_config('my_handle', 
+		    [ $data_source, $user, $auth, \%attr ]);
+    $self->dbh_config('my_other_handle', 
+		    [ $data_source, $user, $auth, \%attr ]);
+
     # ...or use some existing handle you have
     $self->dbh_config($DBH);
-
+    $self->dbh_config('my_handle', $DBH);   # this works too
  }
 
 Used to provide your DBI connection parameters. You can either pass in an existing 
@@ -126,11 +152,6 @@
 stage of L<CGI::Application|CGI::Application>.  If this method is called after the database handle
 has already been accessed, then it will die with an error message.
 
-=head1 LIMITATIONS
-
-To keep things simple only one database handle is supported. Nothing prevents
-you from creating a second handle on your own.
-
 =head1 SEE ALSO
 
 L<Ima::DBI|Ima::DBI> is similar, but has much more complexity and features, including

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed

Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.