/[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.19 by joko, Mon Dec 16 06:46:09 2002 UTC revision 1.46 by jonen, Thu May 6 12:54:34 2004 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.46  2004/05/06 12:54:34  jonen
7    #  + bugfix related to multiple select-'filter'
8    #
9    #  Revision 1.45  2003/12/14 01:48:36  jonen
10    #  small HACK at _insertChildNode: some special Childnodes should not be created because existing objects have to be selected!
11    #  TODO: make this more generic, e.g. implement a special flag at Schema
12    #
13    #  Revision 1.44  2003/12/04 01:01:50  joko
14    #  + sendQuery now returns result even on crud=UPDATE
15    #
16    #  Revision 1.43  2003/07/02 11:07:12  jonen
17    #  re-activate filtering of results *after* results are fetched from tangram
18    #    (needed for e.g. UserManagment)
19    #
20    #  Revision 1.42  2003/07/01 23:24:17  joko
21    #  now using package before using function
22    #
23    #  Revision 1.41  2003/06/29 02:03:45  joko
24    #  fix:? initialize schema on startup
25    #
26    #  Revision 1.40  2003/06/25 22:57:54  joko
27    #  major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting"
28    #
29    #  Revision 1.39  2003/06/06 11:40:40  jonen
30    #  fixed bug at 'getFilteredList'
31    #
32    #  Revision 1.38  2003/05/13 16:38:38  joko
33    #  problems with "tied" on 5.6.1/win32
34    #
35    #  Revision 1.37  2003/05/10 17:37:39  jonen
36    #  corrected last commit
37    #
38    #  Revision 1.36  2003/05/10 17:31:18  jonen
39    #  + added new functions related to 'create' item
40    #   - createNode()
41    #     # creates non-persistent 'deep dummy filled' object
42    #   - insertChildNode()
43    #     # inserts child node at given parent (child node haven't to exists,
44    #         createNode will be injected transparently)
45    #
46    #  Revision 1.35  2003/04/19 16:09:48  jonen
47    #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
48    #
49    #  Revision 1.34  2003/04/11 01:18:53  joko
50    #  sendQuery:
51    #  + introduced crud action 'DELETE'
52    #
53    #  Revision 1.33  2003/04/09 06:07:43  joko
54    #  revamped 'sub sendQuery'
55    #
56    #  Revision 1.32  2003/04/08 22:52:22  joko
57    #  modified 'querySchema': better behaviour regarding filtering result
58    #
59    #  Revision 1.31  2003/04/05 21:24:09  joko
60    #  modified 'sub getChildNodes': now contains code from 'querySchema'
61    #
62    #  Revision 1.30  2003/03/27 15:31:14  joko
63    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
64    #
65    #  Revision 1.29  2003/02/21 01:47:18  joko
66    #  purged old code
67    #  minor cosmetics
68    #
69    #  Revision 1.28  2003/02/20 20:20:26  joko
70    #  tried to get auto-disconnect working again - failed with that
71    #
72    #  Revision 1.27  2003/01/31 06:30:59  joko
73    #  + enabled 'sendQuery'
74    #
75    #  Revision 1.26  2003/01/30 22:29:47  joko
76    #  + fixed module usage (removed dependency on 'libp.pm')
77    #
78    #  Revision 1.25  2003/01/19 02:30:05  joko
79    #  + fix: modified call to '_initSchema'
80    #
81    #  Revision 1.24  2002/12/22 14:13:01  joko
82    #  + sub dropDb
83    #
84    #  Revision 1.23  2002/12/19 16:31:53  joko
85    #  +- renamed sub to 'rebuildDb'
86    #
87    #  Revision 1.22  2002/12/18 22:28:16  jonen
88    #  + added extended logging at 'getObjectByGuid()'
89    #
90    #  Revision 1.21  2002/12/16 22:20:49  jonen
91    #  + fixed bug at 'getObjectByGuid()'
92    #
93    #  Revision 1.20  2002/12/16 20:49:17  jonen
94    #  + added sub 'getObjectByGuid()'
95    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
96    #
97  #  Revision 1.19  2002/12/16 06:46:09  joko  #  Revision 1.19  2002/12/16 06:46:09  joko
98  #  + attempt to introduce a generic '_patchSchema' - cancelled!  #  + attempt to introduce a generic '_patchSchema' - cancelled!
99  #  #
# Line 16  Line 107 
107  #  + fix: encapsulated object-loading inside an 'eval'  #  + fix: encapsulated object-loading inside an 'eval'
108  #  #
109  #  Revision 1.15  2002/12/05 13:55:21  joko  #  Revision 1.15  2002/12/05 13:55:21  joko
110  #  + now utilizing 'object2hash' instead of 'var_deref'  #  + now utilizing 'expand' instead of 'var_deref'
111  #  + played around with having fresh-objects - no progress....  #  + played around with having fresh-objects - no progress....
112  #  #
113  #  Revision 1.14  2002/12/05 09:40:30  jonen  #  Revision 1.14  2002/12/05 09:40:30  jonen
# Line 89  use warnings; Line 180  use warnings;
180  use base ("Data::Storage::Handler");  use base ("Data::Storage::Handler");
181  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
182    
183  use Tangram;  
184  use Data::Dumper;  use Data::Dumper;
185  use libp qw( getNewPerlObjectByPkgName );  use Tangram;
186  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 );  
187    
188    use DesignPattern::Object;
189    use Data::Storage::Result::Tangram;
190    use Data::Mungle::Compare::Struct qw( isEmpty );
191    use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
192    
193  # get logger instance  # get logger instance
194  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
195    
196    #$Tangram::TRACE = *STDOUT;
197    
198  # this holds the complete instantiated schema from tangram  # this holds the complete instantiated schema from tangram
199  my $schema_tangram;  my $schema_tangram;
# Line 117  sub _initSchema { Line 210  sub _initSchema {
210    my $self = shift;    my $self = shift;
211    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
212    #if (!$schema_tangram) {    #if (!$schema_tangram) {
213      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} } );
214      $schema_tangram = $obj->getSchema();      my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
215        $schema_tangram = $obj->getSchema() if $obj;
216    #}    #}
217    if (!$schema_tangram) {    if (!$schema_tangram) {
218      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." );
219      return 0;      return 0;
220    }    }
221    #$self->_patchSchema();    #$self->_patchSchema();
# Line 150  sub connect { Line 244  sub connect {
244      my $self = shift;      my $self = shift;
245            
246      my $dsn = shift;      my $dsn = shift;
247    
248    #print Dumper($self);
249    #exit;
250    
251        # TODO: re-enable
252      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
253      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
254            
255      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 164  sub connect { Line 262  sub connect {
262  #    }  #    }
263    
264      return unless $self->_initSchema();      return unless $self->_initSchema();
265        #$self->_initSchema();
266    
267      # create the main tangram storage object      # create the main tangram storage object
268      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 190  sub connect { Line 289  sub connect {
289  sub getChildNodes {  sub getChildNodes {
290    
291    my $self = shift;    my $self = shift;
292    my @nodes;    my $mode = shift;
293      my $filter = shift;
294      
295      $mode ||= 'core';
296      $filter ||= 'all';
297      
298      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
299    
300    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    if ($mode eq 'core') {
301    
302    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE      my @nodes;
303    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });      
304    #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
305          #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
306    # todo: should we retrieve information from the schema here      #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
307    # rather than poorly getting table names from underlying dbi?      
308    my $storage = $self->_getSubLayerHandle();      # todo: should we retrieve information from the schema here
309    @nodes = @{$storage->getChildNodes()};      # rather than poorly getting table names from underlying dbi?
310    #$storage->_configureCOREHANDLE();      my $storage = $self->_getSubLayerHandle();
311  #print "getchildnodes\n";      @nodes = @{$storage->getChildNodes()};
312  #print Dumper($self);      #$storage->_configureCOREHANDLE();
313    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #print "getchildnodes\n";
314      #print Dumper($self);
315        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
316        
317        # TODO: REVIEW
318        #$storage->disconnect();
319        
320        $self->{meta}->{childnodes} = \@nodes;
321    
322        return \@nodes;
323        
324    # TODO: REVIEW    } elsif ($mode eq 'root') {
325    #$storage->disconnect();      
326        # FIXME: this will return *all* known classes to 'Class::Tangram',
327        # which might not be what you expect since more than one instance
328        # of Tangram may be in memory and Class::Tangram seems to
329        # offer no methods to determine this or filter its result(s) according
330        # to a specific database.
331        my @object_names = Class::Tangram::known_classes();
332        my @concret_names;
333        my $o_cnt;
334        foreach (sort @object_names) {
335          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
336          $o_cnt++;
337        }
338    
339        if ($filter eq 'all') {
340          return \@object_names;
341        } elsif ($filter eq 'concrete') {
342          return \@concret_names;
343        }
344        
345    $self->{meta}->{childnodes} = \@nodes;    }
346        
   return \@nodes;  
347    
348  }  }
349    
   
350  sub testIntegrity {  sub testIntegrity {
351    
352    my $self = shift;    my $self = shift;
# Line 290  sub configureCOREHANDLE { Line 420  sub configureCOREHANDLE {
420    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
421    
422    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
423      #print Dumper($self);
424      #exit;
425    
426    # apply configured modifications    # apply configured modifications
427      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
# Line 355  sub retreatSchema { Line 487  sub retreatSchema {
487    return $ok;    return $ok;
488  }  }
489    
490  sub rebuildDbAndSchema {  sub rebuildDb {
491    my $self = shift;    my $self = shift;
492    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
493    my @results;    my @results;
494    
495    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 392  sub getListFiltered { Line 524  sub getListFiltered {
524    # redirect to unfiltered mode    # redirect to unfiltered mode
525    #return $self->getListUnfiltered(@_);    #return $self->getListUnfiltered(@_);
526    
527    my $nodename = shift;    my $in = {};
528    my $filters = shift;    $in->{nodename} = shift;
529      $in->{filters} = shift;
530      $in->{sorting} = shift;
531      
532    my @results;    my @results;
533    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" );
534    
535      #print "Filter_payload: " . Dumper($in->{filters}) . "\n";
536    
537    #print Dumper($filters);    # 1. "Remote Object Handle" - get set of objects from odbms by object name
538      my $remote = $self->{_COREHANDLE}->remote($in->{nodename});
539      
540      # 2. Transfer $in to $orm_query
541      my $orm_query = {};
542        
543      # 2.a. Filters
544    my @tfilters;    my @tfilters;
545      my $orm_filter = undef;
546        
547    foreach my $filter (@$filters) {    foreach my $filter (@{$in->{filters}}) {
548        
549      # get filter - TODO: for each filter      # get filter - TODO: for each filter
550      #my $filter = $filters->[0];      #my $filter = $filters->[0];
551      
552      # build filter      # 1. The parts of a filter source entry
553      my $lexpr = $filter->{key};      my $lexpr = $filter->{key};
554      #my $op = $filter->{op};      #my $op = $filter->{op};
555      my $op = '=';      my $op = '=';
556      my $rexpr = $filter->{val};      my $rexpr = $filter->{val};
557      my $tight = 100;      my $tight = 100;
558    
559        # 2. Build filter target entry
560            
561    #  my $tfilter = Tangram::Filter->new(      # Test 1 - didn't work out!
562    #    expr => "t1.$lexpr $op '$rexpr'",      #  my $tfilter = Tangram::Filter->new(
563    #    tight => $tight,      #    expr => "t1.$lexpr $op '$rexpr'",
564    #    objects => $objects,      #    tight => $tight,
565    #  );      #    objects => $objects,
566        #  );
567    
568        my $orm_filter_tmp = undef;
569        # was:
570        # TODO: is_op?
571        # dispatch un-common operators if exists
572        if ($filter->{op} eq "ref") {
573          # do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query
574          #print "Filter->op eq 'ref'.\n";
575          #push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";
576        } else {
577          # HACK: build eval-string (sorry) to get filtered list - please give advice here
578          #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
579          # better: calculate expression right here!
580          #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
581          
582          #print "key: ", $filter->{key}, "\n";
583          
584          my $left = $remote->{$filter->{key}};
585          #my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" );
586          my $right = $filter->{val};
587          my $op = $filter->{op};
588          $orm_filter_tmp = $left->$op($right);
589        }
590    
591        if (!ref($orm_filter)) {
592          $orm_filter = $orm_filter_tmp;
593        } else {
594          $orm_filter = $orm_filter->and($orm_filter_tmp);
595        }
596    
597      }
598    
599      $orm_query->{filter} = $orm_filter;
600        
601      # HACK: build eval-string (sorry) to get filtered list - please give advice here    # debug point:
602      push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";    #print "Filter: " . Dumper($orm_query->{filter}) . "\n";
603    
604      # was:
605    
606      # 2.b. sorting [new as of 2003-06-17]
607      if ($in->{sorting}) {
608        my $sorting_rules = [];
609        my $sorting_order = 'ASC';
610        foreach my $sorting_column (keys %{$in->{sorting}}) {
611          $sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column};
612          push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column );
613        }
614        $orm_query->{order} = $sorting_rules;
615        if ($sorting_order eq 'DESC') {
616          $orm_query->{desc} = 1;
617        }
618    }    }
619    
   my $tfilter = join(' & ', @tfilters);  
620    
621    # get set of objects from odbms by object name    # 3. build and issue query - return result as Tangram::Cursor
   my $remote = $self->{_COREHANDLE}->remote($nodename);  
622        
623    # was:    # coerce hash into array (This is valid in Perl)
624    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);    my @orm_query_args = %$orm_query;
625      # send query (arguments) to orm
626      @results = $self->{_COREHANDLE}->select($remote, @orm_query_args);
627      
628      #my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args);
629      #return $cursor;
630      #print Dumper(@results);
631    
632    # is:    # is:
633    # 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
634    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';      #my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');';
635          #print "eval: $evalstring", "\n";
636    # get filtered list/set      # get filtered list/set
637    @results = eval($evalstring);      #@results = eval($evalstring);
638    die $@ if $@;      #die $@ if $@;
639    
640      # filter results - NEEDED for e.g. UserManagment !!
641      if ($in->{filters}->[0]->{op} && ($in->{filters}->[0]->{op} eq "ref")) {
642          #print "Filter->op eq 'ref'.\n";
643          my $att_name = $in->{filters}->[0]->{key};
644          my $att_val = $in->{filters}->[0]->{val};
645          my @filtered;
646          foreach(@results) {
647            if(ref($_->{$att_name}) eq $att_val) {
648              push @filtered, $_;
649            }
650          }
651          @results = @filtered;
652      }
653    
654      #print "results: " . Dumper(\@results);
655        
656    return \@results;    return \@results;
657  }  }
# Line 468  sub createSet { Line 681  sub createSet {
681    return $result;    return $result;
682  }  }
683    
684    sub createResult {
685      my $self = shift;
686      my $rh = shift;
687      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
688      return $result;
689    }
690    
691  sub sendQuery {  sub sendQuery {
692    my $self = shift;    my $self = shift;
693    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);  
694    
695    #print Dumper($query);    #print Dumper($query);
696    
697    # HACK: special case: querying by id does not translate into a common tangram query    # type = ITEM|LIST|TRANSPARENT
698    # just load the object by given id(ent)    my $type = '';
699    if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {    # mode = OID|SPECIAL
700      #print "LOAD!!!", "\n";    my $mode = '';
701      #exit;    my $ident = '';
702      #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );    my $crud = '';
703      my $ident = $query->{criterias}->[0]->{val};    
704  #print "load obj", "\n";    
705      #return $self->createSet() if $ident == 5;    # dispatch type and mode
706      $self->{_COREHANDLE}->unload($ident);    
707      my $object = $self->{_COREHANDLE}->load($ident);      # defaults - 1
708  #print "get id", "\n";      if ($query->{options}) {
709      my $oid = $self->{_COREHANDLE}->id($object);        $crud = $query->{options}->{crud};
710  #print Dumper($object);        $crud ||= $query->{options}->{action};
711  #print "oid: $oid", "\n";      }
712      return $self->createSet($object);  
713      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      # defaults - 2
714    }      $type ||= 'TRANSPARENT';
715        $crud ||= 'RETRIEVE';
716    die("This should not be reached for now - redirect to \$self->getListFiltered() here!");  
717        if ($query->{options}->{OID}) {
718    # TODO: do a common tangram query here        $type = 'ITEM';
719          $mode = 'OID';
720    my @crits;        $ident = $query->{options}->{OID};
721    foreach (@{$query->{criterias}}) {      
722      my $op = '';      } elsif (my $guid = $query->{options}->{GUID}) {
723      $op = '=' if lc $_->{op} eq 'eq';        $type = 'TRANSPARENT';
724      push @crits, "$_->{key}$op'$_->{val}'";        $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
725    }  
726    my $subnodes = {};      # if operator is different (dispatcher for 'getListFiltered')
727    map { $subnodes->{$_}++ } @{$query->{subnodes}};      } elsif (my $op = $query->{options}->{op}) {
728    # HACK: this is hardcoded ;(    expand possibilities!        $type = 'TRANSPARENT';
729    #my $crit = join(' AND ', @crits);        $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
730    #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);  
731    #return $self->sendCommand($sql);      }
732    #my $h = $self->{COREHANDLE}->remote($query->{node});    
733    #my $res = $self->{COREHANDLE}->select($h, $h->{);      # HACK: special case: querying by id does not translate into a common tangram query
734    return $self->createCursor($query->{node});      # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
735        if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
736          $type = 'ITEM';
737          $mode = 'SPECIAL.SYNC';
738          $ident = $query->{criterias}->[0]->{val};
739        }
740      
741    
742      # execute query
743      my $result;
744    
745      if ($type eq 'ITEM' && $ident) {
746    
747        if ($mode eq 'OID') {    
748          # TODO: review this case!
749          $result = $self->getObject($ident, $query->{options});
750        
751        } elsif ($mode eq 'SPECIAL.SYNC') {
752    
753          # V1 - failed
754          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
755      
756          # hmm....
757          #return $self->createSet() if $ident == 5;
758          
759          # Unload single object before doing any further operations to
760          # expect a "fresh" object from orm when performing the next calls.
761          $self->{_COREHANDLE}->unload($ident);
762          
763          # Load object from orm.
764          my $object = $self->{_COREHANDLE}->load($ident);
765      
766          # determine object identifier (OID)
767          my $oid = $self->{_COREHANDLE}->id($object);
768          
769          # encapsulate into result/response container and return this one
770          $result = $self->createSet($object);
771          
772          # debugging
773          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
774    
775        }
776        
777      
778      } elsif ($type eq 'TRANSPARENT') {
779    
780        if ($crud eq 'RETRIEVE') {
781    
782          my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting});
783          #return $list;
784          return $self->createResult($list);
785          
786          #return $self->createSet($object);
787          #return $self->createSet($list);
788          return $self->createSet(@$list);
789        
790          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
791        
792          # try a generic tangram query here
793          # TODO: try to place an oql on top of that (search.cpan.org!)
794          my @crits;
795          foreach (@{$query->{criterias}}) {
796            my $op = '';
797            $op = '=' if lc $_->{op} eq 'eq';
798            push @crits, "$_->{key}$op'$_->{val}'";
799          }
800          my $subnodes = {};
801          map { $subnodes->{$_}++ } @{$query->{subnodes}};
802          # HACK: this is hardcoded ;(    expand possibilities!
803          #my $crit = join(' AND ', @crits);
804          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
805          #return $self->sendCommand($sql);
806          #my $h = $self->{COREHANDLE}->remote($query->{node});
807          #my $res = $self->{COREHANDLE}->select($h, $h->{);
808          $result = $self->createCursor($query->{node});
809    
810        } elsif ($crud eq 'UPDATE') {
811    
812          # Patch current query to be a loader (e.g. change action, remove payload) ...
813          my $childquery = deep_copy($query);
814          $childquery->{options}->{crud} = 'RETRIEVE';
815          delete $childquery->{payload};
816    
817          # ... to use it to fetch a fresh object using ourselves (sendQuery).
818          my $cursor = $self->sendQuery($childquery);
819          my $status = $cursor->getStatus();
820          my $object = $cursor->getNextEntry();
821    
822          # Merge values and apply value modifiers.
823          my $options = { utf8 => 1, php => 1 };
824          merge_to($object, $query->{payload}, $options);
825    
826          #print Dumper($object);
827          
828          # Execute update operation at orm.
829          $self->update($object);
830          $result = $self->createResult([ $object ]);
831        
832        } elsif ($crud eq 'DELETE') {
833    
834          # Patch current query to be a loader (e.g. change action) ...
835          my $childquery = deep_copy($query);
836          $childquery->{options}->{crud} = 'RETRIEVE';
837    
838          # ... to use it to fetch a fresh object using ourselves (sendQuery).
839          my $cursor = $self->sendQuery($childquery);
840          my $status = $cursor->getStatus();
841          my $object = $cursor->getNextEntry();
842    
843          $self->erase($object);
844          $self->unload($object);
845    
846        } elsif ($crud eq 'CREATE') {
847          
848          my $nodename = $query->{node};      
849          my $newnode = $self->createNode($nodename);
850          my $id = $self->{_COREHANDLE}->insert($newnode);
851      
852          print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n";
853          
854          return $newnode;
855    
856        }
857    
858      }
859    
860      return $result;
861    
862  }  }
863    
864  sub eraseAll {  sub eraseAll {
# Line 549  sub getObject { Line 894  sub getObject {
894    return $object if $object;    return $object if $object;
895  }  }
896    
897  sub getObjectAsHash {  sub getObjectByGuid_old {
898    my $self = shift;    my $self = shift;
899    my $oid = shift;    my $guid = shift;
900    my $options = shift;    my $options = shift;
901    my $obj = $self->getObject($oid, $options);    
902      # Guid and Classname is needed
903      if(!$guid || !$options->{classname}) {
904       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
905        return;
906      }
907      
908      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
909      # unload($oid) will only unload object, not deep object hashes
910      $self->{_COREHANDLE}->unload() if ($options->{destroy});
911    
912      # search for object with given Classname and Guid
913      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
914      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
915      
916      # we searched for global unique identifer of some object,
917      # so I think we can trust there would be only one result
918      if($result[0]) {
919        return $result[0];
920      } else {
921        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
922        return;
923      }
924      
925    }
926    
927    sub getObjectAsHash_old {
928      my $self = shift;
929      my $oid = shift;
930      my $options = shift;  
931      my $obj;
932      
933      if($options->{guid}) {
934        $obj = $self->getObjectByGuid($oid, $options);
935      } else {
936        $obj = $self->getObject($oid, $options);
937      }
938        
939    # build options (a callback to unload autovivified objects) for 'expand'    # build options (a callback to unload autovivified objects) for 'expand'
940    # TODO: use $logger to write to debug here!    # TODO: use $logger to write to debug here!
941    my $cb; # = sub {};    my $cb; # = sub {};
942    
943      # deactivated way to get rid of used instances, if requested
944  =pod  =pod
945    if ($options->{destroy}) {      if ($options->{destroy}) {
946      $options->{cb}->{destroy} = sub {        $options->{cb}->{destroy} = sub {
947        print "================ DESTROY", "\n";          print "================ DESTROY", "\n";
948        my $object = shift;          my $object = shift;
949        #print Dumper($object);          #print Dumper($object);
950        $self->{_COREHANDLE}->unload($object);          $self->{_COREHANDLE}->unload($object);
951        #undef($object);          #undef($object);
952      };        };
953    }      }
954  =cut  =cut
955    
956    my $hash = object2hash($obj, $options);    my $hash = expand($obj, $options);
957    #$options->{cb}->{destroy}->($obj);  
958    #$self->{_COREHANDLE}->unload($obj);    # old (unsuccessful) attempts to get rid of used instances, if requested
959      
960    # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML      # V1:
961    # now done in object2hash      #$options->{cb}->{destroy}->($obj);
962    #var2utf8($hash) if ($options->{utf8});      #$self->{_COREHANDLE}->unload($obj);
963        
964    # old (wrong) attempts to get rid of used instances, if requested      # V2:
965      #$obj->clear_refs;      #$obj->clear_refs;
966      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
967      #$obj->DESTROY;      #$obj->DESTROY;
# Line 596  sub getCOREHANDLE { Line 979  sub getCOREHANDLE {
979    return $self->{_COREHANDLE};    return $self->{_COREHANDLE};
980  }  }
981    
982    sub dropDb {
983      my $self = shift;
984      my $storage = $self->_getSubLayerHandle();
985      return $storage->dropDb();
986    }
987    
988    sub testAvailability {
989      my $self = shift;
990      my $storage = $self->_getSubLayerHandle();
991      return $storage->testAvailability();
992    }
993    
994    sub disconnect2 {
995      my $self = shift;
996      my $storage = $self->_getSubLayerHandle();
997      print "DISC!", "\n";
998    
999      my $storage_ll = $storage->{_COREHANDLE};
1000      $storage_ll->disconnect();
1001      
1002      print Dumper($storage);
1003      exit;
1004      
1005      #$self->{_COREHANDLE}
1006      #$storage->disconnect();
1007      $self->{dataStorageLayer}->disconnect();
1008    }
1009    
1010    
1011    sub createNode {
1012      my $self = shift;
1013      my $classname = shift;
1014    
1015      my $obj = $classname->new();
1016      
1017      my $attr_options = Class::Tangram::attribute_options($classname);
1018      #print "Attribute Options: " . Dumper($attr_options);
1019    
1020      my $attr_types = Class::Tangram::attribute_types($classname);
1021      #print "Attribute Types: " . Dumper($attr_types);
1022      
1023      foreach(keys %{$attr_types}) {
1024        if($attr_types->{$_} eq 'string') {
1025          $obj->{$_} = '';
1026        } elsif($attr_types->{$_} eq 'int') {
1027          $obj->{$_} = 0;
1028        } elsif($attr_types->{$_} eq 'real') {
1029          $obj->{$_} = 0;
1030        } elsif($attr_types->{$_} eq 'rawdatetime') {
1031          $obj->{$_} = '0000-00-00 00:00:00';
1032        } elsif($attr_types->{$_} eq 'ref') {
1033          if($attr_options->{$_}->{class}) {
1034            # HACK!!!
1035            # STANDALONE Objects (objects which make sense to instanciat alone) should not created automaticly
1036            # because they maybe exists and should only be SETTED not CREATED!
1037            # TODO: Create a flag at the scheme for that reason!
1038            #   (e.g child_node => 1 for child-nodes only like e.g. UserData)
1039            if($attr_options->{$_}->{class} eq 'NetPerson' || $attr_options->{$_}->{class} eq 'Event') {
1040              #$obj->{$_} = undef();
1041            } else {
1042              $obj->{$_} = $self->createNode($attr_options->{$_}->{class});
1043            }
1044          } else {
1045            #$obj->{$_} = undef();
1046          }
1047        } elsif($attr_types->{$_} eq 'iarray') {
1048            $obj->{$_} = [ ];
1049        } elsif($attr_types->{$_} eq 'hash') {
1050            $obj->{$_} = {  };
1051        } elsif($attr_types->{$_} eq 'flat_hash') {
1052            $obj->{$_} = { };
1053        }
1054      }
1055      
1056      #print "New Object: " . Dumper($obj);
1057      
1058      return $obj;
1059    }
1060    
1061    
1062      sub insertChildNode {
1063        my $self = shift;
1064        my $child_entry = shift;
1065        my $query_args = shift;
1066        
1067        my $core = $self->{_COREHANDLE};
1068        my $nodename = $query_args->{nodename};
1069        
1070        # get parent object
1071        my $query = {
1072              node => $query_args->{parent}->{nodename},
1073              options => { GUID => $query_args->{parent}->{guid}, },
1074              };
1075        my $cursor = $self->sendQuery($query);
1076        my $parent = $cursor->getNextEntry();
1077    
1078        # debug
1079        #print "Parent_org: " . Dumper($parent);
1080            
1081        # Create child node object if isn't already done
1082        # ($child_entry have to be the class name then...)
1083        if(!ref($child_entry)) {
1084          $child_entry = $self->createNode($child_entry);
1085          # it could be insert 'manually' or will be insert 'transparent' if parent will be updated
1086          #$core->insert($child_entry);
1087          #print "Create child object [$nodename]: " . Dumper($child_entry);
1088        }
1089    
1090        # get reference of tied node (seems, only on Linux node's are tied!!)
1091        my $tied_node = tied $parent->{$nodename};
1092    
1093        # insert/change child entry at parent
1094        #print "reference: " . ref($parent->{$nodename}) . "\n";
1095        if(ref($parent->{$nodename}) eq 'ARRAY') {
1096          # (seems, only on Linux node's are tied!!)
1097          if($tied_node) {
1098            # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
1099            # so a 'PUSH' is not implemented (which could be then done transparently)
1100            my $array = $tied_node->FETCH;
1101            push @$array, $child_entry;
1102            $tied_node->STORE($array);
1103            # node will be normaly untied at 'STORE'
1104            if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1105            else { undef $tied_node; }
1106          } else {
1107            push @{$parent->{$nodename}}, $child_entry;
1108          }
1109        }
1110        elsif(ref($parent->{$nodename}) eq 'HASH') {
1111          if(my $key = $query_args->{hash_key}) {
1112            # (seems, only on Linux node's are tied!!)
1113            if($tied_node) {
1114              # same problem as with 'ARRAY':
1115              # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type.
1116              my $hash = $tied_node->FETCH;
1117              $hash->{$key} = $child_entry;
1118              $tied_node->STORE($hash);
1119              # node will be normaly untied at 'STORE'
1120              if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1121              else { undef $tied_node; }
1122            } else {
1123              $parent->{$nodename}->{$key} = $child_entry;
1124            }
1125          } else {
1126           print "ERROR: No HASH KEY given, so not able to insert hash entry!";
1127          }
1128        }
1129        else {
1130          $parent->{$nodename} = $child_entry;
1131        }
1132    
1133        # debug
1134        #print "Parent_new: " . Dumper($parent);
1135        
1136        # save parent
1137        $core->update($parent);
1138        
1139        # debug
1140        #print "Saved Parent: ". Dumper($parent);
1141        
1142        return $child_entry;
1143      }
1144    
1145    
1146  1;  1;
1147    __END__

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.46

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