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

Legend:
Removed from v.1.35  
changed lines
  Added in v.1.45

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