/[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.43 - (hide annotations)
Wed Jul 2 11:07:12 2003 UTC (21 years ago) by jonen
Branch: MAIN
Changes since 1.42: +8 -7 lines
re-activate filtering of results *after* results are fetched from tangram
  (needed for e.g. UserManagment)

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

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