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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (hide annotations)
Fri Jan 31 06:30:59 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.26: +17 -4 lines
+ enabled 'sendQuery'

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

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