/[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.10 - (show annotations)
Tue Dec 3 15:53:23 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.9: +7 -3 lines
+ small bugfix regarding object hierarchy

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

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