/[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.7 by joko, Sun Dec 1 04:46:19 2002 UTC revision 1.25 by joko, Sun Jan 19 02:30:05 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.25  2003/01/19 02:30:05  joko
7    #  + fix: modified call to '_initSchema'
8    #
9    #  Revision 1.24  2002/12/22 14:13:01  joko
10    #  + sub dropDb
11    #
12    #  Revision 1.23  2002/12/19 16:31:53  joko
13    #  +- renamed sub to 'rebuildDb'
14    #
15    #  Revision 1.22  2002/12/18 22:28:16  jonen
16    #  + added extended logging at 'getObjectByGuid()'
17    #
18    #  Revision 1.21  2002/12/16 22:20:49  jonen
19    #  + fixed bug at 'getObjectByGuid()'
20    #
21    #  Revision 1.20  2002/12/16 20:49:17  jonen
22    #  + added sub 'getObjectByGuid()'
23    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
24    #
25    #  Revision 1.19  2002/12/16 06:46:09  joko
26    #  + attempt to introduce a generic '_patchSchema' - cancelled!
27    #
28    #  Revision 1.18  2002/12/13 21:48:07  joko
29    #  + fix to 'sub sendQuery'
30    #
31    #  Revision 1.17  2002/12/12 02:51:09  joko
32    #  + cosmetics
33    #
34    #  Revision 1.16  2002/12/11 06:54:10  joko
35    #  + fix: encapsulated object-loading inside an 'eval'
36    #
37    #  Revision 1.15  2002/12/05 13:55:21  joko
38    #  + now utilizing 'object2hash' instead of 'var_deref'
39    #  + played around with having fresh-objects - no progress....
40    #
41    #  Revision 1.14  2002/12/05 09:40:30  jonen
42    #  + added option->{destroy} at getObject for unloading all instance
43    #
44    #  Revision 1.13  2002/12/05 07:59:04  joko
45    #  + now using Tie::SecureHash as a base for the COREHANDLE
46    #  + former public COREHANDLE becomes private _COREHANDLE now
47    #  + sub getCOREHANDLE
48    #
49    #  Revision 1.12  2002/12/04 11:34:49  joko
50    #  - $schema_tangram doesn't have to be in class?
51    #
52    #  Revision 1.11  2002/12/04 08:54:08  jonen
53    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
54    #
55    #  Revision 1.10  2002/12/03 15:53:23  joko
56    #  + small bugfix regarding object hierarchy
57    #
58    #  Revision 1.9  2002/12/03 05:29:40  joko
59    #  + sub getObject
60    #  + sub getObjectAsHash
61    #
62    #  Revision 1.8  2002/12/01 22:25:51  joko
63    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
64    #
65  #  Revision 1.7  2002/12/01 04:46:19  joko  #  Revision 1.7  2002/12/01 04:46:19  joko
66  #  + sub eraseAll  #  + sub eraseAll
67  #  #
# Line 46  package Data::Storage::Handler::Tangram; Line 105  package Data::Storage::Handler::Tangram;
105  use strict;  use strict;
106  use warnings;  use warnings;
107    
108    use base ("Data::Storage::Handler");
109  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
110    
111  use Tangram;  use Tangram;
# Line 53  use Data::Dumper; Line 113  use Data::Dumper;
113  use libp qw( getNewPerlObjectByPkgName );  use libp qw( getNewPerlObjectByPkgName );
114  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
115  use Data::Compare::Struct qw( isEmpty );  use Data::Compare::Struct qw( isEmpty );
116    use Data::Transform::Deep qw( object2hash );
117    use Data::Transform::Encode qw( var2utf8 );
118    
119    
120  # get logger instance  # get logger instance
121  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
122    
123    
124    # this holds the complete instantiated schema from tangram
125    my $schema_tangram;
126    
127  sub getMetaInfo {  sub getMetaInfo {
128    my $self = shift;    my $self = shift;
129    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
# Line 69  sub getMetaInfo { Line 135  sub getMetaInfo {
135  sub _initSchema {  sub _initSchema {
136    my $self = shift;    my $self = shift;
137    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
138    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
139      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} } );
140      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
141    #}    #}
142    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
143      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
144      return 0;      return 0;
145    }    }
146      #$self->_patchSchema();
147    return 1;    return 1;
148  }  }
149    
150    sub _patchSchema {
151      my $self = shift;
152      foreach (keys %{$schema_tangram->{classes}}) {
153        next if $schema_tangram->{classes}->{$_}->{abstract};
154        #next if ($_ ne 'TsBankAccount');
155        #$_ ne 'AbstractAccount' &&
156        print "class: $_", "\n";
157    #print Dumper($schema_tangram->{classes}->{$_});
158        # create new string property named 'guid'
159        my $tstring = Tangram::String->new();
160        $tstring->{name} = $tstring->{col} = 'guid';
161        # inject property into schema
162        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
163        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
164      }
165    }
166    
167  sub connect {  sub connect {
168    
169      my $self = shift;      my $self = shift;
170            
171      my $dsn = shift;      my $dsn = shift;
172    
173    #print Dumper($self);
174    #exit;
175    
176        # TODO: re-enable
177      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
178      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
179            
180      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 98  sub connect { Line 186  sub connect {
186  #      return;  #      return;
187  #    }  #    }
188    
189      return unless $self->_initSchema();      #return unless $self->_initSchema();
190        $self->_initSchema();
191    
192      # create the main tangram storage object      # create the main tangram storage object
193      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
194      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
195    
196    #print "connect", "\n";
197    #my $core = $self->{_COREHANDLE};
198    #print Dumper($core);
199        
200      # some attempts for configuring the wrapped underlying dbi.....      # some attempts for configuring the wrapped underlying dbi.....
201      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
# Line 138  sub getChildNodes { Line 231  sub getChildNodes {
231  #print Dumper($self);  #print Dumper($self);
232    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
233        
234    $storage->disconnect();    # TODO: REVIEW
235      #$storage->disconnect();
236        
237    $self->{meta}->{childnodes} = \@nodes;    $self->{meta}->{childnodes} = \@nodes;
238        
# Line 207  sub _configureUnderlyingStorage { Line 301  sub _configureUnderlyingStorage {
301    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
302      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
303      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
304      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
305    }    }
306    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
307  }  }
# Line 223  sub configureCOREHANDLE { Line 317  sub configureCOREHANDLE {
317    
318    # apply configured modifications    # apply configured modifications
319      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
320        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
321      }      }
322      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
323        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
324      }      }
325      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
326        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
327      }      }
328      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
329        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
330      }      }
331    
332  }  }
# Line 242  sub deploySchema { Line 336  sub deploySchema {
336    my $args = shift;    my $args = shift;
337        
338    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
339    
340    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
341    
342    my $ok;    my $ok;
343    # TODO: is this DBI->connect okay here like it is? regarding errors.....???    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
   if ( my $dbh = DBI->connect($dsn, '', '', {  
                                                       PrintError => 0,  
                                                     } ) ) {  
   
344      return unless $self->_initSchema();      return unless $self->_initSchema();
345            $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );  
346      $dbh->disconnect();      $dbh->disconnect();
347    }    }
348    return $ok;    return $ok;
# Line 264  sub retreatSchema { Line 352  sub retreatSchema {
352    
353    my $self = shift;    my $self = shift;
354    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
355    
356    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
357    
358    my $ok;    my $ok;
359    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       #PrintError => 0,  
                                                       #RaiseError => 0,  
                                                     } ) ) {  
360    
361      return unless $self->_initSchema();      return unless $self->_initSchema();
362            
363      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
364      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
365            
366      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
367      $ok = 2;    # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status  
368                      # idea: test this by checking for count of tables in database -      # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
369                      #          problem with this: there may be some left not having been included to the schema      # - ... Tangram::Relational->retreat doesn't seem to return a valid status
370        # - possible improvement:
371        #   - test this by checking for count of tables in database
372        #   - problem with this: there may be some left not having been included to the schema
373        #   - maybe better: use "->getChildNodes"?
374        $ok = 2;
375    
376      $dbh->disconnect();      $dbh->disconnect();
377    
378    }    }
379    return $ok;    return $ok;
380  }  }
381    
382  sub rebuildDbAndSchema {  sub rebuildDb {
383    my $self = shift;    my $self = shift;
384    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
385    my @results;    my @results;
386    
387    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 314  sub getListUnfiltered { Line 405  sub getListUnfiltered {
405    my @results;    my @results;
406    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
407    # get set of objects from odbms by object name    # get set of objects from odbms by object name
408    my $object_set = $self->{COREHANDLE}->remote($nodename);    my $object_set = $self->{_COREHANDLE}->remote($nodename);
409    @results = $self->{COREHANDLE}->select($object_set);    @results = $self->{_COREHANDLE}->select($object_set);
410    return \@results;    return \@results;
411  }  }
412    
# Line 360  sub getListFiltered { Line 451  sub getListFiltered {
451    my $tfilter = join(' & ', @tfilters);    my $tfilter = join(' & ', @tfilters);
452    
453    # get set of objects from odbms by object name    # get set of objects from odbms by object name
454    my $remote = $self->{COREHANDLE}->remote($nodename);    my $remote = $self->{_COREHANDLE}->remote($nodename);
455        
456    # was:    # was:
457    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
458    
459    # is:    # is:
460    # 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
461    my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
462        
463    # get filtered list/set    # get filtered list/set
464    @results = eval($evalstring);    @results = eval($evalstring);
# Line 379  sub getListFiltered { Line 470  sub getListFiltered {
470  sub createCursor {  sub createCursor {
471    my $self = shift;    my $self = shift;
472    my $node = shift;    my $node = shift;
473    my $cmdHandle = $self->{COREHANDLE}->cursor($node);    my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
474    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
475    return $result;    return $result;
476  }  }
477    
478  sub createSet {  sub createSet {
479    my $self = shift;    my $self = shift;
480    #print "-" x 80, "\n";
481    #print Dumper(@_);
482    my @objects = @_;    my @objects = @_;
483    my $rh = Set::Object->new();    my $rh = Set::Object->new();
484    foreach (@objects) {    foreach (@objects) {
485      #print Dumper($_);      if (!isEmpty($_)) {
486      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
487          $rh->insert($_);
488        }
489    }    }
490    #print Dumper($rh->members());    #print Dumper($rh->members());
491    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 414  sub sendQuery { Line 509  sub sendQuery {
509      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
510  #print "load obj", "\n";  #print "load obj", "\n";
511      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
512      my $object = $self->{COREHANDLE}->load($ident);      $self->{_COREHANDLE}->unload($ident);
513        my $object = $self->{_COREHANDLE}->load($ident);
514  #print "get id", "\n";  #print "get id", "\n";
515      my $oid = $self->{COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
516    #print Dumper($object);
517    #print "oid: $oid", "\n";
518      return $self->createSet($object);      return $self->createSet($object);
519      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
520    }    }
# Line 445  sub sendQuery { Line 543  sub sendQuery {
543  sub eraseAll {  sub eraseAll {
544    my $self = shift;    my $self = shift;
545    my $classname = shift;    my $classname = shift;
546    my $remote = $self->{storage}->remote($classname);    my $remote = $self->{_COREHANDLE}->remote($classname);
547    my @objs = $self->{storage}->select($remote);    my @objs = $self->{_COREHANDLE}->select($remote);
548    $self->{COREHANDLE}->erase(@objs);    $self->{_COREHANDLE}->erase(@objs);
549    }
550    
551    sub createDb {
552      my $self = shift;
553      my $storage = $self->_getSubLayerHandle();
554      return $storage->createDb();
555    }
556    
557    sub getObject {
558      my $self = shift;
559      my $oid = shift;
560      my $options = shift;
561    
562      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
563      # unload($oid) will only unload object, not deep object hashes
564      $self->{_COREHANDLE}->unload() if ($options->{destroy});
565    
566      # TODO: review this
567      #if (!$self->{COREHANDLE}) { return; }
568    
569      # TODO: review this
570      my $object = eval('$self->{_COREHANDLE}->load($oid);');
571      print $@, "\n" if $@;
572    
573      return $object if $object;
574    }
575    
576    sub getObjectByGuid {
577      my $self = shift;
578      my $guid = shift;
579      my $options = shift;
580      
581      # Guid and Classname is needed
582      if(!$guid || !$options->{classname}) {
583       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
584        return;
585      }
586      
587      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
588      # unload($oid) will only unload object, not deep object hashes
589      $self->{_COREHANDLE}->unload() if ($options->{destroy});
590    
591      # search for object with given Classname and Guid
592      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
593      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
594      
595      # we searched for global unique identifer of some object,
596      # so I think we can trust there would be only one result
597      if($result[0]) {
598        return $result[0];
599      } else {
600        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
601        return;
602      }
603      
604    }
605    
606    sub getObjectAsHash {
607      my $self = shift;
608      my $oid = shift;
609      my $options = shift;  
610      my $obj;
611      
612      if($options->{guid}) {
613        $obj = $self->getObjectByGuid($oid, $options);
614      } else {
615        $obj = $self->getObject($oid, $options);
616      }
617      
618      # build options (a callback to unload autovivified objects) for 'expand'
619      # TODO: use $logger to write to debug here!
620      my $cb; # = sub {};
621    =pod
622      if ($options->{destroy}) {
623        $options->{cb}->{destroy} = sub {
624          print "================ DESTROY", "\n";
625          my $object = shift;
626          #print Dumper($object);
627          $self->{_COREHANDLE}->unload($object);
628          #undef($object);
629        };
630      }
631    =cut
632    
633      my $hash = object2hash($obj, $options);
634      #$options->{cb}->{destroy}->($obj);
635      #$self->{_COREHANDLE}->unload($obj);
636      
637      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
638      # now done in object2hash
639      #var2utf8($hash) if ($options->{utf8});
640      
641      # old (wrong) attempts to get rid of used instances, if requested
642        #$obj->clear_refs;
643        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
644        #$obj->DESTROY;
645        #undef($obj) if($options->{destroy});
646      
647      return $hash;
648    }
649    
650    sub getSchema {
651      return $schema_tangram;
652    }
653    
654    sub getCOREHANDLE {
655      my $self = shift;
656      return $self->{_COREHANDLE};
657    }
658    
659    sub dropDb {
660      my $self = shift;
661      my $storage = $self->_getSubLayerHandle();
662      return $storage->dropDb();
663  }  }
664    
665  1;  1;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.25

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