/[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.23 by joko, Thu Dec 19 16:31:53 2002 UTC
# Line 1  Line 1 
1  #################################  ############################################
2  #  #
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.23  2002/12/19 16:31:53  joko
7    #  +- renamed sub to 'rebuildDb'
8    #
9    #  Revision 1.22  2002/12/18 22:28:16  jonen
10    #  + added extended logging at 'getObjectByGuid()'
11    #
12    #  Revision 1.21  2002/12/16 22:20:49  jonen
13    #  + fixed bug at 'getObjectByGuid()'
14    #
15    #  Revision 1.20  2002/12/16 20:49:17  jonen
16    #  + added sub 'getObjectByGuid()'
17    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
18    #
19    #  Revision 1.19  2002/12/16 06:46:09  joko
20    #  + attempt to introduce a generic '_patchSchema' - cancelled!
21    #
22    #  Revision 1.18  2002/12/13 21:48:07  joko
23    #  + fix to 'sub sendQuery'
24    #
25    #  Revision 1.17  2002/12/12 02:51:09  joko
26    #  + cosmetics
27    #
28    #  Revision 1.16  2002/12/11 06:54:10  joko
29    #  + fix: encapsulated object-loading inside an 'eval'
30    #
31    #  Revision 1.15  2002/12/05 13:55:21  joko
32    #  + now utilizing 'object2hash' instead of 'var_deref'
33    #  + played around with having fresh-objects - no progress....
34    #
35    #  Revision 1.14  2002/12/05 09:40:30  jonen
36    #  + added option->{destroy} at getObject for unloading all instance
37    #
38    #  Revision 1.13  2002/12/05 07:59:04  joko
39    #  + now using Tie::SecureHash as a base for the COREHANDLE
40    #  + former public COREHANDLE becomes private _COREHANDLE now
41    #  + sub getCOREHANDLE
42    #
43    #  Revision 1.12  2002/12/04 11:34:49  joko
44    #  - $schema_tangram doesn't have to be in class?
45    #
46    #  Revision 1.11  2002/12/04 08:54:08  jonen
47    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
48    #
49    #  Revision 1.10  2002/12/03 15:53:23  joko
50    #  + small bugfix regarding object hierarchy
51    #
52    #  Revision 1.9  2002/12/03 05:29:40  joko
53    #  + sub getObject
54    #  + sub getObjectAsHash
55    #
56    #  Revision 1.8  2002/12/01 22:25:51  joko
57    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
58    #
59    #  Revision 1.7  2002/12/01 04:46:19  joko
60    #  + sub eraseAll
61    #
62    #  Revision 1.6  2002/11/29 05:02:30  joko
63    #  - sub getNewPerlObjectByPkgName (moved to libp.pm)
64    #  + sub getMetaInfo
65    #  - sub existsChildNode (moved to Abstract.pm)
66    #  + sub getListUnfiltered
67    #  + sub getListFiltered
68    #  + sub createCursor
69    #  + sub createSet
70    #  + sub sendQuery
71    #
72    #  Revision 1.5  2002/11/17 06:35:18  joko
73    #  + locator metadata can now be reached via ->{locator}
74    #  - getChildNodes is now wrapped via COREHANDLE
75    #
76  #  Revision 1.4  2002/10/25 11:44:44  joko  #  Revision 1.4  2002/10/25 11:44:44  joko
77  #  + sub _initSchema  #  + sub _initSchema
78  #  + sub existsChildNode  #  + sub existsChildNode
# Line 21  Line 91 
91  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
92  #  + new  #  + new
93  #  #
94  #  ############################################
95  #################################  
96    
97  package Data::Storage::Handler::Tangram;  package Data::Storage::Handler::Tangram;
98    
99  use strict;  use strict;
100  use warnings;  use warnings;
101    
102    use base ("Data::Storage::Handler");
103  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
104    
105  use Tangram;  use Tangram;
106  use Data::Dumper;  use Data::Dumper;
107    use libp qw( getNewPerlObjectByPkgName );
108    use Data::Storage::Result::Tangram;
109    use Data::Compare::Struct qw( isEmpty );
110    use Data::Transform::Deep qw( object2hash );
111    use Data::Transform::Encode qw( var2utf8 );
112    
113    
114  # get logger instance  # get logger instance
115  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
116    
117    
118  our $metainfo = {  # this holds the complete instantiated schema from tangram
119    'disconnectMethod' => 'disconnect',  my $schema_tangram;
 };  
120    
121  sub getNewPerlObjectByPkgName {  sub getMetaInfo {
122    my $pkgname = shift;    my $self = shift;
123    my $args = shift;    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
124    $logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" );    return {
125    my $evstring = "use $pkgname;";      'disconnectMethod' => 'disconnect',
126    eval($evstring);    };
   $@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval: " .  $@ );  
   return $pkgname->new($args);  
127  }  }
128    
129  sub _initSchema {  sub _initSchema {
130    my $self = shift;    my $self = shift;
131    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
132    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
133      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} } );
134      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
135    #}    #}
136    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
137      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
138      return 0;      return 0;
139    }    }
140      #$self->_patchSchema();
141    return 1;    return 1;
142  }  }
143    
144    sub _patchSchema {
145      my $self = shift;
146      foreach (keys %{$schema_tangram->{classes}}) {
147        next if $schema_tangram->{classes}->{$_}->{abstract};
148        #next if ($_ ne 'TsBankAccount');
149        #$_ ne 'AbstractAccount' &&
150        print "class: $_", "\n";
151    #print Dumper($schema_tangram->{classes}->{$_});
152        # create new string property named 'guid'
153        my $tstring = Tangram::String->new();
154        $tstring->{name} = $tstring->{col} = 'guid';
155        # inject property into schema
156        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
157        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
158      }
159    }
160    
161  sub connect {  sub connect {
162    
163      my $self = shift;      my $self = shift;
164            
165      my $dsn = shift;      my $dsn = shift;
166      $dsn ||= $self->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
167            
168      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
169            
# Line 86  sub connect { Line 178  sub connect {
178    
179      return unless $self->_initSchema();      return unless $self->_initSchema();
180    
181        # create the main tangram storage object
182      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
183      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
184    
185    #print "connect", "\n";
186    #my $core = $self->{_COREHANDLE};
187    #print Dumper($core);
188        
189        # some attempts for configuring the wrapped underlying dbi.....
190      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
191      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
192      #$self->_configureUnderlyingStorage;      #$self->_configureUnderlyingStorage;
193        
194        # ..... encapsulation wins!
195      $self->configureCOREHANDLE();      $self->configureCOREHANDLE();
196    
197      $self->{locator}->{status}->{connected} = 1;
198    
199    return 1;    return 1;
200    
201  }  }
# Line 108  sub getChildNodes { Line 210  sub getChildNodes {
210    # 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
211    #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} });
212    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
213      
214      # todo: should we retrieve information from the schema here
215      # rather than poorly getting table names from underlying dbi?
216    my $storage = $self->_getSubLayerHandle();    my $storage = $self->_getSubLayerHandle();
217      @nodes = @{$storage->getChildNodes()};
218    #$storage->_configureCOREHANDLE();    #$storage->_configureCOREHANDLE();
219    #print "getchildnodes\n";
220    #print Dumper($self);
221    #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;  
     }  
   }  
