| 3 |
# $Id$ |
# $Id$ |
| 4 |
# |
# |
| 5 |
# $Log$ |
# $Log$ |
| 6 |
|
# Revision 1.44 2003/12/04 01:01:50 joko |
| 7 |
|
# + sendQuery now returns result even on crud=UPDATE |
| 8 |
|
# |
| 9 |
|
# Revision 1.43 2003/07/02 11:07:12 jonen |
| 10 |
|
# re-activate filtering of results *after* results are fetched from tangram |
| 11 |
|
# (needed for e.g. UserManagment) |
| 12 |
|
# |
| 13 |
|
# Revision 1.42 2003/07/01 23:24:17 joko |
| 14 |
|
# now using package before using function |
| 15 |
|
# |
| 16 |
|
# Revision 1.41 2003/06/29 02:03:45 joko |
| 17 |
|
# fix:? initialize schema on startup |
| 18 |
|
# |
| 19 |
|
# Revision 1.40 2003/06/25 22:57:54 joko |
| 20 |
|
# major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting" |
| 21 |
|
# |
| 22 |
|
# Revision 1.39 2003/06/06 11:40:40 jonen |
| 23 |
|
# fixed bug at 'getFilteredList' |
| 24 |
|
# |
| 25 |
|
# Revision 1.38 2003/05/13 16:38:38 joko |
| 26 |
|
# problems with "tied" on 5.6.1/win32 |
| 27 |
|
# |
| 28 |
|
# Revision 1.37 2003/05/10 17:37:39 jonen |
| 29 |
|
# corrected last commit |
| 30 |
|
# |
| 31 |
|
# Revision 1.36 2003/05/10 17:31:18 jonen |
| 32 |
|
# + added new functions related to 'create' item |
| 33 |
|
# - createNode() |
| 34 |
|
# # creates non-persistent 'deep dummy filled' object |
| 35 |
|
# - insertChildNode() |
| 36 |
|
# # inserts child node at given parent (child node haven't to exists, |
| 37 |
|
# createNode will be injected transparently) |
| 38 |
|
# |
| 39 |
# Revision 1.35 2003/04/19 16:09:48 jonen |
# Revision 1.35 2003/04/19 16:09:48 jonen |
| 40 |
# + added operator dispatching (currently for getting ref-type) at 'getListFiltered' |
# + added operator dispatching (currently for getting ref-type) at 'getListFiltered' |
| 41 |
# |
# |
| 176 |
|
|
| 177 |
use Data::Dumper; |
use Data::Dumper; |
| 178 |
use Tangram; |
use Tangram; |
| 179 |
|
use Class::Tangram; |
| 180 |
|
|
| 181 |
use DesignPattern::Object; |
use DesignPattern::Object; |
| 182 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
| 186 |
# get logger instance |
# get logger instance |
| 187 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
| 188 |
|
|
| 189 |
|
#$Tangram::TRACE = *STDOUT; |
| 190 |
|
|
| 191 |
# this holds the complete instantiated schema from tangram |
# this holds the complete instantiated schema from tangram |
| 192 |
my $schema_tangram; |
my $schema_tangram; |
| 205 |
#if (!$schema_tangram) { |
#if (!$schema_tangram) { |
| 206 |
#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} } ); |
| 207 |
my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); |
my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); |
| 208 |
$schema_tangram = $obj->getSchema(); |
$schema_tangram = $obj->getSchema() if $obj; |
| 209 |
#} |
#} |
| 210 |
if (!$schema_tangram) { |
if (!$schema_tangram) { |
| 211 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." ); |
| 212 |
return 0; |
return 0; |
| 213 |
} |
} |
| 214 |
#$self->_patchSchema(); |
#$self->_patchSchema(); |
| 254 |
# return; |
# return; |
| 255 |
# } |
# } |
| 256 |
|
|
| 257 |
#return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
| 258 |
$self->_initSchema(); |
#$self->_initSchema(); |
| 259 |
|
|
| 260 |
# create the main tangram storage object |
# create the main tangram storage object |
| 261 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
| 517 |
# redirect to unfiltered mode |
# redirect to unfiltered mode |
| 518 |
#return $self->getListUnfiltered(@_); |
#return $self->getListUnfiltered(@_); |
| 519 |
|
|
| 520 |
my $nodename = shift; |
my $in = {}; |
| 521 |
my $filters = shift; |
$in->{nodename} = shift; |
| 522 |
|
$in->{filters} = shift; |
| 523 |
|
$in->{sorting} = shift; |
| 524 |
|
|
| 525 |
my @results; |
my @results; |
| 526 |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" ); |
| 527 |
|
|
| 528 |
#print Dumper($filters); |
#print Dumper($filters); |
| 529 |
|
|
| 530 |
|
# 1. "Remote Object Handle" - get set of objects from odbms by object name |
| 531 |
|
my $remote = $self->{_COREHANDLE}->remote($in->{nodename}); |
| 532 |
|
|
| 533 |
|
# 2. Transfer $in to $orm_query |
| 534 |
|
my $orm_query = {}; |
| 535 |
|
|
| 536 |
|
# 2.a. Filters |
| 537 |
my @tfilters; |
my @tfilters; |
| 538 |
|
my $orm_filter = undef; |
| 539 |
|
|
| 540 |
foreach my $filter (@$filters) { |
foreach my $filter (@{$in->{filters}}) { |
| 541 |
|
|
| 542 |
# get filter - TODO: for each filter |
# get filter - TODO: for each filter |
| 543 |
#my $filter = $filters->[0]; |
#my $filter = $filters->[0]; |
| 544 |
|
|
| 545 |
# build filter |
# 1. The parts of a filter source entry |
| 546 |
my $lexpr = $filter->{key}; |
my $lexpr = $filter->{key}; |
| 547 |
#my $op = $filter->{op}; |
#my $op = $filter->{op}; |
| 548 |
my $op = '='; |
my $op = '='; |
| 549 |
my $rexpr = $filter->{val}; |
my $rexpr = $filter->{val}; |
| 550 |
my $tight = 100; |
my $tight = 100; |
| 551 |
|
|
| 552 |
|
# 2. Build filter target entry |
| 553 |
|
|
| 554 |
# my $tfilter = Tangram::Filter->new( |
# Test 1 - didn't work out! |
| 555 |
# expr => "t1.$lexpr $op '$rexpr'", |
# my $tfilter = Tangram::Filter->new( |
| 556 |
# tight => $tight, |
# expr => "t1.$lexpr $op '$rexpr'", |
| 557 |
# objects => $objects, |
# tight => $tight, |
| 558 |
# ); |
# objects => $objects, |
| 559 |
|
# ); |
| 560 |
|
|
| 561 |
|
my $orm_filter_tmp = undef; |
| 562 |
|
# was: |
| 563 |
# TODO: is_op? |
# TODO: is_op? |
| 564 |
# dispatch un-common operators if exists |
# dispatch un-common operators if exists |
| 565 |
if($filter->{op} eq "ref") { |
if ($filter->{op} eq "ref") { |
| 566 |
push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
# do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query |
| 567 |
|
#print "Filter->op eq 'ref'.\n"; |
| 568 |
|
#push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
| 569 |
} else { |
} else { |
| 570 |
# 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 |
| 571 |
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
| 572 |
|
# better: calculate expression right here! |
| 573 |
|
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
| 574 |
|
|
| 575 |
|
#print "key: ", $filter->{key}, "\n"; |
| 576 |
|
|
| 577 |
|
my $left = $remote->{$filter->{key}}; |
| 578 |
|
#my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" ); |
| 579 |
|
my $right = $filter->{val}; |
| 580 |
|
my $op = $filter->{op}; |
| 581 |
|
$orm_filter_tmp = $left->$op($right); |
| 582 |
|
} |
| 583 |
|
|
| 584 |
|
if (not $orm_filter) { |
| 585 |
|
$orm_filter = $orm_filter_tmp; |
| 586 |
|
} else { |
| 587 |
|
$orm_filter = $orm_filter->and($orm_filter_tmp); |
| 588 |
} |
} |
| 589 |
|
|
| 590 |
} |
} |
| 591 |
|
|
| 592 |
my $tfilter = join(' & ', @tfilters); |
$orm_query->{filter} = $orm_filter; |
| 593 |
|
|
|
# get set of objects from odbms by object name |
|
|
my $remote = $self->{_COREHANDLE}->remote($nodename); |
|
|
|
|
| 594 |
# was: |
# was: |
| 595 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
|
| 596 |
|
# 2.b. sorting [new as of 2003-06-17] |
| 597 |
|
if ($in->{sorting}) { |
| 598 |
|
my $sorting_rules = []; |
| 599 |
|
my $sorting_order = 'ASC'; |
| 600 |
|
foreach my $sorting_column (keys %{$in->{sorting}}) { |
| 601 |
|
$sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column}; |
| 602 |
|
push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column ); |
| 603 |
|
} |
| 604 |
|
$orm_query->{order} = $sorting_rules; |
| 605 |
|
if ($sorting_order eq 'DESC') { |
| 606 |
|
$orm_query->{desc} = 1; |
| 607 |
|
} |
| 608 |
|
} |
| 609 |
|
|
| 610 |
|
|
| 611 |
|
# 3. build and issue query - return result as Tangram::Cursor |
| 612 |
|
|
| 613 |
|
# coerce hash into array (This is valid in Perl) |
| 614 |
|
my @orm_query_args = %$orm_query; |
| 615 |
|
# send query (arguments) to orm |
| 616 |
|
@results = $self->{_COREHANDLE}->select($remote, @orm_query_args); |
| 617 |
|
|
| 618 |
|
#my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args); |
| 619 |
|
#return $cursor; |
| 620 |
|
#print Dumper(@results); |
| 621 |
|
|
| 622 |
# is: |
# is: |
| 623 |
# 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 |
| 624 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
#my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');'; |
| 625 |
|
#print "eval: $evalstring", "\n"; |
| 626 |
#print "eval: $evalstring", "\n"; |
# get filtered list/set |
| 627 |
|
#@results = eval($evalstring); |
| 628 |
# get filtered list/set |
#die $@ if $@; |
| 629 |
@results = eval($evalstring); |
|
| 630 |
die $@ if $@; |
# filter results - NEEDED for e.g. UserManagment !! |
| 631 |
|
if ($in->{filters}->[0]->{op} && ($in->{filters}->[0]->{op} eq "ref")) { |
| 632 |
|
#print "Filter->op eq 'ref'.\n"; |
| 633 |
|
my $att_name = $in->{filters}->[0]->{key}; |
| 634 |
|
my $att_val = $in->{filters}->[0]->{val}; |
| 635 |
|
my @filtered; |
| 636 |
|
foreach(@results) { |
| 637 |
|
if(ref($_->{$att_name}) eq $att_val) { |
| 638 |
|
push @filtered, $_; |
| 639 |
|
} |
| 640 |
|
} |
| 641 |
|
@results = @filtered; |
| 642 |
|
} |
| 643 |
|
|
| 644 |
|
#print "results: " . Dumper(\@results); |
| 645 |
|
|
| 646 |
return \@results; |
return \@results; |
| 647 |
} |
} |
| 671 |
return $result; |
return $result; |
| 672 |
} |
} |
| 673 |
|
|
| 674 |
|
sub createResult { |
| 675 |
|
my $self = shift; |
| 676 |
|
my $rh = shift; |
| 677 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
| 678 |
|
return $result; |
| 679 |
|
} |
| 680 |
|
|
| 681 |
sub sendQuery { |
sub sendQuery { |
| 682 |
my $self = shift; |
my $self = shift; |
| 683 |
my $query = shift; |
my $query = shift; |
| 769 |
|
|
| 770 |
if ($crud eq 'RETRIEVE') { |
if ($crud eq 'RETRIEVE') { |
| 771 |
|
|
| 772 |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting}); |
| 773 |
|
#return $list; |
| 774 |
|
return $self->createResult($list); |
| 775 |
|
|
| 776 |
#return $self->createSet($object); |
#return $self->createSet($object); |
| 777 |
#return $self->createSet($list); |
#return $self->createSet($list); |
| 778 |
return $self->createSet(@$list); |
return $self->createSet(@$list); |
| 813 |
my $options = { utf8 => 1, php => 1 }; |
my $options = { utf8 => 1, php => 1 }; |
| 814 |
merge_to($object, $query->{payload}, $options); |
merge_to($object, $query->{payload}, $options); |
| 815 |
|
|
| 816 |
|
#print Dumper($object); |
| 817 |
|
|
| 818 |
# Execute update operation at orm. |
# Execute update operation at orm. |
| 819 |
$self->update($object); |
$self->update($object); |
| 820 |
|
$result = $self->createResult([ $object ]); |
| 821 |
|
|
| 822 |
} elsif ($crud eq 'DELETE') { |
} elsif ($crud eq 'DELETE') { |
| 823 |
|
|
| 832 |
|
|
| 833 |
$self->erase($object); |
$self->erase($object); |
| 834 |
|
|
| 835 |
|
} elsif ($crud eq 'CREATE') { |
| 836 |
|
|
| 837 |
|
my $nodename = $query->{node}; |
| 838 |
|
my $newnode = $self->createNode($nodename); |
| 839 |
|
my $id = $self->{_COREHANDLE}->insert($newnode); |
| 840 |
|
|
| 841 |
|
print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n"; |
| 842 |
|
|
| 843 |
|
return $newnode; |
| 844 |
|
|
| 845 |
} |
} |
| 846 |
|
|
| 847 |
} |
} |
| 996 |
$self->{dataStorageLayer}->disconnect(); |
$self->{dataStorageLayer}->disconnect(); |
| 997 |
} |
} |
| 998 |
|
|
| 999 |
|
|
| 1000 |
|
sub createNode { |
| 1001 |
|
my $self = shift; |
| 1002 |
|
my $classname = shift; |
| 1003 |
|
|
| 1004 |
|
my $obj = $classname->new(); |
| 1005 |
|
|
| 1006 |
|
my $attr_options = Class::Tangram::attribute_options($classname); |
| 1007 |
|
#print "Attribute Options: " . Dumper($attr_options); |
| 1008 |
|
|
| 1009 |
|
my $attr_types = Class::Tangram::attribute_types($classname); |
| 1010 |
|
#print "Attribute Types: " . Dumper($attr_types); |
| 1011 |
|
|
| 1012 |
|
foreach(keys %{$attr_types}) { |
| 1013 |
|
if($attr_types->{$_} eq 'string') { |
| 1014 |
|
$obj->{$_} = ''; |
| 1015 |
|
} elsif($attr_types->{$_} eq 'int') { |
| 1016 |
|
$obj->{$_} = 0; |
| 1017 |
|
} elsif($attr_types->{$_} eq 'real') { |
| 1018 |
|
$obj->{$_} = 0; |
| 1019 |
|
} elsif($attr_types->{$_} eq 'rawdatetime') { |
| 1020 |
|
$obj->{$_} = '0000-00-00 00:00:00'; |
| 1021 |
|
} elsif($attr_types->{$_} eq 'ref') { |
| 1022 |
|
if($attr_options->{$_}->{class}) { |
| 1023 |
|
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
| 1024 |
|
} else { |
| 1025 |
|
#$obj->{$_} = undef(); |
| 1026 |
|
} |
| 1027 |
|
} elsif($attr_types->{$_} eq 'iarray') { |
| 1028 |
|
$obj->{$_} = [ ]; |
| 1029 |
|
} elsif($attr_types->{$_} eq 'hash') { |
| 1030 |
|
$obj->{$_} = { }; |
| 1031 |
|
} elsif($attr_types->{$_} eq 'flat_hash') { |
| 1032 |
|
$obj->{$_} = { }; |
| 1033 |
|
} |
| 1034 |
|
} |
| 1035 |
|
|
| 1036 |
|
#print "New Object: " . Dumper($obj); |
| 1037 |
|
|
| 1038 |
|
return $obj; |
| 1039 |
|
} |
| 1040 |
|
|
| 1041 |
|
|
| 1042 |
|
sub insertChildNode { |
| 1043 |
|
my $self = shift; |
| 1044 |
|
my $child_entry = shift; |
| 1045 |
|
my $query_args = shift; |
| 1046 |
|
|
| 1047 |
|
my $core = $self->{_COREHANDLE}; |
| 1048 |
|
my $nodename = $query_args->{nodename}; |
| 1049 |
|
|
| 1050 |
|
# get parent object |
| 1051 |
|
my $query = { |
| 1052 |
|
node => $query_args->{parent}->{nodename}, |
| 1053 |
|
options => { GUID => $query_args->{parent}->{guid}, }, |
| 1054 |
|
}; |
| 1055 |
|
my $cursor = $self->sendQuery($query); |
| 1056 |
|
my $parent = $cursor->getNextEntry(); |
| 1057 |
|
|
| 1058 |
|
# debug |
| 1059 |
|
#print "Parent_org: " . Dumper($parent); |
| 1060 |
|
|
| 1061 |
|
# Create child node object if isn't already done |
| 1062 |
|
# ($child_entry have to be the class name then...) |
| 1063 |
|
if(!ref($child_entry)) { |
| 1064 |
|
$child_entry = $self->createNode($child_entry); |
| 1065 |
|
# it could be insert 'manually' or will be insert 'transparent' if parent will be updated |
| 1066 |
|
#$core->insert($child_entry); |
| 1067 |
|
#print "Create child object [$nodename]: " . Dumper($child_entry); |
| 1068 |
|
} |
| 1069 |
|
|
| 1070 |
|
# get reference of tied node (seems, only on Linux node's are tied!!) |
| 1071 |
|
my $tied_node = tied $parent->{$nodename}; |
| 1072 |
|
|
| 1073 |
|
# insert/change child entry at parent |
| 1074 |
|
#print "reference: " . ref($parent->{$nodename}) . "\n"; |
| 1075 |
|
if(ref($parent->{$nodename}) eq 'ARRAY') { |
| 1076 |
|
# (seems, only on Linux node's are tied!!) |
| 1077 |
|
if($tied_node) { |
| 1078 |
|
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type, |
| 1079 |
|
# so a 'PUSH' is not implemented (which could be then done transparently) |
| 1080 |
|
my $array = $tied_node->FETCH; |
| 1081 |
|
push @$array, $child_entry; |
| 1082 |
|
$tied_node->STORE($array); |
| 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 |
|
push @{$parent->{$nodename}}, $child_entry; |
| 1088 |
|
} |
| 1089 |
|
} |
| 1090 |
|
elsif(ref($parent->{$nodename}) eq 'HASH') { |
| 1091 |
|
if(my $key = $query_args->{hash_key}) { |
| 1092 |
|
# (seems, only on Linux node's are tied!!) |
| 1093 |
|
if($tied_node) { |
| 1094 |
|
# same problem as with 'ARRAY': |
| 1095 |
|
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type. |
| 1096 |
|
my $hash = $tied_node->FETCH; |
| 1097 |
|
$hash->{$key} = $child_entry; |
| 1098 |
|
$tied_node->STORE($hash); |
| 1099 |
|
# node will be normaly untied at 'STORE' |
| 1100 |
|
if(tied $parent->{$nodename}) { print "already tied !!\n"; } |
| 1101 |
|
else { undef $tied_node; } |
| 1102 |
|
} else { |
| 1103 |
|
$parent->{$nodename}->{$key} = $child_entry; |
| 1104 |
|
} |
| 1105 |
|
} else { |
| 1106 |
|
print "ERROR: No HASH KEY given, so not able to insert hash entry!"; |
| 1107 |
|
} |
| 1108 |
|
} |
| 1109 |
|
else { |
| 1110 |
|
$parent->{$nodename} = $child_entry; |
| 1111 |
|
} |
| 1112 |
|
|
| 1113 |
|
# debug |
| 1114 |
|
#print "Parent_new: " . Dumper($parent); |
| 1115 |
|
|
| 1116 |
|
# save parent |
| 1117 |
|
$core->update($parent); |
| 1118 |
|
|
| 1119 |
|
# debug |
| 1120 |
|
#print "Saved Parent: ". Dumper($parent); |
| 1121 |
|
|
| 1122 |
|
return $child_entry; |
| 1123 |
|
} |
| 1124 |
|
|
| 1125 |
|
|
| 1126 |
1; |
1; |
| 1127 |
__END__ |
__END__ |