/[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.32 by joko, Tue Apr 8 22:52:22 2003 UTC revision 1.39 by jonen, Fri Jun 6 11:40:40 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.39  2003/06/06 11:40:40  jonen
7    #  fixed bug at 'getFilteredList'
8    #
9    #  Revision 1.38  2003/05/13 16:38:38  joko
10    #  problems with "tied" on 5.6.1/win32
11    #
12    #  Revision 1.37  2003/05/10 17:37:39  jonen
13    #  corrected last commit
14    #
15    #  Revision 1.36  2003/05/10 17:31:18  jonen
16    #  + added new functions related to 'create' item
17    #   - createNode()
18    #     # creates non-persistent 'deep dummy filled' object
19    #   - insertChildNode()
20    #     # inserts child node at given parent (child node haven't to exists,
21    #         createNode will be injected transparently)
22    #
23    #  Revision 1.35  2003/04/19 16:09:48  jonen
24    #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
25    #
26    #  Revision 1.34  2003/04/11 01:18:53  joko
27    #  sendQuery:
28    #  + introduced crud action 'DELETE'
29    #
30    #  Revision 1.33  2003/04/09 06:07:43  joko
31    #  revamped 'sub sendQuery'
32    #
33  #  Revision 1.32  2003/04/08 22:52:22  joko  #  Revision 1.32  2003/04/08 22:52:22  joko
34  #  modified 'querySchema': better behaviour regarding filtering result  #  modified 'querySchema': better behaviour regarding filtering result
35  #  #
# Line 137  use Tangram; Line 164  use Tangram;
164  use DesignPattern::Object;  use DesignPattern::Object;
165  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
166  use Data::Mungle::Compare::Struct qw( isEmpty );  use Data::Mungle::Compare::Struct qw( isEmpty );
167  use Data::Mungle::Transform::Deep qw( expand );  use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
168    
169  # get logger instance  # get logger instance
170  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 499  sub getListFiltered { Line 526  sub getListFiltered {
526    #    objects => $objects,    #    objects => $objects,
527    #  );    #  );
528        
529      # HACK: build eval-string (sorry) to get filtered list - please give advice here      # TODO: is_op?
530      push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";      # dispatch un-common operators if exists
531        if($filter->{op} eq "ref") {
532          # do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query
533          #print "Filter->op eq 'ref'.\n";
534          #push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";
535        } else {
536          # HACK: build eval-string (sorry) to get filtered list - please give advice here
537          push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
538        }
539    
540    }    }
541    
# Line 522  sub getListFiltered { Line 557  sub getListFiltered {
557    @results = eval($evalstring);    @results = eval($evalstring);
558    die $@ if $@;    die $@ if $@;
559        
560    
561      # filter results
562      if($filters->[0]->{op} eq "ref") {
563          #print "Filter->op eq 'ref'.\n";
564          my $att_name = $filters->[0]->{key};
565          my $att_val = $filters->[0]->{val};
566          my @filtered;
567          foreach(@results) {
568            if(ref($_->{$att_name}) eq $att_val) {
569              push @filtered, $_;
570            }
571          }
572          @results = @filtered;
573      }
574    
575      #print "results: " . Dumper(\@results);
576      
577    return \@results;    return \@results;
578  }  }
579    
# Line 553  sub createSet { Line 605  sub createSet {
605  sub sendQuery {  sub sendQuery {
606    my $self = shift;    my $self = shift;
607    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);  
608    
609    #print Dumper($query);    #print Dumper($query);
610    
611    # HACK: special case: querying by id does not translate into a common tangram query    # type = ITEM|LIST|TRANSPARENT
612    # just load the object by given id(ent)    my $type = '';
613    if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {    # mode = OID|SPECIAL
614      #print "LOAD!!!", "\n";    my $mode = '';
615      #exit;    my $ident = '';
616      #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );    my $crud = '';
617      my $ident = $query->{criterias}->[0]->{val};    
618  #print "load obj", "\n";    
619      #return $self->createSet() if $ident == 5;    # dispatch type and mode
620      $self->{_COREHANDLE}->unload($ident);    
621      my $object = $self->{_COREHANDLE}->load($ident);      # defaults - 1
622  #print "get id", "\n";      if ($query->{options}) {
623      my $oid = $self->{_COREHANDLE}->id($object);        $crud = $query->{options}->{crud};
624  #print Dumper($object);        $crud ||= $query->{options}->{action};
625  #print "oid: $oid", "\n";      }
     return $self->createSet($object);  
     #return $self->createSet( $self->{COREHANDLE}->load('300090018') );  
   }  
   
   my $list = $self->getListFiltered($query->{node}, $query->{criterias});  
   #return $self->createSet($object);  
   #return $self->createSet($list);  
   return $self->createSet(@$list);  
   
   #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");  
626    
627        # defaults - 2
628        $type ||= 'TRANSPARENT';
629        $crud ||= 'RETRIEVE';
630    
631        if ($query->{options}->{OID}) {
632          $type = 'ITEM';
633          $mode = 'OID';
634          $ident = $query->{options}->{OID};
635        
636        } elsif (my $guid = $query->{options}->{GUID}) {
637          $type = 'TRANSPARENT';
638          $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
639    
640        # if operator is different (dispatcher for 'getListFiltered')
641        } elsif (my $op = $query->{options}->{op}) {
642          $type = 'TRANSPARENT';
643          $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
644    
645        }
646      
647        # HACK: special case: querying by id does not translate into a common tangram query
648        # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
649        if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
650          $type = 'ITEM';
651          $mode = 'SPECIAL.SYNC';
652          $ident = $query->{criterias}->[0]->{val};
653        }
654      
655    
656      # execute query
657      my $result;
658    
659      if ($type eq 'ITEM' && $ident) {
660    
661        if ($mode eq 'OID') {    
662          # TODO: review this case!
663          $result = $self->getObject($ident, $query->{options});
664        
665        } elsif ($mode eq 'SPECIAL.SYNC') {
666    
667          # V1 - failed
668          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
669      
670          # hmm....
671          #return $self->createSet() if $ident == 5;
672          
673          # Unload single object before doing any further operations to
674          # expect a "fresh" object from orm when performing the next calls.
675          $self->{_COREHANDLE}->unload($ident);
676          
677          # Load object from orm.
678          my $object = $self->{_COREHANDLE}->load($ident);
679      
680          # determine object identifier (OID)
681          my $oid = $self->{_COREHANDLE}->id($object);
682          
683          # encapsulate into result/response container and return this one
684          $result = $self->createSet($object);
685          
686          # debugging
687          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
688    
689        }
690        
691      
692      } elsif ($type eq 'TRANSPARENT') {
693    
694        if ($crud eq 'RETRIEVE') {
695    
696          my $list = $self->getListFiltered($query->{node}, $query->{criterias});
697          #return $self->createSet($object);
698          #return $self->createSet($list);
699          return $self->createSet(@$list);
700        
701          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
702        
703          # try a generic tangram query here
704          # TODO: try to place an oql on top of that (search.cpan.org!)
705          my @crits;
706          foreach (@{$query->{criterias}}) {
707            my $op = '';
708            $op = '=' if lc $_->{op} eq 'eq';
709            push @crits, "$_->{key}$op'$_->{val}'";
710          }
711          my $subnodes = {};
712          map { $subnodes->{$_}++ } @{$query->{subnodes}};
713          # HACK: this is hardcoded ;(    expand possibilities!
714          #my $crit = join(' AND ', @crits);
715          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
716          #return $self->sendCommand($sql);
717          #my $h = $self->{COREHANDLE}->remote($query->{node});
718          #my $res = $self->{COREHANDLE}->select($h, $h->{);
719          $result = $self->createCursor($query->{node});
720    
721        } elsif ($crud eq 'UPDATE') {
722    
723          # Patch current query to be a loader (e.g. change action, remove payload) ...
724          my $childquery = deep_copy($query);
725          $childquery->{options}->{crud} = 'RETRIEVE';
726          delete $childquery->{payload};
727    
728          # ... to use it to fetch a fresh object using ourselves (sendQuery).
729          my $cursor = $self->sendQuery($childquery);
730          my $status = $cursor->getStatus();
731          my $object = $cursor->getNextEntry();
732    
733          # Merge values and apply value modifiers.
734          my $options = { utf8 => 1, php => 1 };
735          merge_to($object, $query->{payload}, $options);
736    
737          # Execute update operation at orm.
738          $self->update($object);
739        
740        } elsif ($crud eq 'DELETE') {
741    
742          # Patch current query to be a loader (e.g. change action) ...
743          my $childquery = deep_copy($query);
744          $childquery->{options}->{crud} = 'RETRIEVE';
745    
746          # ... to use it to fetch a fresh object using ourselves (sendQuery).
747          my $cursor = $self->sendQuery($childquery);
748          my $status = $cursor->getStatus();
749          my $object = $cursor->getNextEntry();
750    
751          $self->erase($object);
752    
753        } elsif ($crud eq 'CREATE') {
754          
755          my $nodename = $query->{node};      
756          my $newnode = $self->createNode($nodename);
757          my $id = $self->{_COREHANDLE}->insert($newnode);
758      
759          print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n";
760          
761          return $newnode;
762    
763        }
764    
765      }
766    
767      return $result;
768    
   # try a generic tangram query here  
   # TODO: try to place an oql on top of that (search.cpan.org!)  
   my @crits;  
   foreach (@{$query->{criterias}}) {  
     my $op = '';  
     $op = '=' if lc $_->{op} eq 'eq';  
     push @crits, "$_->{key}$op'$_->{val}'";  
   }  
   my $subnodes = {};  
   map { $subnodes->{$_}++ } @{$query->{subnodes}};  
   # HACK: this is hardcoded ;(    expand possibilities!  
   #my $crit = join(' AND ', @crits);  
   #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);  
   #return $self->sendCommand($sql);  
   #my $h = $self->{COREHANDLE}->remote($query->{node});  
   #my $res = $self->{COREHANDLE}->select($h, $h->{);  
   return $self->createCursor($query->{node});  
