--- nfo/perl/libs/Data/Storage/Handler/DBI.pm 2002/12/15 02:02:22 1.10 +++ nfo/perl/libs/Data/Storage/Handler/DBI.pm 2003/04/09 06:06:04 1.14 @@ -1,8 +1,21 @@ ################################# # -# $Id: DBI.pm,v 1.10 2002/12/15 02:02:22 joko Exp $ +# $Id: DBI.pm,v 1.14 2003/04/09 06:06:04 joko Exp $ # # $Log: DBI.pm,v $ +# Revision 1.14 2003/04/09 06:06:04 joko +# sendQuery now is capable of doing 'SELECT'- or 'INSERT'-queries +# +# Revision 1.13 2003/04/08 23:06:45 joko +# renamed core database helper functions +# +# Revision 1.12 2003/01/30 22:28:21 joko +# + implemented new concrete methods +# +# Revision 1.11 2002/12/19 16:31:05 joko +# + sub dropDb +# + sub rebuildDb +# # Revision 1.10 2002/12/15 02:02:22 joko # + fixed logging-message # @@ -47,11 +60,13 @@ use base ("Data::Storage::Handler::Abstract"); + use DBI; use Data::Dumper; -use libdb qw( getDbNameByDsn hash2Sql ); +use shortcuts::db qw( hash2sql dsn2dbname ); use Data::Storage::Result::DBI; + # get logger instance my $logger = Log::Dispatch::Config->instance; @@ -161,7 +176,7 @@ my $locator = $self->{locator}; #print Dumper($locator); exit; if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { - my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); + my $dbname = dsn2dbname($self->{locator}->{dbi}->{dsn}); my $key = "Tables_in_$dbname"; while ( my $row = $result->getNextEntry() ) { push @nodes, $row->{$key}; @@ -189,6 +204,14 @@ $logger->debug( __PACKAGE__ . "->sendQuery" ); + my $action = $query->{options}->{action}; + $action ||= 'load'; + + if (my $guid = $query->{options}->{GUID}) { + $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; + } + + # check criterias (load & save) #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; @@ -197,12 +220,26 @@ $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); + + # check subnodes (load) + my $subnodes = {}; + map { $subnodes->{$_}++ } @{$query->{subnodes}}; + + # check payload (save) + #print Dumper($query->{payload}); + #$subnodes ||= $query->{payload}; + + # dispatch action + if ($action eq 'load') { + my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit); + return $self->sendCommand($sql); + } elsif ($action eq 'save') { + my $sql = hash2sql($query->{node}, $query->{payload}, 'UPDATE', $crit); + $self->sendCommand($sql); + } + } sub eraseAll { @@ -262,4 +299,76 @@ return $self->{_COREHANDLE}; } +sub dropDb { + my $self = shift; + my $dsn = $self->{locator}->{dbi}->{dsn}; + + $logger->debug( __PACKAGE__ . "->dropDb( dsn $dsn )" ); + + $dsn =~ s/database=(.+?);//; + my $database_name = $1; + + my $ok; + + if ( my $dbh = DBI->connect($dsn, '', '', { + PrintError => 0, + } ) ) { + if ($database_name) { + if ($dbh->do("DROP DATABASE $database_name;")) { + $ok = 1; + } + } + + $dbh->disconnect(); + + } + + return $ok; +} + +sub rebuildDb { + my $self = shift; + $logger->info( __PACKAGE__ . "->rebuildDb()" ); + my @results; + + # sum up results (bool (0/1)) in array + #push @results, $self->retreatSchema(); + push @results, $self->dropDb(); + push @results, $self->createDb(); + #push @results, $self->deploySchema(); + + # scan array for "bad ones" + my $res = 1; + map { + $res = 0 if (!$_); + } @results; + + return $res; +} + +sub testAvailability { + my $self = shift; + my $status = $self->testDsn(); + $self->{locator}->{status}->{available} = $status; + return $status; +} + +sub testDsn { + my $self = shift; + my $dsn = $self->{locator}->{dbi}->{dsn}; + my $result; + if ( my $dbh = DBI->connect($dsn, '', '', { + PrintError => 0, + } ) ) { + + # TODO: REVIEW + $dbh->disconnect(); + + return 1; + } else { + $logger->warning( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); + } +} + 1; +__END__