3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.16 2003/04/11 01:17:18 joko |
7 |
|
# sendQuery: |
8 |
|
# + introduced crud action 'DELETE' |
9 |
|
# + some pre-flight checks |
10 |
|
# |
11 |
|
# Revision 1.15 2003/04/09 07:53:33 joko |
12 |
|
# minor namespace update |
13 |
|
# |
14 |
|
# Revision 1.14 2003/04/09 06:06:04 joko |
15 |
|
# sendQuery now is capable of doing 'SELECT'- or 'INSERT'-queries |
16 |
|
# |
17 |
|
# Revision 1.13 2003/04/08 23:06:45 joko |
18 |
|
# renamed core database helper functions |
19 |
|
# |
20 |
|
# Revision 1.12 2003/01/30 22:28:21 joko |
21 |
|
# + implemented new concrete methods |
22 |
|
# |
23 |
# Revision 1.11 2002/12/19 16:31:05 joko |
# Revision 1.11 2002/12/19 16:31:05 joko |
24 |
# + sub dropDb |
# + sub dropDb |
25 |
# + sub rebuildDb |
# + sub rebuildDb |
68 |
|
|
69 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
70 |
|
|
71 |
|
|
72 |
use DBI; |
use DBI; |
73 |
use Data::Dumper; |
use Data::Dumper; |
74 |
use libdb qw( getDbNameByDsn hash2Sql ); |
use shortcuts::database qw( hash2sql dsn2dbname ); |
75 |
use Data::Storage::Result::DBI; |
use Data::Storage::Result::DBI; |
76 |
|
|
77 |
|
|
78 |
# get logger instance |
# get logger instance |
79 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
80 |
|
|
149 |
my $self = shift; |
my $self = shift; |
150 |
my $sql = shift; |
my $sql = shift; |
151 |
|
|
152 |
|
# pre-flight check: is $sql defined? |
153 |
|
if (!$sql) { |
154 |
|
$logger->warning( __PACKAGE__ . "->_sendSql: \$sql was empty." ); |
155 |
|
return; |
156 |
|
} |
157 |
|
|
158 |
# two-level handling for implicit connect: |
# two-level handling for implicit connect: |
159 |
# if there's no corehandle ... |
# if there's no corehandle ... |
160 |
if (!$self->{_COREHANDLE}) { |
if (!$self->{_COREHANDLE}) { |
190 |
my $locator = $self->{locator}; |
my $locator = $self->{locator}; |
191 |
#print Dumper($locator); exit; |
#print Dumper($locator); exit; |
192 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
193 |
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
my $dbname = dsn2dbname($self->{locator}->{dbi}->{dsn}); |
194 |
my $key = "Tables_in_$dbname"; |
my $key = "Tables_in_$dbname"; |
195 |
while ( my $row = $result->getNextEntry() ) { |
while ( my $row = $result->getNextEntry() ) { |
196 |
push @nodes, $row->{$key}; |
push @nodes, $row->{$key}; |
218 |
|
|
219 |
$logger->debug( __PACKAGE__ . "->sendQuery" ); |
$logger->debug( __PACKAGE__ . "->sendQuery" ); |
220 |
|
|
221 |
|
my $crud = $query->{options}->{crud}; |
222 |
|
$crud ||= $query->{options}->{action}; |
223 |
|
$crud ||= 'RETRIEVE'; |
224 |
|
|
225 |
|
if (my $guid = $query->{options}->{GUID}) { |
226 |
|
$query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; |
227 |
|
} |
228 |
|
|
229 |
|
# check criterias (load & save) |
230 |
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
231 |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
232 |
my @crits; |
my @crits; |
235 |
$op = '=' if lc $_->{op} eq 'eq'; |
$op = '=' if lc $_->{op} eq 'eq'; |
236 |
push @crits, "$_->{key}$op'$_->{val}'"; |
push @crits, "$_->{key}$op'$_->{val}'"; |
237 |
} |
} |
|
my $subnodes = {}; |
|
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
|
238 |
# HACK: this is hardcoded ;( expand possibilities! |
# HACK: this is hardcoded ;( expand possibilities! |
239 |
my $crit = join(' AND ', @crits); |
my $crit = join(' AND ', @crits); |
240 |
my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
|
241 |
return $self->sendCommand($sql); |
# check subnodes (load) |
242 |
|
my $subnodes = {}; |
243 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
244 |
|
|
245 |
|
# check payload (save) |
246 |
|
#print Dumper($query->{payload}); |
247 |
|
#$subnodes ||= $query->{payload}; |
248 |
|
|
249 |
|
# dispatch action |
250 |
|
if ($crud eq 'RETRIEVE') { |
251 |
|
my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit); |
252 |
|
return $self->sendCommand($sql); |
253 |
|
|
254 |
|
} elsif ($crud eq 'UPDATE') { |
255 |
|
my $sql = hash2sql($query->{node}, $query->{payload}, 'UPDATE', $crit); |
256 |
|
$self->sendCommand($sql); |
257 |
|
|
258 |
|
} elsif ($crud eq 'DELETE') { |
259 |
|
my $sql = hash2sql($query->{node}, $query->{payload}, 'DELETE', $crit); |
260 |
|
$self->sendCommand($sql); |
261 |
|
|
262 |
|
} |
263 |
|
|
264 |
} |
} |
265 |
|
|
266 |
sub eraseAll { |
sub eraseAll { |
367 |
return $res; |
return $res; |
368 |
} |
} |
369 |
|
|
370 |
|
sub testAvailability { |
371 |
|
my $self = shift; |
372 |
|
my $status = $self->testDsn(); |
373 |
|
$self->{locator}->{status}->{available} = $status; |
374 |
|
return $status; |
375 |
|
} |
376 |
|
|
377 |
|
sub testDsn { |
378 |
|
my $self = shift; |
379 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
380 |
|
my $result; |
381 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
382 |
|
PrintError => 0, |
383 |
|
} ) ) { |
384 |
|
|
385 |
|
# TODO: REVIEW |
386 |
|
$dbh->disconnect(); |
387 |
|
|
388 |
|
return 1; |
389 |
|
} else { |
390 |
|
$logger->warning( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
391 |
|
} |
392 |
|
} |
393 |
|
|
394 |
1; |
1; |
395 |
|
__END__ |