/[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.40 by joko, Wed Jun 25 22:57:54 2003 UTC
# Line 3  Line 3 
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  #  Revision 1.32  2003/04/08 22:52:22  joko
37  #  modified 'querySchema': better behaviour regarding filtering result  #  modified 'querySchema': better behaviour regarding filtering result
38  #  #
# Line 137  use Tangram; Line 167  use Tangram;
167  use DesignPattern::Object;  use DesignPattern::Object;
168  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
169  use Data::Mungle::Compare::Struct qw( isEmpty );  use Data::Mungle::Compare::Struct qw( isEmpty );
170  use Data::Mungle::Transform::Deep qw( expand );  use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
171    
172  # get logger instance  # get logger instance
173  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 472  sub getListFiltered { Line 502  sub getListFiltered {
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);
514    
515  #print Dumper($filters);    # 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    #print "eval: $evalstring", "\n";      # get filtered list/set
612          #@results = eval($evalstring);
613    # get filtered list/set      #die $@ if $@;
614    @results = eval($evalstring);  
615    die $@ if $@;  =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  }  }
# Line 550  sub createSet { Line 658  sub createSet {
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} && ($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    my $list = $self->getListFiltered($query->{node}, $query->{criterias});  
694    #return $self->createSet($object);      if ($query->{options}->{OID}) {
695    #return $self->createSet($list);        $type = 'ITEM';
696    return $self->createSet(@$list);        $mode = 'OID';
697          $ident = $query->{options}->{OID};
698    #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");      
699        } elsif (my $guid = $query->{options}->{GUID}) {
700          $type = 'TRANSPARENT';
701          $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
702    
703        # if operator is different (dispatcher for 'getListFiltered')
704        } elsif (my $op = $query->{options}->{op}) {
705          $type = 'TRANSPARENT';
706          $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
707    
708        }
709      
710        # HACK: special case: querying by id does not translate into a common tangram query
711        # 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    
   # 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});  
835  }  }
836    
837  sub eraseAll {  sub eraseAll {
# Line 639  sub getObject { Line 867  sub getObject {
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;
# Line 669  sub getObjectByGuid { Line 897  sub getObjectByGuid {
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;  
# Line 752  sub disconnect2 { Line 980  sub disconnect2 {
980    $self->{dataStorageLayer}->disconnect();    $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__

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

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