/[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.49 - (hide annotations)
Wed Nov 3 14:12:34 2004 UTC (19 years, 8 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.48: +6 -3 lines
 replaced print with logger->info

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

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