3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.24 2002/12/22 14:13:01 joko |
7 |
|
# + sub dropDb |
8 |
|
# |
9 |
|
# Revision 1.23 2002/12/19 16:31:53 joko |
10 |
|
# +- renamed sub to 'rebuildDb' |
11 |
|
# |
12 |
|
# Revision 1.22 2002/12/18 22:28:16 jonen |
13 |
|
# + added extended logging at 'getObjectByGuid()' |
14 |
|
# |
15 |
|
# Revision 1.21 2002/12/16 22:20:49 jonen |
16 |
|
# + fixed bug at 'getObjectByGuid()' |
17 |
|
# |
18 |
|
# Revision 1.20 2002/12/16 20:49:17 jonen |
19 |
|
# + added sub 'getObjectByGuid()' |
20 |
|
# + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()' |
21 |
|
# |
22 |
|
# Revision 1.19 2002/12/16 06:46:09 joko |
23 |
|
# + attempt to introduce a generic '_patchSchema' - cancelled! |
24 |
|
# |
25 |
|
# Revision 1.18 2002/12/13 21:48:07 joko |
26 |
|
# + fix to 'sub sendQuery' |
27 |
|
# |
28 |
|
# Revision 1.17 2002/12/12 02:51:09 joko |
29 |
|
# + cosmetics |
30 |
|
# |
31 |
|
# Revision 1.16 2002/12/11 06:54:10 joko |
32 |
|
# + fix: encapsulated object-loading inside an 'eval' |
33 |
|
# |
34 |
|
# Revision 1.15 2002/12/05 13:55:21 joko |
35 |
|
# + now utilizing 'object2hash' instead of 'var_deref' |
36 |
|
# + played around with having fresh-objects - no progress.... |
37 |
|
# |
38 |
|
# Revision 1.14 2002/12/05 09:40:30 jonen |
39 |
|
# + added option->{destroy} at getObject for unloading all instance |
40 |
|
# |
41 |
|
# Revision 1.13 2002/12/05 07:59:04 joko |
42 |
|
# + now using Tie::SecureHash as a base for the COREHANDLE |
43 |
|
# + former public COREHANDLE becomes private _COREHANDLE now |
44 |
|
# + sub getCOREHANDLE |
45 |
|
# |
46 |
|
# Revision 1.12 2002/12/04 11:34:49 joko |
47 |
|
# - $schema_tangram doesn't have to be in class? |
48 |
|
# |
49 |
# Revision 1.11 2002/12/04 08:54:08 jonen |
# Revision 1.11 2002/12/04 08:54:08 jonen |
50 |
# + untested bugfix: undef($object) after transform to hash at getObjectAsHash |
# + untested bugfix: undef($object) after transform to hash at getObjectAsHash |
51 |
# |
# |
102 |
use strict; |
use strict; |
103 |
use warnings; |
use warnings; |
104 |
|
|
105 |
|
use base ("Data::Storage::Handler"); |
106 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
107 |
|
|
108 |
use Tangram; |
use Tangram; |
110 |
use libp qw( getNewPerlObjectByPkgName ); |
use libp qw( getNewPerlObjectByPkgName ); |
111 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
112 |
use Data::Compare::Struct qw( isEmpty ); |
use Data::Compare::Struct qw( isEmpty ); |
113 |
use Data::Transform::Deep qw( var_deref ); |
use Data::Transform::Deep qw( object2hash ); |
114 |
use Data::Transform::Encode qw( var2utf8 ); |
use Data::Transform::Encode qw( var2utf8 ); |
115 |
|
|
116 |
|
|
118 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
119 |
|
|
120 |
|
|
121 |
|
# this holds the complete instantiated schema from tangram |
122 |
|
my $schema_tangram; |
123 |
|
|
124 |
sub getMetaInfo { |
sub getMetaInfo { |
125 |
my $self = shift; |
my $self = shift; |
126 |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
132 |
sub _initSchema { |
sub _initSchema { |
133 |
my $self = shift; |
my $self = shift; |
134 |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
135 |
#if (!$self->{schema_tangram}) { |
#if (!$schema_tangram) { |
136 |
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} } ); |
137 |
$self->{schema_tangram} = $obj->getSchema(); |
$schema_tangram = $obj->getSchema(); |
138 |
#} |
#} |
139 |
if (!$self->{schema_tangram}) { |
if (!$schema_tangram) { |
140 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
141 |
return 0; |
return 0; |
142 |
} |
} |
143 |
|
#$self->_patchSchema(); |
144 |
return 1; |
return 1; |
145 |
} |
} |
146 |
|
|
147 |
|
sub _patchSchema { |
148 |
|
my $self = shift; |
149 |
|
foreach (keys %{$schema_tangram->{classes}}) { |
150 |
|
next if $schema_tangram->{classes}->{$_}->{abstract}; |
151 |
|
#next if ($_ ne 'TsBankAccount'); |
152 |
|
#$_ ne 'AbstractAccount' && |
153 |
|
print "class: $_", "\n"; |
154 |
|
#print Dumper($schema_tangram->{classes}->{$_}); |
155 |
|
# create new string property named 'guid' |
156 |
|
my $tstring = Tangram::String->new(); |
157 |
|
$tstring->{name} = $tstring->{col} = 'guid'; |
158 |
|
# inject property into schema |
159 |
|
#$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring; |
160 |
|
print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
sub connect { |
sub connect { |
165 |
|
|
166 |
my $self = shift; |
my $self = shift; |
183 |
|
|
184 |
# create the main tangram storage object |
# create the main tangram storage object |
185 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
186 |
$self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); |
$self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn ); |
187 |
|
|
188 |
|
#print "connect", "\n"; |
189 |
|
#my $core = $self->{_COREHANDLE}; |
190 |
|
#print Dumper($core); |
191 |
|
|
192 |
# some attempts for configuring the wrapped underlying dbi..... |
# some attempts for configuring the wrapped underlying dbi..... |
193 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
293 |
foreach my $key (keys %{$self->{dbi}}) { |
foreach my $key (keys %{$self->{dbi}}) { |
294 |
my $val = $self->{dbi}->{$key}; |
my $val = $self->{dbi}->{$key}; |
295 |
print "entry: $key; $val", "\n"; |
print "entry: $key; $val", "\n"; |
296 |
$self->{COREHANDLE}->{db}->{$key} = $val; |
$self->{_COREHANDLE}->{db}->{$key} = $val; |
297 |
} |
} |
298 |
#print Dumper($self->{COREHANDLE}->{db}); |
#print Dumper($self->{COREHANDLE}->{db}); |
299 |
} |
} |
309 |
|
|
310 |
# apply configured modifications |
# apply configured modifications |
311 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
312 |
$self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
$self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
313 |
} |
} |
314 |
if (exists $self->{dbi}->{RaiseError}) { |
if (exists $self->{dbi}->{RaiseError}) { |
315 |
$self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
$self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
316 |
} |
} |
317 |
if (exists $self->{dbi}->{PrintError}) { |
if (exists $self->{dbi}->{PrintError}) { |
318 |
$self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
$self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
319 |
} |
} |
320 |
if (exists $self->{dbi}->{HandleError}) { |
if (exists $self->{dbi}->{HandleError}) { |
321 |
$self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
$self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
322 |
} |
} |
323 |
|
|
324 |
} |
} |
334 |
my $ok; |
my $ok; |
335 |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
336 |
return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
337 |
$ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh ); |
$ok = Tangram::Relational->deploy($schema_tangram, $dbh ); |
338 |
$dbh->disconnect(); |
$dbh->disconnect(); |
339 |
} |
} |
340 |
return $ok; |
return $ok; |
355 |
#use Data::Dumper; print Dumper($self); |
#use Data::Dumper; print Dumper($self); |
356 |
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
357 |
|
|
358 |
$ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh ); |
$ok = Tangram::Relational->retreat($schema_tangram, $dbh ); |
359 |
|
|
360 |
# 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... |
361 |
# - ... Tangram::Relational->retreat doesn't seem to return a valid status |
# - ... Tangram::Relational->retreat doesn't seem to return a valid status |
371 |
return $ok; |
return $ok; |
372 |
} |
} |
373 |
|
|
374 |
sub rebuildDbAndSchema { |
sub rebuildDb { |
375 |
my $self = shift; |
my $self = shift; |
376 |
$logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" ); |
$logger->info( __PACKAGE__ . "->rebuildDb()" ); |
377 |
my @results; |
my @results; |
378 |
|
|
379 |
# sum up results (bool (0/1)) in array |
# sum up results (bool (0/1)) in array |
397 |
my @results; |
my @results; |
398 |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
399 |
# get set of objects from odbms by object name |
# get set of objects from odbms by object name |
400 |
my $object_set = $self->{COREHANDLE}->remote($nodename); |
my $object_set = $self->{_COREHANDLE}->remote($nodename); |
401 |
@results = $self->{COREHANDLE}->select($object_set); |
@results = $self->{_COREHANDLE}->select($object_set); |
402 |
return \@results; |
return \@results; |
403 |
} |
} |
404 |
|
|
443 |
my $tfilter = join(' & ', @tfilters); |
my $tfilter = join(' & ', @tfilters); |
444 |
|
|
445 |
# get set of objects from odbms by object name |
# get set of objects from odbms by object name |
446 |
my $remote = $self->{COREHANDLE}->remote($nodename); |
my $remote = $self->{_COREHANDLE}->remote($nodename); |
447 |
|
|
448 |
# was: |
# was: |
449 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
450 |
|
|
451 |
# is: |
# is: |
452 |
# 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 |
453 |
my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');'; |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
454 |
|
|
455 |
# get filtered list/set |
# get filtered list/set |
456 |
@results = eval($evalstring); |
@results = eval($evalstring); |
462 |
sub createCursor { |
sub createCursor { |
463 |
my $self = shift; |
my $self = shift; |
464 |
my $node = shift; |
my $node = shift; |
465 |
my $cmdHandle = $self->{COREHANDLE}->cursor($node); |
my $cmdHandle = $self->{_COREHANDLE}->cursor($node); |
466 |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
467 |
return $result; |
return $result; |
468 |
} |
} |
469 |
|
|
470 |
sub createSet { |
sub createSet { |
471 |
my $self = shift; |
my $self = shift; |
472 |
|
#print "-" x 80, "\n"; |
473 |
|
#print Dumper(@_); |
474 |
my @objects = @_; |
my @objects = @_; |
475 |
my $rh = Set::Object->new(); |
my $rh = Set::Object->new(); |
476 |
foreach (@objects) { |
foreach (@objects) { |
477 |
#print Dumper($_); |
if (!isEmpty($_)) { |
478 |
$rh->insert($_) if !isEmpty($_); |
#print Dumper($_); |
479 |
|
$rh->insert($_); |
480 |
|
} |
481 |
} |
} |
482 |
#print Dumper($rh->members()); |
#print Dumper($rh->members()); |
483 |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
501 |
my $ident = $query->{criterias}->[0]->{val}; |
my $ident = $query->{criterias}->[0]->{val}; |
502 |
#print "load obj", "\n"; |
#print "load obj", "\n"; |
503 |
#return $self->createSet() if $ident == 5; |
#return $self->createSet() if $ident == 5; |
504 |
my $object = $self->{COREHANDLE}->load($ident); |
$self->{_COREHANDLE}->unload($ident); |
505 |
|
my $object = $self->{_COREHANDLE}->load($ident); |
506 |
#print "get id", "\n"; |
#print "get id", "\n"; |
507 |
my $oid = $self->{COREHANDLE}->id($object); |
my $oid = $self->{_COREHANDLE}->id($object); |
508 |
|
#print Dumper($object); |
509 |
|
#print "oid: $oid", "\n"; |
510 |
return $self->createSet($object); |
return $self->createSet($object); |
511 |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
512 |
} |
} |
535 |
sub eraseAll { |
sub eraseAll { |
536 |
my $self = shift; |
my $self = shift; |
537 |
my $classname = shift; |
my $classname = shift; |
538 |
my $remote = $self->{COREHANDLE}->remote($classname); |
my $remote = $self->{_COREHANDLE}->remote($classname); |
539 |
my @objs = $self->{COREHANDLE}->select($remote); |
my @objs = $self->{_COREHANDLE}->select($remote); |
540 |
$self->{COREHANDLE}->erase(@objs); |
$self->{_COREHANDLE}->erase(@objs); |
541 |
} |
} |
542 |
|
|
543 |
sub createDb { |
sub createDb { |
549 |
sub getObject { |
sub getObject { |
550 |
my $self = shift; |
my $self = shift; |
551 |
my $oid = shift; |
my $oid = shift; |
552 |
|
my $options = shift; |
553 |
|
|
554 |
|
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
555 |
|
# unload($oid) will only unload object, not deep object hashes |
556 |
|
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
557 |
|
|
558 |
# TODO: review this |
# TODO: review this |
559 |
#if (!$self->{COREHANDLE}) { return; } |
#if (!$self->{COREHANDLE}) { return; } |
560 |
return $self->{COREHANDLE}->load($oid); |
|
561 |
|
# TODO: review this |
562 |
|
my $object = eval('$self->{_COREHANDLE}->load($oid);'); |
563 |
|
print $@, "\n" if $@; |
564 |
|
|
565 |
|
return $object if $object; |
566 |
|
} |
567 |
|
|
568 |
|
sub getObjectByGuid { |
569 |
|
my $self = shift; |
570 |
|
my $guid = shift; |
571 |
|
my $options = shift; |
572 |
|
|
573 |
|
# Guid and Classname is needed |
574 |
|
if(!$guid || !$options->{classname}) { |
575 |
|
$logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" ); |
576 |
|
return; |
577 |
|
} |
578 |
|
|
579 |
|
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
580 |
|
# unload($oid) will only unload object, not deep object hashes |
581 |
|
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
582 |
|
|
583 |
|
# search for object with given Classname and Guid |
584 |
|
my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname}); |
585 |
|
my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid); |
586 |
|
|
587 |
|
# we searched for global unique identifer of some object, |
588 |
|
# so I think we can trust there would be only one result |
589 |
|
if($result[0]) { |
590 |
|
return $result[0]; |
591 |
|
} else { |
592 |
|
$logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" ); |
593 |
|
return; |
594 |
|
} |
595 |
|
|
596 |
} |
} |
597 |
|
|
598 |
sub getObjectAsHash { |
sub getObjectAsHash { |
599 |
my $self = shift; |
my $self = shift; |
600 |
my $oid = shift; |
my $oid = shift; |
601 |
my $options = shift; |
my $options = shift; |
602 |
my $obj = $self->getObject($oid); |
my $obj; |
603 |
my $deref = var_deref($obj); |
|
604 |
var2utf8($deref) if ($options->{utf8}); |
if($options->{guid}) { |
605 |
undef($obj) if($options->{destroy}); |
$obj = $self->getObjectByGuid($oid, $options); |
606 |
return $deref; |
} else { |
607 |
|
$obj = $self->getObject($oid, $options); |
608 |
|
} |
609 |
|
|
610 |
|
# build options (a callback to unload autovivified objects) for 'expand' |
611 |
|
# TODO: use $logger to write to debug here! |
612 |
|
my $cb; # = sub {}; |
613 |
|
=pod |
614 |
|
if ($options->{destroy}) { |
615 |
|
$options->{cb}->{destroy} = sub { |
616 |
|
print "================ DESTROY", "\n"; |
617 |
|
my $object = shift; |
618 |
|
#print Dumper($object); |
619 |
|
$self->{_COREHANDLE}->unload($object); |
620 |
|
#undef($object); |
621 |
|
}; |
622 |
|
} |
623 |
|
=cut |
624 |
|
|
625 |
|
my $hash = object2hash($obj, $options); |
626 |
|
#$options->{cb}->{destroy}->($obj); |
627 |
|
#$self->{_COREHANDLE}->unload($obj); |
628 |
|
|
629 |
|
# convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML |
630 |
|
# now done in object2hash |
631 |
|
#var2utf8($hash) if ($options->{utf8}); |
632 |
|
|
633 |
|
# old (wrong) attempts to get rid of used instances, if requested |
634 |
|
#$obj->clear_refs; |
635 |
|
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
636 |
|
#$obj->DESTROY; |
637 |
|
#undef($obj) if($options->{destroy}); |
638 |
|
|
639 |
|
return $hash; |
640 |
|
} |
641 |
|
|
642 |
|
sub getSchema { |
643 |
|
return $schema_tangram; |
644 |
|
} |
645 |
|
|
646 |
|
sub getCOREHANDLE { |
647 |
|
my $self = shift; |
648 |
|
return $self->{_COREHANDLE}; |
649 |
|
} |
650 |
|
|
651 |
|
sub dropDb { |
652 |
|
my $self = shift; |
653 |
|
my $storage = $self->_getSubLayerHandle(); |
654 |
|
return $storage->dropDb(); |
655 |
} |
} |
656 |
|
|
657 |
1; |
1; |