/[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.37 - (hide annotations)
Sat May 10 17:37:39 2003 UTC (21 years, 2 months ago) by jonen
Branch: MAIN
Changes since 1.36: +9 -1 lines
corrected last commit

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

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