From: ivan Date: Wed, 11 Oct 2000 17:31:40 +0000 (+0000) Subject: explicitly disconnect if created_dbh X-Git-Tag: DBIx_DBSchema_0_13~1 X-Git-Url: http://git.freeside.biz/gitweb/?a=commitdiff_plain;h=b8a4cba59bc9186bf7a959b737fbe0a1fff3ed2c;p=DBIx-DBSchema.git explicitly disconnect if created_dbh --- diff --git a/DBSchema.pm b/DBSchema.pm index 6c20d05..e88baa8 100644 --- a/DBSchema.pm +++ b/DBSchema.pm @@ -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 diff --git a/DBSchema/Column.pm b/DBSchema/Column.pm index 75c80b2..d036965 100644 --- a/DBSchema/Column.pm +++ b/DBSchema/Column.pm @@ -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; } diff --git a/DBSchema/Table.pm b/DBSchema/Table.pm index bfb3779..1c2577e 100644 --- a/DBSchema/Table.pm +++ b/DBSchema/Table.pm @@ -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; } #