/[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.23 - (show annotations)
Thu Dec 19 16:31:53 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.22: +6 -3 lines
+- renamed sub to 'rebuildDb'

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

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