222        
223    $storage->disconnect();    # TODO: REVIEW
224      #$storage->disconnect();
225        
226    $self->{meta}->{childnodes} = \@nodes;    $self->{meta}->{childnodes} = \@nodes;
227        
# Line 127  sub getChildNodes { Line 230  sub getChildNodes {
230  }  }
231    
232    
 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}};  
 }  
   
   
233  sub testIntegrity {  sub testIntegrity {
234    
235    my $self = shift;    my $self = shift;
# Line 154  sub testIntegrity { Line 248  sub testIntegrity {
248      return;      return;
249    }    }
250    
251      $self->{locator}->{status}->{integrity} = 1;
252    return 1;    return 1;
253    
254  }  }
# Line 165  sub _getSubLayerHandle { Line 260  sub _getSubLayerHandle {
260        
261    $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );    $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
262    
   use Data::Dumper;  
263    #print Dumper($self);    #print Dumper($self);
264    #exit;  
     
265    # hack, make more generic!    # hack, make more generic!
266    if (!$self->{dataStorageLayer}) {    if (!$self->{dataStorageLayer}) {
267      $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );      $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
268      #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} );
269      my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi} );      my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } );
270      $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );      $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );
271      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();
272      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();
# Line 197  sub _configureUnderlyingStorage { Line 290  sub _configureUnderlyingStorage {
290    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
291      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
292      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
293      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
294    }    }
295    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
296  }  }
# Line 213  sub configureCOREHANDLE { Line 306  sub configureCOREHANDLE {
306    
307    # apply configured modifications    # apply configured modifications
308      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
309        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
310      }      }
311      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
312        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
313      }      }
314      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
315        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
316      }      }
317      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
318        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
319      }      }
320    
321  }  }
322    
323  sub deploySchema {  sub deploySchema {
324    my $self = shift;    my $self = shift;
325    #my $dsn = $self->{locator}->{dbi}->{dsn};    my $args = shift;
326    my $dsn = $self->{dbi}->{dsn};    
327      my $dsn = $self->{locator}->{dbi}->{dsn};
328    
329    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
330    
331    my $ok;    my $ok;
332    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       PrintError => 0,  
                                                     } ) ) {  
   
333      return unless $self->_initSchema();      return unless $self->_initSchema();
334            $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );  
335      $dbh->disconnect();      $dbh->disconnect();
336    }    }
337    return $ok;    return $ok;
# Line 250  sub deploySchema { Line 340  sub deploySchema {
340  sub retreatSchema {  sub retreatSchema {
341    
342    my $self = shift;    my $self = shift;
343    #my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   my $dsn = $self->{dbi}->{dsn};  
344    
345    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
346    
347    my $ok;    my $ok;
348    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       #PrintError => 0,  
                                                       #RaiseError => 0,  
                                                     } ) ) {  
349    
350      return unless $self->_initSchema();      return unless $self->_initSchema();
351            
352      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
353      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
354            
355      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
356      $ok = 2;    # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status  
357                      # 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...
358                      #          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
359        # - possible improvement:
360        #   - test this by checking for count of tables in database
361        #   - problem with this: there may be some left not having been included to the schema
362        #   - maybe better: use "->getChildNodes"?
363        $ok = 2;
364    
365      $dbh->disconnect();      $dbh->disconnect();
366    
367    }    }
368    return $ok;    return $ok;
369  }  }
370    
371  sub rebuildDbAndSchema {  sub rebuildDb {
372    my $self = shift;    my $self = shift;
373    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
374    my @results;    my @results;
375    
376    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 295  sub rebuildDbAndSchema { Line 388  sub rebuildDbAndSchema {
388    return $res;    return $res;
389  }  }
390    
391    sub getListUnfiltered {
392      my $self = shift;
393      my $nodename = shift;
394      my @results;
395      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
396      # get set of objects from odbms by object name
397      my $object_set = $self->{_COREHANDLE}->remote($nodename);
398      @results = $self->{_COREHANDLE}->select($object_set);
399      return \@results;
400    }
401    
402    sub getListFiltered {
403      my $self = shift;
404    
405      # redirect to unfiltered mode
406      #return $self->getListUnfiltered(@_);
407    
408      my $nodename = shift;
409      my $filters = shift;
410      my @results;
411      $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
412    
413      #print Dumper($filters);
414      
415      my @tfilters;
416      
417      foreach my $filter (@$filters) {
418      
419        # get filter - TODO: for each filter
420        #my $filter = $filters->[0];
421      
422        # build filter
423        my $lexpr = $filter->{key};
424        #my $op = $filter->{op};
425        my $op = '=';
426        my $rexpr = $filter->{val};
427        my $tight = 100;
428        
429      #  my $tfilter = Tangram::Filter->new(
430      #    expr => "t1.$lexpr $op '$rexpr'",
431      #    tight => $tight,
432      #    objects => $objects,
433      #  );
434      
435        # HACK: build eval-string (sorry) to get filtered list - please give advice here
436        push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
437    
438      }
439    
440      my $tfilter = join(' & ', @tfilters);
441    
442      # get set of objects from odbms by object name
443      my $remote = $self->{_COREHANDLE}->remote($nodename);
444      
445      # was:
446      #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
447    
448      # is:
449      # HACK: build eval-string (sorry) to get filtered list - please give advice here
450      my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
451      
452      # get filtered list/set
453      @results = eval($evalstring);
454      die $@ if $@;
455      
456      return \@results;
457    }
458    
459    sub createCursor {
460      my $self = shift;
461      my $node = shift;
462      my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
463      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
464      return $result;
465    }
466    
467    sub createSet {
468      my $self = shift;
469    #print "-" x 80, "\n";
470    #print Dumper(@_);
471      my @objects = @_;
472      my $rh = Set::Object->new();
473      foreach (@objects) {
474        if (!isEmpty($_)) {
475    #print Dumper($_);
476          $rh->insert($_);
477        }
478      }
479      #print Dumper($rh->members());
480      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
481      return $result;
482    }
483    
484    sub sendQuery {
485      my $self = shift;
486      my $query = shift;
487      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
488      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
489    
490      #print Dumper($query);
491    
492      # HACK: special case: querying by id does not translate into a common tangram query
493      # just load the object by given id(ent)
494      if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {
495        #print "LOAD!!!", "\n";
496        #exit;
497        #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
498        my $ident = $query->{criterias}->[0]->{val};
499    #print "load obj", "\n";
500        #return $self->createSet() if $ident == 5;
501        $self->{_COREHANDLE}->unload($ident);
502        my $object = $self->{_COREHANDLE}->load($ident);
503    #print "get id", "\n";
504        my $oid = $self->{_COREHANDLE}->id($object);
505    #print Dumper($object);
506    #print "oid: $oid", "\n";
507        return $self->createSet($object);
508        #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
509      }
510    
511      die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
512    
513      # TODO: do a common tangram query here
514    
515      my @crits;
516      foreach (@{$query->{criterias}}) {
517        my $op = '';
518        $op = '=' if lc $_->{op} eq 'eq';
519        push @crits, "$_->{key}$op'$_->{val}'";
520      }
521      my $subnodes = {};
522      map { $subnodes->{$_}++ } @{$query->{subnodes}};
523      # HACK: this is hardcoded ;(    expand possibilities!
524      #my $crit = join(' AND ', @crits);
525      #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
526      #return $self->sendCommand($sql);
527      #my $h = $self->{COREHANDLE}->remote($query->{node});
528      #my $res = $self->{COREHANDLE}->select($h, $h->{);
529      return $self->createCursor($query->{node});
530    }
531    
532    sub eraseAll {
533      my $self = shift;
534      my $classname = shift;
535      my $remote = $self->{_COREHANDLE}->remote($classname);
536      my @objs = $self->{_COREHANDLE}->select($remote);
537      $self->{_COREHANDLE}->erase(@objs);
538    }
539    
540    sub createDb {
541      my $self = shift;
542      my $storage = $self->_getSubLayerHandle();
543      return $storage->createDb();
544    }
545    
546    sub getObject {
547      my $self = shift;
548      my $oid = shift;
549      my $options = shift;
550    
551      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
552      # unload($oid) will only unload object, not deep object hashes
553      $self->{_COREHANDLE}->unload() if ($options->{destroy});
554    
555      # TODO: review this
556      #if (!$self->{COREHANDLE}) { return; }
557    
558      # TODO: review this
559      my $object = eval('$self->{_COREHANDLE}->load($oid);');
560      print $@, "\n" if $@;
561    
562      return $object if $object;
563    }
564    
565    sub getObjectByGuid {
566      my $self = shift;
567      my $guid = shift;
568      my $options = shift;
569      
570      # Guid and Classname is needed
571      if(!$guid || !$options->{classname}) {
572       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
573        return;
574      }
575      
576      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
577      # unload($oid) will only unload object, not deep object hashes
578      $self->{_COREHANDLE}->unload() if ($options->{destroy});
579    
580      # search for object with given Classname and Guid
581      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
582      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
583      
584      # we searched for global unique identifer of some object,
585      # so I think we can trust there would be only one result
586      if($result[0]) {
587        return $result[0];
588      } else {
589        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
590        return;
591      }
592      
593    }
594    
595    sub getObjectAsHash {
596      my $self = shift;
597      my $oid = shift;
598      my $options = shift;  
599      my $obj;
600      
601      if($options->{guid}) {
602        $obj = $self->getObjectByGuid($oid, $options);
603      } else {
604        $obj = $self->getObject($oid, $options);
605      }
606      
607      # build options (a callback to unload autovivified objects) for 'expand'
608      # TODO: use $logger to write to debug here!
609      my $cb; # = sub {};
610    =pod
611      if ($options->{destroy}) {
612        $options->{cb}->{destroy} = sub {
613          print "================ DESTROY", "\n";
614          my $object = shift;
615          #print Dumper($object);
616          $self->{_COREHANDLE}->unload($object);
617          #undef($object);
618        };
619      }
620    =cut
621    
622      my $hash = object2hash($obj, $options);
623      #$options->{cb}->{destroy}->($obj);
624      #$self->{_COREHANDLE}->unload($obj);
625      
626      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
627      # now done in object2hash
628      #var2utf8($hash) if ($options->{utf8});
629      
630      # old (wrong) attempts to get rid of used instances, if requested
631        #$obj->clear_refs;
632        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
633        #$obj->DESTROY;
634        #undef($obj) if($options->{destroy});
635      
636      return $hash;
637    }
638    
639    sub getSchema {
640      return $schema_tangram;
641    }
642    
643    sub getCOREHANDLE {
644      my $self = shift;
645      return $self->{_COREHANDLE};
646    }
647    
648  1;  1;

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

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