/[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.17 - (hide annotations)
Thu Dec 12 02:51:09 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.16: +5 -2 lines
+ cosmetics

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

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