/[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.33 - (hide annotations)
Wed Apr 9 06:07:43 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.32: +131 -48 lines
revamped 'sub sendQuery'

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

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