/[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.42 - (hide annotations)
Tue Jul 1 23:24:17 2003 UTC (21 years ago) by joko
Branch: MAIN
Changes since 1.41: +5 -1 lines
now using package before using function

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

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