/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (show annotations)
Sun Jan 19 02:30:05 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.24: +11 -3 lines
+ fix: modified call to '_initSchema'

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

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