/[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.17 by joko, Thu Dec 12 02:51:09 2002 UTC revision 1.31 by joko, Sat Apr 5 21:24:09 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.31  2003/04/05 21:24:09  joko
7    #  modified 'sub getChildNodes': now contains code from 'querySchema'
8    #
9    #  Revision 1.30  2003/03/27 15:31:14  joko
10    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
11    #
12    #  Revision 1.29  2003/02/21 01:47:18  joko
13    #  purged old code
14    #  minor cosmetics
15    #
16    #  Revision 1.28  2003/02/20 20:20:26  joko
17    #  tried to get auto-disconnect working again - failed with that
18    #
19    #  Revision 1.27  2003/01/31 06:30:59  joko
20    #  + enabled 'sendQuery'
21    #
22    #  Revision 1.26  2003/01/30 22:29:47  joko
23    #  + fixed module usage (removed dependency on 'libp.pm')
24    #
25    #  Revision 1.25  2003/01/19 02:30:05  joko
26    #  + fix: modified call to '_initSchema'
27    #
28    #  Revision 1.24  2002/12/22 14:13:01  joko
29    #  + sub dropDb
30    #
31    #  Revision 1.23  2002/12/19 16:31:53  joko
32    #  +- renamed sub to 'rebuildDb'
33    #
34    #  Revision 1.22  2002/12/18 22:28:16  jonen
35    #  + added extended logging at 'getObjectByGuid()'
36    #
37    #  Revision 1.21  2002/12/16 22:20:49  jonen
38    #  + fixed bug at 'getObjectByGuid()'
39    #
40    #  Revision 1.20  2002/12/16 20:49:17  jonen
41    #  + added sub 'getObjectByGuid()'
42    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
43    #
44    #  Revision 1.19  2002/12/16 06:46:09  joko
45    #  + attempt to introduce a generic '_patchSchema' - cancelled!
46    #
47    #  Revision 1.18  2002/12/13 21:48:07  joko
48    #  + fix to 'sub sendQuery'
49    #
50  #  Revision 1.17  2002/12/12 02:51:09  joko  #  Revision 1.17  2002/12/12 02:51:09  joko
51  #  + cosmetics  #  + cosmetics
52  #  #
# Line 10  Line 54 
54  #  + fix: encapsulated object-loading inside an 'eval'  #  + fix: encapsulated object-loading inside an 'eval'
55  #  #
56  #  Revision 1.15  2002/12/05 13:55:21  joko  #  Revision 1.15  2002/12/05 13:55:21  joko
57  #  + now utilizing 'object2hash' instead of 'var_deref'  #  + now utilizing 'expand' instead of 'var_deref'
58  #  + played around with having fresh-objects - no progress....  #  + played around with having fresh-objects - no progress....
59  #  #
60  #  Revision 1.14  2002/12/05 09:40:30  jonen  #  Revision 1.14  2002/12/05 09:40:30  jonen
# Line 83  use warnings; Line 127  use warnings;
127  use base ("Data::Storage::Handler");  use base ("Data::Storage::Handler");
128  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
129    
130  use Tangram;  
131  use Data::Dumper;  use Data::Dumper;
132  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 );  
133    
134    use DesignPattern::Object;
135    use Data::Storage::Result::Tangram;
136    use Data::Mungle::Compare::Struct qw( isEmpty );
137    use Data::Mungle::Transform::Deep qw( expand );
138    
139  # get logger instance  # get logger instance
140  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 111  sub _initSchema { Line 155  sub _initSchema {
155    my $self = shift;    my $self = shift;
156    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
157    #if (!$schema_tangram) {    #if (!$schema_tangram) {
158      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} } );
159        my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
160      $schema_tangram = $obj->getSchema();      $schema_tangram = $obj->getSchema();
161    #}    #}
162    if (!$schema_tangram) {    if (!$schema_tangram) {
163      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
164      return 0;      return 0;
165    }    }
166      #$self->_patchSchema();
167    return 1;    return 1;
168  }  }
169    
170    sub _patchSchema {
171      my $self = shift;
172      foreach (keys %{$schema_tangram->{classes}}) {
173        next if $schema_tangram->{classes}->{$_}->{abstract};
174        #next if ($_ ne 'TsBankAccount');
175        #$_ ne 'AbstractAccount' &&
176        print "class: $_", "\n";
177    #print Dumper($schema_tangram->{classes}->{$_});
178        # create new string property named 'guid'
179        my $tstring = Tangram::String->new();
180        $tstring->{name} = $tstring->{col} = 'guid';
181        # inject property into schema
182        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
183        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
184      }
185    }
186    
187  sub connect {  sub connect {
188    
189      my $self = shift;      my $self = shift;
190            
191      my $dsn = shift;      my $dsn = shift;
192    
193    #print Dumper($self);
194    #exit;
195    
196        # TODO: re-enable
197      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
198      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
199            
200      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 139  sub connect { Line 206  sub connect {
206  #      return;  #      return;
207  #    }  #    }
208    
209      return unless $self->_initSchema();      #return unless $self->_initSchema();
210        $self->_initSchema();
211    
212      # create the main tangram storage object      # create the main tangram storage object
213      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 166  sub connect { Line 234  sub connect {
234  sub getChildNodes {  sub getChildNodes {
235    
236    my $self = shift;    my $self = shift;
237    my @nodes;    my $mode = shift;
238      $mode ||= 'core';
239      
240      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
241    
242    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    if ($mode eq 'core') {
243    
244        my @nodes;
245        
246        # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
247        #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
248        #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
249        
250        # todo: should we retrieve information from the schema here
251        # rather than poorly getting table names from underlying dbi?
252        my $storage = $self->_getSubLayerHandle();
253        @nodes = @{$storage->getChildNodes()};
254        #$storage->_configureCOREHANDLE();
255      #print "getchildnodes\n";
256      #print Dumper($self);
257        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
258        
259        # TODO: REVIEW
260        #$storage->disconnect();
261        
262        $self->{meta}->{childnodes} = \@nodes;
263    
264    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE      return \@nodes;
   #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });  
   #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );  
265        
266    # todo: should we retrieve information from the schema here    } elsif ($mode eq 'root') {
267    # rather than poorly getting table names from underlying dbi?      
268    my $storage = $self->_getSubLayerHandle();      # FIXME: this will return *all* known classes to 'Class::Tangram',
269    @nodes = @{$storage->getChildNodes()};      # which might not be what you expect since more than one instance
270    #$storage->_configureCOREHANDLE();      # of Tangram may be in memory and Class::Tangram seems to
271  #print "getchildnodes\n";      # offer no methods to determine this or filter its result(s) according
272  #print Dumper($self);      # to a specific database.
273    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {      my @object_names = Class::Tangram::known_classes();
274        my @concret_names;
275        my $o_cnt;
276        foreach (sort @object_names) {
277          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
278          $o_cnt++;
279        }
280        
281    # TODO: REVIEW      my $result = {
282    #$storage->disconnect();        all => \@object_names,
283          concrete => \@concret_names,
284        };
285        return $result;
286        
287    $self->{meta}->{childnodes} = \@nodes;    }
288        
   return \@nodes;  
289    
290  }  }
291    
   
292  sub testIntegrity {  sub testIntegrity {
293    
294    my $self = shift;    my $self = shift;
# Line 266  sub configureCOREHANDLE { Line 362  sub configureCOREHANDLE {
362    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
363    
364    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
365      #print Dumper($self);
366      #exit;
367    
368    # apply configured modifications    # apply configured modifications
369      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
# Line 331  sub retreatSchema { Line 429  sub retreatSchema {
429    return $ok;    return $ok;
430  }  }
431    
432  sub rebuildDbAndSchema {  sub rebuildDb {
433    my $self = shift;    my $self = shift;
434    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
435    my @results;    my @results;
436    
437    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 373  sub getListFiltered { Line 471  sub getListFiltered {
471    my @results;    my @results;
472    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
473    
474    #print Dumper($filters);  #print Dumper($filters);
475        
476    my @tfilters;    my @tfilters;
477        
# Line 412  sub getListFiltered { Line 510  sub getListFiltered {
510    # 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
511    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
512        
513      #print "eval: $evalstring", "\n";
514      
515    # get filtered list/set    # get filtered list/set
516    @results = eval($evalstring);    @results = eval($evalstring);
517    die $@ if $@;    die $@ if $@;
# Line 429  sub createCursor { Line 529  sub createCursor {
529    
530  sub createSet {  sub createSet {
531    my $self = shift;    my $self = shift;
532    #print "-" x 80, "\n";
533    #print Dumper(@_);
534    my @objects = @_;    my @objects = @_;
535    my $rh = Set::Object->new();    my $rh = Set::Object->new();
536    foreach (@objects) {    foreach (@objects) {
537      #print Dumper($_);      if (!isEmpty($_)) {
538      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
539          $rh->insert($_);
540        }
541    }    }
542    #print Dumper($rh->members());    #print Dumper($rh->members());
543    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 457  sub sendQuery { Line 561  sub sendQuery {
561      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
562  #print "load obj", "\n";  #print "load obj", "\n";
563      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
564        $self->{_COREHANDLE}->unload($ident);
565      my $object = $self->{_COREHANDLE}->load($ident);      my $object = $self->{_COREHANDLE}->load($ident);
566  #print "get id", "\n";  #print "get id", "\n";
567      my $oid = $self->{_COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
568    #print Dumper($object);
569    #print "oid: $oid", "\n";
570      return $self->createSet($object);      return $self->createSet($object);
571      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
572    }    }
573    
574    die("This should not be reached for now - redirect to \$self->getListFiltered() here!");    my $list = $self->getListFiltered($query->{node}, $query->{criterias});
575      #return $self->createSet($object);
576      #return $self->createSet($list);
577      return $self->createSet(@$list);
578    
579      #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
580    
581    
   # TODO: do a common tangram query here  
582    
583    
584      # try a generic tangram query here
585      # TODO: try to place an oql on top of that (search.cpan.org!)
586    my @crits;    my @crits;
587    foreach (@{$query->{criterias}}) {    foreach (@{$query->{criterias}}) {
588      my $op = '';      my $op = '';
# Line 518  sub getObject { Line 633  sub getObject {
633    return $object if $object;    return $object if $object;
634  }  }
635    
636    sub getObjectByGuid {
637      my $self = shift;
638      my $guid = shift;
639      my $options = shift;
640      
641      # Guid and Classname is needed
642      if(!$guid || !$options->{classname}) {
643       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
644        return;
645      }
646      
647      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
648      # unload($oid) will only unload object, not deep object hashes
649      $self->{_COREHANDLE}->unload() if ($options->{destroy});
650    
651      # search for object with given Classname and Guid
652      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
653      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
654      
655      # we searched for global unique identifer of some object,
656      # so I think we can trust there would be only one result
657      if($result[0]) {
658        return $result[0];
659      } else {
660        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
661        return;
662      }
663      
664    }
665    
666  sub getObjectAsHash {  sub getObjectAsHash {
667    my $self = shift;    my $self = shift;
668    my $oid = shift;    my $oid = shift;
669    my $options = shift;    my $options = shift;  
670    my $obj = $self->getObject($oid, $options);    my $obj;
671      
672      if($options->{guid}) {
673        $obj = $self->getObjectByGuid($oid, $options);
674      } else {
675        $obj = $self->getObject($oid, $options);
676      }
677        
678    # build options (a callback to unload autovivified objects) for 'expand'    # build options (a callback to unload autovivified objects) for 'expand'
679    # TODO: use $logger to write to debug here!    # TODO: use $logger to write to debug here!
680    my $cb; # = sub {};    my $cb; # = sub {};
681    
682      # deactivated way to get rid of used instances, if requested
683  =pod  =pod
684    if ($options->{destroy}) {      if ($options->{destroy}) {
685      $options->{cb}->{destroy} = sub {        $options->{cb}->{destroy} = sub {
686        print "================ DESTROY", "\n";          print "================ DESTROY", "\n";
687        my $object = shift;          my $object = shift;
688        #print Dumper($object);          #print Dumper($object);
689        $self->{_COREHANDLE}->unload($object);          $self->{_COREHANDLE}->unload($object);
690        #undef($object);          #undef($object);
691      };        };
692    }      }
693  =cut  =cut
694    
695    my $hash = object2hash($obj, $options);    my $hash = expand($obj, $options);
696    #$options->{cb}->{destroy}->($obj);  
697    #$self->{_COREHANDLE}->unload($obj);    # old (unsuccessful) attempts to get rid of used instances, if requested
698      
699    # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML      # V1:
700    # now done in object2hash      #$options->{cb}->{destroy}->($obj);
701    #var2utf8($hash) if ($options->{utf8});      #$self->{_COREHANDLE}->unload($obj);
702        
703    # old (wrong) attempts to get rid of used instances, if requested      # V2:
704      #$obj->clear_refs;      #$obj->clear_refs;
705      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
706      #$obj->DESTROY;      #$obj->DESTROY;
# Line 565  sub getCOREHANDLE { Line 718  sub getCOREHANDLE {
718    return $self->{_COREHANDLE};    return $self->{_COREHANDLE};
719  }  }
720    
721    sub dropDb {
722      my $self = shift;
723      my $storage = $self->_getSubLayerHandle();
724      return $storage->dropDb();
725    }
726    
727    sub testAvailability {
728      my $self = shift;
729      my $storage = $self->_getSubLayerHandle();
730      return $storage->testAvailability();
731    }
732    
733    sub disconnect2 {
734      my $self = shift;
735      my $storage = $self->_getSubLayerHandle();
736      print "DISC!", "\n";
737    
738      my $storage_ll = $storage->{_COREHANDLE};
739      $storage_ll->disconnect();
740      
741      print Dumper($storage);
742      exit;
743      
744      #$self->{_COREHANDLE}
745      #$storage->disconnect();
746      $self->{dataStorageLayer}->disconnect();
747    }
748    
749  1;  1;
750    __END__

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.31

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