/[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.24 - (show annotations)
Sun Dec 22 14:13:01 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.23: +10 -1 lines
+ sub dropDb

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

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