/[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.28 - (hide annotations)
Thu Feb 20 20:20:26 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.27: +28 -7 lines
tried to get auto-disconnect working again - failed with that

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

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