3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.15 2003/04/09 07:53:33 joko |
7 |
|
# minor namespace update |
8 |
|
# |
9 |
|
# Revision 1.14 2003/04/09 06:06:04 joko |
10 |
|
# sendQuery now is capable of doing 'SELECT'- or 'INSERT'-queries |
11 |
|
# |
12 |
|
# Revision 1.13 2003/04/08 23:06:45 joko |
13 |
|
# renamed core database helper functions |
14 |
|
# |
15 |
|
# Revision 1.12 2003/01/30 22:28:21 joko |
16 |
|
# + implemented new concrete methods |
17 |
|
# |
18 |
|
# Revision 1.11 2002/12/19 16:31:05 joko |
19 |
|
# + sub dropDb |
20 |
|
# + sub rebuildDb |
21 |
|
# |
22 |
|
# Revision 1.10 2002/12/15 02:02:22 joko |
23 |
|
# + fixed logging-message |
24 |
|
# |
25 |
|
# Revision 1.9 2002/12/05 07:58:20 joko |
26 |
|
# + now using Tie::SecureHash as a base for the COREHANDLE |
27 |
|
# + former public COREHANDLE becomes private _COREHANDLE now |
28 |
|
# |
29 |
# Revision 1.8 2002/12/01 22:20:43 joko |
# Revision 1.8 2002/12/01 22:20:43 joko |
30 |
# + sub createDb (from Storage.pm) |
# + sub createDb (from Storage.pm) |
31 |
# |
# |
63 |
|
|
64 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
65 |
|
|
66 |
|
|
67 |
use DBI; |
use DBI; |
68 |
use Data::Dumper; |
use Data::Dumper; |
69 |
use libdb qw( getDbNameByDsn hash2Sql ); |
use shortcuts::database qw( hash2sql dsn2dbname ); |
70 |
use Data::Storage::Result::DBI; |
use Data::Storage::Result::DBI; |
71 |
|
|
72 |
|
|
73 |
# get logger instance |
# get logger instance |
74 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
75 |
|
|
99 |
#use Data::Dumper; print Dumper($self->{dbi}); |
#use Data::Dumper; print Dumper($self->{dbi}); |
100 |
|
|
101 |
eval { |
eval { |
102 |
$self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} ); |
$self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} ); |
103 |
if (!$self->{COREHANDLE}) { |
if (!$self->{_COREHANDLE}) { |
104 |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
105 |
return; |
return; |
106 |
} |
} |
122 |
|
|
123 |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
124 |
|
|
125 |
return if !$self->{COREHANDLE}; |
return if !$self->{_COREHANDLE}; |
126 |
|
|
127 |
# apply configured modifications to DBI-handle |
# apply configured modifications to DBI-handle |
128 |
if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { |
if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { |
129 |
$self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); |
$self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); |
130 |
} |
} |
131 |
if (exists $self->{locator}->{dbi}->{RaiseError}) { |
if (exists $self->{locator}->{dbi}->{RaiseError}) { |
132 |
$self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError}; |
$self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError}; |
133 |
} |
} |
134 |
if (exists $self->{locator}->{dbi}->{PrintError}) { |
if (exists $self->{locator}->{dbi}->{PrintError}) { |
135 |
$self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError}; |
$self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError}; |
136 |
} |
} |
137 |
if (exists $self->{locator}->{dbi}->{HandleError}) { |
if (exists $self->{locator}->{dbi}->{HandleError}) { |
138 |
$self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError}; |
$self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError}; |
139 |
} |
} |
140 |
|
|
141 |
} |
} |
146 |
|
|
147 |
# two-level handling for implicit connect: |
# two-level handling for implicit connect: |
148 |
# if there's no corehandle ... |
# if there's no corehandle ... |
149 |
if (!$self->{COREHANDLE}) { |
if (!$self->{_COREHANDLE}) { |
150 |
# ... try to connect, but ... |
# ... try to connect, but ... |
151 |
$self->connect(); |
$self->connect(); |
152 |
# ... if this still fails, there's something wrong probably, so we won't continue |
# ... if this still fails, there's something wrong probably, so we won't continue |
153 |
if (!$self->{COREHANDLE}) { |
if (!$self->{_COREHANDLE}) { |
154 |
return; |
return; |
155 |
} |
} |
156 |
} |
} |
157 |
|
|
158 |
#print "prepare sql: $sql\n"; |
#print "prepare sql: $sql\n"; |
159 |
|
|
160 |
my $sth = $self->{COREHANDLE}->prepare($sql); |
my $sth = $self->{_COREHANDLE}->prepare($sql); |
161 |
$sth->execute(); |
$sth->execute(); |
162 |
return $sth; |
return $sth; |
163 |
} |
} |
176 |
my $self = shift; |
my $self = shift; |
177 |
my @nodes; |
my @nodes; |
178 |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
179 |
|
my $locator = $self->{locator}; |
180 |
|
#print Dumper($locator); exit; |
181 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
182 |
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
my $dbname = dsn2dbname($self->{locator}->{dbi}->{dsn}); |
183 |
my $key = "Tables_in_$dbname"; |
my $key = "Tables_in_$dbname"; |
184 |
while ( my $row = $result->getNextEntry() ) { |
while ( my $row = $result->getNextEntry() ) { |
185 |
push @nodes, $row->{$key}; |
push @nodes, $row->{$key}; |
204 |
sub sendQuery { |
sub sendQuery { |
205 |
my $self = shift; |
my $self = shift; |
206 |
my $query = shift; |
my $query = shift; |
207 |
|
|
208 |
|
$logger->debug( __PACKAGE__ . "->sendQuery" ); |
209 |
|
|
210 |
|
my $action = $query->{options}->{action}; |
211 |
|
$action ||= 'load'; |
212 |
|
|
213 |
|
if (my $guid = $query->{options}->{GUID}) { |
214 |
|
$query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; |
215 |
|
} |
216 |
|
|
217 |
|
# check criterias (load & save) |
218 |
#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}';"; |
219 |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
220 |
my @crits; |
my @crits; |
223 |
$op = '=' if lc $_->{op} eq 'eq'; |
$op = '=' if lc $_->{op} eq 'eq'; |
224 |
push @crits, "$_->{key}$op'$_->{val}'"; |
push @crits, "$_->{key}$op'$_->{val}'"; |
225 |
} |
} |
|
my $subnodes = {}; |
|
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
|
226 |
# HACK: this is hardcoded ;( expand possibilities! |
# HACK: this is hardcoded ;( expand possibilities! |
227 |
my $crit = join(' AND ', @crits); |
my $crit = join(' AND ', @crits); |
228 |
my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
|
229 |
return $self->sendCommand($sql); |
# check subnodes (load) |
230 |
|
my $subnodes = {}; |
231 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
232 |
|
|
233 |
|
# check payload (save) |
234 |
|
#print Dumper($query->{payload}); |
235 |
|
#$subnodes ||= $query->{payload}; |
236 |
|
|
237 |
|
# dispatch action |
238 |
|
if ($action eq 'load') { |
239 |
|
my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit); |
240 |
|
return $self->sendCommand($sql); |
241 |
|
} elsif ($action eq 'save') { |
242 |
|
my $sql = hash2sql($query->{node}, $query->{payload}, 'UPDATE', $crit); |
243 |
|
$self->sendCommand($sql); |
244 |
|
} |
245 |
|
|
246 |
} |
} |
247 |
|
|
248 |
sub eraseAll { |
sub eraseAll { |
249 |
my $self = shift; |
my $self = shift; |
250 |
my $classname = shift; |
my $classname = shift; |
251 |
|
$logger->debug( __PACKAGE__ . "->eraseAll" ); |
252 |
my $sql = "DELETE FROM $classname"; |
my $sql = "DELETE FROM $classname"; |
253 |
$self->sendCommand($sql); |
$self->sendCommand($sql); |
254 |
} |
} |
297 |
|
|
298 |
} |
} |
299 |
|
|
300 |
|
sub getCOREHANDLE2 { |
301 |
|
my $self = shift; |
302 |
|
return $self->{_COREHANDLE}; |
303 |
|
} |
304 |
|
|
305 |
|
sub dropDb { |
306 |
|
my $self = shift; |
307 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
308 |
|
|
309 |
|
$logger->debug( __PACKAGE__ . "->dropDb( dsn $dsn )" ); |
310 |
|
|
311 |
|
$dsn =~ s/database=(.+?);//; |
312 |
|
my $database_name = $1; |
313 |
|
|
314 |
|
my $ok; |
315 |
|
|
316 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
317 |
|
PrintError => 0, |
318 |
|
} ) ) { |
319 |
|
if ($database_name) { |
320 |
|
if ($dbh->do("DROP DATABASE $database_name;")) { |
321 |
|
$ok = 1; |
322 |
|
} |
323 |
|
} |
324 |
|
|
325 |
|
$dbh->disconnect(); |
326 |
|
|
327 |
|
} |
328 |
|
|
329 |
|
return $ok; |
330 |
|
} |
331 |
|
|
332 |
|
sub rebuildDb { |
333 |
|
my $self = shift; |
334 |
|
$logger->info( __PACKAGE__ . "->rebuildDb()" ); |
335 |
|
my @results; |
336 |
|
|
337 |
|
# sum up results (bool (0/1)) in array |
338 |
|
#push @results, $self->retreatSchema(); |
339 |
|
push @results, $self->dropDb(); |
340 |
|
push @results, $self->createDb(); |
341 |
|
#push @results, $self->deploySchema(); |
342 |
|
|
343 |
|
# scan array for "bad ones" |
344 |
|
my $res = 1; |
345 |
|
map { |
346 |
|
$res = 0 if (!$_); |
347 |
|
} @results; |
348 |
|
|
349 |
|
return $res; |
350 |
|
} |
351 |
|
|
352 |
|
sub testAvailability { |
353 |
|
my $self = shift; |
354 |
|
my $status = $self->testDsn(); |
355 |
|
$self->{locator}->{status}->{available} = $status; |
356 |
|
return $status; |
357 |
|
} |
358 |
|
|
359 |
|
sub testDsn { |
360 |
|
my $self = shift; |
361 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
362 |
|
my $result; |
363 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
364 |
|
PrintError => 0, |
365 |
|
} ) ) { |
366 |
|
|
367 |
|
# TODO: REVIEW |
368 |
|
$dbh->disconnect(); |
369 |
|
|
370 |
|
return 1; |
371 |
|
} else { |
372 |
|
$logger->warning( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
1; |
1; |
377 |
|
__END__ |