/[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.34 - (hide annotations)
Fri Apr 11 01:18:53 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.33: +26 -8 lines
sendQuery:
+ introduced crud action 'DELETE'

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

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