Patch from jesper to work round dumbass DBDs
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Storage / DBI.pm
index 47f88df..7cf09e6 100644 (file)
@@ -6,6 +6,7 @@ use DBI;
 use SQL::Abstract::Limit;
 use DBIx::Class::Storage::DBI::Cursor;
 use IO::File;
+use Carp::Clan qw/DBIx::Class/;
 
 BEGIN {
 
@@ -157,7 +158,7 @@ sub new {
   $new->transaction_depth(0);
   if (defined($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}) &&
      ($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} =~ /=(.+)$/)) {
-    $new->debugfh(IO::File->new($1, 'w')||die "Cannot open trace file $1");
+    $new->debugfh(IO::File->new($1, 'w')||croak "Cannot open trace file $1");
   } else {
     $new->debugfh(IO::File->new('>&STDERR'));
   }
@@ -197,12 +198,31 @@ L<DBIX_CLASS_STORAGE_DBI_DEBUG> environment variable.
 
 =cut
 
-sub dbh {
+sub disconnect {
+  my ($self) = @_;
+
+  $self->_dbh->disconnect if $self->_dbh;
+}
+
+sub connected {
   my ($self) = @_;
+
   my $dbh;
-  unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
+  (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping)
+}
+
+sub ensure_connected {
+  my ($self) = @_;
+
+  unless ($self->connected) {
     $self->_populate_dbh;
   }
+}
+
+sub dbh {
+  my ($self) = @_;
+
+  $self->ensure_connected;
   return $self->_dbh;
 }
 
@@ -280,13 +300,18 @@ sub _execute {
   $self->debugfh->print("$sql: @bind\n") if $self->debug;
   my $sth = $self->sth($sql,$op);
   @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
-  my $rv = $sth->execute(@bind);
+  my $rv;
+  if ($sth) {  
+    $rv = $sth->execute(@bind);
+  } else { 
+    croak "'$sql' did not generate a statement.";
+  }
   return (wantarray ? ($rv, $sth, @bind) : $rv);
 }
 
 sub insert {
   my ($self, $ident, $to_insert) = @_;
-  $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
+  croak( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
     unless ($self->_execute('insert' => [], $ident, $to_insert));
   return $to_insert;
 }
@@ -325,10 +350,14 @@ sub select {
   return $self->cursor->new($self, \@_, $attrs);
 }
 
+# Need to call finish() to work round broken DBDs
+
 sub select_single {
   my $self = shift;
   my ($rv, $sth, @bind) = $self->_select(@_);
-  return $sth->fetchrow_array;
+  my @row = $sth->fetchrow_array;
+  $sth->finish();
+  return @row;
 }
 
 sub sth {