/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.33 by joko, Wed Apr 9 06:07:43 2003 UTC revision 1.38 by joko, Tue May 13 16:38:38 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.38  2003/05/13 16:38:38  joko
7    #  problems with "tied" on 5.6.1/win32
8    #
9    #  Revision 1.37  2003/05/10 17:37:39  jonen
10    #  corrected last commit
11    #
12    #  Revision 1.36  2003/05/10 17:31:18  jonen
13    #  + added new functions related to 'create' item
14    #   - createNode()
15    #     # creates non-persistent 'deep dummy filled' object
16    #   - insertChildNode()
17    #     # inserts child node at given parent (child node haven't to exists,
18    #         createNode will be injected transparently)
19    #
20    #  Revision 1.35  2003/04/19 16:09:48  jonen
21    #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
22    #
23    #  Revision 1.34  2003/04/11 01:18:53  joko
24    #  sendQuery:
25    #  + introduced crud action 'DELETE'
26    #
27  #  Revision 1.33  2003/04/09 06:07:43  joko  #  Revision 1.33  2003/04/09 06:07:43  joko
28  #  revamped 'sub sendQuery'  #  revamped 'sub sendQuery'
29  #  #
# Line 502  sub getListFiltered { Line 523  sub getListFiltered {
523    #    objects => $objects,    #    objects => $objects,
524    #  );    #  );
525        
526      # HACK: build eval-string (sorry) to get filtered list - please give advice here      # TODO: is_op?
527      push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";      # dispatch un-common operators if exists
528        if($filter->{op} eq "ref") {
529          push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";
530        } else {
531          # HACK: build eval-string (sorry) to get filtered list - please give advice here
532          push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
533        }
534    
535    }    }
536    
# Line 564  sub sendQuery { Line 591  sub sendQuery {
591    # mode = OID|SPECIAL    # mode = OID|SPECIAL
592    my $mode = '';    my $mode = '';
593    my $ident = '';    my $ident = '';
594    my $action = '';    my $crud = '';
595        
596        
597    # dispatch type and mode    # dispatch type and mode
598        
599      # defaults - 1      # defaults - 1
600      if ($query->{options}) {      if ($query->{options}) {
601        $action = $query->{options}->{action};        $crud = $query->{options}->{crud};
602          $crud ||= $query->{options}->{action};
603      }      }
604    
605      # defaults - 2      # defaults - 2
606      $type ||= 'TRANSPARENT';      $type ||= 'TRANSPARENT';
607      $action ||= 'load';      $crud ||= 'RETRIEVE';
608    
609      if ($query->{options}->{OID}) {      if ($query->{options}->{OID}) {
610        $type = 'ITEM';        $type = 'ITEM';
# Line 585  sub sendQuery { Line 613  sub sendQuery {
613            
614      } elsif (my $guid = $query->{options}->{GUID}) {      } elsif (my $guid = $query->{options}->{GUID}) {
615        $type = 'TRANSPARENT';        $type = 'TRANSPARENT';
616        $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];        $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
617        
618        # if operator is different (dispatcher for 'getListFiltered')
619        } elsif (my $op = $query->{options}->{op}) {
620          $type = 'TRANSPARENT';
621          $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
622    
623      }      }
624        
625      # HACK: special case: querying by id does not translate into a common tangram query      # HACK: special case: querying by id does not translate into a common tangram query
# Line 604  sub sendQuery { Line 637  sub sendQuery {
637    if ($type eq 'ITEM' && $ident) {    if ($type eq 'ITEM' && $ident) {
638    
639      if ($mode eq 'OID') {          if ($mode eq 'OID') {    
640          # TODO: review this case!
641        $result = $self->getObject($ident, $query->{options});        $result = $self->getObject($ident, $query->{options});
642            
643      } elsif ($mode eq 'SPECIAL.SYNC') {      } elsif ($mode eq 'SPECIAL.SYNC') {
# Line 635  sub sendQuery { Line 669  sub sendQuery {
669        
670    } elsif ($type eq 'TRANSPARENT') {    } elsif ($type eq 'TRANSPARENT') {
671    
672      if ($action eq 'load') {      if ($crud eq 'RETRIEVE') {
673    
674        my $list = $self->getListFiltered($query->{node}, $query->{criterias});        my $list = $self->getListFiltered($query->{node}, $query->{criterias});
675        #return $self->createSet($object);        #return $self->createSet($object);
# Line 662  sub sendQuery { Line 696  sub sendQuery {
696        #my $res = $self->{COREHANDLE}->select($h, $h->{);        #my $res = $self->{COREHANDLE}->select($h, $h->{);
697        $result = $self->createCursor($query->{node});        $result = $self->createCursor($query->{node});
698    
699      } elsif ($action eq 'save') {      } elsif ($crud eq 'UPDATE') {
700    
701        # Patch current query to be a loader (e.g. change action, remove payload) ...        # Patch current query to be a loader (e.g. change action, remove payload) ...
702        my $childquery = deep_copy($query);        my $childquery = deep_copy($query);
703        $childquery->{options}->{action} = 'load';        $childquery->{options}->{crud} = 'RETRIEVE';
704        delete $childquery->{payload};        delete $childquery->{payload};
705    
706        # ... to use it to fetch fresh object using ourselves (sendQuery).        # ... to use it to fetch a fresh object using ourselves (sendQuery).
707        my $cursor = $self->sendQuery($childquery);        my $cursor = $self->sendQuery($childquery);
708        my $status = $cursor->getStatus();        my $status = $cursor->getStatus();
709        my $object = $cursor->getNextEntry();        my $object = $cursor->getNextEntry();
# Line 681  sub sendQuery { Line 715  sub sendQuery {
715        # Execute update operation at orm.        # Execute update operation at orm.
716        $self->update($object);        $self->update($object);
717            
718        } elsif ($crud eq 'DELETE') {
719    
720          # Patch current query to be a loader (e.g. change action) ...
721          my $childquery = deep_copy($query);
722          $childquery->{options}->{crud} = 'RETRIEVE';
723    
724          # ... to use it to fetch a fresh object using ourselves (sendQuery).
725          my $cursor = $self->sendQuery($childquery);
726          my $status = $cursor->getStatus();
727          my $object = $cursor->getNextEntry();
728    
729          $self->erase($object);
730    
731        } elsif ($crud eq 'CREATE') {
732          
733          my $nodename = $query->{node};      
734          my $newnode = $self->createNode($nodename);
735          my $id = $self->{_COREHANDLE}->insert($newnode);
736      
737          print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n";
738          
739          return $newnode;
740    
741      }      }
742    
743    }    }
# Line 835  sub disconnect2 { Line 892  sub disconnect2 {
892    $self->{dataStorageLayer}->disconnect();    $self->{dataStorageLayer}->disconnect();
893  }  }
894    
895    
896    sub createNode {
897      my $self = shift;
898      my $classname = shift;
899    
900      my $obj = $classname->new();
901      
902      my $attr_options = Class::Tangram::attribute_options($classname);
903      #print "Attribute Options: " . Dumper($attr_options);
904    
905      my $attr_types = Class::Tangram::attribute_types($classname);
906      #print "Attribute Types: " . Dumper($attr_types);
907      
908      foreach(keys %{$attr_types}) {
909        if($attr_types->{$_} eq 'string') {
910          $obj->{$_} = '';
911        } elsif($attr_types->{$_} eq 'int') {
912          $obj->{$_} = 0;
913        } elsif($attr_types->{$_} eq 'real') {
914          $obj->{$_} = 0;
915        } elsif($attr_types->{$_} eq 'rawdatetime') {
916          $obj->{$_} = '0000-00-00 00:00:00';
917        } elsif($attr_types->{$_} eq 'ref') {
918          if($attr_options->{$_}->{class}) {
919            $obj->{$_} = $self->createNode($attr_options->{$_}->{class});
920          } else {
921            #$obj->{$_} = undef();
922          }
923        } elsif($attr_types->{$_} eq 'iarray') {
924            $obj->{$_} = [ ];
925        } elsif($attr_types->{$_} eq 'hash') {
926            $obj->{$_} = {  };
927        } elsif($attr_types->{$_} eq 'flat_hash') {
928            $obj->{$_} = { };
929        }
930      }
931      
932      #print "New Object: " . Dumper($obj);
933      
934      return $obj;
935    }
936    
937    
938      sub insertChildNode {
939        my $self = shift;
940        my $child_entry = shift;
941        my $query_args = shift;
942        
943        my $core = $self->{_COREHANDLE};
944        my $nodename = $query_args->{nodename};
945        
946        # get parent object
947        my $query = {
948              node => $query_args->{parent}->{nodename},
949              options => { GUID => $query_args->{parent}->{guid}, },
950              };
951        my $cursor = $self->sendQuery($query);
952        my $parent = $cursor->getNextEntry();
953    
954        # debug
955        #print "Parent_org: " . Dumper($parent);
956            
957        # Create child node object if isn't already done
958        # ($child_entry have to be the class name then...)
959        if(!ref($child_entry)) {
960          $child_entry = $self->createNode($child_entry);
961          # it could be insert 'manually' or will be insert 'transparent' if parent will be updated
962          #$core->insert($child_entry);
963          #print "Create child object [$nodename]: " . Dumper($child_entry);
964        }
965    
966        # get reference of tied node (seems, only on Linux node's are tied!!)
967        my $tied_node = tied $parent->{$nodename};
968    
969        # insert/change child entry at parent
970        #print "reference: " . ref($parent->{$nodename}) . "\n";
971        if(ref($parent->{$nodename}) eq 'ARRAY') {
972          # (seems, only on Linux node's are tied!!)
973          if($tied_node) {
974            # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
975            # so a 'PUSH' is not implemented (which could be then done transparently)
976            my $array = $tied_node->FETCH;
977            push @$array, $child_entry;
978            $tied_node->STORE($array);
979            # node will be normaly untied at 'STORE'
980            if(tied $parent->{$nodename}) { print "already tied !!\n"; }
981            else { undef $tied_node; }
982          } else {
983            push @{$parent->{$nodename}}, $child_entry;
984          }
985        }
986        elsif(ref($parent->{$nodename}) eq 'HASH') {
987          if(my $key = $query_args->{hash_key}) {
988            # (seems, only on Linux node's are tied!!)
989            if($tied_node) {
990              # same problem as with 'ARRAY':
991              # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type.
992              my $hash = $tied_node->FETCH;
993              $hash->{$key} = $child_entry;
994              $tied_node->STORE($hash);
995              # node will be normaly untied at 'STORE'
996              if(tied $parent->{$nodename}) { print "already tied !!\n"; }
997              else { undef $tied_node; }
998            } else {
999              $parent->{$nodename}->{$key} = $child_entry;
1000            }
1001          } else {
1002           print "ERROR: No HASH KEY given, so not able to insert hash entry!";
1003          }
1004        }
1005        else {
1006          $parent->{$nodename} = $child_entry;
1007        }
1008    
1009        # debug
1010        #print "Parent_new: " . Dumper($parent);
1011        
1012        # save parent
1013        $core->update($parent);
1014        
1015        # debug
1016        #print "Saved Parent: ". Dumper($parent);
1017        
1018        return $child_entry;
1019      }
1020    
1021    
1022  1;  1;
1023  __END__  __END__

Legend:
Removed from v.1.33  
changed lines
  Added in v.1.38

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed