3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.13 2003/04/08 23:06:45 joko |
7 |
|
# renamed core database helper functions |
8 |
|
# |
9 |
|
# Revision 1.12 2003/01/30 22:28:21 joko |
10 |
|
# + implemented new concrete methods |
11 |
|
# |
12 |
|
# Revision 1.11 2002/12/19 16:31:05 joko |
13 |
|
# + sub dropDb |
14 |
|
# + sub rebuildDb |
15 |
|
# |
16 |
|
# Revision 1.10 2002/12/15 02:02:22 joko |
17 |
|
# + fixed logging-message |
18 |
|
# |
19 |
# Revision 1.9 2002/12/05 07:58:20 joko |
# Revision 1.9 2002/12/05 07:58:20 joko |
20 |
# + now using Tie::SecureHash as a base for the COREHANDLE |
# + now using Tie::SecureHash as a base for the COREHANDLE |
21 |
# + former public COREHANDLE becomes private _COREHANDLE now |
# + former public COREHANDLE becomes private _COREHANDLE now |
57 |
|
|
58 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
59 |
|
|
60 |
|
|
61 |
use DBI; |
use DBI; |
62 |
use Data::Dumper; |
use Data::Dumper; |
63 |
use libdb qw( getDbNameByDsn hash2Sql ); |
use shortcuts::db qw( hash2sql dsn2dbname ); |
64 |
use Data::Storage::Result::DBI; |
use Data::Storage::Result::DBI; |
65 |
|
|
66 |
|
|
67 |
# get logger instance |
# get logger instance |
68 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
69 |
|
|
173 |
my $locator = $self->{locator}; |
my $locator = $self->{locator}; |
174 |
#print Dumper($locator); exit; |
#print Dumper($locator); exit; |
175 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
176 |
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
my $dbname = dsn2dbname($self->{locator}->{dbi}->{dsn}); |
177 |
my $key = "Tables_in_$dbname"; |
my $key = "Tables_in_$dbname"; |
178 |
while ( my $row = $result->getNextEntry() ) { |
while ( my $row = $result->getNextEntry() ) { |
179 |
push @nodes, $row->{$key}; |
push @nodes, $row->{$key}; |
198 |
sub sendQuery { |
sub sendQuery { |
199 |
my $self = shift; |
my $self = shift; |
200 |
my $query = shift; |
my $query = shift; |
201 |
|
|
202 |
|
$logger->debug( __PACKAGE__ . "->sendQuery" ); |
203 |
|
|
204 |
#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}';"; |
205 |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
206 |
my @crits; |
my @crits; |
213 |
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
214 |
# HACK: this is hardcoded ;( expand possibilities! |
# HACK: this is hardcoded ;( expand possibilities! |
215 |
my $crit = join(' AND ', @crits); |
my $crit = join(' AND ', @crits); |
216 |
my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit); |
217 |
return $self->sendCommand($sql); |
return $self->sendCommand($sql); |
218 |
} |
} |
219 |
|
|
220 |
sub eraseAll { |
sub eraseAll { |
221 |
my $self = shift; |
my $self = shift; |
222 |
my $classname = shift; |
my $classname = shift; |
223 |
|
$logger->debug( __PACKAGE__ . "->eraseAll" ); |
224 |
my $sql = "DELETE FROM $classname"; |
my $sql = "DELETE FROM $classname"; |
225 |
$self->sendCommand($sql); |
$self->sendCommand($sql); |
226 |
} |
} |
274 |
return $self->{_COREHANDLE}; |
return $self->{_COREHANDLE}; |
275 |
} |
} |
276 |
|
|
277 |
|
sub dropDb { |
278 |
|
my $self = shift; |
279 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
280 |
|
|
281 |
|
$logger->debug( __PACKAGE__ . "->dropDb( dsn $dsn )" ); |
282 |
|
|
283 |
|
$dsn =~ s/database=(.+?);//; |
284 |
|
my $database_name = $1; |
285 |
|
|
286 |
|
my $ok; |
287 |
|
|
288 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
289 |
|
PrintError => 0, |
290 |
|
} ) ) { |
291 |
|
if ($database_name) { |
292 |
|
if ($dbh->do("DROP DATABASE $database_name;")) { |
293 |
|
$ok = 1; |
294 |
|
} |
295 |
|
} |
296 |
|
|
297 |
|
$dbh->disconnect(); |
298 |
|
|
299 |
|
} |
300 |
|
|
301 |
|
return $ok; |
302 |
|
} |
303 |
|
|
304 |
|
sub rebuildDb { |
305 |
|
my $self = shift; |
306 |
|
$logger->info( __PACKAGE__ . "->rebuildDb()" ); |
307 |
|
my @results; |
308 |
|
|
309 |
|
# sum up results (bool (0/1)) in array |
310 |
|
#push @results, $self->retreatSchema(); |
311 |
|
push @results, $self->dropDb(); |
312 |
|
push @results, $self->createDb(); |
313 |
|
#push @results, $self->deploySchema(); |
314 |
|
|
315 |
|
# scan array for "bad ones" |
316 |
|
my $res = 1; |
317 |
|
map { |
318 |
|
$res = 0 if (!$_); |
319 |
|
} @results; |
320 |
|
|
321 |
|
return $res; |
322 |
|
} |
323 |
|
|
324 |
|
sub testAvailability { |
325 |
|
my $self = shift; |
326 |
|
my $status = $self->testDsn(); |
327 |
|
$self->{locator}->{status}->{available} = $status; |
328 |
|
return $status; |
329 |
|
} |
330 |
|
|
331 |
|
sub testDsn { |
332 |
|
my $self = shift; |
333 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
334 |
|
my $result; |
335 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
336 |
|
PrintError => 0, |
337 |
|
} ) ) { |
338 |
|
|
339 |
|
# TODO: REVIEW |
340 |
|
$dbh->disconnect(); |
341 |
|
|
342 |
|
return 1; |
343 |
|
} else { |
344 |
|
$logger->warning( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
345 |
|
} |
346 |
|
} |
347 |
|
|
348 |
1; |
1; |
349 |
|
__END__ |