/[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.16 - (hide annotations)
Wed Dec 11 06:54:10 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.15: +14 -3 lines
+ fix: encapsulated object-loading inside an 'eval'

1 joko 1.6 ############################################
2 cvsjoko 1.1 #
3 joko 1.16 # $Id: Tangram.pm,v 1.15 2002/12/05 13:55:21 joko Exp $
4 joko 1.2 #
5     # $Log: Tangram.pm,v $
6 joko 1.16 # Revision 1.15 2002/12/05 13:55:21 joko
7     # + now utilizing 'object2hash' instead of 'var_deref'
8     # + played around with having fresh-objects - no progress....
9     #
10 joko 1.15 # Revision 1.14 2002/12/05 09:40:30 jonen
11     # + added option->{destroy} at getObject for unloading all instance
12     #
13 jonen 1.14 # Revision 1.13 2002/12/05 07:59:04 joko
14     # + now using Tie::SecureHash as a base for the COREHANDLE
15     # + former public COREHANDLE becomes private _COREHANDLE now
16     # + sub getCOREHANDLE
17     #
18 joko 1.13 # Revision 1.12 2002/12/04 11:34:49 joko
19     # - $schema_tangram doesn't have to be in class?
20     #
21 joko 1.12 # Revision 1.11 2002/12/04 08:54:08 jonen
22     # + untested bugfix: undef($object) after transform to hash at getObjectAsHash
23     #
24 jonen 1.11 # Revision 1.10 2002/12/03 15:53:23 joko
25     # + small bugfix regarding object hierarchy
26     #
27 joko 1.10 # Revision 1.9 2002/12/03 05:29:40 joko
28     # + sub getObject
29     # + sub getObjectAsHash
30     #
31 joko 1.9 # Revision 1.8 2002/12/01 22:25:51 joko
32     # + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
33     #
34 joko 1.8 # Revision 1.7 2002/12/01 04:46:19 joko
35     # + sub eraseAll
36     #
37 joko 1.7 # Revision 1.6 2002/11/29 05:02:30 joko
38     # - sub getNewPerlObjectByPkgName (moved to libp.pm)
39     # + sub getMetaInfo
40     # - sub existsChildNode (moved to Abstract.pm)
41     # + sub getListUnfiltered
42     # + sub getListFiltered
43     # + sub createCursor
44     # + sub createSet
45     # + sub sendQuery
46     #
47 joko 1.6 # Revision 1.5 2002/11/17 06:35:18 joko
48     # + locator metadata can now be reached via ->{locator}
49     # - getChildNodes is now wrapped via COREHANDLE
50     #
51 joko 1.5 # Revision 1.4 2002/10/25 11:44:44 joko
52     # + sub _initSchema
53     # + sub existsChildNode
54     # + sub testIntegrity
55     # + sub rebuildDbAndSchema
56     #
57 joko 1.4 # Revision 1.3 2002/10/17 03:56:55 joko
58     # + bugfix: trapped eval error
59     #
60 joko 1.3 # Revision 1.2 2002/10/17 00:10:05 joko
61     # + removed dependency from tsobj.pm, schema is now independent
62     # + sub getNewPerlObjectByPkgName
63     # + sub deploySchema
64     # + sub retreatSchema
65     #
66 joko 1.2 # Revision 1.1 2002/10/10 03:44:07 cvsjoko
67     # + new
68 cvsjoko 1.1 #
69 joko 1.6 ############################################
70    
71 cvsjoko 1.1
72     package Data::Storage::Handler::Tangram;
73    
74     use strict;
75     use warnings;
76    
77 joko 1.13 use base ("Data::Storage::Handler");
78 cvsjoko 1.1 use base ("Data::Storage::Handler::Abstract");
79    
80     use Tangram;
81     use Data::Dumper;
82 joko 1.6 use libp qw( getNewPerlObjectByPkgName );
83     use Data::Storage::Result::Tangram;
84     use Data::Compare::Struct qw( isEmpty );
85 joko 1.15 use Data::Transform::Deep qw( object2hash );
86 joko 1.9 use Data::Transform::Encode qw( var2utf8 );
87    
88 cvsjoko 1.1
89     # get logger instance
90     my $logger = Log::Dispatch::Config->instance;
91    
92    
93 joko 1.12 # this holds the complete instantiated schema from tangram
94     my $schema_tangram;
95    
96 joko 1.6 sub getMetaInfo {
97     my $self = shift;
98     $logger->debug( __PACKAGE__ . "->getMetaInfo()" );
99     return {
100     'disconnectMethod' => 'disconnect',
101     };
102 joko 1.2 }
103    
104 joko 1.4 sub _initSchema {
105     my $self = shift;
106     $logger->debug( __PACKAGE__ . "->_initSchema()" );
107 joko 1.12 #if (!$schema_tangram) {
108 joko 1.5 my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
109 joko 1.12 $schema_tangram = $obj->getSchema();
110 joko 1.4 #}
111 joko 1.12 if (!$schema_tangram) {
112 joko 1.4 $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
113     return 0;
114     }
115     return 1;
116     }
117    
118 cvsjoko 1.1 sub connect {
119    
120     my $self = shift;
121    
122     my $dsn = shift;
123 joko 1.5 $dsn ||= $self->{locator}->{dbi}->{dsn};
124 cvsjoko 1.1
125 joko 1.2 $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
126 cvsjoko 1.1
127     #my $storage = Tangram::Relational->connect( $schema, $dsn );
128     #my $storage = Tangram::mysql->connect( $schema, $dsn );
129     #$storage = Tangram::Relational->connect( Project->schema, $dsn );
130    
131     # if (!testDsn($dsn)) {
132     # croak("Database at \"$dsn\" is not available");
133     # return;
134     # }
135    
136 joko 1.4 return unless $self->_initSchema();
137    
138 joko 1.6 # create the main tangram storage object
139 joko 1.2 #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
140 joko 1.13 $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
141    
142     #print "connect", "\n";
143     #my $core = $self->{_COREHANDLE};
144     #print Dumper($core);
145 cvsjoko 1.1
146 joko 1.6 # some attempts for configuring the wrapped underlying dbi.....
147 cvsjoko 1.1 #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
148     #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
149     #$self->_configureUnderlyingStorage;
150 joko 1.6
151     # ..... encapsulation wins!
152 cvsjoko 1.1 $self->configureCOREHANDLE();
153    
154 joko 1.5 $self->{locator}->{status}->{connected} = 1;
155    
156 joko 1.4 return 1;
157    
158 cvsjoko 1.1 }
159    
160     sub getChildNodes {
161    
162     my $self = shift;
163     my @nodes;
164    
165 joko 1.4 $logger->debug( __PACKAGE__ . "->getChildNodes()" );
166    
167 cvsjoko 1.1 # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
168     #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
169     #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
170 joko 1.6
171     # todo: should we retrieve information from the schema here
172     # rather than poorly getting table names from underlying dbi?
173 cvsjoko 1.1 my $storage = $self->_getSubLayerHandle();
174 joko 1.5 @nodes = @{$storage->getChildNodes()};
175 cvsjoko 1.1 #$storage->_configureCOREHANDLE();
176 joko 1.5 #print "getchildnodes\n";
177     #print Dumper($self);
178 cvsjoko 1.1 #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
179    
180 joko 1.8 # TODO: REVIEW
181     #$storage->disconnect();
182 cvsjoko 1.1
183 joko 1.4 $self->{meta}->{childnodes} = \@nodes;
184    
185 cvsjoko 1.1 return \@nodes;
186    
187     }
188    
189    
190 joko 1.4 sub testIntegrity {
191    
192     my $self = shift;
193    
194     $logger->debug( __PACKAGE__ . "->testIntegrity()" );
195    
196     # 1st test: are there tables?
197     if (!$self->getChildNodes()) {
198     $logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" );
199     return;
200     }
201    
202     # 2nd test: is there a table named "Tangram"?
203     if (!$self->existsChildNode("Tangram")) {
204     $logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" );
205     return;
206     }
207    
208 joko 1.5 $self->{locator}->{status}->{integrity} = 1;
209 joko 1.4 return 1;
210    
211     }
212    
213    
214 cvsjoko 1.1 sub _getSubLayerHandle {
215    
216     my $self = shift;
217    
218 joko 1.4 $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
219    
220 cvsjoko 1.1 #print Dumper($self);
221 joko 1.5
222 cvsjoko 1.1 # hack, make more generic!
223 joko 1.4 if (!$self->{dataStorageLayer}) {
224     $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
225     #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );
226 joko 1.5 my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } );
227 joko 1.4 $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );
228 cvsjoko 1.1 #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();
229     #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();
230     }
231    
232     #print Dumper($self->{STORAGE_UNDER_THE_HOOD});
233    
234 joko 1.4 return $self->{dataStorageLayer};
235 cvsjoko 1.1
236     }
237    
238     sub _configureUnderlyingStorage {
239    
240     my $self = shift;
241 joko 1.4
242     $logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" );
243    
244 cvsjoko 1.1 $self->_configureCOREHANDLE_DBI();
245     return;
246    
247     foreach my $key (keys %{$self->{dbi}}) {
248     my $val = $self->{dbi}->{$key};
249     print "entry: $key; $val", "\n";
250 joko 1.13 $self->{_COREHANDLE}->{db}->{$key} = $val;
251 cvsjoko 1.1 }
252     #print Dumper($self->{COREHANDLE}->{db});
253     }
254    
255    
256     sub configureCOREHANDLE {
257    
258     my $self = shift;
259    
260 joko 1.4 $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
261 cvsjoko 1.1
262     #my $subLayer = $self->_getSubLayerHandle();
263    
264     # apply configured modifications
265     if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
266 joko 1.13 $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
267 cvsjoko 1.1 }
268     if (exists $self->{dbi}->{RaiseError}) {
269 joko 1.13 $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
270 cvsjoko 1.1 }
271     if (exists $self->{dbi}->{PrintError}) {
272 joko 1.13 $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
273 cvsjoko 1.1 }
274     if (exists $self->{dbi}->{HandleError}) {
275 joko 1.13 $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
276 cvsjoko 1.1 }
277    
278 joko 1.2 }
279    
280     sub deploySchema {
281     my $self = shift;
282 joko 1.5 my $args = shift;
283    
284     my $dsn = $self->{locator}->{dbi}->{dsn};
285 joko 1.4
286     $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
287    
288 joko 1.2 my $ok;
289 joko 1.8 if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
290 joko 1.4 return unless $self->_initSchema();
291 joko 1.12 $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
292 joko 1.2 $dbh->disconnect();
293     }
294     return $ok;
295     }
296    
297     sub retreatSchema {
298 joko 1.4
299 joko 1.2 my $self = shift;
300 joko 1.5 my $dsn = $self->{locator}->{dbi}->{dsn};
301 joko 1.4
302     $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
303    
304 joko 1.2 my $ok;
305 joko 1.8 if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
306 joko 1.4
307     return unless $self->_initSchema();
308    
309     #use Data::Dumper; print Dumper($self);
310     $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
311    
312 joko 1.12 $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
313 joko 1.8
314     # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
315     # - ... Tangram::Relational->retreat doesn't seem to return a valid status
316     # - possible improvement:
317     # - test this by checking for count of tables in database
318     # - problem with this: there may be some left not having been included to the schema
319     # - maybe better: use "->getChildNodes"?
320     $ok = 2;
321    
322 joko 1.2 $dbh->disconnect();
323 joko 1.8
324 joko 1.2 }
325     return $ok;
326 joko 1.4 }
327    
328     sub rebuildDbAndSchema {
329     my $self = shift;
330     $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );
331     my @results;
332    
333     # sum up results (bool (0/1)) in array
334     push @results, $self->retreatSchema();
335     push @results, $self->{dataStorageLayer}->dropDb();
336     push @results, $self->{dataStorageLayer}->createDb();
337     push @results, $self->deploySchema();
338    
339     # scan array for "bad ones"
340     my $res = 1;
341     map {
342     $res = 0 if (!$_);
343     } @results;
344    
345     return $res;
346 joko 1.6 }
347    
348     sub getListUnfiltered {
349     my $self = shift;
350     my $nodename = shift;
351     my @results;
352     $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
353     # get set of objects from odbms by object name
354 joko 1.13 my $object_set = $self->{_COREHANDLE}->remote($nodename);
355     @results = $self->{_COREHANDLE}->select($object_set);
356 joko 1.6 return \@results;
357     }
358    
359     sub getListFiltered {
360     my $self = shift;
361    
362     # redirect to unfiltered mode
363     #return $self->getListUnfiltered(@_);
364    
365     my $nodename = shift;
366     my $filters = shift;
367     my @results;
368     $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
369    
370     #print Dumper($filters);
371    
372     my @tfilters;
373    
374     foreach my $filter (@$filters) {
375    
376     # get filter - TODO: for each filter
377     #my $filter = $filters->[0];
378    
379     # build filter
380     my $lexpr = $filter->{key};
381     #my $op = $filter->{op};
382     my $op = '=';
383     my $rexpr = $filter->{val};
384     my $tight = 100;
385    
386     # my $tfilter = Tangram::Filter->new(
387     # expr => "t1.$lexpr $op '$rexpr'",
388     # tight => $tight,
389     # objects => $objects,
390     # );
391    
392     # HACK: build eval-string (sorry) to get filtered list - please give advice here
393     push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
394    
395     }
396    
397     my $tfilter = join(' & ', @tfilters);
398    
399     # get set of objects from odbms by object name
400 joko 1.13 my $remote = $self->{_COREHANDLE}->remote($nodename);
401 joko 1.6
402     # was:
403     #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
404    
405     # is:
406     # HACK: build eval-string (sorry) to get filtered list - please give advice here
407 joko 1.13 my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
408 joko 1.6
409     # get filtered list/set
410     @results = eval($evalstring);
411     die $@ if $@;
412    
413     return \@results;
414     }
415    
416     sub createCursor {
417     my $self = shift;
418     my $node = shift;
419 joko 1.13 my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
420 joko 1.6 my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
421     return $result;
422     }
423    
424     sub createSet {
425     my $self = shift;
426     my @objects = @_;
427     my $rh = Set::Object->new();
428     foreach (@objects) {
429     #print Dumper($_);
430     $rh->insert($_) if !isEmpty($_);
431     }
432     #print Dumper($rh->members());
433     my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
434     return $result;
435     }
436    
437     sub sendQuery {
438     my $self = shift;
439     my $query = shift;
440     #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
441     #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
442    
443     #print Dumper($query);
444    
445     # HACK: special case: querying by id does not translate into a common tangram query
446     # just load the object by given id(ent)
447     if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {
448     #print "LOAD!!!", "\n";
449     #exit;
450     #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
451     my $ident = $query->{criterias}->[0]->{val};
452     #print "load obj", "\n";
453     #return $self->createSet() if $ident == 5;
454 joko 1.13 my $object = $self->{_COREHANDLE}->load($ident);
455 joko 1.6 #print "get id", "\n";
456 joko 1.13 my $oid = $self->{_COREHANDLE}->id($object);
457 joko 1.6 return $self->createSet($object);
458     #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
459     }
460    
461     die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
462    
463     # TODO: do a common tangram query here
464    
465     my @crits;
466     foreach (@{$query->{criterias}}) {
467     my $op = '';
468     $op = '=' if lc $_->{op} eq 'eq';
469     push @crits, "$_->{key}$op'$_->{val}'";
470     }
471     my $subnodes = {};
472     map { $subnodes->{$_}++ } @{$query->{subnodes}};
473     # HACK: this is hardcoded ;( expand possibilities!
474     #my $crit = join(' AND ', @crits);
475     #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
476     #return $self->sendCommand($sql);
477     #my $h = $self->{COREHANDLE}->remote($query->{node});
478     #my $res = $self->{COREHANDLE}->select($h, $h->{);
479     return $self->createCursor($query->{node});
480 joko 1.7 }
481    
482     sub eraseAll {
483     my $self = shift;
484     my $classname = shift;
485 joko 1.13 my $remote = $self->{_COREHANDLE}->remote($classname);
486     my @objs = $self->{_COREHANDLE}->select($remote);
487     $self->{_COREHANDLE}->erase(@objs);
488 joko 1.8 }
489    
490     sub createDb {
491     my $self = shift;
492     my $storage = $self->_getSubLayerHandle();
493     return $storage->createDb();
494 joko 1.9 }
495    
496     sub getObject {
497     my $self = shift;
498     my $oid = shift;
499 jonen 1.14 my $options = shift;
500 joko 1.16
501     # TODO: create a deep_unload method (currently _all_ objects are unloaded)
502 jonen 1.14 # unload($oid) will only unload object, not deep object hashes
503     $self->{_COREHANDLE}->unload() if($options->{destroy});
504 joko 1.16
505 joko 1.9 # TODO: review this
506     #if (!$self->{COREHANDLE}) { return; }
507 joko 1.16
508     # TODO: review this
509     my $object = eval('$self->{_COREHANDLE}->load($oid);');
510     print $@, "\n" if $@;
511    
512     return $object if $object;
513 joko 1.9 }
514    
515     sub getObjectAsHash {
516     my $self = shift;
517     my $oid = shift;
518     my $options = shift;
519 jonen 1.14 my $obj = $self->getObject($oid, $options);
520 joko 1.15
521     # build options (a callback to unload autovivified objects) for 'expand'
522     # TODO: use $logger to write to debug here!
523     my $cb; # = sub {};
524     =pod
525     if ($options->{destroy}) {
526     $options->{cb}->{destroy} = sub {
527     print "================ DESTROY", "\n";
528     my $object = shift;
529     #print Dumper($object);
530     $self->{_COREHANDLE}->unload($object);
531     #undef($object);
532     };
533     }
534     =cut
535    
536     my $hash = object2hash($obj, $options);
537     #$options->{cb}->{destroy}->($obj);
538     #$self->{_COREHANDLE}->unload($obj);
539    
540     # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
541     # now done in object2hash
542     #var2utf8($hash) if ($options->{utf8});
543    
544     # old (wrong) attempts to get rid of used instances, if requested
545     #$obj->clear_refs;
546     #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
547     #$obj->DESTROY;
548     #undef($obj) if($options->{destroy});
549    
550     return $hash;
551 joko 1.12 }
552    
553     sub getSchema {
554     return $schema_tangram;
555 joko 1.13 }
556    
557     sub getCOREHANDLE {
558     my $self = shift;
559     return $self->{_COREHANDLE};
560 cvsjoko 1.1 }
561    
562     1;

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