/[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.20 by jonen, Mon Dec 16 20:49:17 2002 UTC revision 1.43 by jonen, Wed Jul 2 11:07:12 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.43  2003/07/02 11:07:12  jonen
7    #  re-activate filtering of results *after* results are fetched from tangram
8    #    (needed for e.g. UserManagment)
9    #
10    #  Revision 1.42  2003/07/01 23:24:17  joko
11    #  now using package before using function
12    #
13    #  Revision 1.41  2003/06/29 02:03:45  joko
14    #  fix:? initialize schema on startup
15    #
16    #  Revision 1.40  2003/06/25 22:57:54  joko
17    #  major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting"
18    #
19    #  Revision 1.39  2003/06/06 11:40:40  jonen
20    #  fixed bug at 'getFilteredList'
21    #
22    #  Revision 1.38  2003/05/13 16:38:38  joko
23    #  problems with "tied" on 5.6.1/win32
24    #
25    #  Revision 1.37  2003/05/10 17:37:39  jonen
26    #  corrected last commit
27    #
28    #  Revision 1.36  2003/05/10 17:31:18  jonen
29    #  + added new functions related to 'create' item
30    #   - createNode()
31    #     # creates non-persistent 'deep dummy filled' object
32    #   - insertChildNode()
33    #     # inserts child node at given parent (child node haven't to exists,
34    #         createNode will be injected transparently)
35    #
36    #  Revision 1.35  2003/04/19 16:09:48  jonen
37    #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
38    #
39    #  Revision 1.34  2003/04/11 01:18:53  joko
40    #  sendQuery:
41    #  + introduced crud action 'DELETE'
42    #
43    #  Revision 1.33  2003/04/09 06:07:43  joko
44    #  revamped 'sub sendQuery'
45    #
46    #  Revision 1.32  2003/04/08 22:52:22  joko
47    #  modified 'querySchema': better behaviour regarding filtering result
48    #
49    #  Revision 1.31  2003/04/05 21:24:09  joko
50    #  modified 'sub getChildNodes': now contains code from 'querySchema'
51    #
52    #  Revision 1.30  2003/03/27 15:31:14  joko
53    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
54    #
55    #  Revision 1.29  2003/02/21 01:47:18  joko
56    #  purged old code
57    #  minor cosmetics
58    #
59    #  Revision 1.28  2003/02/20 20:20:26  joko
60    #  tried to get auto-disconnect working again - failed with that
61    #
62    #  Revision 1.27  2003/01/31 06:30:59  joko
63    #  + enabled 'sendQuery'
64    #
65    #  Revision 1.26  2003/01/30 22:29:47  joko
66    #  + fixed module usage (removed dependency on 'libp.pm')
67    #
68    #  Revision 1.25  2003/01/19 02:30:05  joko
69    #  + fix: modified call to '_initSchema'
70    #
71    #  Revision 1.24  2002/12/22 14:13:01  joko
72    #  + sub dropDb
73    #
74    #  Revision 1.23  2002/12/19 16:31:53  joko
75    #  +- renamed sub to 'rebuildDb'
76    #
77    #  Revision 1.22  2002/12/18 22:28:16  jonen
78    #  + added extended logging at 'getObjectByGuid()'
79    #
80    #  Revision 1.21  2002/12/16 22:20:49  jonen
81    #  + fixed bug at 'getObjectByGuid()'
82    #
83  #  Revision 1.20  2002/12/16 20:49:17  jonen  #  Revision 1.20  2002/12/16 20:49:17  jonen
84  #  + added sub 'getObjectByGuid()'  #  + added sub 'getObjectByGuid()'
85  #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'  #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
# Line 20  Line 97 
97  #  + fix: encapsulated object-loading inside an 'eval'  #  + fix: encapsulated object-loading inside an 'eval'
98  #  #
99  #  Revision 1.15  2002/12/05 13:55:21  joko  #  Revision 1.15  2002/12/05 13:55:21  joko
100  #  + now utilizing 'object2hash' instead of 'var_deref'  #  + now utilizing 'expand' instead of 'var_deref'
101  #  + played around with having fresh-objects - no progress....  #  + played around with having fresh-objects - no progress....
102  #  #
103  #  Revision 1.14  2002/12/05 09:40:30  jonen  #  Revision 1.14  2002/12/05 09:40:30  jonen
# Line 93  use warnings; Line 170  use warnings;
170  use base ("Data::Storage::Handler");  use base ("Data::Storage::Handler");
171  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
172    
173  use Tangram;  
174  use Data::Dumper;  use Data::Dumper;
175  use libp qw( getNewPerlObjectByPkgName );  use Tangram;
176  use Data::Storage::Result::Tangram;  use Class::Tangram;
 use Data::Compare::Struct qw( isEmpty );  
 use Data::Transform::Deep qw( object2hash );  
 use Data::Transform::Encode qw( var2utf8 );  
177    
178    use DesignPattern::Object;
179    use Data::Storage::Result::Tangram;
180    use Data::Mungle::Compare::Struct qw( isEmpty );
181    use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
182    
183  # get logger instance  # get logger instance
184  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 121  sub _initSchema { Line 199  sub _initSchema {
199    my $self = shift;    my $self = shift;
200    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
201    #if (!$schema_tangram) {    #if (!$schema_tangram) {
202      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} } );
203      $schema_tangram = $obj->getSchema();      my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
204        $schema_tangram = $obj->getSchema() if $obj;
205    #}    #}
206    if (!$schema_tangram) {    if (!$schema_tangram) {
207      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." );
208      return 0;      return 0;
209    }    }
210    #$self->_patchSchema();    #$self->_patchSchema();
# Line 154  sub connect { Line 233  sub connect {
233      my $self = shift;      my $self = shift;
234            
235      my $dsn = shift;      my $dsn = shift;
236    
237    #print Dumper($self);
238    #exit;
239    
240        # TODO: re-enable
241      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
242      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
243            
244      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 168  sub connect { Line 251  sub connect {
251  #    }  #    }
252    
253      return unless $self->_initSchema();      return unless $self->_initSchema();
254        #$self->_initSchema();
255    
256      # create the main tangram storage object      # create the main tangram storage object
257      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 194  sub connect { Line 278  sub connect {
278  sub getChildNodes {  sub getChildNodes {
279    
280    my $self = shift;    my $self = shift;
281    my @nodes;    my $mode = shift;
282      my $filter = shift;
283      
284      $mode ||= 'core';
285      $filter ||= 'all';
286      
287      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
288    
289    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    if ($mode eq 'core') {
290    
291    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE      my @nodes;
292    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });      
293    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );      # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
294          #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
295    # todo: should we retrieve information from the schema here      #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
296    # rather than poorly getting table names from underlying dbi?      
297    my $storage = $self->_getSubLayerHandle();      # todo: should we retrieve information from the schema here
298    @nodes = @{$storage->getChildNodes()};      # rather than poorly getting table names from underlying dbi?
299    #$storage->_configureCOREHANDLE();      my $storage = $self->_getSubLayerHandle();
300  #print "getchildnodes\n";      @nodes = @{$storage->getChildNodes()};
301  #print Dumper($self);      #$storage->_configureCOREHANDLE();
302    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #print "getchildnodes\n";
303      #print Dumper($self);
304        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
305        
306        # TODO: REVIEW
307        #$storage->disconnect();
308        
309        $self->{meta}->{childnodes} = \@nodes;
310    
311        return \@nodes;
312        
313    # TODO: REVIEW    } elsif ($mode eq 'root') {
314    #$storage->disconnect();      
315        # FIXME: this will return *all* known classes to 'Class::Tangram',
316        # which might not be what you expect since more than one instance
317        # of Tangram may be in memory and Class::Tangram seems to
318        # offer no methods to determine this or filter its result(s) according
319        # to a specific database.
320        my @object_names = Class::Tangram::known_classes();
321        my @concret_names;
322        my $o_cnt;
323        foreach (sort @object_names) {
324          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
325          $o_cnt++;
326        }
327    
328        if ($filter eq 'all') {
329          return \@object_names;
330        } elsif ($filter eq 'concrete') {
331          return \@concret_names;
332        }
333        
334    $self->{meta}->{childnodes} = \@nodes;    }
335        
   return \@nodes;  
336    
337  }  }
338    
   
339  sub testIntegrity {  sub testIntegrity {
340    
341    my $self = shift;    my $self = shift;
# Line 294  sub configureCOREHANDLE { Line 409  sub configureCOREHANDLE {
409    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
410    
411    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
412      #print Dumper($self);
413      #exit;
414    
415    # apply configured modifications    # apply configured modifications
416      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
# Line 359  sub retreatSchema { Line 476  sub retreatSchema {
476    return $ok;    return $ok;
477  }  }
478    
479  sub rebuildDbAndSchema {  sub rebuildDb {
480    my $self = shift;    my $self = shift;
481    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
482    my @results;    my @results;
483    
484    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 396  sub getListFiltered { Line 513  sub getListFiltered {
513    # redirect to unfiltered mode    # redirect to unfiltered mode
514    #return $self->getListUnfiltered(@_);    #return $self->getListUnfiltered(@_);
515    
516    my $nodename = shift;    my $in = {};
517    my $filters = shift;    $in->{nodename} = shift;
518      $in->{filters} = shift;
519      $in->{sorting} = shift;
520      
521    my @results;    my @results;
522    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" );
523    
524    #print Dumper($filters);    #print Dumper($filters);
525    
526      # 1. "Remote Object Handle" - get set of objects from odbms by object name
527      my $remote = $self->{_COREHANDLE}->remote($in->{nodename});
528      
529      # 2. Transfer $in to $orm_query
530      my $orm_query = {};
531        
532      # 2.a. Filters
533    my @tfilters;    my @tfilters;
534      my $orm_filter = undef;
535        
536    foreach my $filter (@$filters) {    foreach my $filter (@{$in->{filters}}) {
537        
538      # get filter - TODO: for each filter      # get filter - TODO: for each filter
539      #my $filter = $filters->[0];      #my $filter = $filters->[0];
540      
541      # build filter      # 1. The parts of a filter source entry
542      my $lexpr = $filter->{key};      my $lexpr = $filter->{key};
543      #my $op = $filter->{op};      #my $op = $filter->{op};
544      my $op = '=';      my $op = '=';
545      my $rexpr = $filter->{val};      my $rexpr = $filter->{val};
546      my $tight = 100;      my $tight = 100;
547    
548        # 2. Build filter target entry
549            
550    #  my $tfilter = Tangram::Filter->new(      # Test 1 - didn't work out!
551    #    expr => "t1.$lexpr $op '$rexpr'",      #  my $tfilter = Tangram::Filter->new(
552    #    tight => $tight,      #    expr => "t1.$lexpr $op '$rexpr'",
553    #    objects => $objects,      #    tight => $tight,
554    #  );      #    objects => $objects,
555          #  );
556      # HACK: build eval-string (sorry) to get filtered list - please give advice here  
557      push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";      my $orm_filter_tmp = undef;
558        # was:
559        # TODO: is_op?
560        # dispatch un-common operators if exists
561        if ($filter->{op} eq "ref") {
562          # do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query
563          #print "Filter->op eq 'ref'.\n";
564          #push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";
565        } else {
566          # HACK: build eval-string (sorry) to get filtered list - please give advice here
567          #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
568          # better: calculate expression right here!
569          #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
570          
571          #print "key: ", $filter->{key}, "\n";
572          
573          my $left = $remote->{$filter->{key}};
574          #my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" );
575          my $right = $filter->{val};
576          my $op = $filter->{op};
577          $orm_filter_tmp = $left->$op($right);
578        }
579    
580        if (not $orm_filter) {
581          $orm_filter = $orm_filter_tmp;
582        } else {
583          $orm_filter = $orm_filter->and($orm_filter_tmp);
584        }
585    
586    }    }
587    
588    my $tfilter = join(' & ', @tfilters);    $orm_query->{filter} = $orm_filter;
589    
   # get set of objects from odbms by object name  
   my $remote = $self->{_COREHANDLE}->remote($nodename);  
     
590    # was:    # was:
591    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);  
592      # 2.b. sorting [new as of 2003-06-17]
593      if ($in->{sorting}) {
594        my $sorting_rules = [];
595        my $sorting_order = 'ASC';
596        foreach my $sorting_column (keys %{$in->{sorting}}) {
597          $sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column};
598          push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column );
599        }
600        $orm_query->{order} = $sorting_rules;
601        if ($sorting_order eq 'DESC') {
602          $orm_query->{desc} = 1;
603        }
604      }
605    
606    
607      # 3. build and issue query - return result as Tangram::Cursor
608      
609      # coerce hash into array (This is valid in Perl)
610      my @orm_query_args = %$orm_query;
611      # send query (arguments) to orm
612      @results = $self->{_COREHANDLE}->select($remote, @orm_query_args);
613      
614      #my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args);
615      #return $cursor;
616      #print Dumper(@results);
617    
618    # is:    # is:
619    # 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
620    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';      #my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');';
621          #print "eval: $evalstring", "\n";
622    # get filtered list/set      # get filtered list/set
623    @results = eval($evalstring);      #@results = eval($evalstring);
624    die $@ if $@;      #die $@ if $@;
625    
626      # filter results - NEEDED for e.g. UserManagment !!
627      if ($in->{filters}->[0]->{op} && ($in->{filters}->[0]->{op} eq "ref")) {
628          #print "Filter->op eq 'ref'.\n";
629          my $att_name = $in->{filters}->[0]->{key};
630          my $att_val = $in->{filters}->[0]->{val};
631          my @filtered;
632          foreach(@results) {
633            if(ref($_->{$att_name}) eq $att_val) {
634              push @filtered, $_;
635            }
636          }
637          @results = @filtered;
638      }
639    
640      #print "results: " . Dumper(\@results);
641        
642    return \@results;    return \@results;
643  }  }
# Line 472  sub createSet { Line 667  sub createSet {
667    return $result;    return $result;
668  }  }
669    
670    sub createResult {
671      my $self = shift;
672      my $rh = shift;
673      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
674      return $result;
675    }
676    
677  sub sendQuery {  sub sendQuery {
678    my $self = shift;    my $self = shift;
679    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);  
680    
681    #print Dumper($query);    #print Dumper($query);
682    
683    # HACK: special case: querying by id does not translate into a common tangram query    # type = ITEM|LIST|TRANSPARENT
684    # just load the object by given id(ent)    my $type = '';
685    if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {    # mode = OID|SPECIAL
686      #print "LOAD!!!", "\n";    my $mode = '';
687      #exit;    my $ident = '';
688      #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );    my $crud = '';
689      my $ident = $query->{criterias}->[0]->{val};    
690  #print "load obj", "\n";    
691      #return $self->createSet() if $ident == 5;    # dispatch type and mode
692      $self->{_COREHANDLE}->unload($ident);    
693      my $object = $self->{_COREHANDLE}->load($ident);      # defaults - 1
694  #print "get id", "\n";      if ($query->{options}) {
695      my $oid = $self->{_COREHANDLE}->id($object);        $crud = $query->{options}->{crud};
696  #print Dumper($object);        $crud ||= $query->{options}->{action};
697  #print "oid: $oid", "\n";      }
698      return $self->createSet($object);  
699      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      # defaults - 2
700    }      $type ||= 'TRANSPARENT';
701        $crud ||= 'RETRIEVE';
702    die("This should not be reached for now - redirect to \$self->getListFiltered() here!");  
703        if ($query->{options}->{OID}) {
704    # TODO: do a common tangram query here        $type = 'ITEM';
705          $mode = 'OID';
706    my @crits;        $ident = $query->{options}->{OID};
707    foreach (@{$query->{criterias}}) {      
708      my $op = '';      } elsif (my $guid = $query->{options}->{GUID}) {
709      $op = '=' if lc $_->{op} eq 'eq';        $type = 'TRANSPARENT';
710      push @crits, "$_->{key}$op'$_->{val}'";        $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
711    }  
712    my $subnodes = {};      # if operator is different (dispatcher for 'getListFiltered')
713    map { $subnodes->{$_}++ } @{$query->{subnodes}};      } elsif (my $op = $query->{options}->{op}) {
714    # HACK: this is hardcoded ;(    expand possibilities!        $type = 'TRANSPARENT';
715    #my $crit = join(' AND ', @crits);        $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
716    #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);  
717    #return $self->sendCommand($sql);      }
718    #my $h = $self->{COREHANDLE}->remote($query->{node});    
719    #my $res = $self->{COREHANDLE}->select($h, $h->{);      # HACK: special case: querying by id does not translate into a common tangram query
720    return $self->createCursor($query->{node});      # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
721        if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
722          $type = 'ITEM';
723          $mode = 'SPECIAL.SYNC';
724          $ident = $query->{criterias}->[0]->{val};
725        }
726      
727    
728      # execute query
729      my $result;
730    
731      if ($type eq 'ITEM' && $ident) {
732    
733        if ($mode eq 'OID') {    
734          # TODO: review this case!
735          $result = $self->getObject($ident, $query->{options});
736        
737        } elsif ($mode eq 'SPECIAL.SYNC') {
738    
739          # V1 - failed
740          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
741      
742          # hmm....
743          #return $self->createSet() if $ident == 5;
744          
745          # Unload single object before doing any further operations to
746          # expect a "fresh" object from orm when performing the next calls.
747          $self->{_COREHANDLE}->unload($ident);
748          
749          # Load object from orm.
750          my $object = $self->{_COREHANDLE}->load($ident);
751      
752          # determine object identifier (OID)
753          my $oid = $self->{_COREHANDLE}->id($object);
754          
755          # encapsulate into result/response container and return this one
756          $result = $self->createSet($object);
757          
758          # debugging
759          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
760    
761        }
762        
763      
764      } elsif ($type eq 'TRANSPARENT') {
765    
766        if ($crud eq 'RETRIEVE') {
767    
768          my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting});
769          #return $list;
770          return $self->createResult($list);
771          
772          #return $self->createSet($object);
773          #return $self->createSet($list);
774          return $self->createSet(@$list);
775        
776          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
777        
778          # try a generic tangram query here
779          # TODO: try to place an oql on top of that (search.cpan.org!)
780          my @crits;
781          foreach (@{$query->{criterias}}) {
782            my $op = '';
783            $op = '=' if lc $_->{op} eq 'eq';
784            push @crits, "$_->{key}$op'$_->{val}'";
785          }
786          my $subnodes = {};
787          map { $subnodes->{$_}++ } @{$query->{subnodes}};
788          # HACK: this is hardcoded ;(    expand possibilities!
789          #my $crit = join(' AND ', @crits);
790          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
791          #return $self->sendCommand($sql);
792          #my $h = $self->{COREHANDLE}->remote($query->{node});
793          #my $res = $self->{COREHANDLE}->select($h, $h->{);
794          $result = $self->createCursor($query->{node});
795    
796        } elsif ($crud eq 'UPDATE') {
797    
798          # Patch current query to be a loader (e.g. change action, remove payload) ...
799          my $childquery = deep_copy($query);
800          $childquery->{options}->{crud} = 'RETRIEVE';
801          delete $childquery->{payload};
802    
803          # ... to use it to fetch a fresh object using ourselves (sendQuery).
804          my $cursor = $self->sendQuery($childquery);
805          my $status = $cursor->getStatus();
806          my $object = $cursor->getNextEntry();
807    
808          # Merge values and apply value modifiers.
809          my $options = { utf8 => 1, php => 1 };
810          merge_to($object, $query->{payload}, $options);
811    
812          # Execute update operation at orm.
813          $self->update($object);
814        
815        } elsif ($crud eq 'DELETE') {
816    
817          # Patch current query to be a loader (e.g. change action) ...
818          my $childquery = deep_copy($query);
819          $childquery->{options}->{crud} = 'RETRIEVE';
820    
821          # ... to use it to fetch a fresh object using ourselves (sendQuery).
822          my $cursor = $self->sendQuery($childquery);
823          my $status = $cursor->getStatus();
824          my $object = $cursor->getNextEntry();
825    
826          $self->erase($object);
827    
828        } elsif ($crud eq 'CREATE') {
829          
830          my $nodename = $query->{node};      
831          my $newnode = $self->createNode($nodename);
832          my $id = $self->{_COREHANDLE}->insert($newnode);
833      
834          print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n";
835          
836          return $newnode;
837    
838        }
839    
840      }
841    
842      return $result;
843    
844  }  }
845    
846  sub eraseAll {  sub eraseAll {
# Line 553  sub getObject { Line 876  sub getObject {
876    return $object if $object;    return $object if $object;
877  }  }
878    
879  sub getObjectByGuid {  sub getObjectByGuid_old {
880    my $self = shift;    my $self = shift;
881    my $guid = shift;    my $guid = shift;
882    my $options = shift;    my $options = shift;
883        
884    # Guid and Classname is needed    # Guid and Classname is needed
885    if(!$guid || !$options->{classname}) {    if(!$guid || !$options->{classname}) {
886       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
887      return;      return;
888    }    }
889        
# Line 568  sub getObjectByGuid { Line 892  sub getObjectByGuid {
892    $self->{_COREHANDLE}->unload() if ($options->{destroy});    $self->{_COREHANDLE}->unload() if ($options->{destroy});
893    
894    # search for object with given Classname and Guid    # search for object with given Classname and Guid
895    my $obj_tmp = $self->{_COREHANDLE}->remote($classname);    my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
896    my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);    my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
897        
898    # we searched for global unique identifer of some object,    # we searched for global unique identifer of some object,
899    # so it think we can trust there would be only one result    # so I think we can trust there would be only one result
900    if($result[0]) {    if($result[0]) {
901      return $result[0];      return $result[0];
902    } else {    } else {
903      return "No Object with Classname $classname and GUID $options->{guid} found!";      $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
904        return;
905    }    }
906        
907  }  }
908    
909  sub getObjectAsHash {  sub getObjectAsHash_old {
910    my $self = shift;    my $self = shift;
911    my $oid = shift;    my $oid = shift;
912    my $options = shift;      my $options = shift;  
# Line 596  sub getObjectAsHash { Line 921  sub getObjectAsHash {
921    # build options (a callback to unload autovivified objects) for 'expand'    # build options (a callback to unload autovivified objects) for 'expand'
922    # TODO: use $logger to write to debug here!    # TODO: use $logger to write to debug here!
923    my $cb; # = sub {};    my $cb; # = sub {};
924    
925      # deactivated way to get rid of used instances, if requested
926  =pod  =pod
927    if ($options->{destroy}) {      if ($options->{destroy}) {
928      $options->{cb}->{destroy} = sub {        $options->{cb}->{destroy} = sub {
929        print "================ DESTROY", "\n";          print "================ DESTROY", "\n";
930        my $object = shift;          my $object = shift;
931        #print Dumper($object);          #print Dumper($object);
932        $self->{_COREHANDLE}->unload($object);          $self->{_COREHANDLE}->unload($object);
933        #undef($object);          #undef($object);
934      };        };
935    }      }
936  =cut  =cut
937    
938    my $hash = object2hash($obj, $options);    my $hash = expand($obj, $options);
939    #$options->{cb}->{destroy}->($obj);  
940    #$self->{_COREHANDLE}->unload($obj);    # old (unsuccessful) attempts to get rid of used instances, if requested
941      
942    # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML      # V1:
943    # now done in object2hash      #$options->{cb}->{destroy}->($obj);
944    #var2utf8($hash) if ($options->{utf8});      #$self->{_COREHANDLE}->unload($obj);
945        
946    # old (wrong) attempts to get rid of used instances, if requested      # V2:
947      #$obj->clear_refs;      #$obj->clear_refs;
948      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
949      #$obj->DESTROY;      #$obj->DESTROY;
# Line 634  sub getCOREHANDLE { Line 961  sub getCOREHANDLE {
961    return $self->{_COREHANDLE};    return $self->{_COREHANDLE};
962  }  }
963    
964    sub dropDb {
965      my $self = shift;
966      my $storage = $self->_getSubLayerHandle();
967      return $storage->dropDb();
968    }
969    
970    sub testAvailability {
971      my $self = shift;
972      my $storage = $self->_getSubLayerHandle();
973      return $storage->testAvailability();
974    }
975    
976    sub disconnect2 {
977      my $self = shift;
978      my $storage = $self->_getSubLayerHandle();
979      print "DISC!", "\n";
980    
981      my $storage_ll = $storage->{_COREHANDLE};
982      $storage_ll->disconnect();
983      
984      print Dumper($storage);
985      exit;
986      
987      #$self->{_COREHANDLE}
988      #$storage->disconnect();
989      $self->{dataStorageLayer}->disconnect();
990    }
991    
992    
993    sub createNode {
994      my $self = shift;
995      my $classname = shift;
996    
997      my $obj = $classname->new();
998      
999      my $attr_options = Class::Tangram::attribute_options($classname);
1000      #print "Attribute Options: " . Dumper($attr_options);
1001    
1002      my $attr_types = Class::Tangram::attribute_types($classname);
1003      #print "Attribute Types: " . Dumper($attr_types);
1004      
1005      foreach(keys %{$attr_types}) {
1006        if($attr_types->{$_} eq 'string') {
1007          $obj->{$_} = '';
1008        } elsif($attr_types->{$_} eq 'int') {
1009          $obj->{$_} = 0;
1010        } elsif($attr_types->{$_} eq 'real') {
1011          $obj->{$_} = 0;
1012        } elsif($attr_types->{$_} eq 'rawdatetime') {
1013          $obj->{$_} = '0000-00-00 00:00:00';
1014        } elsif($attr_types->{$_} eq 'ref') {
1015          if($attr_options->{$_}->{class}) {
1016            $obj->{$_} = $self->createNode($attr_options->{$_}->{class});
1017          } else {
1018            #$obj->{$_} = undef();
1019          }
1020        } elsif($attr_types->{$_} eq 'iarray') {
1021            $obj->{$_} = [ ];
1022        } elsif($attr_types->{$_} eq 'hash') {
1023            $obj->{$_} = {  };
1024        } elsif($attr_types->{$_} eq 'flat_hash') {
1025            $obj->{$_} = { };
1026        }
1027      }
1028      
1029      #print "New Object: " . Dumper($obj);
1030      
1031      return $obj;
1032    }
1033    
1034    
1035      sub insertChildNode {
1036        my $self = shift;
1037        my $child_entry = shift;
1038        my $query_args = shift;
1039        
1040        my $core = $self->{_COREHANDLE};
1041        my $nodename = $query_args->{nodename};
1042        
1043        # get parent object
1044        my $query = {
1045              node => $query_args->{parent}->{nodename},
1046              options => { GUID => $query_args->{parent}->{guid}, },
1047              };
1048        my $cursor = $self->sendQuery($query);
1049        my $parent = $cursor->getNextEntry();
1050    
1051        # debug
1052        #print "Parent_org: " . Dumper($parent);
1053            
1054        # Create child node object if isn't already done
1055        # ($child_entry have to be the class name then...)
1056        if(!ref($child_entry)) {
1057          $child_entry = $self->createNode($child_entry);
1058          # it could be insert 'manually' or will be insert 'transparent' if parent will be updated
1059          #$core->insert($child_entry);
1060          #print "Create child object [$nodename]: " . Dumper($child_entry);
1061        }
1062    
1063        # get reference of tied node (seems, only on Linux node's are tied!!)
1064        my $tied_node = tied $parent->{$nodename};
1065    
1066        # insert/change child entry at parent
1067        #print "reference: " . ref($parent->{$nodename}) . "\n";
1068        if(ref($parent->{$nodename}) eq 'ARRAY') {
1069          # (seems, only on Linux node's are tied!!)
1070          if($tied_node) {
1071            # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
1072            # so a 'PUSH' is not implemented (which could be then done transparently)
1073            my $array = $tied_node->FETCH;
1074            push @$array, $child_entry;
1075            $tied_node->STORE($array);
1076            # node will be normaly untied at 'STORE'
1077            if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1078            else { undef $tied_node; }
1079          } else {
1080            push @{$parent->{$nodename}}, $child_entry;
1081          }
1082        }
1083        elsif(ref($parent->{$nodename}) eq 'HASH') {
1084          if(my $key = $query_args->{hash_key}) {
1085            # (seems, only on Linux node's are tied!!)
1086            if($tied_node) {
1087              # same problem as with 'ARRAY':
1088              # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type.
1089              my $hash = $tied_node->FETCH;
1090              $hash->{$key} = $child_entry;
1091              $tied_node->STORE($hash);
1092              # node will be normaly untied at 'STORE'
1093              if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1094              else { undef $tied_node; }
1095            } else {
1096              $parent->{$nodename}->{$key} = $child_entry;
1097            }
1098          } else {
1099           print "ERROR: No HASH KEY given, so not able to insert hash entry!";
1100          }
1101        }
1102        else {
1103          $parent->{$nodename} = $child_entry;
1104        }
1105    
1106        # debug
1107        #print "Parent_new: " . Dumper($parent);
1108        
1109        # save parent
1110        $core->update($parent);
1111        
1112        # debug
1113        #print "Saved Parent: ". Dumper($parent);
1114        
1115        return $child_entry;
1116      }
1117    
1118    
1119  1;  1;
1120    __END__

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.43

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