--- nfo/perl/libs/Data/Storage/Handler/DBI.pm 2003/04/08 23:06:45 1.13 +++ nfo/perl/libs/Data/Storage/Handler/DBI.pm 2003/04/11 01:17:18 1.16 @@ -1,8 +1,19 @@ ################################# # -# $Id: DBI.pm,v 1.13 2003/04/08 23:06:45 joko Exp $ +# $Id: DBI.pm,v 1.16 2003/04/11 01:17:18 joko Exp $ # # $Log: DBI.pm,v $ +# Revision 1.16 2003/04/11 01:17:18 joko +# sendQuery: +# + introduced crud action 'DELETE' +# + some pre-flight checks +# +# Revision 1.15 2003/04/09 07:53:33 joko +# minor namespace update +# +# 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 # @@ -60,7 +71,7 @@ use DBI; use Data::Dumper; -use shortcuts::db qw( hash2sql dsn2dbname ); +use shortcuts::database qw( hash2sql dsn2dbname ); use Data::Storage::Result::DBI; @@ -138,6 +149,12 @@ my $self = shift; my $sql = shift; + # pre-flight check: is $sql defined? + if (!$sql) { + $logger->warning( __PACKAGE__ . "->_sendSql: \$sql was empty." ); + return; + } + # two-level handling for implicit connect: # if there's no corehandle ... if (!$self->{_COREHANDLE}) { @@ -201,6 +218,15 @@ $logger->debug( __PACKAGE__ . "->sendQuery" ); + my $crud = $query->{options}->{crud}; + $crud ||= $query->{options}->{action}; + $crud ||= 'RETRIEVE'; + + 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; @@ -209,12 +235,32 @@ $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 ($crud eq 'RETRIEVE') { + my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit); + return $self->sendCommand($sql); + + } elsif ($crud eq 'UPDATE') { + my $sql = hash2sql($query->{node}, $query->{payload}, 'UPDATE', $crit); + $self->sendCommand($sql); + + } elsif ($crud eq 'DELETE') { + my $sql = hash2sql($query->{node}, $query->{payload}, 'DELETE', $crit); + $self->sendCommand($sql); + + } + } sub eraseAll {