3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.40 2003/06/25 22:57:54 joko |
7 |
|
# major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting" |
8 |
|
# |
9 |
|
# Revision 1.39 2003/06/06 11:40:40 jonen |
10 |
|
# fixed bug at 'getFilteredList' |
11 |
|
# |
12 |
|
# Revision 1.38 2003/05/13 16:38:38 joko |
13 |
|
# problems with "tied" on 5.6.1/win32 |
14 |
|
# |
15 |
|
# Revision 1.37 2003/05/10 17:37:39 jonen |
16 |
|
# corrected last commit |
17 |
|
# |
18 |
|
# Revision 1.36 2003/05/10 17:31:18 jonen |
19 |
|
# + added new functions related to 'create' item |
20 |
|
# - createNode() |
21 |
|
# # creates non-persistent 'deep dummy filled' object |
22 |
|
# - insertChildNode() |
23 |
|
# # inserts child node at given parent (child node haven't to exists, |
24 |
|
# createNode will be injected transparently) |
25 |
|
# |
26 |
|
# Revision 1.35 2003/04/19 16:09:48 jonen |
27 |
|
# + added operator dispatching (currently for getting ref-type) at 'getListFiltered' |
28 |
|
# |
29 |
|
# Revision 1.34 2003/04/11 01:18:53 joko |
30 |
|
# sendQuery: |
31 |
|
# + introduced crud action 'DELETE' |
32 |
|
# |
33 |
|
# Revision 1.33 2003/04/09 06:07:43 joko |
34 |
|
# revamped 'sub sendQuery' |
35 |
|
# |
36 |
|
# Revision 1.32 2003/04/08 22:52:22 joko |
37 |
|
# modified 'querySchema': better behaviour regarding filtering result |
38 |
|
# |
39 |
|
# Revision 1.31 2003/04/05 21:24:09 joko |
40 |
|
# modified 'sub getChildNodes': now contains code from 'querySchema' |
41 |
|
# |
42 |
|
# Revision 1.30 2003/03/27 15:31:14 joko |
43 |
|
# fixes to modules regarding new namespace(s) below Data::Mungle::* |
44 |
|
# |
45 |
|
# Revision 1.29 2003/02/21 01:47:18 joko |
46 |
|
# purged old code |
47 |
|
# minor cosmetics |
48 |
|
# |
49 |
|
# Revision 1.28 2003/02/20 20:20:26 joko |
50 |
|
# tried to get auto-disconnect working again - failed with that |
51 |
|
# |
52 |
|
# Revision 1.27 2003/01/31 06:30:59 joko |
53 |
|
# + enabled 'sendQuery' |
54 |
|
# |
55 |
# Revision 1.26 2003/01/30 22:29:47 joko |
# Revision 1.26 2003/01/30 22:29:47 joko |
56 |
# + fixed module usage (removed dependency on 'libp.pm') |
# + fixed module usage (removed dependency on 'libp.pm') |
57 |
# |
# |
87 |
# + fix: encapsulated object-loading inside an 'eval' |
# + fix: encapsulated object-loading inside an 'eval' |
88 |
# |
# |
89 |
# Revision 1.15 2002/12/05 13:55:21 joko |
# Revision 1.15 2002/12/05 13:55:21 joko |
90 |
# + now utilizing 'object2hash' instead of 'var_deref' |
# + now utilizing 'expand' instead of 'var_deref' |
91 |
# + played around with having fresh-objects - no progress.... |
# + played around with having fresh-objects - no progress.... |
92 |
# |
# |
93 |
# Revision 1.14 2002/12/05 09:40:30 jonen |
# Revision 1.14 2002/12/05 09:40:30 jonen |
160 |
use base ("Data::Storage::Handler"); |
use base ("Data::Storage::Handler"); |
161 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
162 |
|
|
163 |
use Tangram; |
|
164 |
use Data::Dumper; |
use Data::Dumper; |
165 |
|
use Tangram; |
166 |
|
|
167 |
use DesignPattern::Object; |
use DesignPattern::Object; |
168 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
169 |
use Data::Compare::Struct qw( isEmpty ); |
use Data::Mungle::Compare::Struct qw( isEmpty ); |
170 |
use Data::Transform::Deep qw( object2hash ); |
use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to ); |
|
use Data::Transform::Encode qw( var2utf8 ); |
|
171 |
|
|
172 |
# get logger instance |
# get logger instance |
173 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
267 |
sub getChildNodes { |
sub getChildNodes { |
268 |
|
|
269 |
my $self = shift; |
my $self = shift; |
270 |
my @nodes; |
my $mode = shift; |
271 |
|
my $filter = shift; |
272 |
|
|
273 |
|
$mode ||= 'core'; |
274 |
|
$filter ||= 'all'; |
275 |
|
|
276 |
|
$logger->debug( __PACKAGE__ . "->getChildNodes($mode)" ); |
277 |
|
|
278 |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
if ($mode eq 'core') { |
279 |
|
|
280 |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
my @nodes; |
281 |
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
|
282 |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
283 |
|
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
284 |
# todo: should we retrieve information from the schema here |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
285 |
# rather than poorly getting table names from underlying dbi? |
|
286 |
my $storage = $self->_getSubLayerHandle(); |
# todo: should we retrieve information from the schema here |
287 |
@nodes = @{$storage->getChildNodes()}; |
# rather than poorly getting table names from underlying dbi? |
288 |
#$storage->_configureCOREHANDLE(); |
my $storage = $self->_getSubLayerHandle(); |
289 |
#print "getchildnodes\n"; |
@nodes = @{$storage->getChildNodes()}; |
290 |
#print Dumper($self); |
#$storage->_configureCOREHANDLE(); |
291 |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
#print "getchildnodes\n"; |
292 |
|
#print Dumper($self); |
293 |
|
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
294 |
|
|
295 |
|
# TODO: REVIEW |
296 |
|
#$storage->disconnect(); |
297 |
|
|
298 |
|
$self->{meta}->{childnodes} = \@nodes; |
299 |
|
|
300 |
|
return \@nodes; |
301 |
|
|
302 |
# TODO: REVIEW |
} elsif ($mode eq 'root') { |
303 |
#$storage->disconnect(); |
|
304 |
|
# FIXME: this will return *all* known classes to 'Class::Tangram', |
305 |
|
# which might not be what you expect since more than one instance |
306 |
|
# of Tangram may be in memory and Class::Tangram seems to |
307 |
|
# offer no methods to determine this or filter its result(s) according |
308 |
|
# to a specific database. |
309 |
|
my @object_names = Class::Tangram::known_classes(); |
310 |
|
my @concret_names; |
311 |
|
my $o_cnt; |
312 |
|
foreach (sort @object_names) { |
313 |
|
push @concret_names, $_ if (!Class::Tangram::class_is_abstract($_)); |
314 |
|
$o_cnt++; |
315 |
|
} |
316 |
|
|
317 |
|
if ($filter eq 'all') { |
318 |
|
return \@object_names; |
319 |
|
} elsif ($filter eq 'concrete') { |
320 |
|
return \@concret_names; |
321 |
|
} |
322 |
|
|
323 |
$self->{meta}->{childnodes} = \@nodes; |
} |
324 |
|
|
|
return \@nodes; |
|
325 |
|
|
326 |
} |
} |
327 |
|
|
|
|
|
328 |
sub testIntegrity { |
sub testIntegrity { |
329 |
|
|
330 |
my $self = shift; |
my $self = shift; |
502 |
# redirect to unfiltered mode |
# redirect to unfiltered mode |
503 |
#return $self->getListUnfiltered(@_); |
#return $self->getListUnfiltered(@_); |
504 |
|
|
505 |
my $nodename = shift; |
my $in = {}; |
506 |
my $filters = shift; |
$in->{nodename} = shift; |
507 |
|
$in->{filters} = shift; |
508 |
|
$in->{sorting} = shift; |
509 |
|
|
510 |
my @results; |
my @results; |
511 |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" ); |
512 |
|
|
513 |
#print Dumper($filters); |
#print Dumper($filters); |
514 |
|
|
515 |
|
# 1. "Remote Object Handle" - get set of objects from odbms by object name |
516 |
|
my $remote = $self->{_COREHANDLE}->remote($in->{nodename}); |
517 |
|
|
518 |
|
# 2. Transfer $in to $orm_query |
519 |
|
my $orm_query = {}; |
520 |
|
|
521 |
|
# 2.a. Filters |
522 |
my @tfilters; |
my @tfilters; |
523 |
|
my $orm_filter = undef; |
524 |
|
|
525 |
foreach my $filter (@$filters) { |
foreach my $filter (@{$in->{filters}}) { |
526 |
|
|
527 |
# get filter - TODO: for each filter |
# get filter - TODO: for each filter |
528 |
#my $filter = $filters->[0]; |
#my $filter = $filters->[0]; |
529 |
|
|
530 |
# build filter |
# 1. The parts of a filter source entry |
531 |
my $lexpr = $filter->{key}; |
my $lexpr = $filter->{key}; |
532 |
#my $op = $filter->{op}; |
#my $op = $filter->{op}; |
533 |
my $op = '='; |
my $op = '='; |
534 |
my $rexpr = $filter->{val}; |
my $rexpr = $filter->{val}; |
535 |
my $tight = 100; |
my $tight = 100; |
536 |
|
|
537 |
|
# 2. Build filter target entry |
538 |
|
|
539 |
# my $tfilter = Tangram::Filter->new( |
# Test 1 - didn't work out! |
540 |
# expr => "t1.$lexpr $op '$rexpr'", |
# my $tfilter = Tangram::Filter->new( |
541 |
# tight => $tight, |
# expr => "t1.$lexpr $op '$rexpr'", |
542 |
# objects => $objects, |
# tight => $tight, |
543 |
# ); |
# objects => $objects, |
544 |
|
# ); |
545 |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
|
546 |
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
my $orm_filter_tmp = undef; |
547 |
|
# was: |
548 |
|
# TODO: is_op? |
549 |
|
# dispatch un-common operators if exists |
550 |
|
if ($filter->{op} eq "ref") { |
551 |
|
# do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query |
552 |
|
#print "Filter->op eq 'ref'.\n"; |
553 |
|
#push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
554 |
|
} else { |
555 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
556 |
|
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
557 |
|
# better: calculate expression right here! |
558 |
|
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
559 |
|
|
560 |
|
#print "key: ", $filter->{key}, "\n"; |
561 |
|
|
562 |
|
my $left = $remote->{$filter->{key}}; |
563 |
|
#my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" ); |
564 |
|
my $right = $filter->{val}; |
565 |
|
my $op = $filter->{op}; |
566 |
|
$orm_filter_tmp = $left->$op($right); |
567 |
|
} |
568 |
|
|
569 |
|
if (not $orm_filter) { |
570 |
|
$orm_filter = $orm_filter_tmp; |
571 |
|
} else { |
572 |
|
$orm_filter = $orm_filter->and($orm_filter_tmp); |
573 |
|
} |
574 |
|
|
575 |
} |
} |
576 |
|
|
577 |
my $tfilter = join(' & ', @tfilters); |
$orm_query->{filter} = $orm_filter; |
578 |
|
|
|
# get set of objects from odbms by object name |
|
|
my $remote = $self->{_COREHANDLE}->remote($nodename); |
|
|
|
|
579 |
# was: |
# was: |
580 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
|
581 |
|
# 2.b. sorting [new as of 2003-06-17] |
582 |
|
if ($in->{sorting}) { |
583 |
|
my $sorting_rules = []; |
584 |
|
my $sorting_order = 'ASC'; |
585 |
|
foreach my $sorting_column (keys %{$in->{sorting}}) { |
586 |
|
$sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column}; |
587 |
|
push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column ); |
588 |
|
} |
589 |
|
$orm_query->{order} = $sorting_rules; |
590 |
|
if ($sorting_order eq 'DESC') { |
591 |
|
$orm_query->{desc} = 1; |
592 |
|
} |
593 |
|
} |
594 |
|
|
595 |
|
|
596 |
|
# 3. build and issue query - return result as Tangram::Cursor |
597 |
|
|
598 |
|
# coerce hash into array (This is valid in Perl) |
599 |
|
my @orm_query_args = %$orm_query; |
600 |
|
# send query (arguments) to orm |
601 |
|
@results = $self->{_COREHANDLE}->select($remote, @orm_query_args); |
602 |
|
|
603 |
|
#my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args); |
604 |
|
#return $cursor; |
605 |
|
#print Dumper(@results); |
606 |
|
|
607 |
# is: |
# is: |
608 |
# 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 |
609 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
#my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');'; |
610 |
|
#print "eval: $evalstring", "\n"; |
611 |
# get filtered list/set |
# get filtered list/set |
612 |
@results = eval($evalstring); |
#@results = eval($evalstring); |
613 |
die $@ if $@; |
#die $@ if $@; |
614 |
|
|
615 |
|
=pod |
616 |
|
# filter results |
617 |
|
if ($filters->[0]->{op} && ($filters->[0]->{op} eq "ref")) { |
618 |
|
#print "Filter->op eq 'ref'.\n"; |
619 |
|
my $att_name = $filters->[0]->{key}; |
620 |
|
my $att_val = $filters->[0]->{val}; |
621 |
|
my @filtered; |
622 |
|
foreach(@results) { |
623 |
|
if(ref($_->{$att_name}) eq $att_val) { |
624 |
|
push @filtered, $_; |
625 |
|
} |
626 |
|
} |
627 |
|
@results = @filtered; |
628 |
|
} |
629 |
|
=cut |
630 |
|
|
631 |
|
#print "results: " . Dumper(\@results); |
632 |
|
|
633 |
return \@results; |
return \@results; |
634 |
} |
} |
658 |
return $result; |
return $result; |
659 |
} |
} |
660 |
|
|
661 |
|
sub createResult { |
662 |
|
my $self = shift; |
663 |
|
my $rh = shift; |
664 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
665 |
|
return $result; |
666 |
|
} |
667 |
|
|
668 |
sub sendQuery { |
sub sendQuery { |
669 |
my $self = shift; |
my $self = shift; |
670 |
my $query = shift; |
my $query = shift; |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
|
|
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
|
671 |
|
|
672 |
#print Dumper($query); |
#print Dumper($query); |
673 |
|
|
674 |
# HACK: special case: querying by id does not translate into a common tangram query |
# type = ITEM|LIST|TRANSPARENT |
675 |
# just load the object by given id(ent) |
my $type = ''; |
676 |
if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') { |
# mode = OID|SPECIAL |
677 |
#print "LOAD!!!", "\n"; |
my $mode = ''; |
678 |
#exit; |
my $ident = ''; |
679 |
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
my $crud = ''; |
680 |
my $ident = $query->{criterias}->[0]->{val}; |
|
681 |
#print "load obj", "\n"; |
|
682 |
#return $self->createSet() if $ident == 5; |
# dispatch type and mode |
683 |
$self->{_COREHANDLE}->unload($ident); |
|
684 |
my $object = $self->{_COREHANDLE}->load($ident); |
# defaults - 1 |
685 |
#print "get id", "\n"; |
if ($query->{options}) { |
686 |
my $oid = $self->{_COREHANDLE}->id($object); |
$crud = $query->{options}->{crud}; |
687 |
#print Dumper($object); |
$crud ||= $query->{options}->{action}; |
688 |
#print "oid: $oid", "\n"; |
} |
689 |
return $self->createSet($object); |
|
690 |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
# defaults - 2 |
691 |
} |
$type ||= 'TRANSPARENT'; |
692 |
|
$crud ||= 'RETRIEVE'; |
693 |
die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
|
694 |
|
if ($query->{options}->{OID}) { |
695 |
# TODO: do a common tangram query here |
$type = 'ITEM'; |
696 |
|
$mode = 'OID'; |
697 |
my @crits; |
$ident = $query->{options}->{OID}; |
698 |
foreach (@{$query->{criterias}}) { |
|
699 |
my $op = ''; |
} elsif (my $guid = $query->{options}->{GUID}) { |
700 |
$op = '=' if lc $_->{op} eq 'eq'; |
$type = 'TRANSPARENT'; |
701 |
push @crits, "$_->{key}$op'$_->{val}'"; |
$query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; |
702 |
} |
|
703 |
my $subnodes = {}; |
# if operator is different (dispatcher for 'getListFiltered') |
704 |
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
} elsif (my $op = $query->{options}->{op}) { |
705 |
# HACK: this is hardcoded ;( expand possibilities! |
$type = 'TRANSPARENT'; |
706 |
#my $crit = join(' AND ', @crits); |
$query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ]; |
707 |
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
|
708 |
#return $self->sendCommand($sql); |
} |
709 |
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
|
710 |
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
# HACK: special case: querying by id does not translate into a common tangram query |
711 |
return $self->createCursor($query->{node}); |
# just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync! |
712 |
|
if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) { |
713 |
|
$type = 'ITEM'; |
714 |
|
$mode = 'SPECIAL.SYNC'; |
715 |
|
$ident = $query->{criterias}->[0]->{val}; |
716 |
|
} |
717 |
|
|
718 |
|
|
719 |
|
# execute query |
720 |
|
my $result; |
721 |
|
|
722 |
|
if ($type eq 'ITEM' && $ident) { |
723 |
|
|
724 |
|
if ($mode eq 'OID') { |
725 |
|
# TODO: review this case! |
726 |
|
$result = $self->getObject($ident, $query->{options}); |
727 |
|
|
728 |
|
} elsif ($mode eq 'SPECIAL.SYNC') { |
729 |
|
|
730 |
|
# V1 - failed |
731 |
|
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
732 |
|
|
733 |
|
# hmm.... |
734 |
|
#return $self->createSet() if $ident == 5; |
735 |
|
|
736 |
|
# Unload single object before doing any further operations to |
737 |
|
# expect a "fresh" object from orm when performing the next calls. |
738 |
|
$self->{_COREHANDLE}->unload($ident); |
739 |
|
|
740 |
|
# Load object from orm. |
741 |
|
my $object = $self->{_COREHANDLE}->load($ident); |
742 |
|
|
743 |
|
# determine object identifier (OID) |
744 |
|
my $oid = $self->{_COREHANDLE}->id($object); |
745 |
|
|
746 |
|
# encapsulate into result/response container and return this one |
747 |
|
$result = $self->createSet($object); |
748 |
|
|
749 |
|
# debugging |
750 |
|
#$result = $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
751 |
|
|
752 |
|
} |
753 |
|
|
754 |
|
|
755 |
|
} elsif ($type eq 'TRANSPARENT') { |
756 |
|
|
757 |
|
if ($crud eq 'RETRIEVE') { |
758 |
|
|
759 |
|
my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting}); |
760 |
|
#return $list; |
761 |
|
return $self->createResult($list); |
762 |
|
|
763 |
|
#return $self->createSet($object); |
764 |
|
#return $self->createSet($list); |
765 |
|
return $self->createSet(@$list); |
766 |
|
|
767 |
|
#die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
768 |
|
|
769 |
|
# try a generic tangram query here |
770 |
|
# TODO: try to place an oql on top of that (search.cpan.org!) |
771 |
|
my @crits; |
772 |
|
foreach (@{$query->{criterias}}) { |
773 |
|
my $op = ''; |
774 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
775 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
776 |
|
} |
777 |
|
my $subnodes = {}; |
778 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
779 |
|
# HACK: this is hardcoded ;( expand possibilities! |
780 |
|
#my $crit = join(' AND ', @crits); |
781 |
|
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
782 |
|
#return $self->sendCommand($sql); |
783 |
|
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
784 |
|
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
785 |
|
$result = $self->createCursor($query->{node}); |
786 |
|
|
787 |
|
} elsif ($crud eq 'UPDATE') { |
788 |
|
|
789 |
|
# Patch current query to be a loader (e.g. change action, remove payload) ... |
790 |
|
my $childquery = deep_copy($query); |
791 |
|
$childquery->{options}->{crud} = 'RETRIEVE'; |
792 |
|
delete $childquery->{payload}; |
793 |
|
|
794 |
|
# ... to use it to fetch a fresh object using ourselves (sendQuery). |
795 |
|
my $cursor = $self->sendQuery($childquery); |
796 |
|
my $status = $cursor->getStatus(); |
797 |
|
my $object = $cursor->getNextEntry(); |
798 |
|
|
799 |
|
# Merge values and apply value modifiers. |
800 |
|
my $options = { utf8 => 1, php => 1 }; |
801 |
|
merge_to($object, $query->{payload}, $options); |
802 |
|
|
803 |
|
# Execute update operation at orm. |
804 |
|
$self->update($object); |
805 |
|
|
806 |
|
} elsif ($crud eq 'DELETE') { |
807 |
|
|
808 |
|
# Patch current query to be a loader (e.g. change action) ... |
809 |
|
my $childquery = deep_copy($query); |
810 |
|
$childquery->{options}->{crud} = 'RETRIEVE'; |
811 |
|
|
812 |
|
# ... to use it to fetch a fresh object using ourselves (sendQuery). |
813 |
|
my $cursor = $self->sendQuery($childquery); |
814 |
|
my $status = $cursor->getStatus(); |
815 |
|
my $object = $cursor->getNextEntry(); |
816 |
|
|
817 |
|
$self->erase($object); |
818 |
|
|
819 |
|
} elsif ($crud eq 'CREATE') { |
820 |
|
|
821 |
|
my $nodename = $query->{node}; |
822 |
|
my $newnode = $self->createNode($nodename); |
823 |
|
my $id = $self->{_COREHANDLE}->insert($newnode); |
824 |
|
|
825 |
|
print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n"; |
826 |
|
|
827 |
|
return $newnode; |
828 |
|
|
829 |
|
} |
830 |
|
|
831 |
|
} |
832 |
|
|
833 |
|
return $result; |
834 |
|
|
835 |
} |
} |
836 |
|
|
837 |
sub eraseAll { |
sub eraseAll { |
867 |
return $object if $object; |
return $object if $object; |
868 |
} |
} |
869 |
|
|
870 |
sub getObjectByGuid { |
sub getObjectByGuid_old { |
871 |
my $self = shift; |
my $self = shift; |
872 |
my $guid = shift; |
my $guid = shift; |
873 |
my $options = shift; |
my $options = shift; |
897 |
|
|
898 |
} |
} |
899 |
|
|
900 |
sub getObjectAsHash { |
sub getObjectAsHash_old { |
901 |
my $self = shift; |
my $self = shift; |
902 |
my $oid = shift; |
my $oid = shift; |
903 |
my $options = shift; |
my $options = shift; |
912 |
# build options (a callback to unload autovivified objects) for 'expand' |
# build options (a callback to unload autovivified objects) for 'expand' |
913 |
# TODO: use $logger to write to debug here! |
# TODO: use $logger to write to debug here! |
914 |
my $cb; # = sub {}; |
my $cb; # = sub {}; |
915 |
|
|
916 |
|
# deactivated way to get rid of used instances, if requested |
917 |
=pod |
=pod |
918 |
if ($options->{destroy}) { |
if ($options->{destroy}) { |
919 |
$options->{cb}->{destroy} = sub { |
$options->{cb}->{destroy} = sub { |
920 |
print "================ DESTROY", "\n"; |
print "================ DESTROY", "\n"; |
921 |
my $object = shift; |
my $object = shift; |
922 |
#print Dumper($object); |
#print Dumper($object); |
923 |
$self->{_COREHANDLE}->unload($object); |
$self->{_COREHANDLE}->unload($object); |
924 |
#undef($object); |
#undef($object); |
925 |
}; |
}; |
926 |
} |
} |
927 |
=cut |
=cut |
928 |
|
|
929 |
my $hash = object2hash($obj, $options); |
my $hash = expand($obj, $options); |
930 |
#$options->{cb}->{destroy}->($obj); |
|
931 |
#$self->{_COREHANDLE}->unload($obj); |
# old (unsuccessful) attempts to get rid of used instances, if requested |
932 |
|
|
933 |
# convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML |
# V1: |
934 |
# now done in object2hash |
#$options->{cb}->{destroy}->($obj); |
935 |
#var2utf8($hash) if ($options->{utf8}); |
#$self->{_COREHANDLE}->unload($obj); |
936 |
|
|
937 |
# old (wrong) attempts to get rid of used instances, if requested |
# V2: |
938 |
#$obj->clear_refs; |
#$obj->clear_refs; |
939 |
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
940 |
#$obj->DESTROY; |
#$obj->DESTROY; |
964 |
return $storage->testAvailability(); |
return $storage->testAvailability(); |
965 |
} |
} |
966 |
|
|
967 |
|
sub disconnect2 { |
968 |
|
my $self = shift; |
969 |
|
my $storage = $self->_getSubLayerHandle(); |
970 |
|
print "DISC!", "\n"; |
971 |
|
|
972 |
|
my $storage_ll = $storage->{_COREHANDLE}; |
973 |
|
$storage_ll->disconnect(); |
974 |
|
|
975 |
|
print Dumper($storage); |
976 |
|
exit; |
977 |
|
|
978 |
|
#$self->{_COREHANDLE} |
979 |
|
#$storage->disconnect(); |
980 |
|
$self->{dataStorageLayer}->disconnect(); |
981 |
|
} |
982 |
|
|
983 |
|
|
984 |
|
sub createNode { |
985 |
|
my $self = shift; |
986 |
|
my $classname = shift; |
987 |
|
|
988 |
|
my $obj = $classname->new(); |
989 |
|
|
990 |
|
my $attr_options = Class::Tangram::attribute_options($classname); |
991 |
|
#print "Attribute Options: " . Dumper($attr_options); |
992 |
|
|
993 |
|
my $attr_types = Class::Tangram::attribute_types($classname); |
994 |
|
#print "Attribute Types: " . Dumper($attr_types); |
995 |
|
|
996 |
|
foreach(keys %{$attr_types}) { |
997 |
|
if($attr_types->{$_} eq 'string') { |
998 |
|
$obj->{$_} = ''; |
999 |
|
} elsif($attr_types->{$_} eq 'int') { |
1000 |
|
$obj->{$_} = 0; |
1001 |
|
} elsif($attr_types->{$_} eq 'real') { |
1002 |
|
$obj->{$_} = 0; |
1003 |
|
} elsif($attr_types->{$_} eq 'rawdatetime') { |
1004 |
|
$obj->{$_} = '0000-00-00 00:00:00'; |
1005 |
|
} elsif($attr_types->{$_} eq 'ref') { |
1006 |
|
if($attr_options->{$_}->{class}) { |
1007 |
|
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
1008 |
|
} else { |
1009 |
|
#$obj->{$_} = undef(); |
1010 |
|
} |
1011 |
|
} elsif($attr_types->{$_} eq 'iarray') { |
1012 |
|
$obj->{$_} = [ ]; |
1013 |
|
} elsif($attr_types->{$_} eq 'hash') { |
1014 |
|
$obj->{$_} = { }; |
1015 |
|
} elsif($attr_types->{$_} eq 'flat_hash') { |
1016 |
|
$obj->{$_} = { }; |
1017 |
|
} |
1018 |
|
} |
1019 |
|
|
1020 |
|
#print "New Object: " . Dumper($obj); |
1021 |
|
|
1022 |
|
return $obj; |
1023 |
|
} |
1024 |
|
|
1025 |
|
|
1026 |
|
sub insertChildNode { |
1027 |
|
my $self = shift; |
1028 |
|
my $child_entry = shift; |
1029 |
|
my $query_args = shift; |
1030 |
|
|
1031 |
|
my $core = $self->{_COREHANDLE}; |
1032 |
|
my $nodename = $query_args->{nodename}; |
1033 |
|
|
1034 |
|
# get parent object |
1035 |
|
my $query = { |
1036 |
|
node => $query_args->{parent}->{nodename}, |
1037 |
|
options => { GUID => $query_args->{parent}->{guid}, }, |
1038 |
|
}; |
1039 |
|
my $cursor = $self->sendQuery($query); |
1040 |
|
my $parent = $cursor->getNextEntry(); |
1041 |
|
|
1042 |
|
# debug |
1043 |
|
#print "Parent_org: " . Dumper($parent); |
1044 |
|
|
1045 |
|
# Create child node object if isn't already done |
1046 |
|
# ($child_entry have to be the class name then...) |
1047 |
|
if(!ref($child_entry)) { |
1048 |
|
$child_entry = $self->createNode($child_entry); |
1049 |
|
# it could be insert 'manually' or will be insert 'transparent' if parent will be updated |
1050 |
|
#$core->insert($child_entry); |
1051 |
|
#print "Create child object [$nodename]: " . Dumper($child_entry); |
1052 |
|
} |
1053 |
|
|
1054 |
|
# get reference of tied node (seems, only on Linux node's are tied!!) |
1055 |
|
my $tied_node = tied $parent->{$nodename}; |
1056 |
|
|
1057 |
|
# insert/change child entry at parent |
1058 |
|
#print "reference: " . ref($parent->{$nodename}) . "\n"; |
1059 |
|
if(ref($parent->{$nodename}) eq 'ARRAY') { |
1060 |
|
# (seems, only on Linux node's are tied!!) |
1061 |
|
if($tied_node) { |
1062 |
|
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type, |
1063 |
|
# so a 'PUSH' is not implemented (which could be then done transparently) |
1064 |
|
my $array = $tied_node->FETCH; |
1065 |
|
push @$array, $child_entry; |
1066 |
|
$tied_node->STORE($array); |
1067 |
|
# node will be normaly untied at 'STORE' |
1068 |
|
if(tied $parent->{$nodename}) { print "already tied !!\n"; } |
1069 |
|
else { undef $tied_node; } |
1070 |
|
} else { |
1071 |
|
push @{$parent->{$nodename}}, $child_entry; |
1072 |
|
} |
1073 |
|
} |
1074 |
|
elsif(ref($parent->{$nodename}) eq 'HASH') { |
1075 |
|
if(my $key = $query_args->{hash_key}) { |
1076 |
|
# (seems, only on Linux node's are tied!!) |
1077 |
|
if($tied_node) { |
1078 |
|
# same problem as with 'ARRAY': |
1079 |
|
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type. |
1080 |
|
my $hash = $tied_node->FETCH; |
1081 |
|
$hash->{$key} = $child_entry; |
1082 |
|
$tied_node->STORE($hash); |
1083 |
|
# node will be normaly untied at 'STORE' |
1084 |
|
if(tied $parent->{$nodename}) { print "already tied !!\n"; } |
1085 |
|
else { undef $tied_node; } |
1086 |
|
} else { |
1087 |
|
$parent->{$nodename}->{$key} = $child_entry; |
1088 |
|
} |
1089 |
|
} else { |
1090 |
|
print "ERROR: No HASH KEY given, so not able to insert hash entry!"; |
1091 |
|
} |
1092 |
|
} |
1093 |
|
else { |
1094 |
|
$parent->{$nodename} = $child_entry; |
1095 |
|
} |
1096 |
|
|
1097 |
|
# debug |
1098 |
|
#print "Parent_new: " . Dumper($parent); |
1099 |
|
|
1100 |
|
# save parent |
1101 |
|
$core->update($parent); |
1102 |
|
|
1103 |
|
# debug |
1104 |
|
#print "Saved Parent: ". Dumper($parent); |
1105 |
|
|
1106 |
|
return $child_entry; |
1107 |
|
} |
1108 |
|
|
1109 |
|
|
1110 |
1; |
1; |
1111 |
__END__ |
__END__ |