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 |
20 |
|
# + now using Tie::SecureHash as a base for the COREHANDLE |
21 |
|
# + former public COREHANDLE becomes private _COREHANDLE now |
22 |
|
# |
23 |
|
# Revision 1.8 2002/12/01 22:20:43 joko |
24 |
|
# + sub createDb (from Storage.pm) |
25 |
|
# |
26 |
|
# Revision 1.7 2002/12/01 07:09:09 joko |
27 |
|
# + sub getListFiltered (dummy redirecting to getListUnfiltered) |
28 |
|
# |
29 |
|
# Revision 1.6 2002/12/01 04:46:01 joko |
30 |
|
# + sub eraseAll |
31 |
|
# |
32 |
|
# Revision 1.5 2002/11/29 05:00:26 joko |
33 |
|
# + sub getListUnfiltered |
34 |
|
# + sub sendQuery |
35 |
|
# |
36 |
|
# Revision 1.4 2002/11/17 08:46:42 jonen |
37 |
|
# + wrapped eval around DBI->connect to prevent deaths |
38 |
|
# |
39 |
|
# Revision 1.3 2002/11/17 06:34:39 joko |
40 |
|
# + locator metadata can now be reached via ->{locator} |
41 |
|
# - sub hash2sql now taken from libdb |
42 |
|
# |
43 |
# Revision 1.2 2002/10/25 11:43:27 joko |
# Revision 1.2 2002/10/25 11:43:27 joko |
44 |
# + enhanced robustness |
# + enhanced robustness |
45 |
# + more logging for debug-levels |
# + more logging for debug-levels |
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; |
63 |
|
use shortcuts::db qw( hash2sql dsn2dbname ); |
64 |
|
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 |
|
|
70 |
|
|
71 |
our $metainfo = { |
sub getMetaInfo { |
72 |
'disconnectMethod' => 'disconnect', |
my $self = shift; |
73 |
}; |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
74 |
|
return { |
75 |
|
'disconnectMethod' => 'disconnect', |
76 |
|
}; |
77 |
|
} |
78 |
|
|
79 |
sub connect { |
sub connect { |
80 |
|
|
81 |
my $self = shift; |
my $self = shift; |
82 |
|
|
83 |
# create handle |
# create handle |
84 |
if ( my $dsn = $self->{dbi}->{dsn} ) { |
if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) { |
85 |
|
#if ( my $dsn = $self->{locator}->{dsn} ) { |
86 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
87 |
|
|
88 |
# HACK: |
# HACK: |
92 |
|
|
93 |
#use Data::Dumper; print Dumper($self->{dbi}); |
#use Data::Dumper; print Dumper($self->{dbi}); |
94 |
|
|
95 |
$self->{COREHANDLE} = DBI->connect( |
eval { |
96 |
$dsn, '', '', { |
$self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} ); |
97 |
RaiseError => $self->{dbi}->{RaiseError}, |
if (!$self->{_COREHANDLE}) { |
98 |
#RaiseError => 1, |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
99 |
PrintError => $self->{dbi}->{PrintError}, |
return; |
|
HandleError => $self->{dbi}->{HandleError}, |
|
100 |
} |
} |
101 |
); |
}; |
102 |
if (!$self->{COREHANDLE}) { |
$logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@; |
103 |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
|
|
return; |
|
|
} |
|
104 |
} |
} |
105 |
$self->configureCOREHANDLE(); |
$self->configureCOREHANDLE(); |
106 |
|
|
107 |
|
$self->{locator}->{status}->{connected} = 1; |
108 |
|
|
109 |
return 1; |
return 1; |
110 |
|
|
111 |
} |
} |
114 |
|
|
115 |
my $self = shift; |
my $self = shift; |
116 |
|
|
117 |
$logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
118 |
|
|
119 |
|
return if !$self->{_COREHANDLE}; |
120 |
|
|
121 |
# apply configured modifications |
# apply configured modifications to DBI-handle |
122 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { |
123 |
$self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
$self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); |
124 |
} |
} |
125 |
if (exists $self->{dbi}->{RaiseError}) { |
if (exists $self->{locator}->{dbi}->{RaiseError}) { |
126 |
$self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError}; |
$self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError}; |
127 |
} |
} |
128 |
if (exists $self->{dbi}->{PrintError}) { |
if (exists $self->{locator}->{dbi}->{PrintError}) { |
129 |
$self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError}; |
$self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError}; |
130 |
} |
} |
131 |
if (exists $self->{dbi}->{HandleError}) { |
if (exists $self->{locator}->{dbi}->{HandleError}) { |
132 |
$self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError}; |
$self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError}; |
133 |
} |
} |
134 |
|
|
135 |
} |
} |
140 |
|
|
141 |
# two-level handling for implicit connect: |
# two-level handling for implicit connect: |
142 |
# if there's no corehandle ... |
# if there's no corehandle ... |
143 |
if (!$self->{COREHANDLE}) { |
if (!$self->{_COREHANDLE}) { |
144 |
# ... try to connect, but ... |
# ... try to connect, but ... |
145 |
$self->connect(); |
$self->connect(); |
146 |
# ... 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 |
147 |
if (!$self->{COREHANDLE}) { |
if (!$self->{_COREHANDLE}) { |
148 |
return; |
return; |
149 |
} |
} |
150 |
} |
} |
151 |
|
|
152 |
my $sth = $self->{COREHANDLE}->prepare($sql); |
#print "prepare sql: $sql\n"; |
153 |
|
|
154 |
|
my $sth = $self->{_COREHANDLE}->prepare($sql); |
155 |
$sth->execute(); |
$sth->execute(); |
156 |
return $sth; |
return $sth; |
157 |
} |
} |
159 |
sub sendCommand { |
sub sendCommand { |
160 |
my $self = shift; |
my $self = shift; |
161 |
my $command = shift; |
my $command = shift; |
162 |
|
# TODO: when tracing: yes, do actually log this |
163 |
#$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); |
#$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); |
164 |
my $cmdHandle = $self->_sendSql($command); |
my $cmdHandle = $self->_sendSql($command); |
165 |
my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); |
my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); |
166 |
return $result; |
return $result; |
167 |
} |
} |
168 |
|
|
169 |
sub quoteSql { |
sub getChildNodes { |
170 |
my $self = shift; |
my $self = shift; |
171 |
my $string = shift; |
my @nodes; |
172 |
if ($string) { |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
173 |
$string =~ s/'/\\'/g; |
my $locator = $self->{locator}; |
174 |
|
#print Dumper($locator); exit; |
175 |
|
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
176 |
|
my $dbname = dsn2dbname($self->{locator}->{dbi}->{dsn}); |
177 |
|
my $key = "Tables_in_$dbname"; |
178 |
|
while ( my $row = $result->getNextEntry() ) { |
179 |
|
push @nodes, $row->{$key}; |
180 |
|
} |
181 |
} |
} |
182 |
return $string; |
return \@nodes; |
183 |
} |
} |
184 |
|
|
185 |
sub hash2Sql { |
sub getListUnfiltered { |
|
|
|
186 |
my $self = shift; |
my $self = shift; |
187 |
|
my $nodename = shift; |
188 |
my $table = shift; |
my @list; |
189 |
my $hash = shift; |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
190 |
my $mode = shift; |
# get list of rows from rdbms by table name |
191 |
my $crit = shift; |
my $result = $self->sendCommand("SELECT * FROM $nodename"); |
192 |
|
while ( my $row = $result->getNextEntry() ) { |
193 |
my $sql; |
push @list, $row; |
|
if ($mode eq 'SQL_INSERT') { |
|
|
$sql = "INSERT INTO $table (#fields#) VALUES (#values#);"; |
|
|
} |
|
|
if ($mode eq 'SQL_UPDATE') { |
|
|
$sql = "UPDATE $table SET #fields-values# WHERE $crit;"; |
|
194 |
} |
} |
195 |
|
return \@list; |
196 |
my (@fields, @values); |
} |
197 |
foreach my $key (keys %{$hash}) { |
|
198 |
push @fields, $key; |
sub sendQuery { |
199 |
push @values, $hash->{$key}; |
my $self = shift; |
200 |
} |
my $query = shift; |
201 |
# quote each element |
|
202 |
map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values; |
$logger->debug( __PACKAGE__ . "->sendQuery" ); |
203 |
|
|
204 |
my $fields = join(', ', @fields); |
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
205 |
my $values = join(', ', @values); |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
206 |
my $fields_values = ''; |
my @crits; |
207 |
my $fc = 0; |
foreach (@{$query->{criterias}}) { |
208 |
foreach (@fields) { |
my $op = ''; |
209 |
$fields_values .= $_ . '=' . $values[$fc] . ', '; |
$op = '=' if lc $_->{op} eq 'eq'; |
210 |
$fc++; |
push @crits, "$_->{key}$op'$_->{val}'"; |
211 |
} |
} |
212 |
$fields_values = substr($fields_values, 0, -2); |
my $subnodes = {}; |
213 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
214 |
$sql =~ s/#fields#/$fields/; |
# HACK: this is hardcoded ;( expand possibilities! |
215 |
$sql =~ s/#values#/$values/; |
my $crit = join(' AND ', @crits); |
216 |
$sql =~ s/#fields-values#/$fields_values/; |
my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit); |
217 |
|
return $self->sendCommand($sql); |
|
return $sql; |
|
218 |
} |
} |
219 |
|
|
220 |
|
sub eraseAll { |
221 |
|
my $self = shift; |
222 |
|
my $classname = shift; |
223 |
|
$logger->debug( __PACKAGE__ . "->eraseAll" ); |
224 |
|
my $sql = "DELETE FROM $classname"; |
225 |
|
$self->sendCommand($sql); |
226 |
|
} |
227 |
|
|
228 |
sub getChildNodes { |
# TODO: actually implement the filtering functionality using $this->sendQuery |
229 |
|
sub getListFiltered { |
230 |
|
my $self = shift; |
231 |
|
my $nodename = shift; |
232 |
|
return $self->getListUnfiltered($nodename); |
233 |
|
} |
234 |
|
|
235 |
|
# TODO: do this via a parametrized "$self->connect(<connect just to database server - don't select database>)" |
236 |
|
sub createDb { |
237 |
|
|
238 |
my $self = shift; |
my $self = shift; |
239 |
my @nodes; |
|
240 |
|
# get dsn from Data::Storage::Locator instance |
241 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
242 |
|
|
243 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
$logger->debug( __PACKAGE__ . "->createDb( dsn $dsn )" ); |
244 |
while ( my $row = $result->_getNextEntry() ) { |
|
245 |
push @nodes, $row; |
# remove database setting from dsn-string |
246 |
|
$dsn =~ s/database=(.+?);//; |
247 |
|
|
248 |
|
# remember extracted database name to know what actually to create right now |
249 |
|
my $database_name = $1; |
250 |
|
|
251 |
|
# flag to indicate goodness |
252 |
|
my $ok; |
253 |
|
|
254 |
|
# connect to database server - don't select/use any specific database |
255 |
|
#if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0 } ) ) { |
256 |
|
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
257 |
|
|
258 |
|
if ($database_name) { |
259 |
|
if ($dbh->do("CREATE DATABASE $database_name")) { |
260 |
|
$ok = 1; |
261 |
|
} |
262 |
} |
} |
263 |
|
|
264 |
|
$dbh->disconnect(); |
265 |
|
|
266 |
} |
} |
267 |
|
|
268 |
return \@nodes; |
return $ok; |
269 |
|
|
270 |
|
} |
271 |
|
|
272 |
|
sub getCOREHANDLE2 { |
273 |
|
my $self = shift; |
274 |
|
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 |
package Data::Storage::Result::DBI; |
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 |
use strict; |
$dbh->disconnect(); |
|
use warnings; |
|
298 |
|
|
299 |
use base ("Data::Storage::Result"); |
} |
300 |
|
|
301 |
|
return $ok; |
302 |
|
} |
303 |
|
|
304 |
sub DESTROY { |
sub rebuildDb { |
305 |
my $self = shift; |
my $self = shift; |
306 |
#$logger->debug( __PACKAGE__ . "->" . "DESTROY" ); |
$logger->info( __PACKAGE__ . "->rebuildDb()" ); |
307 |
$self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish(); |
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 _getNextEntry { |
sub testAvailability { |
325 |
my $self = shift; |
my $self = shift; |
326 |
$self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref; |
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 |
|
|
|
1; |
|
348 |
|
1; |
349 |
|
__END__ |