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

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

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