/[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.8 - (show annotations)
Sun Dec 1 22:25:51 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.7: +24 -17 lines
+ now utilizing metadata from storage locator when connecting to DBI in "raw"-mode

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

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