/[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.4 by joko, Fri Oct 25 11:44:44 2002 UTC revision 1.12 by joko, Wed Dec 4 11:34:49 2002 UTC
# Line 1  Line 1 
1  #################################  ############################################
2  #  #
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.12  2002/12/04 11:34:49  joko
7    #  - $schema_tangram doesn't have to be in class?
8    #
9    #  Revision 1.11  2002/12/04 08:54:08  jonen
10    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
11    #
12    #  Revision 1.10  2002/12/03 15:53:23  joko
13    #  + small bugfix regarding object hierarchy
14    #
15    #  Revision 1.9  2002/12/03 05:29:40  joko
16    #  + sub getObject
17    #  + sub getObjectAsHash
18    #
19    #  Revision 1.8  2002/12/01 22:25:51  joko
20    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
21    #
22    #  Revision 1.7  2002/12/01 04:46:19  joko
23    #  + sub eraseAll
24    #
25    #  Revision 1.6  2002/11/29 05:02:30  joko
26    #  - sub getNewPerlObjectByPkgName (moved to libp.pm)
27    #  + sub getMetaInfo
28    #  - sub existsChildNode (moved to Abstract.pm)
29    #  + sub getListUnfiltered
30    #  + sub getListFiltered
31    #  + sub createCursor
32    #  + sub createSet
33    #  + sub sendQuery
34    #
35    #  Revision 1.5  2002/11/17 06:35:18  joko
36    #  + locator metadata can now be reached via ->{locator}
37    #  - getChildNodes is now wrapped via COREHANDLE
38    #
39  #  Revision 1.4  2002/10/25 11:44:44  joko  #  Revision 1.4  2002/10/25 11:44:44  joko
40  #  + sub _initSchema  #  + sub _initSchema
41  #  + sub existsChildNode  #  + sub existsChildNode
# Line 21  Line 54 
54  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
55  #  + new  #  + new
56  #  #
57  #  ############################################
58  #################################  
59    
60  package Data::Storage::Handler::Tangram;  package Data::Storage::Handler::Tangram;
61    
# Line 33  use base ("Data::Storage::Handler::Abstr Line 66  use base ("Data::Storage::Handler::Abstr
66    
67  use Tangram;  use Tangram;
68  use Data::Dumper;  use Data::Dumper;
69    use libp qw( getNewPerlObjectByPkgName );
70    use Data::Storage::Result::Tangram;
71    use Data::Compare::Struct qw( isEmpty );
72    use Data::Transform::Deep qw( var_deref );
73    use Data::Transform::Encode qw( var2utf8 );
74    
75    
76  # get logger instance  # get logger instance
77  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
78    
79    
80  our $metainfo = {  # this holds the complete instantiated schema from tangram
81    'disconnectMethod' => 'disconnect',  my $schema_tangram;
 };  
82    
83  sub getNewPerlObjectByPkgName {  sub getMetaInfo {
84    my $pkgname = shift;    my $self = shift;
85    my $args = shift;    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
86    $logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" );    return {
87    my $evstring = "use $pkgname;";      'disconnectMethod' => 'disconnect',
88    eval($evstring);    };
   $@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval: " .  $@ );  
   return $pkgname->new($args);  
89  }  }
90    
91  sub _initSchema {  sub _initSchema {
92    my $self = shift;    my $self = shift;
93    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
94    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
95      my $obj = getNewPerlObjectByPkgName($self->{schema}, { EXPORT_OBJECTS => $self->{classnames} } );      my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
96      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
97    #}    #}
98    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
99      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
100      return 0;      return 0;
101    }    }
# Line 71  sub connect { Line 107  sub connect {
107      my $self = shift;      my $self = shift;
108            
109      my $dsn = shift;      my $dsn = shift;
110      $dsn ||= $self->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
111            
112      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
113            
# Line 86  sub connect { Line 122  sub connect {
122    
123      return unless $self->_initSchema();      return unless $self->_initSchema();
124    
125        # create the main tangram storage object
126      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
127      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
128        
129        # some attempts for configuring the wrapped underlying dbi.....
130      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
131      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
132      #$self->_configureUnderlyingStorage;      #$self->_configureUnderlyingStorage;
133        
134        # ..... encapsulation wins!
135      $self->configureCOREHANDLE();      $self->configureCOREHANDLE();
136    
137      $self->{locator}->{status}->{connected} = 1;
138    
139    return 1;    return 1;
140    
141  }  }
# Line 108  sub getChildNodes { Line 150  sub getChildNodes {
150    # 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
151    #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} });
152    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
153      
154      # todo: should we retrieve information from the schema here
155      # rather than poorly getting table names from underlying dbi?
156    my $storage = $self->_getSubLayerHandle();    my $storage = $self->_getSubLayerHandle();
157      @nodes = @{$storage->getChildNodes()};
158    #$storage->_configureCOREHANDLE();    #$storage->_configureCOREHANDLE();
159    #print "getchildnodes\n";
160    #print Dumper($self);
161    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
   if (my $result = $storage->sendCommand( 'SHOW TABLES;' ) ) {  
     while ( my $row = $result->_getNextEntry() ) {  
       push @nodes, $row;  
     }  
   }  
