1 |
################################# |
############################################ |
2 |
# |
# |
3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.13 2002/12/05 07:59:04 joko |
7 |
|
# + now using Tie::SecureHash as a base for the COREHANDLE |
8 |
|
# + former public COREHANDLE becomes private _COREHANDLE now |
9 |
|
# + sub getCOREHANDLE |
10 |
|
# |
11 |
|
# Revision 1.12 2002/12/04 11:34:49 joko |
12 |
|
# - $schema_tangram doesn't have to be in class? |
13 |
|
# |
14 |
|
# Revision 1.11 2002/12/04 08:54:08 jonen |
15 |
|
# + untested bugfix: undef($object) after transform to hash at getObjectAsHash |
16 |
|
# |
17 |
|
# Revision 1.10 2002/12/03 15:53:23 joko |
18 |
|
# + small bugfix regarding object hierarchy |
19 |
|
# |
20 |
|
# Revision 1.9 2002/12/03 05:29:40 joko |
21 |
|
# + sub getObject |
22 |
|
# + sub getObjectAsHash |
23 |
|
# |
24 |
|
# Revision 1.8 2002/12/01 22:25:51 joko |
25 |
|
# + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode |
26 |
|
# |
27 |
|
# Revision 1.7 2002/12/01 04:46:19 joko |
28 |
|
# + sub eraseAll |
29 |
|
# |
30 |
|
# Revision 1.6 2002/11/29 05:02:30 joko |
31 |
|
# - sub getNewPerlObjectByPkgName (moved to libp.pm) |
32 |
|
# + sub getMetaInfo |
33 |
|
# - sub existsChildNode (moved to Abstract.pm) |
34 |
|
# + sub getListUnfiltered |
35 |
|
# + sub getListFiltered |
36 |
|
# + sub createCursor |
37 |
|
# + sub createSet |
38 |
|
# + sub sendQuery |
39 |
|
# |
40 |
# Revision 1.5 2002/11/17 06:35:18 joko |
# Revision 1.5 2002/11/17 06:35:18 joko |
41 |
# + locator metadata can now be reached via ->{locator} |
# + locator metadata can now be reached via ->{locator} |
42 |
# - getChildNodes is now wrapped via COREHANDLE |
# - getChildNodes is now wrapped via COREHANDLE |
59 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
60 |
# + new |
# + new |
61 |
# |
# |
62 |
# |
############################################ |
63 |
################################# |
|
64 |
|
|
65 |
package Data::Storage::Handler::Tangram; |
package Data::Storage::Handler::Tangram; |
66 |
|
|
67 |
use strict; |
use strict; |
68 |
use warnings; |
use warnings; |
69 |
|
|
70 |
|
use base ("Data::Storage::Handler"); |
71 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
72 |
|
|
73 |
use Tangram; |
use Tangram; |
74 |
use Data::Dumper; |
use Data::Dumper; |
75 |
|
use libp qw( getNewPerlObjectByPkgName ); |
76 |
|
use Data::Storage::Result::Tangram; |
77 |
|
use Data::Compare::Struct qw( isEmpty ); |
78 |
|
use Data::Transform::Deep qw( var_deref ); |
79 |
|
use Data::Transform::Encode qw( var2utf8 ); |
80 |
|
|
81 |
|
|
82 |
# get logger instance |
# get logger instance |
83 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
84 |
|
|
85 |
|
|
86 |
our $metainfo = { |
# this holds the complete instantiated schema from tangram |
87 |
'disconnectMethod' => 'disconnect', |
my $schema_tangram; |
|
}; |
|
88 |
|
|
89 |
sub getNewPerlObjectByPkgName { |
sub getMetaInfo { |
90 |
my $pkgname = shift; |
my $self = shift; |
91 |
my $args = shift; |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
92 |
$logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" ); |
return { |
93 |
my $evstring = "use $pkgname;"; |
'disconnectMethod' => 'disconnect', |
94 |
eval($evstring); |
}; |
|
$@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval $evstring: " . $@ ); |
|
|
return $pkgname->new($args); |
|
95 |
} |
} |
96 |
|
|
97 |
sub _initSchema { |
sub _initSchema { |
98 |
my $self = shift; |
my $self = shift; |
99 |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
100 |
#if (!$self->{schema_tangram}) { |
#if (!$schema_tangram) { |
101 |
my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
102 |
$self->{schema_tangram} = $obj->getSchema(); |
$schema_tangram = $obj->getSchema(); |
103 |
#} |
#} |
104 |
if (!$self->{schema_tangram}) { |
if (!$schema_tangram) { |
105 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
106 |
return 0; |
return 0; |
107 |
} |
} |
128 |
|
|
129 |
return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
130 |
|
|
131 |
|
# create the main tangram storage object |
132 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
133 |
$self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); |
$self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn ); |
134 |
|
|
135 |
|
#print "connect", "\n"; |
136 |
|
#my $core = $self->{_COREHANDLE}; |
137 |
|
#print Dumper($core); |
138 |
|
|
139 |
|
# some attempts for configuring the wrapped underlying dbi..... |
140 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
141 |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
142 |
#$self->_configureUnderlyingStorage; |
#$self->_configureUnderlyingStorage; |
143 |
|
|
144 |
|
# ..... encapsulation wins! |
145 |
$self->configureCOREHANDLE(); |
$self->configureCOREHANDLE(); |
146 |
|
|
147 |
$self->{locator}->{status}->{connected} = 1; |
$self->{locator}->{status}->{connected} = 1; |
160 |
# 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 |
161 |
#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} }); |
162 |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
163 |
|
|
164 |
|
# todo: should we retrieve information from the schema here |
165 |
|
# rather than poorly getting table names from underlying dbi? |
166 |
my $storage = $self->_getSubLayerHandle(); |
my $storage = $self->_getSubLayerHandle(); |
167 |
@nodes = @{$storage->getChildNodes()}; |
@nodes = @{$storage->getChildNodes()}; |
168 |
#$storage->_configureCOREHANDLE(); |
#$storage->_configureCOREHANDLE(); |
170 |
#print Dumper($self); |
#print Dumper($self); |
171 |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
172 |
|
|
173 |
$storage->disconnect(); |
# TODO: REVIEW |
174 |
|
#$storage->disconnect(); |
175 |
|
|
176 |
$self->{meta}->{childnodes} = \@nodes; |
$self->{meta}->{childnodes} = \@nodes; |
177 |
|
|
180 |
} |
} |
181 |
|
|
182 |
|
|
|
sub existsChildNode { |
|
|
my $self = shift; |
|
|
my $nodename = shift; |
|
|
$logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" ); |
|
|
$self->getChildNodes() unless $self->{meta}->{childnodes}; |
|
|
#print Dumper($self->{meta}->{childnodes}); |
|
|
return grep $nodename, @{$self->{meta}->{childnodes}}; |
|
|
} |
|
|
|
|
|
|
|
183 |
sub testIntegrity { |
sub testIntegrity { |
184 |
|
|
185 |
my $self = shift; |
my $self = shift; |
240 |
foreach my $key (keys %{$self->{dbi}}) { |
foreach my $key (keys %{$self->{dbi}}) { |
241 |
my $val = $self->{dbi}->{$key}; |
my $val = $self->{dbi}->{$key}; |
242 |
print "entry: $key; $val", "\n"; |
print "entry: $key; $val", "\n"; |
243 |
$self->{COREHANDLE}->{db}->{$key} = $val; |
$self->{_COREHANDLE}->{db}->{$key} = $val; |
244 |
} |
} |
245 |
#print Dumper($self->{COREHANDLE}->{db}); |
#print Dumper($self->{COREHANDLE}->{db}); |
246 |
} |
} |
256 |
|
|
257 |
# apply configured modifications |
# apply configured modifications |
258 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
259 |
$self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
$self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
260 |
} |
} |
261 |
if (exists $self->{dbi}->{RaiseError}) { |
if (exists $self->{dbi}->{RaiseError}) { |
262 |
$self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
$self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
263 |
} |
} |
264 |
if (exists $self->{dbi}->{PrintError}) { |
if (exists $self->{dbi}->{PrintError}) { |
265 |
$self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
$self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
266 |
} |
} |
267 |
if (exists $self->{dbi}->{HandleError}) { |
if (exists $self->{dbi}->{HandleError}) { |
268 |
$self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
$self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
269 |
} |
} |
270 |
|
|
271 |
} |
} |
275 |
my $args = shift; |
my $args = shift; |
276 |
|
|
277 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
|
#my $dsn = $self->{dbi}->{dsn}; |
|
278 |
|
|
279 |
$logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); |
280 |
|
|
281 |
my $ok; |
my $ok; |
282 |
# TODO: is this DBI->connect okay here like it is? regarding errors.....??? |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
|
|
PrintError => 0, |
|
|
} ) ) { |
|
|
|
|
283 |
return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
284 |
|
$ok = Tangram::Relational->deploy($schema_tangram, $dbh ); |
|
$ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh ); |
|
285 |
$dbh->disconnect(); |
$dbh->disconnect(); |
286 |
} |
} |
287 |
return $ok; |
return $ok; |
291 |
|
|
292 |
my $self = shift; |
my $self = shift; |
293 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
|
#my $dsn = $self->{dbi}->{dsn}; |
|
294 |
|
|
295 |
$logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); |
296 |
|
|
297 |
my $ok; |
my $ok; |
298 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
|
#PrintError => 0, |
|
|
#RaiseError => 0, |
|
|
} ) ) { |
|
299 |
|
|
300 |
return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
301 |
|
|
302 |
#use Data::Dumper; print Dumper($self); |
#use Data::Dumper; print Dumper($self); |
303 |
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
304 |
|
|
305 |
$ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh ); |
$ok = Tangram::Relational->retreat($schema_tangram, $dbh ); |
306 |
$ok = 2; # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status |
|
307 |
# idea: test this by checking for count of tables in database - |
# answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because... |
308 |
# problem with this: there may be some left not having been included to the schema |
# - ... Tangram::Relational->retreat doesn't seem to return a valid status |
309 |
|
# - possible improvement: |
310 |
|
# - test this by checking for count of tables in database |
311 |
|
# - problem with this: there may be some left not having been included to the schema |
312 |
|
# - maybe better: use "->getChildNodes"? |
313 |
|
$ok = 2; |
314 |
|
|
315 |
$dbh->disconnect(); |
$dbh->disconnect(); |
316 |
|
|
317 |
} |
} |
318 |
return $ok; |
return $ok; |
319 |
} |
} |
338 |
return $res; |
return $res; |
339 |
} |
} |
340 |
|
|
341 |
|
sub getListUnfiltered { |
342 |
|
my $self = shift; |
343 |
|
my $nodename = shift; |
344 |
|
my @results; |
345 |
|
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
346 |
|
# get set of objects from odbms by object name |
347 |
|
my $object_set = $self->{_COREHANDLE}->remote($nodename); |
348 |
|
@results = $self->{_COREHANDLE}->select($object_set); |
349 |
|
return \@results; |
350 |
|
} |
351 |
|
|
352 |
|
sub getListFiltered { |
353 |
|
my $self = shift; |
354 |
|
|
355 |
|
# redirect to unfiltered mode |
356 |
|
#return $self->getListUnfiltered(@_); |
357 |
|
|
358 |
|
my $nodename = shift; |
359 |
|
my $filters = shift; |
360 |
|
my @results; |
361 |
|
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
362 |
|
|
363 |
|
#print Dumper($filters); |
364 |
|
|
365 |
|
my @tfilters; |
366 |
|
|
367 |
|
foreach my $filter (@$filters) { |
368 |
|
|
369 |
|
# get filter - TODO: for each filter |
370 |
|
#my $filter = $filters->[0]; |
371 |
|
|
372 |
|
# build filter |
373 |
|
my $lexpr = $filter->{key}; |
374 |
|
#my $op = $filter->{op}; |
375 |
|
my $op = '='; |
376 |
|
my $rexpr = $filter->{val}; |
377 |
|
my $tight = 100; |
378 |
|
|
379 |
|
# my $tfilter = Tangram::Filter->new( |
380 |
|
# expr => "t1.$lexpr $op '$rexpr'", |
381 |
|
# tight => $tight, |
382 |
|
# objects => $objects, |
383 |
|
# ); |
384 |
|
|
385 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
386 |
|
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
387 |
|
|
388 |
|
} |
389 |
|
|
390 |
|
my $tfilter = join(' & ', @tfilters); |
391 |
|
|
392 |
|
# get set of objects from odbms by object name |
393 |
|
my $remote = $self->{_COREHANDLE}->remote($nodename); |
394 |
|
|
395 |
|
# was: |
396 |
|
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
397 |
|
|
398 |
|
# is: |
399 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
400 |
|
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
401 |
|
|
402 |
|
# get filtered list/set |
403 |
|
@results = eval($evalstring); |
404 |
|
die $@ if $@; |
405 |
|
|
406 |
|
return \@results; |
407 |
|
} |
408 |
|
|
409 |
|
sub createCursor { |
410 |
|
my $self = shift; |
411 |
|
my $node = shift; |
412 |
|
my $cmdHandle = $self->{_COREHANDLE}->cursor($node); |
413 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
414 |
|
return $result; |
415 |
|
} |
416 |
|
|
417 |
|
sub createSet { |
418 |
|
my $self = shift; |
419 |
|
my @objects = @_; |
420 |
|
my $rh = Set::Object->new(); |
421 |
|
foreach (@objects) { |
422 |
|
#print Dumper($_); |
423 |
|
$rh->insert($_) if !isEmpty($_); |
424 |
|
} |
425 |
|
#print Dumper($rh->members()); |
426 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
427 |
|
return $result; |
428 |
|
} |
429 |
|
|
430 |
|
sub sendQuery { |
431 |
|
my $self = shift; |
432 |
|
my $query = shift; |
433 |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
434 |
|
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
435 |
|
|
436 |
|
#print Dumper($query); |
437 |
|
|
438 |
|
# HACK: special case: querying by id does not translate into a common tangram query |
439 |
|
# just load the object by given id(ent) |
440 |
|
if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') { |
441 |
|
#print "LOAD!!!", "\n"; |
442 |
|
#exit; |
443 |
|
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
444 |
|
my $ident = $query->{criterias}->[0]->{val}; |
445 |
|
#print "load obj", "\n"; |
446 |
|
#return $self->createSet() if $ident == 5; |
447 |
|
my $object = $self->{_COREHANDLE}->load($ident); |
448 |
|
#print "get id", "\n"; |
449 |
|
my $oid = $self->{_COREHANDLE}->id($object); |
450 |
|
return $self->createSet($object); |
451 |
|
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
452 |
|
} |
453 |
|
|
454 |
|
die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
455 |
|
|
456 |
|
# TODO: do a common tangram query here |
457 |
|
|
458 |
|
my @crits; |
459 |
|
foreach (@{$query->{criterias}}) { |
460 |
|
my $op = ''; |
461 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
462 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
463 |
|
} |
464 |
|
my $subnodes = {}; |
465 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
466 |
|
# HACK: this is hardcoded ;( expand possibilities! |
467 |
|
#my $crit = join(' AND ', @crits); |
468 |
|
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
469 |
|
#return $self->sendCommand($sql); |
470 |
|
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
471 |
|
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
472 |
|
return $self->createCursor($query->{node}); |
473 |
|
} |
474 |
|
|
475 |
|
sub eraseAll { |
476 |
|
my $self = shift; |
477 |
|
my $classname = shift; |
478 |
|
my $remote = $self->{_COREHANDLE}->remote($classname); |
479 |
|
my @objs = $self->{_COREHANDLE}->select($remote); |
480 |
|
$self->{_COREHANDLE}->erase(@objs); |
481 |
|
} |
482 |
|
|
483 |
|
sub createDb { |
484 |
|
my $self = shift; |
485 |
|
my $storage = $self->_getSubLayerHandle(); |
486 |
|
return $storage->createDb(); |
487 |
|
} |
488 |
|
|
489 |
|
sub getObject { |
490 |
|
my $self = shift; |
491 |
|
my $oid = shift; |
492 |
|
# TODO: review this |
493 |
|
#if (!$self->{COREHANDLE}) { return; } |
494 |
|
return $self->{_COREHANDLE}->load($oid); |
495 |
|
} |
496 |
|
|
497 |
|
sub getObjectAsHash { |
498 |
|
my $self = shift; |
499 |
|
my $oid = shift; |
500 |
|
my $options = shift; |
501 |
|
my $obj = $self->getObject($oid); |
502 |
|
my $deref = var_deref($obj); |
503 |
|
var2utf8($deref) if ($options->{utf8}); |
504 |
|
undef($obj) if($options->{destroy}); |
505 |
|
return $deref; |
506 |
|
} |
507 |
|
|
508 |
|
sub getSchema { |
509 |
|
return $schema_tangram; |
510 |
|
} |
511 |
|
|
512 |
|
sub getCOREHANDLE { |
513 |
|
my $self = shift; |
514 |
|
return $self->{_COREHANDLE}; |
515 |
|
} |
516 |
|
|
517 |
1; |
1; |