/[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.18 - (show annotations)
Fri Dec 13 21:48:07 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.17: +13 -3 lines
+ fix to 'sub sendQuery'

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

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