/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Fri Nov 29 05:02:30 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.5: +158 -26 lines
- sub getNewPerlObjectByPkgName (moved to libp.pm)
+ sub getMetaInfo
- sub existsChildNode (moved to Abstract.pm)
+ sub getListUnfiltered
+ sub getListFiltered
+ sub createCursor
+ sub createSet
+ sub sendQuery

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

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