--- nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2003/04/09 06:07:43 1.33 +++ nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2003/05/13 16:38:38 1.38 @@ -1,8 +1,29 @@ ############################################ # -# $Id: Tangram.pm,v 1.33 2003/04/09 06:07:43 joko Exp $ +# $Id: Tangram.pm,v 1.38 2003/05/13 16:38:38 joko Exp $ # # $Log: Tangram.pm,v $ +# Revision 1.38 2003/05/13 16:38:38 joko +# problems with "tied" on 5.6.1/win32 +# +# Revision 1.37 2003/05/10 17:37:39 jonen +# corrected last commit +# +# Revision 1.36 2003/05/10 17:31:18 jonen +# + added new functions related to 'create' item +# - createNode() +# # creates non-persistent 'deep dummy filled' object +# - insertChildNode() +# # inserts child node at given parent (child node haven't to exists, +# createNode will be injected transparently) +# +# Revision 1.35 2003/04/19 16:09:48 jonen +# + added operator dispatching (currently for getting ref-type) at 'getListFiltered' +# +# Revision 1.34 2003/04/11 01:18:53 joko +# sendQuery: +# + introduced crud action 'DELETE' +# # Revision 1.33 2003/04/09 06:07:43 joko # revamped 'sub sendQuery' # @@ -502,8 +523,14 @@ # objects => $objects, # ); - # HACK: build eval-string (sorry) to get filtered list - please give advice here - push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; + # TODO: is_op? + # dispatch un-common operators if exists + if($filter->{op} eq "ref") { + push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; + } else { + # HACK: build eval-string (sorry) to get filtered list - please give advice here + push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; + } } @@ -564,19 +591,20 @@ # mode = OID|SPECIAL my $mode = ''; my $ident = ''; - my $action = ''; + my $crud = ''; # dispatch type and mode # defaults - 1 if ($query->{options}) { - $action = $query->{options}->{action}; + $crud = $query->{options}->{crud}; + $crud ||= $query->{options}->{action}; } # defaults - 2 $type ||= 'TRANSPARENT'; - $action ||= 'load'; + $crud ||= 'RETRIEVE'; if ($query->{options}->{OID}) { $type = 'ITEM'; @@ -585,8 +613,13 @@ } elsif (my $guid = $query->{options}->{GUID}) { $type = 'TRANSPARENT'; - $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; - + $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; + + # if operator is different (dispatcher for 'getListFiltered') + } elsif (my $op = $query->{options}->{op}) { + $type = 'TRANSPARENT'; + $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ]; + } # HACK: special case: querying by id does not translate into a common tangram query @@ -604,6 +637,7 @@ if ($type eq 'ITEM' && $ident) { if ($mode eq 'OID') { + # TODO: review this case! $result = $self->getObject($ident, $query->{options}); } elsif ($mode eq 'SPECIAL.SYNC') { @@ -635,7 +669,7 @@ } elsif ($type eq 'TRANSPARENT') { - if ($action eq 'load') { + if ($crud eq 'RETRIEVE') { my $list = $self->getListFiltered($query->{node}, $query->{criterias}); #return $self->createSet($object); @@ -662,14 +696,14 @@ #my $res = $self->{COREHANDLE}->select($h, $h->{); $result = $self->createCursor($query->{node}); - } elsif ($action eq 'save') { + } elsif ($crud eq 'UPDATE') { # Patch current query to be a loader (e.g. change action, remove payload) ... my $childquery = deep_copy($query); - $childquery->{options}->{action} = 'load'; + $childquery->{options}->{crud} = 'RETRIEVE'; delete $childquery->{payload}; - # ... to use it to fetch fresh object using ourselves (sendQuery). + # ... to use it to fetch a fresh object using ourselves (sendQuery). my $cursor = $self->sendQuery($childquery); my $status = $cursor->getStatus(); my $object = $cursor->getNextEntry(); @@ -681,6 +715,29 @@ # Execute update operation at orm. $self->update($object); + } elsif ($crud eq 'DELETE') { + + # Patch current query to be a loader (e.g. change action) ... + my $childquery = deep_copy($query); + $childquery->{options}->{crud} = 'RETRIEVE'; + + # ... to use it to fetch a fresh object using ourselves (sendQuery). + my $cursor = $self->sendQuery($childquery); + my $status = $cursor->getStatus(); + my $object = $cursor->getNextEntry(); + + $self->erase($object); + + } elsif ($crud eq 'CREATE') { + + my $nodename = $query->{node}; + my $newnode = $self->createNode($nodename); + my $id = $self->{_COREHANDLE}->insert($newnode); + + print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n"; + + return $newnode; + } } @@ -835,5 +892,132 @@ $self->{dataStorageLayer}->disconnect(); } + +sub createNode { + my $self = shift; + my $classname = shift; + + my $obj = $classname->new(); + + my $attr_options = Class::Tangram::attribute_options($classname); + #print "Attribute Options: " . Dumper($attr_options); + + my $attr_types = Class::Tangram::attribute_types($classname); + #print "Attribute Types: " . Dumper($attr_types); + + foreach(keys %{$attr_types}) { + if($attr_types->{$_} eq 'string') { + $obj->{$_} = ''; + } elsif($attr_types->{$_} eq 'int') { + $obj->{$_} = 0; + } elsif($attr_types->{$_} eq 'real') { + $obj->{$_} = 0; + } elsif($attr_types->{$_} eq 'rawdatetime') { + $obj->{$_} = '0000-00-00 00:00:00'; + } elsif($attr_types->{$_} eq 'ref') { + if($attr_options->{$_}->{class}) { + $obj->{$_} = $self->createNode($attr_options->{$_}->{class}); + } else { + #$obj->{$_} = undef(); + } + } elsif($attr_types->{$_} eq 'iarray') { + $obj->{$_} = [ ]; + } elsif($attr_types->{$_} eq 'hash') { + $obj->{$_} = { }; + } elsif($attr_types->{$_} eq 'flat_hash') { + $obj->{$_} = { }; + } + } + + #print "New Object: " . Dumper($obj); + + return $obj; +} + + + sub insertChildNode { + my $self = shift; + my $child_entry = shift; + my $query_args = shift; + + my $core = $self->{_COREHANDLE}; + my $nodename = $query_args->{nodename}; + + # get parent object + my $query = { + node => $query_args->{parent}->{nodename}, + options => { GUID => $query_args->{parent}->{guid}, }, + }; + my $cursor = $self->sendQuery($query); + my $parent = $cursor->getNextEntry(); + + # debug + #print "Parent_org: " . Dumper($parent); + + # Create child node object if isn't already done + # ($child_entry have to be the class name then...) + if(!ref($child_entry)) { + $child_entry = $self->createNode($child_entry); + # it could be insert 'manually' or will be insert 'transparent' if parent will be updated + #$core->insert($child_entry); + #print "Create child object [$nodename]: " . Dumper($child_entry); + } + + # get reference of tied node (seems, only on Linux node's are tied!!) + my $tied_node = tied $parent->{$nodename}; + + # insert/change child entry at parent + #print "reference: " . ref($parent->{$nodename}) . "\n"; + if(ref($parent->{$nodename}) eq 'ARRAY') { + # (seems, only on Linux node's are tied!!) + if($tied_node) { + # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type, + # so a 'PUSH' is not implemented (which could be then done transparently) + my $array = $tied_node->FETCH; + push @$array, $child_entry; + $tied_node->STORE($array); + # node will be normaly untied at 'STORE' + if(tied $parent->{$nodename}) { print "already tied !!\n"; } + else { undef $tied_node; } + } else { + push @{$parent->{$nodename}}, $child_entry; + } + } + elsif(ref($parent->{$nodename}) eq 'HASH') { + if(my $key = $query_args->{hash_key}) { + # (seems, only on Linux node's are tied!!) + if($tied_node) { + # same problem as with 'ARRAY': + # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type. + my $hash = $tied_node->FETCH; + $hash->{$key} = $child_entry; + $tied_node->STORE($hash); + # node will be normaly untied at 'STORE' + if(tied $parent->{$nodename}) { print "already tied !!\n"; } + else { undef $tied_node; } + } else { + $parent->{$nodename}->{$key} = $child_entry; + } + } else { + print "ERROR: No HASH KEY given, so not able to insert hash entry!"; + } + } + else { + $parent->{$nodename} = $child_entry; + } + + # debug + #print "Parent_new: " . Dumper($parent); + + # save parent + $core->update($parent); + + # debug + #print "Saved Parent: ". Dumper($parent); + + return $child_entry; + } + + 1; __END__