/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

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

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