--- nfo/perl/libs/Data/Storage/Handler/DBI.pm 2002/10/10 03:44:07 1.1 +++ nfo/perl/libs/Data/Storage/Handler/DBI.pm 2002/10/25 11:43:27 1.2 @@ -1,8 +1,12 @@ ################################# # -# $Id: DBI.pm,v 1.1 2002/10/10 03:44:07 cvsjoko Exp $ +# $Id: DBI.pm,v 1.2 2002/10/25 11:43:27 joko Exp $ # # $Log: DBI.pm,v $ +# Revision 1.2 2002/10/25 11:43:27 joko +# + enhanced robustness +# + more logging for debug-levels +# # Revision 1.1 2002/10/10 03:44:07 cvsjoko # + new # @@ -32,11 +36,32 @@ # create handle if ( my $dsn = $self->{dbi}->{dsn} ) { - $logger->debug( __PACKAGE__ . "->connect($dsn)" ); - $self->{COREHANDLE} = DBI->connect($dsn); + $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); + + # HACK: + # set errorhandler before actually calling DBI->connect + # in order to catch errors from the very beginning + #DBI->{HandleError} = $self->{dbi}->{HandleError}; + + #use Data::Dumper; print Dumper($self->{dbi}); + + $self->{COREHANDLE} = DBI->connect( + $dsn, '', '', { + RaiseError => $self->{dbi}->{RaiseError}, + #RaiseError => 1, + PrintError => $self->{dbi}->{PrintError}, + HandleError => $self->{dbi}->{HandleError}, + } + ); + if (!$self->{COREHANDLE}) { + $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); + return; + } } $self->configureCOREHANDLE(); + return 1; + } sub configureCOREHANDLE { @@ -64,6 +89,18 @@ sub _sendSql { my $self = shift; my $sql = shift; + + # two-level handling for implicit connect: + # if there's no corehandle ... + if (!$self->{COREHANDLE}) { + # ... try to connect, but ... + $self->connect(); + # ... if this still fails, there's something wrong probably, so we won't continue + if (!$self->{COREHANDLE}) { + return; + } + } + my $sth = $self->{COREHANDLE}->prepare($sql); $sth->execute(); return $sth; @@ -72,6 +109,7 @@ sub sendCommand { my $self = shift; my $command = shift; + #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); my $cmdHandle = $self->_sendSql($command); my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); return $result; @@ -157,12 +195,12 @@ sub DESTROY { my $self = shift; #$logger->debug( __PACKAGE__ . "->" . "DESTROY" ); - $self->{RESULTHANDLE}->finish(); + $self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish(); } sub _getNextEntry { my $self = shift; - return $self->{RESULTHANDLE}->fetchrow_hashref; + $self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref; }