/[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.48 - (hide annotations)
Thu Oct 28 11:35:51 2004 UTC (19 years, 8 months ago) by jonen
Branch: MAIN
Changes since 1.47: +16 -6 lines
+ bugfix related to select-criterias(filter)

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

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