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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.28

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