/[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.20 - (show annotations)
Mon Dec 16 20:49:17 2002 UTC (21 years, 6 months ago) by jonen
Branch: MAIN
Changes since 1.19: +40 -3 lines
+ added sub 'getObjectByGuid()'
+ added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'

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

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