/[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.5 by joko, Sun Nov 17 06:35:18 2002 UTC revision 1.19 by joko, Mon Dec 16 06:46:09 2002 UTC
# Line 1  Line 1 
1  #################################  ############################################
2  #  #
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.19  2002/12/16 06:46:09  joko
7    #  + attempt to introduce a generic '_patchSchema' - cancelled!
8    #
9    #  Revision 1.18  2002/12/13 21:48:07  joko
10    #  + fix to 'sub sendQuery'
11    #
12    #  Revision 1.17  2002/12/12 02:51:09  joko
13    #  + cosmetics
14    #
15    #  Revision 1.16  2002/12/11 06:54:10  joko
16    #  + fix: encapsulated object-loading inside an 'eval'
17    #
18    #  Revision 1.15  2002/12/05 13:55:21  joko
19    #  + now utilizing 'object2hash' instead of 'var_deref'
20    #  + played around with having fresh-objects - no progress....
21    #
22    #  Revision 1.14  2002/12/05 09:40:30  jonen
23    #  + added option->{destroy} at getObject for unloading all instance
24    #
25    #  Revision 1.13  2002/12/05 07:59:04  joko
26    #  + now using Tie::SecureHash as a base for the COREHANDLE
27    #  + former public COREHANDLE becomes private _COREHANDLE now
28    #  + sub getCOREHANDLE
29    #
30    #  Revision 1.12  2002/12/04 11:34:49  joko
31    #  - $schema_tangram doesn't have to be in class?
32    #
33    #  Revision 1.11  2002/12/04 08:54:08  jonen
34    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
35    #
36    #  Revision 1.10  2002/12/03 15:53:23  joko
37    #  + small bugfix regarding object hierarchy
38    #
39    #  Revision 1.9  2002/12/03 05:29:40  joko
40    #  + sub getObject
41    #  + sub getObjectAsHash
42    #
43    #  Revision 1.8  2002/12/01 22:25:51  joko
44    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
45    #
46    #  Revision 1.7  2002/12/01 04:46:19  joko
47    #  + sub eraseAll
48    #
49    #  Revision 1.6  2002/11/29 05:02:30  joko
50    #  - sub getNewPerlObjectByPkgName (moved to libp.pm)
51    #  + sub getMetaInfo
52    #  - sub existsChildNode (moved to Abstract.pm)
53    #  + sub getListUnfiltered
54    #  + sub getListFiltered
55    #  + sub createCursor
56    #  + sub createSet
57    #  + sub sendQuery
58    #
59  #  Revision 1.5  2002/11/17 06:35:18  joko  #  Revision 1.5  2002/11/17 06:35:18  joko
60  #  + locator metadata can now be reached via ->{locator}  #  + locator metadata can now be reached via ->{locator}
61  #  - getChildNodes is now wrapped via COREHANDLE  #  - getChildNodes is now wrapped via COREHANDLE
# Line 25  Line 78 
78  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
79  #  + new  #  + new
80  #  #
81  #  ############################################
82  #################################  
83    
84  package Data::Storage::Handler::Tangram;  package Data::Storage::Handler::Tangram;
85    
86  use strict;  use strict;
87  use warnings;  use warnings;
88    
89    use base ("Data::Storage::Handler");
90  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
91    
92  use Tangram;  use Tangram;
93  use Data::Dumper;  use Data::Dumper;
94    use libp qw( getNewPerlObjectByPkgName );
95    use Data::Storage::Result::Tangram;
96    use Data::Compare::Struct qw( isEmpty );
97    use Data::Transform::Deep qw( object2hash );
98    use Data::Transform::Encode qw( var2utf8 );
99    
100    
101  # get logger instance  # get logger instance
102  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
103    
104    
105  our $metainfo = {  # this holds the complete instantiated schema from tangram
106    'disconnectMethod' => 'disconnect',  my $schema_tangram;
 };  
107    
108  sub getNewPerlObjectByPkgName {  sub getMetaInfo {
109    my $pkgname = shift;    my $self = shift;
110    my $args = shift;    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
111    $logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" );    return {
112    my $evstring = "use $pkgname;";      'disconnectMethod' => 'disconnect',
113    eval($evstring);    };
   $@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval $evstring: " .  $@ );  
   return $pkgname->new($args);  
114  }  }
115    
116  sub _initSchema {  sub _initSchema {
117    my $self = shift;    my $self = shift;
118    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
119    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
120      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} } );
121      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
122    #}    #}
123    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
124      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
125      return 0;      return 0;
126    }    }
127      #$self->_patchSchema();
128    return 1;    return 1;
129  }  }
130    
131    sub _patchSchema {
132      my $self = shift;
133      foreach (keys %{$schema_tangram->{classes}}) {
134        next if $schema_tangram->{classes}->{$_}->{abstract};
135        #next if ($_ ne 'TsBankAccount');
136        #$_ ne 'AbstractAccount' &&
137        print "class: $_", "\n";
138    #print Dumper($schema_tangram->{classes}->{$_});
139        # create new string property named 'guid'
140        my $tstring = Tangram::String->new();
141        $tstring->{name} = $tstring->{col} = 'guid';
142        # inject property into schema
143        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
144        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
145      }
146    }
147    
148  sub connect {  sub connect {
149    
150      my $self = shift;      my $self = shift;
# Line 90  sub connect { Line 165  sub connect {
165    
166      return unless $self->_initSchema();      return unless $self->_initSchema();
167    
168        # create the main tangram storage object
169      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
170      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
171    
172    #print "connect", "\n";
173    #my $core = $self->{_COREHANDLE};
174    #print Dumper($core);
175        
176        # some attempts for configuring the wrapped underlying dbi.....
177      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
178      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
179      #$self->_configureUnderlyingStorage;      #$self->_configureUnderlyingStorage;
180        
181        # ..... encapsulation wins!
182      $self->configureCOREHANDLE();      $self->configureCOREHANDLE();
183    
184    $self->{locator}->{status}->{connected} = 1;    $self->{locator}->{status}->{connected} = 1;
# Line 114  sub getChildNodes { Line 197  sub getChildNodes {
197    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
198    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
199    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
200      
201      # todo: should we retrieve information from the schema here
202      # rather than poorly getting table names from underlying dbi?
203    my $storage = $self->_getSubLayerHandle();    my $storage = $self->_getSubLayerHandle();
204    @nodes = @{$storage->getChildNodes()};    @nodes = @{$storage->getChildNodes()};
205    #$storage->_configureCOREHANDLE();    #$storage->_configureCOREHANDLE();
# Line 121  sub getChildNodes { Line 207  sub getChildNodes {
207  #print Dumper($self);  #print Dumper($self);
208    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
209        
210    $storage->disconnect();    # TODO: REVIEW
211      #$storage->disconnect();
212        
213    $self->{meta}->{childnodes} = \@nodes;    $self->{meta}->{childnodes} = \@nodes;
214        
# Line 130  sub getChildNodes { Line 217  sub getChildNodes {
217  }  }
218    
219    
 sub existsChildNode {  
   my $self = shift;  
   my $nodename = shift;  
   $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" );  
   $self->getChildNodes() unless $self->{meta}->{childnodes};  
   #print Dumper($self->{meta}->{childnodes});  
   return grep $nodename, @{$self->{meta}->{childnodes}};  
 }  
   
   
