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

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

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