/[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.28 by joko, Thu Feb 20 20:20:26 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
25    #  + added new functions related to 'create' item
26    #   - createNode()
27    #     # 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
33    #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
34    #
35    #  Revision 1.34  2003/04/11 01:18:53  joko
36    #  sendQuery:
37    #  + introduced crud action 'DELETE'
38    #
39    #  Revision 1.33  2003/04/09 06:07:43  joko
40    #  revamped 'sub sendQuery'
41    #
42    #  Revision 1.32  2003/04/08 22:52:22  joko
43    #  modified 'querySchema': better behaviour regarding filtering result
44    #
45    #  Revision 1.31  2003/04/05 21:24:09  joko
46    #  modified 'sub getChildNodes': now contains code from 'querySchema'
47    #
48    #  Revision 1.30  2003/03/27 15:31:14  joko
49    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
50    #
51    #  Revision 1.29  2003/02/21 01:47:18  joko
52    #  purged old code
53    #  minor cosmetics
54    #
55  #  Revision 1.28  2003/02/20 20:20:26  joko  #  Revision 1.28  2003/02/20 20:20:26  joko
56  #  tried to get auto-disconnect working again - failed with that  #  tried to get auto-disconnect working again - failed with that
57  #  #
# Line 120  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;
176  use Data::Compare::Struct qw( isEmpty );  use Data::Mungle::Compare::Struct qw( isEmpty );
177  use Data::Transform::Deep qw( expand );  use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
 #use Data::Transform::Encode qw( var2utf8 );  
178    
179  # get logger instance  # get logger instance
180  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 148  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 197  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 225  sub connect { Line 274  sub connect {
274  sub getChildNodes {  sub getChildNodes {
275    
276    my $self = shift;    my $self = shift;
277    my @nodes;    my $mode = shift;
278      my $filter = shift;
279      
280      $mode ||= 'core';
281      $filter ||= 'all';
282      
283      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
284    
285    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    if ($mode eq 'core') {
286    
287    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE      my @nodes;
288    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });      
289    #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
290          #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
291    # todo: should we retrieve information from the schema here      #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
292    # rather than poorly getting table names from underlying dbi?      
293    my $storage = $self->_getSubLayerHandle();      # todo: should we retrieve information from the schema here
294    @nodes = @{$storage->getChildNodes()};      # rather than poorly getting table names from underlying dbi?
295    #$storage->_configureCOREHANDLE();      my $storage = $self->_getSubLayerHandle();
296  #print "getchildnodes\n";      @nodes = @{$storage->getChildNodes()};
297  #print Dumper($self);      #$storage->_configureCOREHANDLE();
298    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #print "getchildnodes\n";
299      #print Dumper($self);
300        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
301        
302        # TODO: REVIEW
303        #$storage->disconnect();
304        
305        $self->{meta}->{childnodes} = \@nodes;
306    
307        return \@nodes;
308        
309    # TODO: REVIEW    } elsif ($mode eq 'root') {
310    #$storage->disconnect();      
311        # FIXME: this will return *all* known classes to 'Class::Tangram',
312        # which might not be what you expect since more than one instance
313        # of Tangram may be in memory and Class::Tangram seems to
314        # offer no methods to determine this or filter its result(s) according
315        # to a specific database.
316        my @object_names = Class::Tangram::known_classes();
317        my @concret_names;
318        my $o_cnt;
319        foreach (sort @object_names) {
320          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
321          $o_cnt++;
322        }
323    
324        if ($filter eq 'all') {
325          return \@object_names;
326        } elsif ($filter eq 'concrete') {
327          return \@concret_names;
328        }
329        
330    $self->{meta}->{childnodes} = \@nodes;    }
331        
   return \@nodes;  
332    
333  }  }
334    
   
335  sub testIntegrity {  sub testIntegrity {
336    
337    my $self = shift;    my $self = shift;
# Line 429  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);    #print Dumper($filters);
521    
522      # 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      # HACK: build eval-string (sorry) to get filtered list - please give advice here  
553      push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";      my $orm_filter_tmp = undef;
554        # was:
555        # TODO: is_op?
556        # dispatch un-common operators if exists
557        if ($filter->{op} eq "ref") {
558          # 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 {
562          # HACK: build eval-string (sorry) to get filtered list - please give advice here
563          #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 507  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;
   #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);  
678    
679    #print Dumper($query);    #print Dumper($query);
680    
681    # HACK: special case: querying by id does not translate into a common tangram query    # type = ITEM|LIST|TRANSPARENT
682    # just load the object by given id(ent)    my $type = '';
683    if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {    # mode = OID|SPECIAL
684      #print "LOAD!!!", "\n";    my $mode = '';
685      #exit;    my $ident = '';
686      #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );    my $crud = '';
687      my $ident = $query->{criterias}->[0]->{val};    
688  #print "load obj", "\n";    
689      #return $self->createSet() if $ident == 5;    # dispatch type and mode
690      $self->{_COREHANDLE}->unload($ident);    
691      my $object = $self->{_COREHANDLE}->load($ident);      # defaults - 1
692  #print "get id", "\n";      if ($query->{options}) {
693      my $oid = $self->{_COREHANDLE}->id($object);        $crud = $query->{options}->{crud};
694  #print Dumper($object);        $crud ||= $query->{options}->{action};
695  #print "oid: $oid", "\n";      }
696      return $self->createSet($object);  
697      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      # defaults - 2
698    }      $type ||= 'TRANSPARENT';
699        $crud ||= 'RETRIEVE';
700    my $list = $self->getListFiltered($query->{node}, $query->{criterias});  
701    #return $self->createSet($object);      if ($query->{options}->{OID}) {
702    #return $self->createSet($list);        $type = 'ITEM';
703    return $self->createSet(@$list);        $mode = 'OID';
704          $ident = $query->{options}->{OID};
705    #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");      
706        } elsif (my $guid = $query->{options}->{GUID}) {
707          $type = 'TRANSPARENT';
708          $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
709    
710        # if operator is different (dispatcher for 'getListFiltered')
711        } elsif (my $op = $query->{options}->{op}) {
712          $type = 'TRANSPARENT';
713          $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
714    
715        }
716      
717        # HACK: special case: querying by id does not translate into a common tangram query
718        # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
719        if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
720          $type = 'ITEM';
721          $mode = 'SPECIAL.SYNC';
722          $ident = $query->{criterias}->[0]->{val};
723        }
724      
725    
726      # execute query
727      my $result;
728    
729      if ($type eq 'ITEM' && $ident) {
730    
731        if ($mode eq 'OID') {    
732          # TODO: review this case!
733          $result = $self->getObject($ident, $query->{options});
734        
735        } elsif ($mode eq 'SPECIAL.SYNC') {
736    
737          # V1 - failed
738          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
739      
740          # hmm....
741          #return $self->createSet() if $ident == 5;
742          
743          # Unload single object before doing any further operations to
744          # expect a "fresh" object from orm when performing the next calls.
745          $self->{_COREHANDLE}->unload($ident);
746          
747          # Load object from orm.
748          my $object = $self->{_COREHANDLE}->load($ident);
749      
750          # determine object identifier (OID)
751          my $oid = $self->{_COREHANDLE}->id($object);
752          
753          # encapsulate into result/response container and return this one
754          $result = $self->createSet($object);
755          
756          # debugging
757          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
758    
759        }
760        
761      
762      } elsif ($type eq 'TRANSPARENT') {
763    
764        if ($crud eq 'RETRIEVE') {
765    
766          my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting});
767          #return $list;
768          return $self->createResult($list);
769          
770          #return $self->createSet($object);
771          #return $self->createSet($list);
772          return $self->createSet(@$list);
773        
774          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
775        
776          # try a generic tangram query here
777          # TODO: try to place an oql on top of that (search.cpan.org!)
778          my @crits;
779          foreach (@{$query->{criterias}}) {
780            my $op = '';
781            $op = '=' if lc $_->{op} eq 'eq';
782            push @crits, "$_->{key}$op'$_->{val}'";
783          }
784          my $subnodes = {};
785          map { $subnodes->{$_}++ } @{$query->{subnodes}};
786          # HACK: this is hardcoded ;(    expand possibilities!
787          #my $crit = join(' AND ', @crits);
788          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
789          #return $self->sendCommand($sql);
790          #my $h = $self->{COREHANDLE}->remote($query->{node});
791          #my $res = $self->{COREHANDLE}->select($h, $h->{);
792          $result = $self->createCursor($query->{node});
793    
794        } elsif ($crud eq 'UPDATE') {
795    
796          # Patch current query to be a loader (e.g. change action, remove payload) ...
797          my $childquery = deep_copy($query);
798          $childquery->{options}->{crud} = 'RETRIEVE';
799          delete $childquery->{payload};
800    
801          # ... to use it to fetch a fresh object using ourselves (sendQuery).
802          my $cursor = $self->sendQuery($childquery);
803          my $status = $cursor->getStatus();
804          my $object = $cursor->getNextEntry();
805    
806          # Merge values and apply value modifiers.
807          my $options = { utf8 => 1, php => 1 };
808          merge_to($object, $query->{payload}, $options);
809    
810          # Execute update operation at orm.
811          $self->update($object);
812        
813        } elsif ($crud eq 'DELETE') {
814    
815          # Patch current query to be a loader (e.g. change action) ...
816          my $childquery = deep_copy($query);
817          $childquery->{options}->{crud} = 'RETRIEVE';
818    
819          # ... to use it to fetch a fresh object using ourselves (sendQuery).
820          my $cursor = $self->sendQuery($childquery);
821          my $status = $cursor->getStatus();
822          my $object = $cursor->getNextEntry();
823    
824          $self->erase($object);
825    
826        } elsif ($crud eq 'CREATE') {
827          
828          my $nodename = $query->{node};      
829          my $newnode = $self->createNode($nodename);
830          my $id = $self->{_COREHANDLE}->insert($newnode);
831      
832          print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n";
833          
834          return $newnode;
835    
836        }
837    
838      }
839    
840      return $result;
841    
   # try a generic tangram query here  
   # TODO: try to place an oql on top of that (search.cpan.org!)  
   my @crits;  
   foreach (@{$query->{criterias}}) {  
     my $op = '';  
     $op = '=' if lc $_->{op} eq 'eq';  
     push @crits, "$_->{key}$op'$_->{val}'";  
   }  
   my $subnodes = {};  
   map { $subnodes->{$_}++ } @{$query->{subnodes}};  
   # HACK: this is hardcoded ;(    expand possibilities!  
   #my $crit = join(' AND ', @crits);  
   #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);  
   #return $self->sendCommand($sql);  
   #my $h = $self->{COREHANDLE}->remote($query->{node});  
   #my $res = $self->{COREHANDLE}->select($h, $h->{);  
   return $self->createCursor($query->{node});  
