/[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.34 by joko, Fri Apr 11 01:18:53 2003 UTC
# Line 1  Line 1 
1  #################################  ############################################
2  #  #
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.34  2003/04/11 01:18:53  joko
7    #  sendQuery:
8    #  + introduced crud action 'DELETE'
9    #
10    #  Revision 1.33  2003/04/09 06:07:43  joko
11    #  revamped 'sub sendQuery'
12    #
13    #  Revision 1.32  2003/04/08 22:52:22  joko
14    #  modified 'querySchema': better behaviour regarding filtering result
15    #
16    #  Revision 1.31  2003/04/05 21:24:09  joko
17    #  modified 'sub getChildNodes': now contains code from 'querySchema'
18    #
19    #  Revision 1.30  2003/03/27 15:31:14  joko
20    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
21    #
22    #  Revision 1.29  2003/02/21 01:47:18  joko
23    #  purged old code
24    #  minor cosmetics
25    #
26    #  Revision 1.28  2003/02/20 20:20:26  joko
27    #  tried to get auto-disconnect working again - failed with that
28    #
29    #  Revision 1.27  2003/01/31 06:30:59  joko
30    #  + enabled 'sendQuery'
31    #
32    #  Revision 1.26  2003/01/30 22:29:47  joko
33    #  + fixed module usage (removed dependency on 'libp.pm')
34    #
35    #  Revision 1.25  2003/01/19 02:30:05  joko
36    #  + fix: modified call to '_initSchema'
37    #
38    #  Revision 1.24  2002/12/22 14:13:01  joko
39    #  + sub dropDb
40    #
41    #  Revision 1.23  2002/12/19 16:31:53  joko
42    #  +- renamed sub to 'rebuildDb'
43    #
44    #  Revision 1.22  2002/12/18 22:28:16  jonen
45    #  + added extended logging at 'getObjectByGuid()'
46    #
47    #  Revision 1.21  2002/12/16 22:20:49  jonen
48    #  + fixed bug at 'getObjectByGuid()'
49    #
50    #  Revision 1.20  2002/12/16 20:49:17  jonen
51    #  + added sub 'getObjectByGuid()'
52    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
53    #
54    #  Revision 1.19  2002/12/16 06:46:09  joko
55    #  + attempt to introduce a generic '_patchSchema' - cancelled!
56    #
57    #  Revision 1.18  2002/12/13 21:48:07  joko
58    #  + fix to 'sub sendQuery'
59    #
60    #  Revision 1.17  2002/12/12 02:51:09  joko
61    #  + cosmetics
62    #
63    #  Revision 1.16  2002/12/11 06:54:10  joko
64    #  + fix: encapsulated object-loading inside an 'eval'
65    #
66    #  Revision 1.15  2002/12/05 13:55:21  joko
67    #  + now utilizing 'expand' instead of 'var_deref'
68    #  + played around with having fresh-objects - no progress....
69    #
70    #  Revision 1.14  2002/12/05 09:40:30  jonen
71    #  + added option->{destroy} at getObject for unloading all instance
72    #
73    #  Revision 1.13  2002/12/05 07:59:04  joko
74    #  + now using Tie::SecureHash as a base for the COREHANDLE
75    #  + former public COREHANDLE becomes private _COREHANDLE now
76    #  + sub getCOREHANDLE
77    #
78    #  Revision 1.12  2002/12/04 11:34:49  joko
79    #  - $schema_tangram doesn't have to be in class?
80    #
81    #  Revision 1.11  2002/12/04 08:54:08  jonen
82    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
83    #
84    #  Revision 1.10  2002/12/03 15:53:23  joko
85    #  + small bugfix regarding object hierarchy
86    #
87    #  Revision 1.9  2002/12/03 05:29:40  joko
88    #  + sub getObject
89    #  + sub getObjectAsHash
90    #
91    #  Revision 1.8  2002/12/01 22:25:51  joko
92    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
93    #
94    #  Revision 1.7  2002/12/01 04:46:19  joko
95    #  + sub eraseAll
96    #
97    #  Revision 1.6  2002/11/29 05:02:30  joko
98    #  - sub getNewPerlObjectByPkgName (moved to libp.pm)
99    #  + sub getMetaInfo
100    #  - sub existsChildNode (moved to Abstract.pm)
101    #  + sub getListUnfiltered
102    #  + sub getListFiltered
103    #  + sub createCursor
104    #  + sub createSet
105    #  + sub sendQuery
106    #
107    #  Revision 1.5  2002/11/17 06:35:18  joko
108    #  + locator metadata can now be reached via ->{locator}
109    #  - getChildNodes is now wrapped via COREHANDLE
110    #
111    #  Revision 1.4  2002/10/25 11:44:44  joko
112    #  + sub _initSchema
113    #  + sub existsChildNode
114    #  + sub testIntegrity
115    #  + sub rebuildDbAndSchema
116    #
117    #  Revision 1.3  2002/10/17 03:56:55  joko
118    #  + bugfix: trapped eval error
119    #
120  #  Revision 1.2  2002/10/17 00:10:05  joko  #  Revision 1.2  2002/10/17 00:10:05  joko
121  #  + removed dependency from tsobj.pm, schema is now independent  #  + removed dependency from tsobj.pm, schema is now independent
122  #  + sub getNewPerlObjectByPkgName  #  + sub getNewPerlObjectByPkgName
# Line 12  Line 126 
126  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
127  #  + new  #  + new
128  #  #
129  #  ############################################
130  #################################  
131    
132  package Data::Storage::Handler::Tangram;  package Data::Storage::Handler::Tangram;
133    
134  use strict;  use strict;
135  use warnings;  use warnings;
136    
137    use base ("Data::Storage::Handler");
138  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
139    
140  use Tangram;  
141  use Data::Dumper;  use Data::Dumper;
142    use Tangram;
143    
144    use DesignPattern::Object;
145    use Data::Storage::Result::Tangram;
146    use Data::Mungle::Compare::Struct qw( isEmpty );
147    use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
148    
149  # get logger instance  # get logger instance
150  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
151    
152    
153  our $metainfo = {  # this holds the complete instantiated schema from tangram
154    'disconnectMethod' => 'disconnect',  my $schema_tangram;
 };  
155    
156  sub getNewPerlObjectByPkgName {  sub getMetaInfo {
157    my $pkgname = shift;    my $self = shift;
158    my $args = shift;    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
159    my $evstring = "use $pkgname;";    return {
160    eval($evstring);      'disconnectMethod' => 'disconnect',
161    return $pkgname->new($args);    };
162    }
163    
164    sub _initSchema {
165      my $self = shift;
166      $logger->debug( __PACKAGE__ . "->_initSchema()" );
167      #if (!$schema_tangram) {
168        #my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
169        my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
170        $schema_tangram = $obj->getSchema();
171      #}
172      if (!$schema_tangram) {
173        $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
174        return 0;
175      }
176      #$self->_patchSchema();
177      return 1;
178    }
179    
180    sub _patchSchema {
181      my $self = shift;
182      foreach (keys %{$schema_tangram->{classes}}) {
183        next if $schema_tangram->{classes}->{$_}->{abstract};
184        #next if ($_ ne 'TsBankAccount');
185        #$_ ne 'AbstractAccount' &&
186        print "class: $_", "\n";
187    #print Dumper($schema_tangram->{classes}->{$_});
188        # create new string property named 'guid'
189        my $tstring = Tangram::String->new();
190        $tstring->{name} = $tstring->{col} = 'guid';
191        # inject property into schema
192        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
193        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
194      }
195  }  }
196    
197  sub connect {  sub connect {
# Line 46  sub connect { Line 199  sub connect {
199      my $self = shift;      my $self = shift;
200            
201      my $dsn = shift;      my $dsn = shift;
202      $dsn ||= $self->{dbi}->{dsn};  
203        #print Dumper($self);
204    #exit;
205    
206        # TODO: re-enable
207        $dsn ||= $self->{locator}->{dbi}->{dsn};
208      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
209            
210      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 59  sub connect { Line 216  sub connect {
216  #      return;  #      return;
217  #    }  #    }
218    
219      my $obj = getNewPerlObjectByPkgName($self->{schema}, { EXPORT_OBJECTS => $self->{classnames} } );      #return unless $self->_initSchema();
220      $self->{schema_tangram} = $obj->getSchema();      $self->_initSchema();
221        
222        # create the main tangram storage object
223      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
224      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
225    
226    #print "connect", "\n";
227    #my $core = $self->{_COREHANDLE};
228    #print Dumper($core);
229        
230        # some attempts for configuring the wrapped underlying dbi.....
231      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
232      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
233      #$self->_configureUnderlyingStorage;      #$self->_configureUnderlyingStorage;
234        
235        # ..... encapsulation wins!
236      $self->configureCOREHANDLE();      $self->configureCOREHANDLE();
237    
238      $self->{locator}->{status}->{connected} = 1;
239    
240      return 1;
241    
242  }  }
243    
244  sub getChildNodes {  sub getChildNodes {
245    
246    my $self = shift;    my $self = shift;
247    my @nodes;    my $mode = shift;
248      my $filter = shift;
249      
250      $mode ||= 'core';
251      $filter ||= 'all';
252      
253      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
254    
255    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE    if ($mode eq 'core') {
256    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });  
257    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );      my @nodes;
258    my $storage = $self->_getSubLayerHandle();      
259    #$storage->_configureCOREHANDLE();      # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
260        #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
261        #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
262        
263        # todo: should we retrieve information from the schema here
264        # rather than poorly getting table names from underlying dbi?
265        my $storage = $self->_getSubLayerHandle();
266        @nodes = @{$storage->getChildNodes()};
267        #$storage->_configureCOREHANDLE();
268      #print "getchildnodes\n";
269      #print Dumper($self);
270        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
271        
272        # TODO: REVIEW
273        #$storage->disconnect();
274        
275        $self->{meta}->{childnodes} = \@nodes;
276    
277    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {      return \@nodes;
278    if (my $result = $storage->sendCommand( 'SHOW TABLES;' ) ) {    
279      while ( my $row = $result->_getNextEntry() ) {    } elsif ($mode eq 'root') {
280        push @nodes, $row;      
281        # FIXME: this will return *all* known classes to 'Class::Tangram',
282        # which might not be what you expect since more than one instance
283        # of Tangram may be in memory and Class::Tangram seems to
284        # offer no methods to determine this or filter its result(s) according
285        # to a specific database.
286        my @object_names = Class::Tangram::known_classes();
287        my @concret_names;
288        my $o_cnt;
289        foreach (sort @object_names) {
290          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
291          $o_cnt++;
292        }
293    
294        if ($filter eq 'all') {
295          return \@object_names;
296        } elsif ($filter eq 'concrete') {
297          return \@concret_names;
298      }      }
   }  
299        
300    $storage->disconnect();    }
301        
302    return \@nodes;  
303    }
304    
305    sub testIntegrity {
306    
307      my $self = shift;
308    
309      $logger->debug( __PACKAGE__ . "->testIntegrity()" );
310    
311      # 1st test: are there tables?
312      if (!$self->getChildNodes()) {
313        $logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" );
314        return;
315      }
316    
317      # 2nd test: is there a table named "Tangram"?
318      if (!$self->existsChildNode("Tangram")) {
319        $logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" );
320        return;
321      }
322    
323      $self->{locator}->{status}->{integrity} = 1;
324      return 1;
325    
326  }  }
327    
# Line 101  sub _getSubLayerHandle { Line 330  sub _getSubLayerHandle {
330        
331    my $self = shift;    my $self = shift;
332        
333    use Data::Dumper;    $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
334    
335    #print Dumper($self);    #print Dumper($self);
336      
337    # hack, make more generic!    # hack, make more generic!
338    if (!$self->{STORAGE_SUBLAYER}) {    if (!$self->{dataStorageLayer}) {
339      my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );      $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
340      $self->{STORAGE_SUBLAYER} = Data::Storage->new( $loc, { protected => 1 } );      #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );
341        my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } );
342        $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );
343      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();
344      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();
345    }    }
346        
347    #print Dumper($self->{STORAGE_UNDER_THE_HOOD});    #print Dumper($self->{STORAGE_UNDER_THE_HOOD});
348        
349    return $self->{STORAGE_SUBLAYER};    return $self->{dataStorageLayer};
350        
351  }  }
352    
353  sub _configureUnderlyingStorage {  sub _configureUnderlyingStorage {
354        
355    my $self = shift;    my $self = shift;
356    
357      $logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" );
358    
359    $self->_configureCOREHANDLE_DBI();    $self->_configureCOREHANDLE_DBI();
360    return;    return;
361        
362    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
363      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
364      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
365      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
366    }    }
367    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
368  }  }
# Line 137  sub configureCOREHANDLE { Line 372  sub configureCOREHANDLE {
372    
373    my $self = shift;    my $self = shift;
374    
375    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
376    
377    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
378      #print Dumper($self);
379      #exit;
380    
381    # apply configured modifications    # apply configured modifications
382      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
383        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
384      }      }
385      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
386        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
387      }      }
388      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
389        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
390      }      }
391      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
392        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
393      }      }
394    
395  }  }
396    
397  sub deploySchema {  sub deploySchema {
398    my $self = shift;    my $self = shift;
399    #my $dsn = $self->{locator}->{dbi}->{dsn};    my $args = shift;
400    my $dsn = $self->{dbi}->{dsn};    
401      my $dsn = $self->{locator}->{dbi}->{dsn};
402    
403      $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
404    
405    my $ok;    my $ok;
406    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
407                                                        PrintError => 0,      return unless $self->_initSchema();
408                                                      } ) ) {      $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema}, $dbh );  
409      $dbh->disconnect();      $dbh->disconnect();
410    }    }
411    return $ok;    return $ok;
412  }  }
413    
414  sub retreatSchema {  sub retreatSchema {
415    print "retreat\n";  
416    my $self = shift;    my $self = shift;
417    #my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
418    my $dsn = $self->{dbi}->{dsn};  
419      $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
420    
421    my $ok;    my $ok;
422    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
423                                                        PrintError => 0,  
424                                                      } ) ) {      return unless $self->_initSchema();
425      $ok = Tangram::Relational->retreat($self->{schema}, $dbh );      
426        #use Data::Dumper; print Dumper($self);
427        $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
428        
429        $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
430    
431        # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
432        # - ... Tangram::Relational->retreat doesn't seem to return a valid status
433        # - possible improvement:
434        #   - test this by checking for count of tables in database
435        #   - problem with this: there may be some left not having been included to the schema
436        #   - maybe better: use "->getChildNodes"?
437        $ok = 2;
438    
439      $dbh->disconnect();      $dbh->disconnect();
440    
441    }    }
442    return $ok;    return $ok;
443  }  }
444    
445    sub rebuildDb {
446      my $self = shift;
447      $logger->info( __PACKAGE__ . "->rebuildDb()" );
448      my @results;
449    
450      # sum up results (bool (0/1)) in array
451      push @results, $self->retreatSchema();
452      push @results, $self->{dataStorageLayer}->dropDb();
453      push @results, $self->{dataStorageLayer}->createDb();
454      push @results, $self->deploySchema();
455    
456      # scan array for "bad ones"
457      my $res = 1;
458      map {
459        $res = 0 if (!$_);
460      } @results;
461      
462      return $res;
463    }
464    
465    sub getListUnfiltered {
466      my $self = shift;
467      my $nodename = shift;
468      my @results;
469      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
470      # get set of objects from odbms by object name
471      my $object_set = $self->{_COREHANDLE}->remote($nodename);
472      @results = $self->{_COREHANDLE}->select($object_set);
473      return \@results;
474    }
475    
476    sub getListFiltered {
477      my $self = shift;
478    
479      # redirect to unfiltered mode
480      #return $self->getListUnfiltered(@_);
481    
482      my $nodename = shift;
483      my $filters = shift;
484      my @results;
485      $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
486    
487    #print Dumper($filters);
488      
489      my @tfilters;
490      
491      foreach my $filter (@$filters) {
492      
493        # get filter - TODO: for each filter
494        #my $filter = $filters->[0];
495      
496        # build filter
497        my $lexpr = $filter->{key};
498        #my $op = $filter->{op};
499        my $op = '=';
500        my $rexpr = $filter->{val};
501        my $tight = 100;
502        
503      #  my $tfilter = Tangram::Filter->new(
504      #    expr => "t1.$lexpr $op '$rexpr'",
505      #    tight => $tight,
506      #    objects => $objects,
507      #  );
508      
509        # HACK: build eval-string (sorry) to get filtered list - please give advice here
510        push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
511    
512      }
513    
514      my $tfilter = join(' & ', @tfilters);
515    
516      # get set of objects from odbms by object name
517      my $remote = $self->{_COREHANDLE}->remote($nodename);
518      
519      # was:
520      #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
521    
522      # is:
523      # HACK: build eval-string (sorry) to get filtered list - please give advice here
524      my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
525      
526      #print "eval: $evalstring", "\n";
527      
528      # get filtered list/set
529      @results = eval($evalstring);
530      die $@ if $@;
531      
532      return \@results;
533    }
534    
535    sub createCursor {
536      my $self = shift;
537      my $node = shift;
538      my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
539      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
540      return $result;
541    }
542    
543    sub createSet {
544      my $self = shift;
545    #print "-" x 80, "\n";
546    #print Dumper(@_);
547      my @objects = @_;
548      my $rh = Set::Object->new();
549      foreach (@objects) {
550        if (!isEmpty($_)) {
551    #print Dumper($_);
552          $rh->insert($_);
553        }
554      }
555      #print Dumper($rh->members());
556      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
557      return $result;
558    }
559    
560    sub sendQuery {
561      my $self = shift;
562      my $query = shift;
563    
564      #print Dumper($query);
565    
566      # type = ITEM|LIST|TRANSPARENT
567      my $type = '';
568      # mode = OID|SPECIAL
569      my $mode = '';
570      my $ident = '';
571      my $crud = '';
572      
573      
574      # dispatch type and mode
575      
576        # defaults - 1
577        if ($query->{options}) {
578          $crud = $query->{options}->{crud};
579          $crud ||= $query->{options}->{action};
580        }
581    
582        # defaults - 2
583        $type ||= 'TRANSPARENT';
584        $crud ||= 'RETRIEVE';
585    
586        if ($query->{options}->{OID}) {
587          $type = 'ITEM';
588          $mode = 'OID';
589          $ident = $query->{options}->{OID};
590        
591        } elsif (my $guid = $query->{options}->{GUID}) {
592          $type = 'TRANSPARENT';
593          $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];
594        
595        }
596      
597        # HACK: special case: querying by id does not translate into a common tangram query
598        # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
599        if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
600          $type = 'ITEM';
601          $mode = 'SPECIAL.SYNC';
602          $ident = $query->{criterias}->[0]->{val};
603        }
604      
605    
606      # execute query
607      my $result;
608    
609      if ($type eq 'ITEM' && $ident) {
610    
611        if ($mode eq 'OID') {    
612          # TODO: review this case!
613          $result = $self->getObject($ident, $query->{options});
614        
615        } elsif ($mode eq 'SPECIAL.SYNC') {
616    
617          # V1 - failed
618          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
619      
620          # hmm....
621          #return $self->createSet() if $ident == 5;
622          
623          # Unload single object before doing any further operations to
624          # expect a "fresh" object from orm when performing the next calls.
625          $self->{_COREHANDLE}->unload($ident);
626          
627          # Load object from orm.
628          my $object = $self->{_COREHANDLE}->load($ident);
629      
630          # determine object identifier (OID)
631          my $oid = $self->{_COREHANDLE}->id($object);
632          
633          # encapsulate into result/response container and return this one
634          $result = $self->createSet($object);
635          
636          # debugging
637          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
638    
639        }
640        
641      
642      } elsif ($type eq 'TRANSPARENT') {
643    
644        if ($crud eq 'RETRIEVE') {
645    
646          my $list = $self->getListFiltered($query->{node}, $query->{criterias});
647          #return $self->createSet($object);
648          #return $self->createSet($list);
649          return $self->createSet(@$list);
650        
651          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
652        
653          # try a generic tangram query here
654          # TODO: try to place an oql on top of that (search.cpan.org!)
655          my @crits;
656          foreach (@{$query->{criterias}}) {
657            my $op = '';
658            $op = '=' if lc $_->{op} eq 'eq';
659            push @crits, "$_->{key}$op'$_->{val}'";
660          }
661          my $subnodes = {};
662          map { $subnodes->{$_}++ } @{$query->{subnodes}};
663          # HACK: this is hardcoded ;(    expand possibilities!
664          #my $crit = join(' AND ', @crits);
665          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
666          #return $self->sendCommand($sql);
667          #my $h = $self->{COREHANDLE}->remote($query->{node});
668          #my $res = $self->{COREHANDLE}->select($h, $h->{);
669          $result = $self->createCursor($query->{node});
670    
671        } elsif ($crud eq 'UPDATE') {
672    
673          # Patch current query to be a loader (e.g. change action, remove payload) ...
674          my $childquery = deep_copy($query);
675          $childquery->{options}->{crud} = 'RETRIEVE';
676          delete $childquery->{payload};
677    
678          # ... to use it to fetch a fresh object using ourselves (sendQuery).
679          my $cursor = $self->sendQuery($childquery);
680          my $status = $cursor->getStatus();
681          my $object = $cursor->getNextEntry();
682    
683          # Merge values and apply value modifiers.
684          my $options = { utf8 => 1, php => 1 };
685          merge_to($object, $query->{payload}, $options);
686    
687          # Execute update operation at orm.
688          $self->update($object);
689        
690        } elsif ($crud eq 'DELETE') {
691    
692          # Patch current query to be a loader (e.g. change action) ...
693          my $childquery = deep_copy($query);
694          $childquery->{options}->{crud} = 'RETRIEVE';
695    
696          # ... to use it to fetch a fresh object using ourselves (sendQuery).
697          my $cursor = $self->sendQuery($childquery);
698          my $status = $cursor->getStatus();
699          my $object = $cursor->getNextEntry();
700    
701          $self->erase($object);
702    
703        }
704    
705      }
706    
707      return $result;
708    
709    }
710    
711    sub eraseAll {
712      my $self = shift;
713      my $classname = shift;
714      my $remote = $self->{_COREHANDLE}->remote($classname);
715      my @objs = $self->{_COREHANDLE}->select($remote);
716      $self->{_COREHANDLE}->erase(@objs);
717    }
718    
719    sub createDb {
720      my $self = shift;
721      my $storage = $self->_getSubLayerHandle();
722      return $storage->createDb();
723    }
724    
725    sub getObject {
726      my $self = shift;
727      my $oid = shift;
728      my $options = shift;
729    
730      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
731      # unload($oid) will only unload object, not deep object hashes
732      $self->{_COREHANDLE}->unload() if ($options->{destroy});
733    
734      # TODO: review this
735      #if (!$self->{COREHANDLE}) { return; }
736    
737      # TODO: review this
738      my $object = eval('$self->{_COREHANDLE}->load($oid);');
739      print $@, "\n" if $@;
740    
741      return $object if $object;
742    }
743    
744    sub getObjectByGuid_old {
745      my $self = shift;
746      my $guid = shift;
747      my $options = shift;
748      
749      # Guid and Classname is needed
750      if(!$guid || !$options->{classname}) {
751       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
752        return;
753      }
754      
755      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
756      # unload($oid) will only unload object, not deep object hashes
757      $self->{_COREHANDLE}->unload() if ($options->{destroy});
758    
759      # search for object with given Classname and Guid
760      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
761      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
762      
763      # we searched for global unique identifer of some object,
764      # so I think we can trust there would be only one result
765      if($result[0]) {
766        return $result[0];
767      } else {
768        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
769        return;
770      }
771      
772    }
773    
774    sub getObjectAsHash_old {
775      my $self = shift;
776      my $oid = shift;
777      my $options = shift;  
778      my $obj;
779      
780      if($options->{guid}) {
781        $obj = $self->getObjectByGuid($oid, $options);
782      } else {
783        $obj = $self->getObject($oid, $options);
784      }
785      
786      # build options (a callback to unload autovivified objects) for 'expand'
787      # TODO: use $logger to write to debug here!
788      my $cb; # = sub {};
789    
790      # deactivated way to get rid of used instances, if requested
791    =pod
792        if ($options->{destroy}) {
793          $options->{cb}->{destroy} = sub {
794            print "================ DESTROY", "\n";
795            my $object = shift;
796            #print Dumper($object);
797            $self->{_COREHANDLE}->unload($object);
798            #undef($object);
799          };
800        }
801    =cut
802    
803      my $hash = expand($obj, $options);
804    
805      # old (unsuccessful) attempts to get rid of used instances, if requested
806    
807        # V1:
808        #$options->{cb}->{destroy}->($obj);
809        #$self->{_COREHANDLE}->unload($obj);
810      
811        # V2:
812        #$obj->clear_refs;
813        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
814        #$obj->DESTROY;
815        #undef($obj) if($options->{destroy});
816      
817      return $hash;
818    }
819    
820    sub getSchema {
821      return $schema_tangram;
822    }
823    
824    sub getCOREHANDLE {
825      my $self = shift;
826      return $self->{_COREHANDLE};
827    }
828    
829    sub dropDb {
830      my $self = shift;
831      my $storage = $self->_getSubLayerHandle();
832      return $storage->dropDb();
833    }
834    
835    sub testAvailability {
836      my $self = shift;
837      my $storage = $self->_getSubLayerHandle();
838      return $storage->testAvailability();
839    }
840    
841    sub disconnect2 {
842      my $self = shift;
843      my $storage = $self->_getSubLayerHandle();
844      print "DISC!", "\n";
845    
846      my $storage_ll = $storage->{_COREHANDLE};
847      $storage_ll->disconnect();
848      
849      print Dumper($storage);
850      exit;
851      
852      #$self->{_COREHANDLE}
853      #$storage->disconnect();
854      $self->{dataStorageLayer}->disconnect();
855    }
856    
857  1;  1;
858    __END__

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.34

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