769  }  }
770    
771  sub eraseAll {  sub eraseAll {
# Line 639  sub getObject { Line 801  sub getObject {
801    return $object if $object;    return $object if $object;
802  }  }
803    
804  sub getObjectByGuid {  sub getObjectByGuid_old {
805    my $self = shift;    my $self = shift;
806    my $guid = shift;    my $guid = shift;
807    my $options = shift;    my $options = shift;
# Line 669  sub getObjectByGuid { Line 831  sub getObjectByGuid {
831        
832  }  }
833    
834  sub getObjectAsHash {  sub getObjectAsHash_old {
835    my $self = shift;    my $self = shift;
836    my $oid = shift;    my $oid = shift;
837    my $options = shift;      my $options = shift;  
# Line 752  sub disconnect2 { Line 914  sub disconnect2 {
914    $self->{dataStorageLayer}->disconnect();    $self->{dataStorageLayer}->disconnect();
915  }  }
916    
917    
918    sub createNode {
919      my $self = shift;
920      my $classname = shift;
921    
922      my $obj = $classname->new();
923      
924      my $attr_options = Class::Tangram::attribute_options($classname);
925      #print "Attribute Options: " . Dumper($attr_options);
926    
927      my $attr_types = Class::Tangram::attribute_types($classname);
928      #print "Attribute Types: " . Dumper($attr_types);
929      
930      foreach(keys %{$attr_types}) {
931        if($attr_types->{$_} eq 'string') {
932          $obj->{$_} = '';
933        } elsif($attr_types->{$_} eq 'int') {
934          $obj->{$_} = 0;
935        } elsif($attr_types->{$_} eq 'real') {
936          $obj->{$_} = 0;
937        } elsif($attr_types->{$_} eq 'rawdatetime') {
938          $obj->{$_} = '0000-00-00 00:00:00';
939        } elsif($attr_types->{$_} eq 'ref') {
940          if($attr_options->{$_}->{class}) {
941            $obj->{$_} = $self->createNode($attr_options->{$_}->{class});
942          } else {
943            #$obj->{$_} = undef();
944          }
945        } elsif($attr_types->{$_} eq 'iarray') {
946            $obj->{$_} = [ ];
947        } elsif($attr_types->{$_} eq 'hash') {
948            $obj->{$_} = {  };
949        } elsif($attr_types->{$_} eq 'flat_hash') {
950            $obj->{$_} = { };
951        }
952      }
953      
954      #print "New Object: " . Dumper($obj);
955      
956      return $obj;
957    }
958    
959    
960      sub insertChildNode {
961        my $self = shift;
962        my $child_entry = shift;
963        my $query_args = shift;
964        
965        my $core = $self->{_COREHANDLE};
966        my $nodename = $query_args->{nodename};
967        
968        # get parent object
969        my $query = {
970              node => $query_args->{parent}->{nodename},
971              options => { GUID => $query_args->{parent}->{guid}, },
972              };
973        my $cursor = $self->sendQuery($query);
974        my $parent = $cursor->getNextEntry();
975    
976        # debug
977        #print "Parent_org: " . Dumper($parent);
978            
979        # Create child node object if isn't already done
980        # ($child_entry have to be the class name then...)
981        if(!ref($child_entry)) {
982          $child_entry = $self->createNode($child_entry);
983          # it could be insert 'manually' or will be insert 'transparent' if parent will be updated
984          #$core->insert($child_entry);
985          #print "Create child object [$nodename]: " . Dumper($child_entry);
986        }
987    
988        # get reference of tied node (seems, only on Linux node's are tied!!)
989        my $tied_node = tied $parent->{$nodename};
990    
991        # insert/change child entry at parent
992        #print "reference: " . ref($parent->{$nodename}) . "\n";
993        if(ref($parent->{$nodename}) eq 'ARRAY') {
994          # (seems, only on Linux node's are tied!!)
995          if($tied_node) {
996            # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
997            # so a 'PUSH' is not implemented (which could be then done transparently)
998            my $array = $tied_node->FETCH;
999            push @$array, $child_entry;
1000            $tied_node->STORE($array);
1001            # node will be normaly untied at 'STORE'
1002            if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1003            else { undef $tied_node; }
1004          } else {
1005            push @{$parent->{$nodename}}, $child_entry;
1006          }
1007        }
1008        elsif(ref($parent->{$nodename}) eq 'HASH') {
1009          if(my $key = $query_args->{hash_key}) {
1010            # (seems, only on Linux node's are tied!!)
1011            if($tied_node) {
1012              # same problem as with 'ARRAY':
1013              # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type.
1014              my $hash = $tied_node->FETCH;
1015              $hash->{$key} = $child_entry;
1016              $tied_node->STORE($hash);
1017              # node will be normaly untied at 'STORE'
1018              if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1019              else { undef $tied_node; }
1020            } else {
1021              $parent->{$nodename}->{$key} = $child_entry;
1022            }
1023          } else {
1024           print "ERROR: No HASH KEY given, so not able to insert hash entry!";
1025          }
1026        }
1027        else {
1028          $parent->{$nodename} = $child_entry;
1029        }
1030    
1031        # debug
1032        #print "Parent_new: " . Dumper($parent);
1033        
1034        # save parent
1035        $core->update($parent);
1036        
1037        # debug
1038        #print "Saved Parent: ". Dumper($parent);
1039        
1040        return $child_entry;
1041      }
1042    
1043    
1044  1;  1;
1045  __END__  __END__

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.39

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