220  sub testIntegrity {  sub testIntegrity {
221    
222    my $self = shift;    my $self = shift;
# Line 200  sub _configureUnderlyingStorage { Line 277  sub _configureUnderlyingStorage {
277    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
278      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
279      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
280      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
281    }    }
282    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
283  }  }
# Line 216  sub configureCOREHANDLE { Line 293  sub configureCOREHANDLE {
293    
294    # apply configured modifications    # apply configured modifications
295      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
296        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
297      }      }
298      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
299        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
300      }      }
301      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
302        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
303      }      }
304      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
305        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
306      }      }
307    
308  }  }
# Line 235  sub deploySchema { Line 312  sub deploySchema {
312    my $args = shift;    my $args = shift;
313        
314    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
315    
316    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
317    
318    my $ok;    my $ok;
319    # 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,  
                                                     } ) ) {  
   
320      return unless $self->_initSchema();      return unless $self->_initSchema();
321            $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );  
322      $dbh->disconnect();      $dbh->disconnect();
323    }    }
324    return $ok;    return $ok;
# Line 257  sub retreatSchema { Line 328  sub retreatSchema {
328    
329    my $self = shift;    my $self = shift;
330    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
331    
332    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
333    
334    my $ok;    my $ok;
335    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       #PrintError => 0,  
                                                       #RaiseError => 0,  
                                                     } ) ) {  
336    
337      return unless $self->_initSchema();      return unless $self->_initSchema();
338            
339      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
340      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
341            
342      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
343      $ok = 2;    # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status  
344                      # 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...
345                      #          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
346        # - possible improvement:
347        #   - test this by checking for count of tables in database
348        #   - problem with this: there may be some left not having been included to the schema
349        #   - maybe better: use "->getChildNodes"?
350        $ok = 2;
351    
352      $dbh->disconnect();      $dbh->disconnect();
353    
354    }    }
355    return $ok;    return $ok;
356  }  }
# Line 301  sub rebuildDbAndSchema { Line 375  sub rebuildDbAndSchema {
375    return $res;    return $res;
376  }  }
377    
378    sub getListUnfiltered {
379      my $self = shift;
380      my $nodename = shift;
381      my @results;
382      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
383      # get set of objects from odbms by object name
384      my $object_set = $self->{_COREHANDLE}->remote($nodename);
385      @results = $self->{_COREHANDLE}->select($object_set);
386      return \@results;
387    }
388    
389    sub getListFiltered {
390      my $self = shift;
391    
392      # redirect to unfiltered mode
393      #return $self->getListUnfiltered(@_);
394    
395      my $nodename = shift;
396      my $filters = shift;
397      my @results;
398      $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
399    
400      #print Dumper($filters);
401      
402      my @tfilters;
403      
404      foreach my $filter (@$filters) {
405      
406        # get filter - TODO: for each filter
407        #my $filter = $filters->[0];
408      
409        # build filter
410        my $lexpr = $filter->{key};
411        #my $op = $filter->{op};
412        my $op = '=';
413        my $rexpr = $filter->{val};
414        my $tight = 100;
415        
416      #  my $tfilter = Tangram::Filter->new(
417      #    expr => "t1.$lexpr $op '$rexpr'",
418      #    tight => $tight,
419      #    objects => $objects,
420      #  );
421      
422        # HACK: build eval-string (sorry) to get filtered list - please give advice here
423        push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
424    
425      }
426    
427      my $tfilter = join(' & ', @tfilters);
428    
429      # get set of objects from odbms by object name
430      my $remote = $self->{_COREHANDLE}->remote($nodename);
431      
432      # was:
433      #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
434    
435      # is:
436      # HACK: build eval-string (sorry) to get filtered list - please give advice here
437      my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
438      
439      # get filtered list/set
440      @results = eval($evalstring);
441      die $@ if $@;
442      
443      return \@results;
444    }
445    
446    sub createCursor {
447      my $self = shift;
448      my $node = shift;
449      my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
450      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
451      return $result;
452    }
453    
454    sub createSet {
455      my $self = shift;
456    #print "-" x 80, "\n";
457    #print Dumper(@_);
458      my @objects = @_;
459      my $rh = Set::Object->new();
460      foreach (@objects) {
461        if (!isEmpty($_)) {
462    #print Dumper($_);
463          $rh->insert($_);
464        }
465      }
466      #print Dumper($rh->members());
467      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
468      return $result;
469    }
470    
471    sub sendQuery {
472      my $self = shift;
473      my $query = shift;
474      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
475      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
476    
477      #print Dumper($query);
478    
479      # HACK: special case: querying by id does not translate into a common tangram query
480      # just load the object by given id(ent)
481      if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {
482        #print "LOAD!!!", "\n";
483        #exit;
484        #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
485        my $ident = $query->{criterias}->[0]->{val};
486    #print "load obj", "\n";
487        #return $self->createSet() if $ident == 5;
488        $self->{_COREHANDLE}->unload($ident);
489        my $object = $self->{_COREHANDLE}->load($ident);
490    #print "get id", "\n";
491        my $oid = $self->{_COREHANDLE}->id($object);
492    #print Dumper($object);
493    #print "oid: $oid", "\n";
494        return $self->createSet($object);
495        #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
496      }
497    
498      die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
499    
500      # TODO: do a common tangram query here
501    
502      my @crits;
503      foreach (@{$query->{criterias}}) {
504        my $op = '';
505        $op = '=' if lc $_->{op} eq 'eq';
506        push @crits, "$_->{key}$op'$_->{val}'";
507      }
508      my $subnodes = {};
509      map { $subnodes->{$_}++ } @{$query->{subnodes}};
510      # HACK: this is hardcoded ;(    expand possibilities!
511      #my $crit = join(' AND ', @crits);
512      #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
513      #return $self->sendCommand($sql);
514      #my $h = $self->{COREHANDLE}->remote($query->{node});
515      #my $res = $self->{COREHANDLE}->select($h, $h->{);
516      return $self->createCursor($query->{node});
517    }
518    
519    sub eraseAll {
520      my $self = shift;
521      my $classname = shift;
522      my $remote = $self->{_COREHANDLE}->remote($classname);
523      my @objs = $self->{_COREHANDLE}->select($remote);
524      $self->{_COREHANDLE}->erase(@objs);
525    }
526    
527    sub createDb {
528      my $self = shift;
529      my $storage = $self->_getSubLayerHandle();
530      return $storage->createDb();
531    }
532    
533    sub getObject {
534      my $self = shift;
535      my $oid = shift;
536      my $options = shift;
537    
538      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
539      # unload($oid) will only unload object, not deep object hashes
540      $self->{_COREHANDLE}->unload() if ($options->{destroy});
541    
542      # TODO: review this
543      #if (!$self->{COREHANDLE}) { return; }
544    
545      # TODO: review this
546      my $object = eval('$self->{_COREHANDLE}->load($oid);');
547      print $@, "\n" if $@;
548    
549      return $object if $object;
550    }
551    
552    sub getObjectAsHash {
553      my $self = shift;
554      my $oid = shift;
555      my $options = shift;
556      my $obj = $self->getObject($oid, $options);
557      
558      # build options (a callback to unload autovivified objects) for 'expand'
559      # TODO: use $logger to write to debug here!
560      my $cb; # = sub {};
561    =pod
562      if ($options->{destroy}) {
563        $options->{cb}->{destroy} = sub {
564          print "================ DESTROY", "\n";
565          my $object = shift;
566          #print Dumper($object);
567          $self->{_COREHANDLE}->unload($object);
568          #undef($object);
569        };
570      }
571    =cut
572    
573      my $hash = object2hash($obj, $options);
574      #$options->{cb}->{destroy}->($obj);
575      #$self->{_COREHANDLE}->unload($obj);
576      
577      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
578      # now done in object2hash
579      #var2utf8($hash) if ($options->{utf8});
580      
581      # old (wrong) attempts to get rid of used instances, if requested
582        #$obj->clear_refs;
583        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
584        #$obj->DESTROY;
585        #undef($obj) if($options->{destroy});
586      
587      return $hash;
588    }
589    
590    sub getSchema {
591      return $schema_tangram;
592    }
593    
594    sub getCOREHANDLE {
595      my $self = shift;
596      return $self->{_COREHANDLE};
597    }
598    
599  1;  1;

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

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