/[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.36 by jonen, Sat May 10 17:31:18 2003 UTC revision 1.42 by joko, Tue Jul 1 23:24:17 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.42  2003/07/01 23:24:17  joko
7    #  now using package before using function
8    #
9    #  Revision 1.41  2003/06/29 02:03:45  joko
10    #  fix:? initialize schema on startup
11    #
12    #  Revision 1.40  2003/06/25 22:57:54  joko
13    #  major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting"
14    #
15    #  Revision 1.39  2003/06/06 11:40:40  jonen
16    #  fixed bug at 'getFilteredList'
17    #
18    #  Revision 1.38  2003/05/13 16:38:38  joko
19    #  problems with "tied" on 5.6.1/win32
20    #
21    #  Revision 1.37  2003/05/10 17:37:39  jonen
22    #  corrected last commit
23    #
24  #  Revision 1.36  2003/05/10 17:31:18  jonen  #  Revision 1.36  2003/05/10 17:31:18  jonen
25  #  + added new functions related to 'create' item  #  + added new functions related to 'create' item
26  #   - createNode()  #   - createNode()
27  #     # creates non-persistent 'deep dummy filled' object  #     # creates non-persistent 'deep dummy filled' object
28  #   -  #   - insertChildNode()
29    #     # inserts child node at given parent (child node haven't to exists,
30    #         createNode will be injected transparently)
31  #  #
32  #  Revision 1.35  2003/04/19 16:09:48  jonen  #  Revision 1.35  2003/04/19 16:09:48  jonen
33  #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'  #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
# Line 149  use base ("Data::Storage::Handler::Abstr Line 169  use base ("Data::Storage::Handler::Abstr
169    
170  use Data::Dumper;  use Data::Dumper;
171  use Tangram;  use Tangram;
172    use Class::Tangram;
173    
174  use DesignPattern::Object;  use DesignPattern::Object;
175  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
# Line 176  sub _initSchema { Line 197  sub _initSchema {
197    #if (!$schema_tangram) {    #if (!$schema_tangram) {
198      #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} } );
199      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} } );
200      $schema_tangram = $obj->getSchema();      $schema_tangram = $obj->getSchema() if $obj;
201    #}    #}
202    if (!$schema_tangram) {    if (!$schema_tangram) {
203      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." );
204      return 0;      return 0;
205    }    }
206    #$self->_patchSchema();    #$self->_patchSchema();
# Line 225  sub connect { Line 246  sub connect {
246  #      return;  #      return;
247  #    }  #    }
248    
249      #return unless $self->_initSchema();      return unless $self->_initSchema();
250      $self->_initSchema();      #$self->_initSchema();
251    
252      # create the main tangram storage object      # create the main tangram storage object
253      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 488  sub getListFiltered { Line 509  sub getListFiltered {
509    # redirect to unfiltered mode    # redirect to unfiltered mode
510    #return $self->getListUnfiltered(@_);    #return $self->getListUnfiltered(@_);
511    
512    my $nodename = shift;    my $in = {};
513    my $filters = shift;    $in->{nodename} = shift;
514      $in->{filters} = shift;
515      $in->{sorting} = shift;
516      
517    my @results;    my @results;
518    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" );
519    
520      #print Dumper($filters);
521    
522  #print Dumper($filters);    # 1. "Remote Object Handle" - get set of objects from odbms by object name
523      my $remote = $self->{_COREHANDLE}->remote($in->{nodename});
524        
525      # 2. Transfer $in to $orm_query
526      my $orm_query = {};
527      
528      # 2.a. Filters
529    my @tfilters;    my @tfilters;
530      my $orm_filter = undef;
531        
532    foreach my $filter (@$filters) {    foreach my $filter (@{$in->{filters}}) {
533        
534      # get filter - TODO: for each filter      # get filter - TODO: for each filter
535      #my $filter = $filters->[0];      #my $filter = $filters->[0];
536      
537      # build filter      # 1. The parts of a filter source entry
538      my $lexpr = $filter->{key};      my $lexpr = $filter->{key};
539      #my $op = $filter->{op};      #my $op = $filter->{op};
540      my $op = '=';      my $op = '=';
541      my $rexpr = $filter->{val};      my $rexpr = $filter->{val};
542      my $tight = 100;      my $tight = 100;
543    
544        # 2. Build filter target entry
545            
546    #  my $tfilter = Tangram::Filter->new(      # Test 1 - didn't work out!
547    #    expr => "t1.$lexpr $op '$rexpr'",      #  my $tfilter = Tangram::Filter->new(
548    #    tight => $tight,      #    expr => "t1.$lexpr $op '$rexpr'",
549    #    objects => $objects,      #    tight => $tight,
550    #  );      #    objects => $objects,
551          #  );
552    
553        my $orm_filter_tmp = undef;
554        # was:
555      # TODO: is_op?      # TODO: is_op?
556      # dispatch un-common operators if exists      # dispatch un-common operators if exists
557      if($filter->{op} eq "ref") {      if ($filter->{op} eq "ref") {
558        push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";        # do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query
559          #print "Filter->op eq 'ref'.\n";
560          #push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";
561      } else {      } else {
562        # 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
563        push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";        #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
564          # better: calculate expression right here!
565          #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
566          
567          #print "key: ", $filter->{key}, "\n";
568          
569          my $left = $remote->{$filter->{key}};
570          #my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" );
571          my $right = $filter->{val};
572          my $op = $filter->{op};
573          $orm_filter_tmp = $left->$op($right);
574        }
575    
576        if (not $orm_filter) {
577          $orm_filter = $orm_filter_tmp;
578        } else {
579          $orm_filter = $orm_filter->and($orm_filter_tmp);
580      }      }
581    
582    }    }
583    
584    my $tfilter = join(' & ', @tfilters);    $orm_query->{filter} = $orm_filter;
585    
   # get set of objects from odbms by object name  
   my $remote = $self->{_COREHANDLE}->remote($nodename);  
     
586    # was:    # was:
587    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);  
588      # 2.b. sorting [new as of 2003-06-17]
589      if ($in->{sorting}) {
590        my $sorting_rules = [];
591        my $sorting_order = 'ASC';
592        foreach my $sorting_column (keys %{$in->{sorting}}) {
593          $sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column};
594          push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column );
595        }
596        $orm_query->{order} = $sorting_rules;
597        if ($sorting_order eq 'DESC') {
598          $orm_query->{desc} = 1;
599        }
600      }
601    
602    
603      # 3. build and issue query - return result as Tangram::Cursor
604      
605      # coerce hash into array (This is valid in Perl)
606      my @orm_query_args = %$orm_query;
607      # send query (arguments) to orm
608      @results = $self->{_COREHANDLE}->select($remote, @orm_query_args);
609      
610      #my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args);
611      #return $cursor;
612      #print Dumper(@results);
613    
614    # is:    # is:
615    # 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
616    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';      #my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');';
617          #print "eval: $evalstring", "\n";
618    #print "eval: $evalstring", "\n";      # get filtered list/set
619          #@results = eval($evalstring);
620    # get filtered list/set      #die $@ if $@;
621    @results = eval($evalstring);  
622    die $@ if $@;  =pod
623      # filter results
624      if ($filters->[0]->{op} && ($filters->[0]->{op} eq "ref")) {
625          #print "Filter->op eq 'ref'.\n";
626          my $att_name = $filters->[0]->{key};
627          my $att_val = $filters->[0]->{val};
628          my @filtered;
629          foreach(@results) {
630            if(ref($_->{$att_name}) eq $att_val) {
631              push @filtered, $_;
632            }
633          }
634          @results = @filtered;
635      }
636    =cut
637    
638      #print "results: " . Dumper(\@results);
639        
640    return \@results;    return \@results;
641  }  }
# Line 572  sub createSet { Line 665  sub createSet {
665    return $result;    return $result;
666  }  }
667    
668    sub createResult {
669      my $self = shift;
670      my $rh = shift;
671      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
672      return $result;
673    }
674    
675  sub sendQuery {  sub sendQuery {
676    my $self = shift;    my $self = shift;
677    my $query = shift;    my $query = shift;
# Line 663  sub sendQuery { Line 763  sub sendQuery {
763    
764      if ($crud eq 'RETRIEVE') {      if ($crud eq 'RETRIEVE') {
765    
766        my $list = $self->getListFiltered($query->{node}, $query->{criterias});        my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting});
767          #return $list;
768          return $self->createResult($list);
769          
770        #return $self->createSet($object);        #return $self->createSet($object);
771        #return $self->createSet($list);        #return $self->createSet($list);
772        return $self->createSet(@$list);        return $self->createSet(@$list);
# Line 955  sub createNode { Line 1058  sub createNode {
1058        #print "Create child object [$nodename]: " . Dumper($child_entry);        #print "Create child object [$nodename]: " . Dumper($child_entry);
1059      }      }
1060    
1061      # get reference of tied node      # get reference of tied node (seems, only on Linux node's are tied!!)
1062      my $tied_node = tied $parent->{$nodename};      my $tied_node = tied $parent->{$nodename};
1063    
1064      # insert/change child entry at parent      # insert/change child entry at parent
1065      #print "reference: " . ref($parent->{$nodename}) . "\n";      #print "reference: " . ref($parent->{$nodename}) . "\n";
1066      if(ref($parent->{$nodename}) eq 'ARRAY') {      if(ref($parent->{$nodename}) eq 'ARRAY') {
1067        # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,        # (seems, only on Linux node's are tied!!)
1068        # so a 'PUSH' is not implemented (which could be then done transparently)        if($tied_node) {
1069        my $array = $tied_node->FETCH;          # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
1070        push @$array, $child_entry;          # so a 'PUSH' is not implemented (which could be then done transparently)
1071        $tied_node->STORE($array);          my $array = $tied_node->FETCH;
1072        # node will be normaly untied at 'STORE'          push @$array, $child_entry;
1073        if(tied $parent->{$nodename}) { print "already tied !!\n"; }          $tied_node->STORE($array);
       else { undef $tied_node; }  
     }  
     elsif(ref($parent->{$nodename}) eq 'HASH') {  
       if(my $key = $query_args->{hash_key}) {  
         # 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);  
1074          # node will be normaly untied at 'STORE'          # node will be normaly untied at 'STORE'
1075          if(tied $parent->{$nodename}) { print "already tied !!\n"; }          if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1076          else { undef $tied_node; }          else { undef $tied_node; }
1077        } else {        } else {
1078            push @{$parent->{$nodename}}, $child_entry;
1079          }
1080        }
1081        elsif(ref($parent->{$nodename}) eq 'HASH') {
1082          if(my $key = $query_args->{hash_key}) {
1083            # (seems, only on Linux node's are tied!!)
1084            if($tied_node) {
1085              # same problem as with 'ARRAY':
1086              # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type.
1087              my $hash = $tied_node->FETCH;
1088              $hash->{$key} = $child_entry;
1089              $tied_node->STORE($hash);
1090              # node will be normaly untied at 'STORE'
1091              if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1092              else { undef $tied_node; }
1093            } else {
1094              $parent->{$nodename}->{$key} = $child_entry;
1095            }
1096          } else {
1097         print "ERROR: No HASH KEY given, so not able to insert hash entry!";         print "ERROR: No HASH KEY given, so not able to insert hash entry!";
1098        }        }
1099      }      }

Legend:
Removed from v.1.36  
changed lines
  Added in v.1.42

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