/[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.26 - (hide annotations)
Thu Jan 30 22:29:47 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.25: +16 -4 lines
+ fixed module usage (removed dependency on 'libp.pm')

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

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