1 |
################################# |
############################################ |
2 |
# |
# |
3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.7 2002/12/01 04:46:19 joko |
7 |
|
# + sub eraseAll |
8 |
|
# |
9 |
|
# Revision 1.6 2002/11/29 05:02:30 joko |
10 |
|
# - sub getNewPerlObjectByPkgName (moved to libp.pm) |
11 |
|
# + sub getMetaInfo |
12 |
|
# - sub existsChildNode (moved to Abstract.pm) |
13 |
|
# + sub getListUnfiltered |
14 |
|
# + sub getListFiltered |
15 |
|
# + sub createCursor |
16 |
|
# + sub createSet |
17 |
|
# + sub sendQuery |
18 |
|
# |
19 |
|
# Revision 1.5 2002/11/17 06:35:18 joko |
20 |
|
# + locator metadata can now be reached via ->{locator} |
21 |
|
# - getChildNodes is now wrapped via COREHANDLE |
22 |
|
# |
23 |
|
# Revision 1.4 2002/10/25 11:44:44 joko |
24 |
|
# + sub _initSchema |
25 |
|
# + sub existsChildNode |
26 |
|
# + sub testIntegrity |
27 |
|
# + sub rebuildDbAndSchema |
28 |
|
# |
29 |
|
# Revision 1.3 2002/10/17 03:56:55 joko |
30 |
|
# + bugfix: trapped eval error |
31 |
|
# |
32 |
# Revision 1.2 2002/10/17 00:10:05 joko |
# Revision 1.2 2002/10/17 00:10:05 joko |
33 |
# + removed dependency from tsobj.pm, schema is now independent |
# + removed dependency from tsobj.pm, schema is now independent |
34 |
# + sub getNewPerlObjectByPkgName |
# + sub getNewPerlObjectByPkgName |
38 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
39 |
# + new |
# + new |
40 |
# |
# |
41 |
# |
############################################ |
42 |
################################# |
|
43 |
|
|
44 |
package Data::Storage::Handler::Tangram; |
package Data::Storage::Handler::Tangram; |
45 |
|
|
50 |
|
|
51 |
use Tangram; |
use Tangram; |
52 |
use Data::Dumper; |
use Data::Dumper; |
53 |
|
use libp qw( getNewPerlObjectByPkgName ); |
54 |
|
use Data::Storage::Result::Tangram; |
55 |
|
use Data::Compare::Struct qw( isEmpty ); |
56 |
|
|
57 |
# get logger instance |
# get logger instance |
58 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
59 |
|
|
60 |
|
|
61 |
our $metainfo = { |
sub getMetaInfo { |
62 |
'disconnectMethod' => 'disconnect', |
my $self = shift; |
63 |
}; |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
64 |
|
return { |
65 |
|
'disconnectMethod' => 'disconnect', |
66 |
|
}; |
67 |
|
} |
68 |
|
|
69 |
sub getNewPerlObjectByPkgName { |
sub _initSchema { |
70 |
my $pkgname = shift; |
my $self = shift; |
71 |
my $args = shift; |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
72 |
my $evstring = "use $pkgname;"; |
#if (!$self->{schema_tangram}) { |
73 |
eval($evstring); |
my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
74 |
return $pkgname->new($args); |
$self->{schema_tangram} = $obj->getSchema(); |
75 |
|
#} |
76 |
|
if (!$self->{schema_tangram}) { |
77 |
|
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
78 |
|
return 0; |
79 |
|
} |
80 |
|
return 1; |
81 |
} |
} |
82 |
|
|
83 |
sub connect { |
sub connect { |
85 |
my $self = shift; |
my $self = shift; |
86 |
|
|
87 |
my $dsn = shift; |
my $dsn = shift; |
88 |
$dsn ||= $self->{dbi}->{dsn}; |
$dsn ||= $self->{locator}->{dbi}->{dsn}; |
89 |
|
|
90 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
91 |
|
|
98 |
# return; |
# return; |
99 |
# } |
# } |
100 |
|
|
101 |
my $obj = getNewPerlObjectByPkgName($self->{schema}, { EXPORT_OBJECTS => $self->{classnames} } ); |
return unless $self->_initSchema(); |
102 |
$self->{schema_tangram} = $obj->getSchema(); |
|
103 |
|
# create the main tangram storage object |
104 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
105 |
$self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); |
$self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); |
106 |
|
|
107 |
|
# some attempts for configuring the wrapped underlying dbi..... |
108 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
109 |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
110 |
#$self->_configureUnderlyingStorage; |
#$self->_configureUnderlyingStorage; |
111 |
|
|
112 |
|
# ..... encapsulation wins! |
113 |
$self->configureCOREHANDLE(); |
$self->configureCOREHANDLE(); |
114 |
|
|
115 |
|
$self->{locator}->{status}->{connected} = 1; |
116 |
|
|
117 |
|
return 1; |
118 |
|
|
119 |
} |
} |
120 |
|
|
121 |
sub getChildNodes { |
sub getChildNodes { |
123 |
my $self = shift; |
my $self = shift; |
124 |
my @nodes; |
my @nodes; |
125 |
|
|
126 |
|
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
127 |
|
|
128 |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
129 |
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
130 |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
131 |
|
|
132 |
|
# todo: should we retrieve information from the schema here |
133 |
|
# rather than poorly getting table names from underlying dbi? |
134 |
my $storage = $self->_getSubLayerHandle(); |
my $storage = $self->_getSubLayerHandle(); |
135 |
|
@nodes = @{$storage->getChildNodes()}; |
136 |
#$storage->_configureCOREHANDLE(); |
#$storage->_configureCOREHANDLE(); |
137 |
|
#print "getchildnodes\n"; |
138 |
|
#print Dumper($self); |
139 |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
|
if (my $result = $storage->sendCommand( 'SHOW TABLES;' ) ) { |
|
|
while ( my $row = $result->_getNextEntry() ) { |
|
|
push @nodes, $row; |
|
|
} |
|
|
} |
|
140 |
|
|
141 |
$storage->disconnect(); |
$storage->disconnect(); |
142 |
|
|
143 |
|
$self->{meta}->{childnodes} = \@nodes; |
144 |
|
|
145 |
return \@nodes; |
return \@nodes; |
146 |
|
|
147 |
} |
} |
148 |
|
|
149 |
|
|
150 |
|
sub testIntegrity { |
151 |
|
|
152 |
|
my $self = shift; |
153 |
|
|
154 |
|
$logger->debug( __PACKAGE__ . "->testIntegrity()" ); |
155 |
|
|
156 |
|
# 1st test: are there tables? |
157 |
|
if (!$self->getChildNodes()) { |
158 |
|
$logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" ); |
159 |
|
return; |
160 |
|
} |
161 |
|
|
162 |
|
# 2nd test: is there a table named "Tangram"? |
163 |
|
if (!$self->existsChildNode("Tangram")) { |
164 |
|
$logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" ); |
165 |
|
return; |
166 |
|
} |
167 |
|
|
168 |
|
$self->{locator}->{status}->{integrity} = 1; |
169 |
|
return 1; |
170 |
|
|
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
sub _getSubLayerHandle { |
sub _getSubLayerHandle { |
175 |
|
|
176 |
my $self = shift; |
my $self = shift; |
177 |
|
|
178 |
use Data::Dumper; |
$logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" ); |
179 |
|
|
180 |
#print Dumper($self); |
#print Dumper($self); |
181 |
|
|
182 |
# hack, make more generic! |
# hack, make more generic! |
183 |
if (!$self->{STORAGE_SUBLAYER}) { |
if (!$self->{dataStorageLayer}) { |
184 |
my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} ); |
$logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" ); |
185 |
$self->{STORAGE_SUBLAYER} = Data::Storage->new( $loc, { protected => 1 } ); |
#my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} ); |
186 |
|
my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } ); |
187 |
|
$self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } ); |
188 |
#$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE(); |
#$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE(); |
189 |
#$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE(); |
#$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE(); |
190 |
} |
} |
191 |
|
|
192 |
#print Dumper($self->{STORAGE_UNDER_THE_HOOD}); |
#print Dumper($self->{STORAGE_UNDER_THE_HOOD}); |
193 |
|
|
194 |
return $self->{STORAGE_SUBLAYER}; |
return $self->{dataStorageLayer}; |
195 |
|
|
196 |
} |
} |
197 |
|
|
198 |
sub _configureUnderlyingStorage { |
sub _configureUnderlyingStorage { |
199 |
|
|
200 |
my $self = shift; |
my $self = shift; |
201 |
|
|
202 |
|
$logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" ); |
203 |
|
|
204 |
$self->_configureCOREHANDLE_DBI(); |
$self->_configureCOREHANDLE_DBI(); |
205 |
return; |
return; |
206 |
|
|
217 |
|
|
218 |
my $self = shift; |
my $self = shift; |
219 |
|
|
220 |
$logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
221 |
|
|
222 |
#my $subLayer = $self->_getSubLayerHandle(); |
#my $subLayer = $self->_getSubLayerHandle(); |
223 |
|
|
239 |
|
|
240 |
sub deploySchema { |
sub deploySchema { |
241 |
my $self = shift; |
my $self = shift; |
242 |
#my $dsn = $self->{locator}->{dbi}->{dsn}; |
my $args = shift; |
243 |
my $dsn = $self->{dbi}->{dsn}; |
|
244 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
245 |
|
#my $dsn = $self->{dbi}->{dsn}; |
246 |
|
|
247 |
|
$logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); |
248 |
|
|
249 |
my $ok; |
my $ok; |
250 |
|
# TODO: is this DBI->connect okay here like it is? regarding errors.....??? |
251 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
if ( my $dbh = DBI->connect($dsn, '', '', { |
252 |
PrintError => 0, |
PrintError => 0, |
253 |
} ) ) { |
} ) ) { |
254 |
$ok = Tangram::Relational->deploy($self->{schema}, $dbh ); |
|
255 |
|
return unless $self->_initSchema(); |
256 |
|
|
257 |
|
$ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh ); |
258 |
$dbh->disconnect(); |
$dbh->disconnect(); |
259 |
} |
} |
260 |
return $ok; |
return $ok; |
261 |
} |
} |
262 |
|
|
263 |
sub retreatSchema { |
sub retreatSchema { |
264 |
print "retreat\n"; |
|
265 |
my $self = shift; |
my $self = shift; |
266 |
#my $dsn = $self->{locator}->{dbi}->{dsn}; |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
267 |
my $dsn = $self->{dbi}->{dsn}; |
#my $dsn = $self->{dbi}->{dsn}; |
268 |
|
|
269 |
|
$logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); |
270 |
|
|
271 |
my $ok; |
my $ok; |
272 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
if ( my $dbh = DBI->connect($dsn, '', '', { |
273 |
PrintError => 0, |
#PrintError => 0, |
274 |
|
#RaiseError => 0, |
275 |
} ) ) { |
} ) ) { |
276 |
$ok = Tangram::Relational->retreat($self->{schema}, $dbh ); |
|
277 |
|
return unless $self->_initSchema(); |
278 |
|
|
279 |
|
#use Data::Dumper; print Dumper($self); |
280 |
|
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
281 |
|
|
282 |
|
$ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh ); |
283 |
|
$ok = 2; # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status |
284 |
|
# idea: test this by checking for count of tables in database - |
285 |
|
# problem with this: there may be some left not having been included to the schema |
286 |
$dbh->disconnect(); |
$dbh->disconnect(); |
287 |
} |
} |
288 |
return $ok; |
return $ok; |
289 |
} |
} |
290 |
|
|
291 |
|
sub rebuildDbAndSchema { |
292 |
|
my $self = shift; |
293 |
|
$logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" ); |
294 |
|
my @results; |
295 |
|
|
296 |
|
# sum up results (bool (0/1)) in array |
297 |
|
push @results, $self->retreatSchema(); |
298 |
|
push @results, $self->{dataStorageLayer}->dropDb(); |
299 |
|
push @results, $self->{dataStorageLayer}->createDb(); |
300 |
|
push @results, $self->deploySchema(); |
301 |
|
|
302 |
|
# scan array for "bad ones" |
303 |
|
my $res = 1; |
304 |
|
map { |
305 |
|
$res = 0 if (!$_); |
306 |
|
} @results; |
307 |
|
|
308 |
|
return $res; |
309 |
|
} |
310 |
|
|
311 |
|
sub getListUnfiltered { |
312 |
|
my $self = shift; |
313 |
|
my $nodename = shift; |
314 |
|
my @results; |
315 |
|
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
316 |
|
# get set of objects from odbms by object name |
317 |
|
my $object_set = $self->{COREHANDLE}->remote($nodename); |
318 |
|
@results = $self->{COREHANDLE}->select($object_set); |
319 |
|
return \@results; |
320 |
|
} |
321 |
|
|
322 |
|
sub getListFiltered { |
323 |
|
my $self = shift; |
324 |
|
|
325 |
|
# redirect to unfiltered mode |
326 |
|
#return $self->getListUnfiltered(@_); |
327 |
|
|
328 |
|
my $nodename = shift; |
329 |
|
my $filters = shift; |
330 |
|
my @results; |
331 |
|
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
332 |
|
|
333 |
|
#print Dumper($filters); |
334 |
|
|
335 |
|
my @tfilters; |
336 |
|
|
337 |
|
foreach my $filter (@$filters) { |
338 |
|
|
339 |
|
# get filter - TODO: for each filter |
340 |
|
#my $filter = $filters->[0]; |
341 |
|
|
342 |
|
# build filter |
343 |
|
my $lexpr = $filter->{key}; |
344 |
|
#my $op = $filter->{op}; |
345 |
|
my $op = '='; |
346 |
|
my $rexpr = $filter->{val}; |
347 |
|
my $tight = 100; |
348 |
|
|
349 |
|
# my $tfilter = Tangram::Filter->new( |
350 |
|
# expr => "t1.$lexpr $op '$rexpr'", |
351 |
|
# tight => $tight, |
352 |
|
# objects => $objects, |
353 |
|
# ); |
354 |
|
|
355 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
356 |
|
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
357 |
|
|
358 |
|
} |
359 |
|
|
360 |
|
my $tfilter = join(' & ', @tfilters); |
361 |
|
|
362 |
|
# get set of objects from odbms by object name |
363 |
|
my $remote = $self->{COREHANDLE}->remote($nodename); |
364 |
|
|
365 |
|
# was: |
366 |
|
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
367 |
|
|
368 |
|
# is: |
369 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
370 |
|
my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');'; |
371 |
|
|
372 |
|
# get filtered list/set |
373 |
|
@results = eval($evalstring); |
374 |
|
die $@ if $@; |
375 |
|
|
376 |
|
return \@results; |
377 |
|
} |
378 |
|
|
379 |
|
sub createCursor { |
380 |
|
my $self = shift; |
381 |
|
my $node = shift; |
382 |
|
my $cmdHandle = $self->{COREHANDLE}->cursor($node); |
383 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
384 |
|
return $result; |
385 |
|
} |
386 |
|
|
387 |
|
sub createSet { |
388 |
|
my $self = shift; |
389 |
|
my @objects = @_; |
390 |
|
my $rh = Set::Object->new(); |
391 |
|
foreach (@objects) { |
392 |
|
#print Dumper($_); |
393 |
|
$rh->insert($_) if !isEmpty($_); |
394 |
|
} |
395 |
|
#print Dumper($rh->members()); |
396 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
397 |
|
return $result; |
398 |
|
} |
399 |
|
|
400 |
|
sub sendQuery { |
401 |
|
my $self = shift; |
402 |
|
my $query = shift; |
403 |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
404 |
|
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
405 |
|
|
406 |
|
#print Dumper($query); |
407 |
|
|
408 |
|
# HACK: special case: querying by id does not translate into a common tangram query |
409 |
|
# just load the object by given id(ent) |
410 |
|
if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') { |
411 |
|
#print "LOAD!!!", "\n"; |
412 |
|
#exit; |
413 |
|
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
414 |
|
my $ident = $query->{criterias}->[0]->{val}; |
415 |
|
#print "load obj", "\n"; |
416 |
|
#return $self->createSet() if $ident == 5; |
417 |
|
my $object = $self->{COREHANDLE}->load($ident); |
418 |
|
#print "get id", "\n"; |
419 |
|
my $oid = $self->{COREHANDLE}->id($object); |
420 |
|
return $self->createSet($object); |
421 |
|
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
422 |
|
} |
423 |
|
|
424 |
|
die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
425 |
|
|
426 |
|
# TODO: do a common tangram query here |
427 |
|
|
428 |
|
my @crits; |
429 |
|
foreach (@{$query->{criterias}}) { |
430 |
|
my $op = ''; |
431 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
432 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
433 |
|
} |
434 |
|
my $subnodes = {}; |
435 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
436 |
|
# HACK: this is hardcoded ;( expand possibilities! |
437 |
|
#my $crit = join(' AND ', @crits); |
438 |
|
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
439 |
|
#return $self->sendCommand($sql); |
440 |
|
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
441 |
|
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
442 |
|
return $self->createCursor($query->{node}); |
443 |
|
} |
444 |
|
|
445 |
|
sub eraseAll { |
446 |
|
my $self = shift; |
447 |
|
my $classname = shift; |
448 |
|
my $remote = $self->{storage}->remote($classname); |
449 |
|
my @objs = $self->{storage}->select($remote); |
450 |
|
$self->{COREHANDLE}->erase(@objs); |
451 |
|
} |
452 |
|
|
453 |
1; |
1; |