explicitly disconnect if created_dbh
authorivan <ivan>
Wed, 11 Oct 2000 17:31:40 +0000 (17:31 +0000)
committerivan <ivan>
Wed, 11 Oct 2000 17:31:40 +0000 (17:31 +0000)
DBSchema.pm
DBSchema/Column.pm
DBSchema/Table.pm

index 6c20d05..e88baa8 100644 (file)
@@ -214,9 +214,14 @@ specified database, will attempt to use generic SQL syntax.
 
 sub sql {
   my($self, $dbh) = (shift, shift);
-  $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr
-    unless ref($dbh) || ! @_;
-  map { $self->table($_)->sql_create_table($dbh); } $self->tables;
+  my $created_dbh = 0;
+  unless ( ref($dbh) || ! @_ ) {
+    $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr;
+    $created_dbh = 1;
+  }
+  my @r = map { $self->table($_)->sql_create_table($dbh); } $self->tables;
+  $dbh->disconnect if $created_dbh;
+  @r;
 }
 
 =item pretty_print
index 75c80b2..d036965 100644 (file)
@@ -208,7 +208,14 @@ for other engines (if applicable) may also be supported in the future.
 =cut
 
 sub line {
-  my($self,$dbh)=@_;
+  my($self,$dbh) = (shift, shift);
+
+  my $created_dbh = 0;
+  unless ( ref($dbh) || ! @_ ) {
+    $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr;
+    my $gratuitous = $DBI::errstr; #surpress superfluous `used only once' error
+    $created_dbh = 1;
+  }
   
   my $driver = DBIx::DBSchema::_load_driver($dbh);
   my %typemap;
@@ -240,7 +247,7 @@ sub line {
     $null =~ s/^NULL$//;
   }
 
-  join(' ',
+  my @r = join(' ',
     $self->name,
     $type. ( $self->length ? '('.$self->length.')' : '' ),
     $null,
@@ -253,6 +260,8 @@ sub line {
       : ''
     ),
   );
+  $dbh->disconnect if $created_dbh;
+  @r;
 
 }
 
index bfb3779..1c2577e 100644 (file)
@@ -306,10 +306,13 @@ supported in the future.
 
 sub sql_create_table { 
   my($self, $dbh) = (shift, shift);
-  $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr
-    unless ref($dbh) || ! @_;
-  my $gratuitous = $DBI::errstr; #surpress superfluous `used only once' error
 
+  my $created_dbh = 0;
+  unless ( ref($dbh) || ! @_ ) {
+    $dbh = DBI->connect( $dbh, @_ ) or die $DBI::errstr;
+    my $gratuitous = $DBI::errstr; #surpress superfluous `used only once' error
+    $created_dbh = 1;
+  }
   #false laziness: nicked from DBSchema::_load_driver
   my $driver;
   if ( ref($dbh) ) {
@@ -330,19 +333,21 @@ sub sql_create_table {
     push @columns, map "INDEX ($_)", $self->index->sql_list;
   }
 
-  "CREATE TABLE ". $self->name. " (\n  ". join(",\n  ", @columns). "\n)\n",
-  ( map {
-    my($index) = $self->name. "__". $_ . "_index";
-    $index =~ s/,\s*/_/g;
-    "CREATE UNIQUE INDEX $index ON ". $self->name. " ($_)\n"
-  } $self->unique->sql_list ),
-  ( map {
-    my($index) = $self->name. "__". $_ . "_index";
-    $index =~ s/,\s*/_/g;
-    "CREATE INDEX $index ON ". $self->name. " ($_)\n"
-  } $self->index->sql_list ),
+  my @r =
+    "CREATE TABLE ". $self->name. " (\n  ". join(",\n  ", @columns). "\n)\n",
+    ( map {
+      my($index) = $self->name. "__". $_ . "_index";
+      $index =~ s/,\s*/_/g;
+      "CREATE UNIQUE INDEX $index ON ". $self->name. " ($_)\n"
+    } $self->unique->sql_list ),
+    ( map {
+      my($index) = $self->name. "__". $_ . "_index";
+      $index =~ s/,\s*/_/g;
+      "CREATE INDEX $index ON ". $self->name. " ($_)\n"
+    } $self->index->sql_list ),
   ;  
-
+  $dbh->disconnect if $created_dbh;
+  @r;
 }
 
 #