/[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.32 - (hide annotations)
Tue Apr 8 22:52:22 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.31: +14 -8 lines
modified 'querySchema': better behaviour regarding filtering result

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

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