/[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.2 by joko, Thu Oct 17 00:10:05 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
80    #  + sub _initSchema
81    #  + sub existsChildNode
82    #  + sub testIntegrity
83    #  + sub rebuildDbAndSchema
84    #
85    #  Revision 1.3  2002/10/17 03:56:55  joko
86    #  + bugfix: trapped eval error
87    #
88  #  Revision 1.2  2002/10/17 00:10:05  joko  #  Revision 1.2  2002/10/17 00:10:05  joko
89  #  + removed dependency from tsobj.pm, schema is now independent  #  + removed dependency from tsobj.pm, schema is now independent
90  #  + sub getNewPerlObjectByPkgName  #  + sub getNewPerlObjectByPkgName
# Line 12  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    my $evstring = "use $pkgname;";    return {
128    eval($evstring);      'disconnectMethod' => 'disconnect',
129    return $pkgname->new($args);    };
130    }
131    
132    sub _initSchema {
133      my $self = shift;
134      $logger->debug( __PACKAGE__ . "->_initSchema()" );
135      #if (!$schema_tangram) {
136        my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
137        $schema_tangram = $obj->getSchema();
138      #}
139      if (!$schema_tangram) {
140        $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
141        return 0;
142      }
143      #$self->_patchSchema();
144      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 {
# Line 46  sub connect { Line 166  sub connect {
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 59  sub connect { Line 179  sub connect {
179  #      return;  #      return;
180  #    }  #    }
181    
182      my $obj = getNewPerlObjectByPkgName($self->{schema}, { EXPORT_OBJECTS => $self->{classnames} } );      return unless $self->_initSchema();
183      $self->{schema_tangram} = $obj->getSchema();  
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;
203    
204  }  }
205    
206  sub getChildNodes {  sub getChildNodes {
# Line 77  sub getChildNodes { Line 208  sub getChildNodes {
208    my $self = shift;    my $self = shift;
209    my @nodes;    my @nodes;
210    
211      $logger->debug( __PACKAGE__ . "->getChildNodes()" );
212    
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;
230        
231    return \@nodes;    return \@nodes;
232    
233  }  }
234    
235    
236    sub testIntegrity {
237    
238      my $self = shift;
239    
240      $logger->debug( __PACKAGE__ . "->testIntegrity()" );
241    
242      # 1st test: are there tables?
243      if (!$self->getChildNodes()) {
244        $logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" );
245        return;
246      }
247    
248      # 2nd test: is there a table named "Tangram"?
249      if (!$self->existsChildNode("Tangram")) {
250        $logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" );
251        return;
252      }
253    
254      $self->{locator}->{status}->{integrity} = 1;
255      return 1;
256    
257    }
258    
259    
260  sub _getSubLayerHandle {  sub _getSubLayerHandle {
261        
262    my $self = shift;    my $self = shift;
263        
264    use Data::Dumper;    $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
265    
266    #print Dumper($self);    #print Dumper($self);
267      
268    # hack, make more generic!    # hack, make more generic!
269    if (!$self->{STORAGE_SUBLAYER}) {    if (!$self->{dataStorageLayer}) {
270      my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );      $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
271      $self->{STORAGE_SUBLAYER} = Data::Storage->new( $loc, { protected => 1 } );      #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->{locator}->{dbi} } );
273        $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();
276    }    }
277        
278    #print Dumper($self->{STORAGE_UNDER_THE_HOOD});    #print Dumper($self->{STORAGE_UNDER_THE_HOOD});
279        
280    return $self->{STORAGE_SUBLAYER};    return $self->{dataStorageLayer};
281        
282  }  }
283    
284  sub _configureUnderlyingStorage {  sub _configureUnderlyingStorage {
285        
286    my $self = shift;    my $self = shift;
287    
288      $logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" );
289    
290    $self->_configureCOREHANDLE_DBI();    $self->_configureCOREHANDLE_DBI();
291    return;    return;
292        
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 137  sub configureCOREHANDLE { Line 303  sub configureCOREHANDLE {
303    
304    my $self = shift;    my $self = shift;
305    
306    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
307    
308    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
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 )" );
333    
334    my $ok;    my $ok;
335    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
336                                                        PrintError => 0,      return unless $self->_initSchema();
337                                                      } ) ) {      $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema}, $dbh );  
338      $dbh->disconnect();      $dbh->disconnect();
339    }    }
340    return $ok;    return $ok;
341  }  }
342    
343  sub retreatSchema {  sub retreatSchema {
344    print "retreat\n";  
345    my $self = shift;    my $self = shift;
346    #my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
347    my $dsn = $self->{dbi}->{dsn};  
348      $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} ) ) {
352                                                        PrintError => 0,  
353                                                      } ) ) {      return unless $self->_initSchema();
354      $ok = Tangram::Relational->retreat($self->{schema}, $dbh );      
355        #use Data::Dumper; print Dumper($self);
356        $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
357        
358        $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
359    
360        # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
361        # - ... 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 rebuildDb {
375      my $self = shift;
376      $logger->info( __PACKAGE__ . "->rebuildDb()" );
377      my @results;
378    
379      # sum up results (bool (0/1)) in array
380      push @results, $self->retreatSchema();
381      push @results, $self->{dataStorageLayer}->dropDb();
382      push @results, $self->{dataStorageLayer}->createDb();
383      push @results, $self->deploySchema();
384    
385      # scan array for "bad ones"
386      my $res = 1;
387      map {
388        $res = 0 if (!$_);
389      } @results;
390      
391      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.2  
changed lines
  Added in v.1.24

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