842  }  }
843    
844  sub eraseAll {  sub eraseAll {
# Line 596  sub getObject { Line 874  sub getObject {
874    return $object if $object;    return $object if $object;
875  }  }
876    
877  sub getObjectByGuid {  sub getObjectByGuid_old {
878    my $self = shift;    my $self = shift;
879    my $guid = shift;    my $guid = shift;
880    my $options = shift;    my $options = shift;
# Line 626  sub getObjectByGuid { Line 904  sub getObjectByGuid {
904        
905  }  }
906    
907  sub getObjectAsHash {  sub getObjectAsHash_old {
908    my $self = shift;    my $self = shift;
909    my $oid = shift;    my $oid = shift;
910    my $options = shift;      my $options = shift;  
# Line 641  sub getObjectAsHash { Line 919  sub getObjectAsHash {
919    # build options (a callback to unload autovivified objects) for 'expand'    # build options (a callback to unload autovivified objects) for 'expand'
920    # TODO: use $logger to write to debug here!    # TODO: use $logger to write to debug here!
921    my $cb; # = sub {};    my $cb; # = sub {};
922    
923      # deactivated way to get rid of used instances, if requested
924  =pod  =pod
925    if ($options->{destroy}) {      if ($options->{destroy}) {
926      $options->{cb}->{destroy} = sub {        $options->{cb}->{destroy} = sub {
927        print "================ DESTROY", "\n";          print "================ DESTROY", "\n";
928        my $object = shift;          my $object = shift;
929        #print Dumper($object);          #print Dumper($object);
930        $self->{_COREHANDLE}->unload($object);          $self->{_COREHANDLE}->unload($object);
931        #undef($object);          #undef($object);
932      };        };
933    }      }
934  =cut  =cut
935    
936    my $hash = expand($obj, $options);    my $hash = expand($obj, $options);
937    #$options->{cb}->{destroy}->($obj);  
938    #$self->{_COREHANDLE}->unload($obj);    # old (unsuccessful) attempts to get rid of used instances, if requested
939      
940    # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML      # V1:
941    # now done in expand      #$options->{cb}->{destroy}->($obj);
942    #var2utf8($hash) if ($options->{utf8});      #$self->{_COREHANDLE}->unload($obj);
943        
944    # old (wrong) attempts to get rid of used instances, if requested      # V2:
945      #$obj->clear_refs;      #$obj->clear_refs;
946      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
947      #$obj->DESTROY;      #$obj->DESTROY;
# Line 707  sub disconnect2 { Line 987  sub disconnect2 {
987    $self->{dataStorageLayer}->disconnect();    $self->{dataStorageLayer}->disconnect();
988  }  }
989    
990    
991    sub createNode {
992      my $self = shift;
993      my $classname = shift;
994    
995      my $obj = $classname->new();
996      
997      my $attr_options = Class::Tangram::attribute_options($classname);
998      #print "Attribute Options: " . Dumper($attr_options);
999    
1000      my $attr_types = Class::Tangram::attribute_types($classname);
1001      #print "Attribute Types: " . Dumper($attr_types);
1002      
1003      foreach(keys %{$attr_types}) {
1004        if($attr_types->{$_} eq 'string') {
1005          $obj->{$_} = '';
1006        } elsif($attr_types->{$_} eq 'int') {
1007          $obj->{$_} = 0;
1008        } elsif($attr_types->{$_} eq 'real') {
1009          $obj->{$_} = 0;
1010        } elsif($attr_types->{$_} eq 'rawdatetime') {
1011          $obj->{$_} = '0000-00-00 00:00:00';
1012        } elsif($attr_types->{$_} eq 'ref') {
1013          if($attr_options->{$_}->{class}) {
1014            $obj->{$_} = $self->createNode($attr_options->{$_}->{class});
1015          } else {
1016            #$obj->{$_} = undef();
1017          }
1018        } elsif($attr_types->{$_} eq 'iarray') {
1019            $obj->{$_} = [ ];
1020        } elsif($attr_types->{$_} eq 'hash') {
1021            $obj->{$_} = {  };
1022        } elsif($attr_types->{$_} eq 'flat_hash') {
1023            $obj->{$_} = { };
1024        }
1025      }
1026      
1027      #print "New Object: " . Dumper($obj);
1028      
1029      return $obj;
1030    }
1031    
1032    
1033      sub insertChildNode {
1034        my $self = shift;
1035        my $child_entry = shift;
1036        my $query_args = shift;
1037        
1038        my $core = $self->{_COREHANDLE};
1039        my $nodename = $query_args->{nodename};
1040        
1041        # get parent object
1042        my $query = {
1043              node => $query_args->{parent}->{nodename},
1044              options => { GUID => $query_args->{parent}->{guid}, },
1045              };
1046        my $cursor = $self->sendQuery($query);
1047        my $parent = $cursor->getNextEntry();
1048    
1049        # debug
1050        #print "Parent_org: " . Dumper($parent);
1051            
1052        # Create child node object if isn't already done
1053        # ($child_entry have to be the class name then...)
1054        if(!ref($child_entry)) {
1055          $child_entry = $self->createNode($child_entry);
1056          # it could be insert 'manually' or will be insert 'transparent' if parent will be updated
1057          #$core->insert($child_entry);
1058          #print "Create child object [$nodename]: " . Dumper($child_entry);
1059        }
1060    
1061        # get reference of tied node (seems, only on Linux node's are tied!!)
1062        my $tied_node = tied $parent->{$nodename};
1063    
1064        # insert/change child entry at parent
1065        #print "reference: " . ref($parent->{$nodename}) . "\n";
1066        if(ref($parent->{$nodename}) eq 'ARRAY') {
1067          # (seems, only on Linux node's are tied!!)
1068          if($tied_node) {
1069            # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
1070            # so a 'PUSH' is not implemented (which could be then done transparently)
1071            my $array = $tied_node->FETCH;
1072            push @$array, $child_entry;
1073            $tied_node->STORE($array);
1074            # node will be normaly untied at 'STORE'
1075            if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1076            else { undef $tied_node; }
1077          } 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!";
1098          }
1099        }
1100        else {
1101          $parent->{$nodename} = $child_entry;
1102        }
1103    
1104        # debug
1105        #print "Parent_new: " . Dumper($parent);
1106        
1107        # save parent
1108        $core->update($parent);
1109        
1110        # debug
1111        #print "Saved Parent: ". Dumper($parent);
1112        
1113        return $child_entry;
1114      }
1115    
1116    
1117  1;  1;
1118  __END__  __END__

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

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