use SQL::Abstract::Limit;
use DBIx::Class::Storage::DBI::Cursor;
use IO::File;
+use Carp::Clan qw/DBIx::Class/;
BEGIN {
$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'));
}
=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;
}
$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;
}
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 {