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

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

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