/[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.33 by joko, Wed Apr 9 06:07:43 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.33  2003/04/09 06:07:43  joko
7    #  revamped 'sub sendQuery'
8    #
9    #  Revision 1.32  2003/04/08 22:52:22  joko
10    #  modified 'querySchema': better behaviour regarding filtering result
11    #
12    #  Revision 1.31  2003/04/05 21:24:09  joko
13    #  modified 'sub getChildNodes': now contains code from 'querySchema'
14    #
15    #  Revision 1.30  2003/03/27 15:31:14  joko
16    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
17    #
18    #  Revision 1.29  2003/02/21 01:47:18  joko
19    #  purged old code
20    #  minor cosmetics
21    #
22    #  Revision 1.28  2003/02/20 20:20:26  joko
23    #  tried to get auto-disconnect working again - failed with that
24    #
25    #  Revision 1.27  2003/01/31 06:30:59  joko
26    #  + enabled 'sendQuery'
27    #
28    #  Revision 1.26  2003/01/30 22:29:47  joko
29    #  + fixed module usage (removed dependency on 'libp.pm')
30    #
31    #  Revision 1.25  2003/01/19 02:30:05  joko
32    #  + fix: modified call to '_initSchema'
33    #
34    #  Revision 1.24  2002/12/22 14:13:01  joko
35    #  + sub dropDb
36    #
37    #  Revision 1.23  2002/12/19 16:31:53  joko
38    #  +- renamed sub to 'rebuildDb'
39    #
40    #  Revision 1.22  2002/12/18 22:28:16  jonen
41    #  + added extended logging at 'getObjectByGuid()'
42    #
43    #  Revision 1.21  2002/12/16 22:20:49  jonen
44    #  + fixed bug at 'getObjectByGuid()'
45    #
46  #  Revision 1.20  2002/12/16 20:49:17  jonen  #  Revision 1.20  2002/12/16 20:49:17  jonen
47  #  + added sub 'getObjectByGuid()'  #  + added sub 'getObjectByGuid()'
48  #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'  #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
# Line 20  Line 60 
60  #  + fix: encapsulated object-loading inside an 'eval'  #  + fix: encapsulated object-loading inside an 'eval'
61  #  #
62  #  Revision 1.15  2002/12/05 13:55:21  joko  #  Revision 1.15  2002/12/05 13:55:21  joko
63  #  + now utilizing 'object2hash' instead of 'var_deref'  #  + now utilizing 'expand' instead of 'var_deref'
64  #  + played around with having fresh-objects - no progress....  #  + played around with having fresh-objects - no progress....
65  #  #
66  #  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 133  use warnings;
133  use base ("Data::Storage::Handler");  use base ("Data::Storage::Handler");
134  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
135    
136  use Tangram;  
137  use Data::Dumper;  use Data::Dumper;
138  use libp qw( getNewPerlObjectByPkgName );  use Tangram;
 use Data::Storage::Result::Tangram;  
 use Data::Compare::Struct qw( isEmpty );  
 use Data::Transform::Deep qw( object2hash );  
 use Data::Transform::Encode qw( var2utf8 );  
139    
140    use DesignPattern::Object;
141    use Data::Storage::Result::Tangram;
142    use Data::Mungle::Compare::Struct qw( isEmpty );
143    use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
144    
145  # get logger instance  # get logger instance
146  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 121  sub _initSchema { Line 161  sub _initSchema {
161    my $self = shift;    my $self = shift;
162    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
163    #if (!$schema_tangram) {    #if (!$schema_tangram) {
164      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} } );
165        my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
166      $schema_tangram = $obj->getSchema();      $schema_tangram = $obj->getSchema();
167    #}    #}
168    if (!$schema_tangram) {    if (!$schema_tangram) {
# Line 154  sub connect { Line 195  sub connect {
195      my $self = shift;      my $self = shift;
196            
197      my $dsn = shift;      my $dsn = shift;
198    
199    #print Dumper($self);
200    #exit;
201    
202        # TODO: re-enable
203      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
204      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
205            
206      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 167  sub connect { Line 212  sub connect {
212  #      return;  #      return;
213  #    }  #    }
214    
215      return unless $self->_initSchema();      #return unless $self->_initSchema();
216        $self->_initSchema();
217    
218      # create the main tangram storage object      # create the main tangram storage object
219      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 194  sub connect { Line 240  sub connect {
240  sub getChildNodes {  sub getChildNodes {
241    
242    my $self = shift;    my $self = shift;
243    my @nodes;    my $mode = shift;
244      my $filter = shift;
245      
246      $mode ||= 'core';
247      $filter ||= 'all';
248      
249      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
250    
251    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    if ($mode eq 'core') {
252    
253    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE      my @nodes;
254    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });      
255    #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
256          #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
257    # todo: should we retrieve information from the schema here      #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
258    # rather than poorly getting table names from underlying dbi?      
259    my $storage = $self->_getSubLayerHandle();      # todo: should we retrieve information from the schema here
260    @nodes = @{$storage->getChildNodes()};      # rather than poorly getting table names from underlying dbi?
261    #$storage->_configureCOREHANDLE();      my $storage = $self->_getSubLayerHandle();
262  #print "getchildnodes\n";      @nodes = @{$storage->getChildNodes()};
263  #print Dumper($self);      #$storage->_configureCOREHANDLE();
264    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #print "getchildnodes\n";
265      #print Dumper($self);
266        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
267        
268        # TODO: REVIEW
269        #$storage->disconnect();
270        
271        $self->{meta}->{childnodes} = \@nodes;
272    
273        return \@nodes;
274        
275    # TODO: REVIEW    } elsif ($mode eq 'root') {
276    #$storage->disconnect();      
277        # FIXME: this will return *all* known classes to 'Class::Tangram',
278        # which might not be what you expect since more than one instance
279        # of Tangram may be in memory and Class::Tangram seems to
280        # offer no methods to determine this or filter its result(s) according
281        # to a specific database.
282        my @object_names = Class::Tangram::known_classes();
283        my @concret_names;
284        my $o_cnt;
285        foreach (sort @object_names) {
286          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
287          $o_cnt++;
288        }
289    
290        if ($filter eq 'all') {
291          return \@object_names;
292        } elsif ($filter eq 'concrete') {
293          return \@concret_names;
294        }
295        
296    $self->{meta}->{childnodes} = \@nodes;    }
297        
   return \@nodes;  
298    
299  }  }
300    
   
301  sub testIntegrity {  sub testIntegrity {
302    
303    my $self = shift;    my $self = shift;
# Line 294  sub configureCOREHANDLE { Line 371  sub configureCOREHANDLE {
371    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
372    
373    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
374      #print Dumper($self);
375      #exit;
376    
377    # apply configured modifications    # apply configured modifications
378      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 438  sub retreatSchema {
438    return $ok;    return $ok;
439  }  }
440    
441  sub rebuildDbAndSchema {  sub rebuildDb {
442    my $self = shift;    my $self = shift;
443    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
444    my @results;    my @results;
445    
446    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 401  sub getListFiltered { Line 480  sub getListFiltered {
480    my @results;    my @results;
481    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
482    
483    #print Dumper($filters);  #print Dumper($filters);
484        
485    my @tfilters;    my @tfilters;
486        
# Line 440  sub getListFiltered { Line 519  sub getListFiltered {
519    # 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
520    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
521        
522      #print "eval: $evalstring", "\n";
523      
524    # get filtered list/set    # get filtered list/set
525    @results = eval($evalstring);    @results = eval($evalstring);
526    die $@ if $@;    die $@ if $@;
# Line 475  sub createSet { Line 556  sub createSet {
556  sub sendQuery {  sub sendQuery {
557    my $self = shift;    my $self = shift;
558    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);  
559    
560    #print Dumper($query);    #print Dumper($query);
561    
562    # HACK: special case: querying by id does not translate into a common tangram query    # type = ITEM|LIST|TRANSPARENT
563    # just load the object by given id(ent)    my $type = '';
564    if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {    # mode = OID|SPECIAL
565      #print "LOAD!!!", "\n";    my $mode = '';
566      #exit;    my $ident = '';
567      #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );    my $action = '';
568      my $ident = $query->{criterias}->[0]->{val};    
569  #print "load obj", "\n";    
570      #return $self->createSet() if $ident == 5;    # dispatch type and mode
571      $self->{_COREHANDLE}->unload($ident);    
572      my $object = $self->{_COREHANDLE}->load($ident);      # defaults - 1
573  #print "get id", "\n";      if ($query->{options}) {
574      my $oid = $self->{_COREHANDLE}->id($object);        $action = $query->{options}->{action};
575  #print Dumper($object);      }
576  #print "oid: $oid", "\n";  
577      return $self->createSet($object);      # defaults - 2
578      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      $type ||= 'TRANSPARENT';
579    }      $action ||= 'load';
580    
581    die("This should not be reached for now - redirect to \$self->getListFiltered() here!");      if ($query->{options}->{OID}) {
582          $type = 'ITEM';
583    # TODO: do a common tangram query here        $mode = 'OID';
584          $ident = $query->{options}->{OID};
585    my @crits;      
586    foreach (@{$query->{criterias}}) {      } elsif (my $guid = $query->{options}->{GUID}) {
587      my $op = '';        $type = 'TRANSPARENT';
588      $op = '=' if lc $_->{op} eq 'eq';        $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];
589      push @crits, "$_->{key}$op'$_->{val}'";      
590    }      }
591    my $subnodes = {};    
592    map { $subnodes->{$_}++ } @{$query->{subnodes}};      # HACK: special case: querying by id does not translate into a common tangram query
593    # HACK: this is hardcoded ;(    expand possibilities!      # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
594    #my $crit = join(' AND ', @crits);      if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
595    #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);        $type = 'ITEM';
596    #return $self->sendCommand($sql);        $mode = 'SPECIAL.SYNC';
597    #my $h = $self->{COREHANDLE}->remote($query->{node});        $ident = $query->{criterias}->[0]->{val};
598    #my $res = $self->{COREHANDLE}->select($h, $h->{);      }
599    return $self->createCursor($query->{node});    
600    
601      # execute query
602      my $result;
603    
604      if ($type eq 'ITEM' && $ident) {
605    
606        if ($mode eq 'OID') {    
607          $result = $self->getObject($ident, $query->{options});
608        
609        } elsif ($mode eq 'SPECIAL.SYNC') {
610    
611          # V1 - failed
612          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
613      
614          # hmm....
615          #return $self->createSet() if $ident == 5;
616          
617          # Unload single object before doing any further operations to
618          # expect a "fresh" object from orm when performing the next calls.
619          $self->{_COREHANDLE}->unload($ident);
620          
621          # Load object from orm.
622          my $object = $self->{_COREHANDLE}->load($ident);
623      
624          # determine object identifier (OID)
625          my $oid = $self->{_COREHANDLE}->id($object);
626          
627          # encapsulate into result/response container and return this one
628          $result = $self->createSet($object);
629          
630          # debugging
631          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
632    
633        }
634        
635      
636      } elsif ($type eq 'TRANSPARENT') {
637    
638        if ($action eq 'load') {
639    
640          my $list = $self->getListFiltered($query->{node}, $query->{criterias});
641          #return $self->createSet($object);
642          #return $self->createSet($list);
643          return $self->createSet(@$list);
644        
645          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
646        
647          # try a generic tangram query here
648          # TODO: try to place an oql on top of that (search.cpan.org!)
649          my @crits;
650          foreach (@{$query->{criterias}}) {
651            my $op = '';
652            $op = '=' if lc $_->{op} eq 'eq';
653            push @crits, "$_->{key}$op'$_->{val}'";
654          }
655          my $subnodes = {};
656          map { $subnodes->{$_}++ } @{$query->{subnodes}};
657          # HACK: this is hardcoded ;(    expand possibilities!
658          #my $crit = join(' AND ', @crits);
659          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
660          #return $self->sendCommand($sql);
661          #my $h = $self->{COREHANDLE}->remote($query->{node});
662          #my $res = $self->{COREHANDLE}->select($h, $h->{);
663          $result = $self->createCursor($query->{node});
664    
665        } elsif ($action eq 'save') {
666    
667          # Patch current query to be a loader (e.g. change action, remove payload) ...
668          my $childquery = deep_copy($query);
669          $childquery->{options}->{action} = 'load';
670          delete $childquery->{payload};
671    
672          # ... to use it to fetch fresh object using ourselves (sendQuery).
673          my $cursor = $self->sendQuery($childquery);
674          my $status = $cursor->getStatus();
675          my $object = $cursor->getNextEntry();
676    
677          # Merge values and apply value modifiers.
678          my $options = { utf8 => 1, php => 1 };
679          merge_to($object, $query->{payload}, $options);
680    
681          # Execute update operation at orm.
682          $self->update($object);
683        
684        }
685    
686      }
687    
688      return $result;
689    
690  }  }
691    
692  sub eraseAll {  sub eraseAll {
# Line 553  sub getObject { Line 722  sub getObject {
722    return $object if $object;    return $object if $object;
723  }  }
724    
725  sub getObjectByGuid {  sub getObjectByGuid_old {
726    my $self = shift;    my $self = shift;
727    my $guid = shift;    my $guid = shift;
728    my $options = shift;    my $options = shift;
729        
730    # Guid and Classname is needed    # Guid and Classname is needed
731    if(!$guid || !$options->{classname}) {    if(!$guid || !$options->{classname}) {
732       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
733      return;      return;
734    }    }
735        
# Line 568  sub getObjectByGuid { Line 738  sub getObjectByGuid {
738    $self->{_COREHANDLE}->unload() if ($options->{destroy});    $self->{_COREHANDLE}->unload() if ($options->{destroy});
739    
740    # search for object with given Classname and Guid    # search for object with given Classname and Guid
741    my $obj_tmp = $self->{_COREHANDLE}->remote($classname);    my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
742    my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);    my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
743        
744    # we searched for global unique identifer of some object,    # we searched for global unique identifer of some object,
745    # so it think we can trust there would be only one result    # so I think we can trust there would be only one result
746    if($result[0]) {    if($result[0]) {
747      return $result[0];      return $result[0];
748    } else {    } else {
749      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!" );
750        return;
751    }    }
752        
753  }  }
754    
755  sub getObjectAsHash {  sub getObjectAsHash_old {
756    my $self = shift;    my $self = shift;
757    my $oid = shift;    my $oid = shift;
758    my $options = shift;      my $options = shift;  
# Line 596  sub getObjectAsHash { Line 767  sub getObjectAsHash {
767    # build options (a callback to unload autovivified objects) for 'expand'    # build options (a callback to unload autovivified objects) for 'expand'
768    # TODO: use $logger to write to debug here!    # TODO: use $logger to write to debug here!
769    my $cb; # = sub {};    my $cb; # = sub {};
770    
771      # deactivated way to get rid of used instances, if requested
772  =pod  =pod
773    if ($options->{destroy}) {      if ($options->{destroy}) {
774      $options->{cb}->{destroy} = sub {        $options->{cb}->{destroy} = sub {
775        print "================ DESTROY", "\n";          print "================ DESTROY", "\n";
776        my $object = shift;          my $object = shift;
777        #print Dumper($object);          #print Dumper($object);
778        $self->{_COREHANDLE}->unload($object);          $self->{_COREHANDLE}->unload($object);
779        #undef($object);          #undef($object);
780      };        };
781    }      }
782  =cut  =cut
783    
784    my $hash = object2hash($obj, $options);    my $hash = expand($obj, $options);
785    #$options->{cb}->{destroy}->($obj);  
786    #$self->{_COREHANDLE}->unload($obj);    # old (unsuccessful) attempts to get rid of used instances, if requested
787      
788    # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML      # V1:
789    # now done in object2hash      #$options->{cb}->{destroy}->($obj);
790    #var2utf8($hash) if ($options->{utf8});      #$self->{_COREHANDLE}->unload($obj);
791        
792    # old (wrong) attempts to get rid of used instances, if requested      # V2:
793      #$obj->clear_refs;      #$obj->clear_refs;
794      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
795      #$obj->DESTROY;      #$obj->DESTROY;
# Line 634  sub getCOREHANDLE { Line 807  sub getCOREHANDLE {
807    return $self->{_COREHANDLE};    return $self->{_COREHANDLE};
808  }  }
809    
810    sub dropDb {
811      my $self = shift;
812      my $storage = $self->_getSubLayerHandle();
813      return $storage->dropDb();
814    }
815    
816    sub testAvailability {
817      my $self = shift;
818      my $storage = $self->_getSubLayerHandle();
819      return $storage->testAvailability();
820    }
821    
822    sub disconnect2 {
823      my $self = shift;
824      my $storage = $self->_getSubLayerHandle();
825      print "DISC!", "\n";
826    
827      my $storage_ll = $storage->{_COREHANDLE};
828      $storage_ll->disconnect();
829      
830      print Dumper($storage);
831      exit;
832      
833      #$self->{_COREHANDLE}
834      #$storage->disconnect();
835      $self->{dataStorageLayer}->disconnect();
836    }
837    
838  1;  1;
839    __END__

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

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