/[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.22 - (show annotations)
Wed Dec 18 22:28:16 2002 UTC (21 years, 6 months ago) by jonen
Branch: MAIN
Changes since 1.21: +8 -3 lines
+ added extended logging at 'getObjectByGuid()'

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

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