/[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.39 - (hide annotations)
Fri Jun 6 11:40:40 2003 UTC (21 years, 1 month ago) by jonen
Branch: MAIN
Changes since 1.38: +24 -2 lines
fixed bug at 'getFilteredList'

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

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