--- 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/11/29 05:00:26 1.5 @@ -1,8 +1,23 @@ ################################# # -# $Id: DBI.pm,v 1.1 2002/10/10 03:44:07 cvsjoko Exp $ +# $Id: DBI.pm,v 1.5 2002/11/29 05:00:26 joko Exp $ # # $Log: DBI.pm,v $ +# Revision 1.5 2002/11/29 05:00:26 joko +# + sub getListUnfiltered +# + sub sendQuery +# +# Revision 1.4 2002/11/17 08:46:42 jonen +# + wrapped eval around DBI->connect to prevent deaths +# +# Revision 1.3 2002/11/17 06:34:39 joko +# + locator metadata can now be reached via ->{locator} +# - sub hash2sql now taken from libdb +# +# 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 # @@ -17,25 +32,53 @@ use base ("Data::Storage::Handler::Abstract"); use DBI; +use Data::Dumper; +use libdb qw( getDbNameByDsn hash2Sql ); +use Data::Storage::Result::DBI; # get logger instance my $logger = Log::Dispatch::Config->instance; -our $metainfo = { - 'disconnectMethod' => 'disconnect', -}; +sub getMetaInfo { + my $self = shift; + $logger->debug( __PACKAGE__ . "->getMetaInfo()" ); + return { + 'disconnectMethod' => 'disconnect', + }; +} sub connect { my $self = shift; # create handle - if ( my $dsn = $self->{dbi}->{dsn} ) { - $logger->debug( __PACKAGE__ . "->connect($dsn)" ); - $self->{COREHANDLE} = DBI->connect($dsn); + if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) { + #if ( my $dsn = $self->{locator}->{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}); + + eval { + $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} ); + if (!$self->{COREHANDLE}) { + $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); + return; + } + }; + $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@; + } $self->configureCOREHANDLE(); + + $self->{locator}->{status}->{connected} = 1; + + return 1; } @@ -43,20 +86,20 @@ my $self = shift; - $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" ); + $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); - # apply configured modifications - if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { - $self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); + # apply configured modifications to DBI-handle + if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { + $self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); } - if (exists $self->{dbi}->{RaiseError}) { - $self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError}; + if (exists $self->{locator}->{dbi}->{RaiseError}) { + $self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError}; } - if (exists $self->{dbi}->{PrintError}) { - $self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError}; + if (exists $self->{locator}->{dbi}->{PrintError}) { + $self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError}; } - if (exists $self->{dbi}->{HandleError}) { - $self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError}; + if (exists $self->{locator}->{dbi}->{HandleError}) { + $self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError}; } } @@ -64,6 +107,20 @@ 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; + } + } + + #print "prepare sql: $sql\n"; + my $sth = $self->{COREHANDLE}->prepare($sql); $sth->execute(); return $sth; @@ -72,98 +129,57 @@ sub sendCommand { my $self = shift; my $command = shift; + # TODO: when tracing: yes, do actually log this + #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); my $cmdHandle = $self->_sendSql($command); my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); return $result; } -sub quoteSql { - my $self = shift; - my $string = shift; - if ($string) { - $string =~ s/'/\\'/g; - } - return $string; -} - -sub hash2Sql { - - my $self = shift; - - my $table = shift; - my $hash = shift; - my $mode = shift; - my $crit = shift; - - my $sql; - if ($mode eq 'SQL_INSERT') { - $sql = "INSERT INTO $table (#fields#) VALUES (#values#);"; - } - if ($mode eq 'SQL_UPDATE') { - $sql = "UPDATE $table SET #fields-values# WHERE $crit;"; - } - - my (@fields, @values); - foreach my $key (keys %{$hash}) { - push @fields, $key; - push @values, $hash->{$key}; - } - # quote each element - map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values; - - my $fields = join(', ', @fields); - my $values = join(', ', @values); - my $fields_values = ''; - my $fc = 0; - foreach (@fields) { - $fields_values .= $_ . '=' . $values[$fc] . ', '; - $fc++; - } - $fields_values = substr($fields_values, 0, -2); - - $sql =~ s/#fields#/$fields/; - $sql =~ s/#values#/$values/; - $sql =~ s/#fields-values#/$fields_values/; - - return $sql; -} - - sub getChildNodes { - my $self = shift; my @nodes; - + $logger->debug( __PACKAGE__ . "->getChildNodes()" ); if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { - while ( my $row = $result->_getNextEntry() ) { - push @nodes, $row; + my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); + my $key = "Tables_in_$dbname"; + while ( my $row = $result->getNextEntry() ) { + push @nodes, $row->{$key}; } } - return \@nodes; - } - - - -package Data::Storage::Result::DBI; - -use strict; -use warnings; - -use base ("Data::Storage::Result"); - -sub DESTROY { - my $self = shift; - #$logger->debug( __PACKAGE__ . "->" . "DESTROY" ); - $self->{RESULTHANDLE}->finish(); -} - -sub _getNextEntry { +sub getListUnfiltered { my $self = shift; - return $self->{RESULTHANDLE}->fetchrow_hashref; + my $nodename = shift; + my @list; + $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); + # get list of rows from rdbms by table name + my $result = $self->sendCommand("SELECT * FROM $nodename"); + while ( my $row = $result->getNextEntry() ) { + push @list, $row; + } + return \@list; +} + +sub sendQuery { + my $self = shift; + my $query = shift; + #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; + #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); + my @crits; + foreach (@{$query->{criterias}}) { + my $op = ''; + $op = '=' if lc $_->{op} eq 'eq'; + push @crits, "$_->{key}$op'$_->{val}'"; + } + my $subnodes = {}; + map { $subnodes->{$_}++ } @{$query->{subnodes}}; + # HACK: this is hardcoded ;( expand possibilities! + my $crit = join(' AND ', @crits); + my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); + return $self->sendCommand($sql); } - -1; \ No newline at end of file +1;