3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.27 2003/01/31 06:30:59 joko |
7 |
|
# + enabled 'sendQuery' |
8 |
|
# |
9 |
|
# Revision 1.26 2003/01/30 22:29:47 joko |
10 |
|
# + fixed module usage (removed dependency on 'libp.pm') |
11 |
|
# |
12 |
|
# Revision 1.25 2003/01/19 02:30:05 joko |
13 |
|
# + fix: modified call to '_initSchema' |
14 |
|
# |
15 |
|
# Revision 1.24 2002/12/22 14:13:01 joko |
16 |
|
# + sub dropDb |
17 |
|
# |
18 |
|
# Revision 1.23 2002/12/19 16:31:53 joko |
19 |
|
# +- renamed sub to 'rebuildDb' |
20 |
|
# |
21 |
|
# Revision 1.22 2002/12/18 22:28:16 jonen |
22 |
|
# + added extended logging at 'getObjectByGuid()' |
23 |
|
# |
24 |
|
# Revision 1.21 2002/12/16 22:20:49 jonen |
25 |
|
# + fixed bug at 'getObjectByGuid()' |
26 |
|
# |
27 |
|
# Revision 1.20 2002/12/16 20:49:17 jonen |
28 |
|
# + added sub 'getObjectByGuid()' |
29 |
|
# + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()' |
30 |
|
# |
31 |
|
# Revision 1.19 2002/12/16 06:46:09 joko |
32 |
|
# + attempt to introduce a generic '_patchSchema' - cancelled! |
33 |
|
# |
34 |
|
# Revision 1.18 2002/12/13 21:48:07 joko |
35 |
|
# + fix to 'sub sendQuery' |
36 |
|
# |
37 |
|
# Revision 1.17 2002/12/12 02:51:09 joko |
38 |
|
# + cosmetics |
39 |
|
# |
40 |
|
# Revision 1.16 2002/12/11 06:54:10 joko |
41 |
|
# + fix: encapsulated object-loading inside an 'eval' |
42 |
|
# |
43 |
|
# Revision 1.15 2002/12/05 13:55:21 joko |
44 |
|
# + now utilizing 'object2hash' instead of 'var_deref' |
45 |
|
# + played around with having fresh-objects - no progress.... |
46 |
|
# |
47 |
# Revision 1.14 2002/12/05 09:40:30 jonen |
# Revision 1.14 2002/12/05 09:40:30 jonen |
48 |
# + added option->{destroy} at getObject for unloading all instance |
# + added option->{destroy} at getObject for unloading all instance |
49 |
# |
# |
116 |
|
|
117 |
use Tangram; |
use Tangram; |
118 |
use Data::Dumper; |
use Data::Dumper; |
119 |
use libp qw( getNewPerlObjectByPkgName ); |
use DesignPattern::Object; |
120 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
121 |
use Data::Compare::Struct qw( isEmpty ); |
use Data::Compare::Struct qw( isEmpty ); |
122 |
use Data::Transform::Deep qw( var_deref ); |
use Data::Transform::Deep qw( object2hash ); |
123 |
use Data::Transform::Encode qw( var2utf8 ); |
use Data::Transform::Encode qw( var2utf8 ); |
124 |
|
|
|
|
|
125 |
# get logger instance |
# get logger instance |
126 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
127 |
|
|
141 |
my $self = shift; |
my $self = shift; |
142 |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
143 |
#if (!$schema_tangram) { |
#if (!$schema_tangram) { |
144 |
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} } ); |
145 |
|
my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); |
146 |
$schema_tangram = $obj->getSchema(); |
$schema_tangram = $obj->getSchema(); |
147 |
#} |
#} |
148 |
if (!$schema_tangram) { |
if (!$schema_tangram) { |
149 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
150 |
return 0; |
return 0; |
151 |
} |
} |
152 |
|
#$self->_patchSchema(); |
153 |
return 1; |
return 1; |
154 |
} |
} |
155 |
|
|
156 |
|
sub _patchSchema { |
157 |
|
my $self = shift; |
158 |
|
foreach (keys %{$schema_tangram->{classes}}) { |
159 |
|
next if $schema_tangram->{classes}->{$_}->{abstract}; |
160 |
|
#next if ($_ ne 'TsBankAccount'); |
161 |
|
#$_ ne 'AbstractAccount' && |
162 |
|
print "class: $_", "\n"; |
163 |
|
#print Dumper($schema_tangram->{classes}->{$_}); |
164 |
|
# create new string property named 'guid' |
165 |
|
my $tstring = Tangram::String->new(); |
166 |
|
$tstring->{name} = $tstring->{col} = 'guid'; |
167 |
|
# inject property into schema |
168 |
|
#$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring; |
169 |
|
print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
sub connect { |
sub connect { |
174 |
|
|
175 |
my $self = shift; |
my $self = shift; |
176 |
|
|
177 |
my $dsn = shift; |
my $dsn = shift; |
178 |
|
|
179 |
|
#print Dumper($self); |
180 |
|
#exit; |
181 |
|
|
182 |
|
# TODO: re-enable |
183 |
$dsn ||= $self->{locator}->{dbi}->{dsn}; |
$dsn ||= $self->{locator}->{dbi}->{dsn}; |
|
|
|
184 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
185 |
|
|
186 |
#my $storage = Tangram::Relational->connect( $schema, $dsn ); |
#my $storage = Tangram::Relational->connect( $schema, $dsn ); |
192 |
# return; |
# return; |
193 |
# } |
# } |
194 |
|
|
195 |
return unless $self->_initSchema(); |
#return unless $self->_initSchema(); |
196 |
|
$self->_initSchema(); |
197 |
|
|
198 |
# create the main tangram storage object |
# create the main tangram storage object |
199 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
320 |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
321 |
|
|
322 |
#my $subLayer = $self->_getSubLayerHandle(); |
#my $subLayer = $self->_getSubLayerHandle(); |
323 |
|
#print Dumper($self); |
324 |
|
#exit; |
325 |
|
|
326 |
# apply configured modifications |
# apply configured modifications |
327 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
387 |
return $ok; |
return $ok; |
388 |
} |
} |
389 |
|
|
390 |
sub rebuildDbAndSchema { |
sub rebuildDb { |
391 |
my $self = shift; |
my $self = shift; |
392 |
$logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" ); |
$logger->info( __PACKAGE__ . "->rebuildDb()" ); |
393 |
my @results; |
my @results; |
394 |
|
|
395 |
# sum up results (bool (0/1)) in array |
# sum up results (bool (0/1)) in array |
429 |
my @results; |
my @results; |
430 |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
431 |
|
|
432 |
#print Dumper($filters); |
#print Dumper($filters); |
433 |
|
|
434 |
my @tfilters; |
my @tfilters; |
435 |
|
|
468 |
# 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 |
469 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
470 |
|
|
471 |
|
#print "eval: $evalstring", "\n"; |
472 |
|
|
473 |
# get filtered list/set |
# get filtered list/set |
474 |
@results = eval($evalstring); |
@results = eval($evalstring); |
475 |
die $@ if $@; |
die $@ if $@; |
487 |
|
|
488 |
sub createSet { |
sub createSet { |
489 |
my $self = shift; |
my $self = shift; |
490 |
|
#print "-" x 80, "\n"; |
491 |
|
#print Dumper(@_); |
492 |
my @objects = @_; |
my @objects = @_; |
493 |
my $rh = Set::Object->new(); |
my $rh = Set::Object->new(); |
494 |
foreach (@objects) { |
foreach (@objects) { |
495 |
#print Dumper($_); |
if (!isEmpty($_)) { |
496 |
$rh->insert($_) if !isEmpty($_); |
#print Dumper($_); |
497 |
|
$rh->insert($_); |
498 |
|
} |
499 |
} |
} |
500 |
#print Dumper($rh->members()); |
#print Dumper($rh->members()); |
501 |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
519 |
my $ident = $query->{criterias}->[0]->{val}; |
my $ident = $query->{criterias}->[0]->{val}; |
520 |
#print "load obj", "\n"; |
#print "load obj", "\n"; |
521 |
#return $self->createSet() if $ident == 5; |
#return $self->createSet() if $ident == 5; |
522 |
|
$self->{_COREHANDLE}->unload($ident); |
523 |
my $object = $self->{_COREHANDLE}->load($ident); |
my $object = $self->{_COREHANDLE}->load($ident); |
524 |
#print "get id", "\n"; |
#print "get id", "\n"; |
525 |
my $oid = $self->{_COREHANDLE}->id($object); |
my $oid = $self->{_COREHANDLE}->id($object); |
526 |
|
#print Dumper($object); |
527 |
|
#print "oid: $oid", "\n"; |
528 |
return $self->createSet($object); |
return $self->createSet($object); |
529 |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
530 |
} |
} |
531 |
|
|
532 |
die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
533 |
|
#return $self->createSet($object); |
534 |
|
#return $self->createSet($list); |
535 |
|
return $self->createSet(@$list); |
536 |
|
|
537 |
|
#die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
538 |
|
|
539 |
|
|
540 |
|
|
|
# TODO: do a common tangram query here |
|
541 |
|
|
542 |
|
# try a generic tangram query here |
543 |
|
# TODO: try to place an oql on top of that (search.cpan.org!) |
544 |
my @crits; |
my @crits; |
545 |
foreach (@{$query->{criterias}}) { |
foreach (@{$query->{criterias}}) { |
546 |
my $op = ''; |
my $op = ''; |
576 |
my $self = shift; |
my $self = shift; |
577 |
my $oid = shift; |
my $oid = shift; |
578 |
my $options = shift; |
my $options = shift; |
579 |
# TODO: create a deep_unload method |
|
580 |
|
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
581 |
# unload($oid) will only unload object, not deep object hashes |
# unload($oid) will only unload object, not deep object hashes |
582 |
$self->{_COREHANDLE}->unload() if($options->{destroy}); |
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
583 |
|
|
584 |
# TODO: review this |
# TODO: review this |
585 |
#if (!$self->{COREHANDLE}) { return; } |
#if (!$self->{COREHANDLE}) { return; } |
586 |
return $self->{_COREHANDLE}->load($oid); |
|
587 |
|
# TODO: review this |
588 |
|
my $object = eval('$self->{_COREHANDLE}->load($oid);'); |
589 |
|
print $@, "\n" if $@; |
590 |
|
|
591 |
|
return $object if $object; |
592 |
|
} |
593 |
|
|
594 |
|
sub getObjectByGuid { |
595 |
|
my $self = shift; |
596 |
|
my $guid = shift; |
597 |
|
my $options = shift; |
598 |
|
|
599 |
|
# Guid and Classname is needed |
600 |
|
if(!$guid || !$options->{classname}) { |
601 |
|
$logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" ); |
602 |
|
return; |
603 |
|
} |
604 |
|
|
605 |
|
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
606 |
|
# unload($oid) will only unload object, not deep object hashes |
607 |
|
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
608 |
|
|
609 |
|
# search for object with given Classname and Guid |
610 |
|
my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname}); |
611 |
|
my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid); |
612 |
|
|
613 |
|
# we searched for global unique identifer of some object, |
614 |
|
# so I think we can trust there would be only one result |
615 |
|
if($result[0]) { |
616 |
|
return $result[0]; |
617 |
|
} else { |
618 |
|
$logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" ); |
619 |
|
return; |
620 |
|
} |
621 |
|
|
622 |
} |
} |
623 |
|
|
624 |
sub getObjectAsHash { |
sub getObjectAsHash { |
625 |
my $self = shift; |
my $self = shift; |
626 |
my $oid = shift; |
my $oid = shift; |
627 |
my $options = shift; |
my $options = shift; |
628 |
my $obj = $self->getObject($oid, $options); |
my $obj; |
629 |
my $deref = var_deref($obj); |
|
630 |
var2utf8($deref) if ($options->{utf8}); |
if($options->{guid}) { |
631 |
#$obj->clear_refs; |
$obj = $self->getObjectByGuid($oid, $options); |
632 |
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
} else { |
633 |
#$obj->DESTROY; |
$obj = $self->getObject($oid, $options); |
634 |
#undef($obj) if($options->{destroy}); |
} |
635 |
return $deref; |
|
636 |
|
# build options (a callback to unload autovivified objects) for 'expand' |
637 |
|
# TODO: use $logger to write to debug here! |
638 |
|
my $cb; # = sub {}; |
639 |
|
=pod |
640 |
|
if ($options->{destroy}) { |
641 |
|
$options->{cb}->{destroy} = sub { |
642 |
|
print "================ DESTROY", "\n"; |
643 |
|
my $object = shift; |
644 |
|
#print Dumper($object); |
645 |
|
$self->{_COREHANDLE}->unload($object); |
646 |
|
#undef($object); |
647 |
|
}; |
648 |
|
} |
649 |
|
=cut |
650 |
|
|
651 |
|
my $hash = object2hash($obj, $options); |
652 |
|
#$options->{cb}->{destroy}->($obj); |
653 |
|
#$self->{_COREHANDLE}->unload($obj); |
654 |
|
|
655 |
|
# convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML |
656 |
|
# now done in object2hash |
657 |
|
#var2utf8($hash) if ($options->{utf8}); |
658 |
|
|
659 |
|
# old (wrong) attempts to get rid of used instances, if requested |
660 |
|
#$obj->clear_refs; |
661 |
|
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
662 |
|
#$obj->DESTROY; |
663 |
|
#undef($obj) if($options->{destroy}); |
664 |
|
|
665 |
|
return $hash; |
666 |
} |
} |
667 |
|
|
668 |
sub getSchema { |
sub getSchema { |
674 |
return $self->{_COREHANDLE}; |
return $self->{_COREHANDLE}; |
675 |
} |
} |
676 |
|
|
677 |
|
sub dropDb { |
678 |
|
my $self = shift; |
679 |
|
my $storage = $self->_getSubLayerHandle(); |
680 |
|
return $storage->dropDb(); |
681 |
|
} |
682 |
|
|
683 |
|
sub testAvailability { |
684 |
|
my $self = shift; |
685 |
|
my $storage = $self->_getSubLayerHandle(); |
686 |
|
return $storage->testAvailability(); |
687 |
|
} |
688 |
|
|
689 |
1; |
1; |
690 |
|
__END__ |