3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.16 2002/12/11 06:54:10 joko |
7 |
|
# + fix: encapsulated object-loading inside an 'eval' |
8 |
|
# |
9 |
|
# Revision 1.15 2002/12/05 13:55:21 joko |
10 |
|
# + now utilizing 'object2hash' instead of 'var_deref' |
11 |
|
# + played around with having fresh-objects - no progress.... |
12 |
|
# |
13 |
|
# Revision 1.14 2002/12/05 09:40:30 jonen |
14 |
|
# + added option->{destroy} at getObject for unloading all instance |
15 |
|
# |
16 |
|
# Revision 1.13 2002/12/05 07:59:04 joko |
17 |
|
# + now using Tie::SecureHash as a base for the COREHANDLE |
18 |
|
# + former public COREHANDLE becomes private _COREHANDLE now |
19 |
|
# + sub getCOREHANDLE |
20 |
|
# |
21 |
|
# Revision 1.12 2002/12/04 11:34:49 joko |
22 |
|
# - $schema_tangram doesn't have to be in class? |
23 |
|
# |
24 |
|
# Revision 1.11 2002/12/04 08:54:08 jonen |
25 |
|
# + untested bugfix: undef($object) after transform to hash at getObjectAsHash |
26 |
|
# |
27 |
# Revision 1.10 2002/12/03 15:53:23 joko |
# Revision 1.10 2002/12/03 15:53:23 joko |
28 |
# + small bugfix regarding object hierarchy |
# + small bugfix regarding object hierarchy |
29 |
# |
# |
77 |
use strict; |
use strict; |
78 |
use warnings; |
use warnings; |
79 |
|
|
80 |
|
use base ("Data::Storage::Handler"); |
81 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
82 |
|
|
83 |
use Tangram; |
use Tangram; |
85 |
use libp qw( getNewPerlObjectByPkgName ); |
use libp qw( getNewPerlObjectByPkgName ); |
86 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
87 |
use Data::Compare::Struct qw( isEmpty ); |
use Data::Compare::Struct qw( isEmpty ); |
88 |
use Data::Transform::Deep qw( var_deref ); |
use Data::Transform::Deep qw( object2hash ); |
89 |
use Data::Transform::Encode qw( var2utf8 ); |
use Data::Transform::Encode qw( var2utf8 ); |
90 |
|
|
91 |
|
|
93 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
94 |
|
|
95 |
|
|
96 |
|
# this holds the complete instantiated schema from tangram |
97 |
|
my $schema_tangram; |
98 |
|
|
99 |
sub getMetaInfo { |
sub getMetaInfo { |
100 |
my $self = shift; |
my $self = shift; |
101 |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
107 |
sub _initSchema { |
sub _initSchema { |
108 |
my $self = shift; |
my $self = shift; |
109 |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
110 |
#if (!$self->{schema_tangram}) { |
#if (!$schema_tangram) { |
111 |
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} } ); |
112 |
$self->{schema_tangram} = $obj->getSchema(); |
$schema_tangram = $obj->getSchema(); |
113 |
#} |
#} |
114 |
if (!$self->{schema_tangram}) { |
if (!$schema_tangram) { |
115 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
116 |
return 0; |
return 0; |
117 |
} |
} |
140 |
|
|
141 |
# create the main tangram storage object |
# create the main tangram storage object |
142 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
143 |
$self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); |
$self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn ); |
144 |
|
|
145 |
|
#print "connect", "\n"; |
146 |
|
#my $core = $self->{_COREHANDLE}; |
147 |
|
#print Dumper($core); |
148 |
|
|
149 |
# some attempts for configuring the wrapped underlying dbi..... |
# some attempts for configuring the wrapped underlying dbi..... |
150 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
250 |
foreach my $key (keys %{$self->{dbi}}) { |
foreach my $key (keys %{$self->{dbi}}) { |
251 |
my $val = $self->{dbi}->{$key}; |
my $val = $self->{dbi}->{$key}; |
252 |
print "entry: $key; $val", "\n"; |
print "entry: $key; $val", "\n"; |
253 |
$self->{COREHANDLE}->{db}->{$key} = $val; |
$self->{_COREHANDLE}->{db}->{$key} = $val; |
254 |
} |
} |
255 |
#print Dumper($self->{COREHANDLE}->{db}); |
#print Dumper($self->{COREHANDLE}->{db}); |
256 |
} |
} |
266 |
|
|
267 |
# apply configured modifications |
# apply configured modifications |
268 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
269 |
$self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
$self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
270 |
} |
} |
271 |
if (exists $self->{dbi}->{RaiseError}) { |
if (exists $self->{dbi}->{RaiseError}) { |
272 |
$self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
$self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
273 |
} |
} |
274 |
if (exists $self->{dbi}->{PrintError}) { |
if (exists $self->{dbi}->{PrintError}) { |
275 |
$self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
$self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
276 |
} |
} |
277 |
if (exists $self->{dbi}->{HandleError}) { |
if (exists $self->{dbi}->{HandleError}) { |
278 |
$self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
$self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
279 |
} |
} |
280 |
|
|
281 |
} |
} |
291 |
my $ok; |
my $ok; |
292 |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
293 |
return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
294 |
$ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh ); |
$ok = Tangram::Relational->deploy($schema_tangram, $dbh ); |
295 |
$dbh->disconnect(); |
$dbh->disconnect(); |
296 |
} |
} |
297 |
return $ok; |
return $ok; |
312 |
#use Data::Dumper; print Dumper($self); |
#use Data::Dumper; print Dumper($self); |
313 |
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
314 |
|
|
315 |
$ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh ); |
$ok = Tangram::Relational->retreat($schema_tangram, $dbh ); |
316 |
|
|
317 |
# answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because... |
# answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because... |
318 |
# - ... Tangram::Relational->retreat doesn't seem to return a valid status |
# - ... Tangram::Relational->retreat doesn't seem to return a valid status |
354 |
my @results; |
my @results; |
355 |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
356 |
# get set of objects from odbms by object name |
# get set of objects from odbms by object name |
357 |
my $object_set = $self->{COREHANDLE}->remote($nodename); |
my $object_set = $self->{_COREHANDLE}->remote($nodename); |
358 |
@results = $self->{COREHANDLE}->select($object_set); |
@results = $self->{_COREHANDLE}->select($object_set); |
359 |
return \@results; |
return \@results; |
360 |
} |
} |
361 |
|
|
400 |
my $tfilter = join(' & ', @tfilters); |
my $tfilter = join(' & ', @tfilters); |
401 |
|
|
402 |
# get set of objects from odbms by object name |
# get set of objects from odbms by object name |
403 |
my $remote = $self->{COREHANDLE}->remote($nodename); |
my $remote = $self->{_COREHANDLE}->remote($nodename); |
404 |
|
|
405 |
# was: |
# was: |
406 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
407 |
|
|
408 |
# is: |
# is: |
409 |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
410 |
my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');'; |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
411 |
|
|
412 |
# get filtered list/set |
# get filtered list/set |
413 |
@results = eval($evalstring); |
@results = eval($evalstring); |
419 |
sub createCursor { |
sub createCursor { |
420 |
my $self = shift; |
my $self = shift; |
421 |
my $node = shift; |
my $node = shift; |
422 |
my $cmdHandle = $self->{COREHANDLE}->cursor($node); |
my $cmdHandle = $self->{_COREHANDLE}->cursor($node); |
423 |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
424 |
return $result; |
return $result; |
425 |
} |
} |
454 |
my $ident = $query->{criterias}->[0]->{val}; |
my $ident = $query->{criterias}->[0]->{val}; |
455 |
#print "load obj", "\n"; |
#print "load obj", "\n"; |
456 |
#return $self->createSet() if $ident == 5; |
#return $self->createSet() if $ident == 5; |
457 |
my $object = $self->{COREHANDLE}->load($ident); |
my $object = $self->{_COREHANDLE}->load($ident); |
458 |
#print "get id", "\n"; |
#print "get id", "\n"; |
459 |
my $oid = $self->{COREHANDLE}->id($object); |
my $oid = $self->{_COREHANDLE}->id($object); |
460 |
return $self->createSet($object); |
return $self->createSet($object); |
461 |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
462 |
} |
} |
485 |
sub eraseAll { |
sub eraseAll { |
486 |
my $self = shift; |
my $self = shift; |
487 |
my $classname = shift; |
my $classname = shift; |
488 |
my $remote = $self->{COREHANDLE}->remote($classname); |
my $remote = $self->{_COREHANDLE}->remote($classname); |
489 |
my @objs = $self->{COREHANDLE}->select($remote); |
my @objs = $self->{_COREHANDLE}->select($remote); |
490 |
$self->{COREHANDLE}->erase(@objs); |
$self->{_COREHANDLE}->erase(@objs); |
491 |
} |
} |
492 |
|
|
493 |
sub createDb { |
sub createDb { |
499 |
sub getObject { |
sub getObject { |
500 |
my $self = shift; |
my $self = shift; |
501 |
my $oid = shift; |
my $oid = shift; |
502 |
|
my $options = shift; |
503 |
|
|
504 |
|
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
505 |
|
# unload($oid) will only unload object, not deep object hashes |
506 |
|
$self->{_COREHANDLE}->unload() if($options->{destroy}); |
507 |
|
|
508 |
# TODO: review this |
# TODO: review this |
509 |
#if (!$self->{COREHANDLE}) { return; } |
#if (!$self->{COREHANDLE}) { return; } |
510 |
return $self->{COREHANDLE}->load($oid); |
|
511 |
|
# TODO: review this |
512 |
|
my $object = eval('$self->{_COREHANDLE}->load($oid);'); |
513 |
|
print $@, "\n" if $@; |
514 |
|
|
515 |
|
return $object if $object; |
516 |
} |
} |
517 |
|
|
518 |
sub getObjectAsHash { |
sub getObjectAsHash { |
519 |
my $self = shift; |
my $self = shift; |
520 |
my $oid = shift; |
my $oid = shift; |
521 |
my $options = shift; |
my $options = shift; |
522 |
my $obj = $self->getObject($oid); |
my $obj = $self->getObject($oid, $options); |
523 |
my $deref = var_deref($obj); |
|
524 |
var2utf8($deref) if ($options->{utf8}); |
# build options (a callback to unload autovivified objects) for 'expand' |
525 |
return $deref; |
# TODO: use $logger to write to debug here! |
526 |
|
my $cb; # = sub {}; |
527 |
|
=pod |
528 |
|
if ($options->{destroy}) { |
529 |
|
$options->{cb}->{destroy} = sub { |
530 |
|
print "================ DESTROY", "\n"; |
531 |
|
my $object = shift; |
532 |
|
#print Dumper($object); |
533 |
|
$self->{_COREHANDLE}->unload($object); |
534 |
|
#undef($object); |
535 |
|
}; |
536 |
|
} |
537 |
|
=cut |
538 |
|
|
539 |
|
my $hash = object2hash($obj, $options); |
540 |
|
#$options->{cb}->{destroy}->($obj); |
541 |
|
#$self->{_COREHANDLE}->unload($obj); |
542 |
|
|
543 |
|
# convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML |
544 |
|
# now done in object2hash |
545 |
|
#var2utf8($hash) if ($options->{utf8}); |
546 |
|
|
547 |
|
# old (wrong) attempts to get rid of used instances, if requested |
548 |
|
#$obj->clear_refs; |
549 |
|
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
550 |
|
#$obj->DESTROY; |
551 |
|
#undef($obj) if($options->{destroy}); |
552 |
|
|
553 |
|
return $hash; |
554 |
|
} |
555 |
|
|
556 |
|
sub getSchema { |
557 |
|
return $schema_tangram; |
558 |
|
} |
559 |
|
|
560 |
|
sub getCOREHANDLE { |
561 |
|
my $self = shift; |
562 |
|
return $self->{_COREHANDLE}; |
563 |
} |
} |
564 |
|
|
565 |
1; |
1; |