162        
163    $storage->disconnect();    # TODO: REVIEW
164      #$storage->disconnect();
165        
166    $self->{meta}->{childnodes} = \@nodes;    $self->{meta}->{childnodes} = \@nodes;
167        
# Line 127  sub getChildNodes { Line 170  sub getChildNodes {
170  }  }
171    
172    
 sub existsChildNode {  
   my $self = shift;  
   my $nodename = shift;  
   $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" );  
   $self->getChildNodes() unless $self->{meta}->{childnodes};  
   return grep $nodename, @{$self->{meta}->{childnodes}};  
 }  
   
   
173  sub testIntegrity {  sub testIntegrity {
174    
175    my $self = shift;    my $self = shift;
# Line 154  sub testIntegrity { Line 188  sub testIntegrity {
188      return;      return;
189    }    }
190    
191      $self->{locator}->{status}->{integrity} = 1;
192    return 1;    return 1;
193    
194  }  }
# Line 165  sub _getSubLayerHandle { Line 200  sub _getSubLayerHandle {
200        
201    $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );    $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
202    
   use Data::Dumper;  
203    #print Dumper($self);    #print Dumper($self);
204    #exit;  
     
205    # hack, make more generic!    # hack, make more generic!
206    if (!$self->{dataStorageLayer}) {    if (!$self->{dataStorageLayer}) {
207      $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );      $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
208      #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );      #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );
209      my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi} );      my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } );
210      $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );      $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );
211      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();
212      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();
# Line 229  sub configureCOREHANDLE { Line 262  sub configureCOREHANDLE {
262    
263  sub deploySchema {  sub deploySchema {
264    my $self = shift;    my $self = shift;
265    #my $dsn = $self->{locator}->{dbi}->{dsn};    my $args = shift;
266    my $dsn = $self->{dbi}->{dsn};    
267      my $dsn = $self->{locator}->{dbi}->{dsn};
268    
269    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
270    
271    my $ok;    my $ok;
272    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       PrintError => 0,  
                                                     } ) ) {  
   
273      return unless $self->_initSchema();      return unless $self->_initSchema();
274            $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );  
275      $dbh->disconnect();      $dbh->disconnect();
276    }    }
277    return $ok;    return $ok;
# Line 250  sub deploySchema { Line 280  sub deploySchema {
280  sub retreatSchema {  sub retreatSchema {
281    
282    my $self = shift;    my $self = shift;
283    #my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   my $dsn = $self->{dbi}->{dsn};  
284    
285    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
286    
287    my $ok;    my $ok;
288    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       #PrintError => 0,  
                                                       #RaiseError => 0,  
                                                     } ) ) {  
289    
290      return unless $self->_initSchema();      return unless $self->_initSchema();
291            
292      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
293      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
294            
295      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
296      $ok = 2;    # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status  
297                      # 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...
298                      #          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
299        # - possible improvement:
300        #   - test this by checking for count of tables in database
301        #   - problem with this: there may be some left not having been included to the schema
302        #   - maybe better: use "->getChildNodes"?
303        $ok = 2;
304    
305      $dbh->disconnect();      $dbh->disconnect();
306    
307    }    }
308    return $ok;    return $ok;
309  }  }
# Line 295  sub rebuildDbAndSchema { Line 328  sub rebuildDbAndSchema {
328    return $res;    return $res;
329  }  }
330    
331    sub getListUnfiltered {
332      my $self = shift;
333      my $nodename = shift;
334      my @results;
335      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
336      # get set of objects from odbms by object name
337      my $object_set = $self->{COREHANDLE}->remote($nodename);
338      @results = $self->{COREHANDLE}->select($object_set);
339      return \@results;
340    }
341    
342    sub getListFiltered {
343      my $self = shift;
344    
345      # redirect to unfiltered mode
346      #return $self->getListUnfiltered(@_);
347    
348      my $nodename = shift;
349      my $filters = shift;
350      my @results;
351      $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
352    
353      #print Dumper($filters);
354      
355      my @tfilters;
356      
357      foreach my $filter (@$filters) {
358      
359        # get filter - TODO: for each filter
360        #my $filter = $filters->[0];
361      
362        # build filter
363        my $lexpr = $filter->{key};
364        #my $op = $filter->{op};
365        my $op = '=';
366        my $rexpr = $filter->{val};
367        my $tight = 100;
368        
369      #  my $tfilter = Tangram::Filter->new(
370      #    expr => "t1.$lexpr $op '$rexpr'",
371      #    tight => $tight,
372      #    objects => $objects,
373      #  );
374      
375        # HACK: build eval-string (sorry) to get filtered list - please give advice here
376        push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
377    
378      }
379    
380      my $tfilter = join(' & ', @tfilters);
381    
382      # get set of objects from odbms by object name
383      my $remote = $self->{COREHANDLE}->remote($nodename);
384      
385      # was:
386      #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
387    
388      # is:
389      # HACK: build eval-string (sorry) to get filtered list - please give advice here
390      my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');';
391      
392      # get filtered list/set
393      @results = eval($evalstring);
394      die $@ if $@;
395      
396      return \@results;
397    }
398    
399    sub createCursor {
400      my $self = shift;
401      my $node = shift;
402      my $cmdHandle = $self->{COREHANDLE}->cursor($node);
403      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
404      return $result;
405    }
406    
407    sub createSet {
408      my $self = shift;
409      my @objects = @_;
410      my $rh = Set::Object->new();
411      foreach (@objects) {
412        #print Dumper($_);
413        $rh->insert($_) if !isEmpty($_);
414      }
415      #print Dumper($rh->members());
416      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
417      return $result;
418    }
419    
420    sub sendQuery {
421      my $self = shift;
422      my $query = shift;
423      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
424      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
425    
426      #print Dumper($query);
427    
428      # HACK: special case: querying by id does not translate into a common tangram query
429      # just load the object by given id(ent)
430      if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {
431        #print "LOAD!!!", "\n";
432        #exit;
433        #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
434        my $ident = $query->{criterias}->[0]->{val};
435    #print "load obj", "\n";
436        #return $self->createSet() if $ident == 5;
437        my $object = $self->{COREHANDLE}->load($ident);
438    #print "get id", "\n";
439        my $oid = $self->{COREHANDLE}->id($object);
440        return $self->createSet($object);
441        #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
442      }
443    
444      die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
445    
446      # TODO: do a common tangram query here
447    
448      my @crits;
449      foreach (@{$query->{criterias}}) {
450        my $op = '';
451        $op = '=' if lc $_->{op} eq 'eq';
452        push @crits, "$_->{key}$op'$_->{val}'";
453      }
454      my $subnodes = {};
455      map { $subnodes->{$_}++ } @{$query->{subnodes}};
456      # HACK: this is hardcoded ;(    expand possibilities!
457      #my $crit = join(' AND ', @crits);
458      #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
459      #return $self->sendCommand($sql);
460      #my $h = $self->{COREHANDLE}->remote($query->{node});
461      #my $res = $self->{COREHANDLE}->select($h, $h->{);
462      return $self->createCursor($query->{node});
463    }
464    
465    sub eraseAll {
466      my $self = shift;
467      my $classname = shift;
468      my $remote = $self->{COREHANDLE}->remote($classname);
469      my @objs = $self->{COREHANDLE}->select($remote);
470      $self->{COREHANDLE}->erase(@objs);
471    }
472    
473    sub createDb {
474      my $self = shift;
475      my $storage = $self->_getSubLayerHandle();
476      return $storage->createDb();
477    }
478    
479    sub getObject {
480      my $self = shift;
481      my $oid = shift;
482      # TODO: review this
483      #if (!$self->{COREHANDLE}) { return; }
484      return $self->{COREHANDLE}->load($oid);
485    }
486    
487    sub getObjectAsHash {
488      my $self = shift;
489      my $oid = shift;
490      my $options = shift;
491      my $obj = $self->getObject($oid);
492      my $deref = var_deref($obj);
493      var2utf8($deref) if ($options->{utf8});
494      undef($obj) if($options->{destroy});
495      return $deref;
496    }
497    
498    sub getSchema {
499      return $schema_tangram;
500    }
501    
502  1;  1;

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.12

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