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

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

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