/[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.5 - (show annotations)
Sun Nov 17 06:35:18 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.4: +26 -18 lines
+ locator metadata can now be reached via ->{locator}
- getChildNodes is now wrapped via COREHANDLE

1 #################################
2 #
3 # $Id: Tangram.pm,v 1.4 2002/10/25 11:44:44 joko Exp $
4 #
5 # $Log: Tangram.pm,v $
6 # Revision 1.4 2002/10/25 11:44:44 joko
7 # + sub _initSchema
8 # + sub existsChildNode
9 # + sub testIntegrity
10 # + sub rebuildDbAndSchema
11 #
12 # Revision 1.3 2002/10/17 03:56:55 joko
13 # + bugfix: trapped eval error
14 #
15 # Revision 1.2 2002/10/17 00:10:05 joko
16 # + removed dependency from tsobj.pm, schema is now independent
17 # + sub getNewPerlObjectByPkgName
18 # + sub deploySchema
19 # + sub retreatSchema
20 #
21 # Revision 1.1 2002/10/10 03:44:07 cvsjoko
22 # + new
23 #
24 #
25 #################################
26
27 package Data::Storage::Handler::Tangram;
28
29 use strict;
30 use warnings;
31
32 use base ("Data::Storage::Handler::Abstract");
33
34 use Tangram;
35 use Data::Dumper;
36
37 # get logger instance
38 my $logger = Log::Dispatch::Config->instance;
39
40
41 our $metainfo = {
42 'disconnectMethod' => 'disconnect',
43 };
44
45 sub getNewPerlObjectByPkgName {
46 my $pkgname = shift;
47 my $args = shift;
48 $logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" );
49 my $evstring = "use $pkgname;";
50 eval($evstring);
51 $@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval $evstring: " . $@ );
52 return $pkgname->new($args);
53 }
54
55 sub _initSchema {
56 my $self = shift;
57 $logger->debug( __PACKAGE__ . "->_initSchema()" );
58 #if (!$self->{schema_tangram}) {
59 my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
60 $self->{schema_tangram} = $obj->getSchema();
61 #}
62 if (!$self->{schema_tangram}) {
63 $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
64 return 0;
65 }
66 return 1;
67 }
68
69 sub connect {
70
71 my $self = shift;
72
73 my $dsn = shift;
74 $dsn ||= $self->{locator}->{dbi}->{dsn};
75
76 $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
77
78 #my $storage = Tangram::Relational->connect( $schema, $dsn );
79 #my $storage = Tangram::mysql->connect( $schema, $dsn );
80 #$storage = Tangram::Relational->connect( Project->schema, $dsn );
81
82 # if (!testDsn($dsn)) {
83 # croak("Database at \"$dsn\" is not available");
84 # return;
85 # }
86
87 return unless $self->_initSchema();
88
89 #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
90 $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );
91
92 #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
93 #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
94 #$self->_configureUnderlyingStorage;
95 $self->configureCOREHANDLE();
96
97 $self->{locator}->{status}->{connected} = 1;
98
99 return 1;
100
101 }
102
103 sub getChildNodes {
104
105 my $self = shift;
106 my @nodes;
107
108 $logger->debug( __PACKAGE__ . "->getChildNodes()" );
109
110 # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
111 #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
112 #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
113 my $storage = $self->_getSubLayerHandle();
114 @nodes = @{$storage->getChildNodes()};
115 #$storage->_configureCOREHANDLE();
116 #print "getchildnodes\n";
117 #print Dumper($self);
118 #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
119
120 $storage->disconnect();
121
122 $self->{meta}->{childnodes} = \@nodes;
123
124 return \@nodes;
125
126 }
127
128
129 sub existsChildNode {
130 my $self = shift;
131 my $nodename = shift;
132 $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" );
133 $self->getChildNodes() unless $self->{meta}->{childnodes};
134 #print Dumper($self->{meta}->{childnodes});
135 return grep $nodename, @{$self->{meta}->{childnodes}};
136 }
137
138
139 sub testIntegrity {
140
141 my $self = shift;
142
143 $logger->debug( __PACKAGE__ . "->testIntegrity()" );
144
145 # 1st test: are there tables?
146 if (!$self->getChildNodes()) {
147 $logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" );
148 return;
149 }
150
151 # 2nd test: is there a table named "Tangram"?
152 if (!$self->existsChildNode("Tangram")) {
153 $logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" );
154 return;
155 }
156
157 $self->{locator}->{status}->{integrity} = 1;
158 return 1;
159
160 }
161
162
163 sub _getSubLayerHandle {
164
165 my $self = shift;
166
167 $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
168
169 #print Dumper($self);
170
171 # hack, make more generic!
172 if (!$self->{dataStorageLayer}) {
173 $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
174 #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );
175 my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } );
176 $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );
177 #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();
178 #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();
179 }
180
181 #print Dumper($self->{STORAGE_UNDER_THE_HOOD});
182
183 return $self->{dataStorageLayer};
184
185 }
186
187 sub _configureUnderlyingStorage {
188
189 my $self = shift;
190
191 $logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" );
192
193 $self->_configureCOREHANDLE_DBI();
194 return;
195
196 foreach my $key (keys %{$self->{dbi}}) {
197 my $val = $self->{dbi}->{$key};
198 print "entry: $key; $val", "\n";
199 $self->{COREHANDLE}->{db}->{$key} = $val;
200 }
201 #print Dumper($self->{COREHANDLE}->{db});
202 }
203
204
205 sub configureCOREHANDLE {
206
207 my $self = shift;
208
209 $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
210
211 #my $subLayer = $self->_getSubLayerHandle();
212
213 # apply configured modifications
214 if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
215 $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
216 }
217 if (exists $self->{dbi}->{RaiseError}) {
218 $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
219 }
220 if (exists $self->{dbi}->{PrintError}) {
221 $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
222 }
223 if (exists $self->{dbi}->{HandleError}) {
224 $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
225 }
226
227 }
228
229 sub deploySchema {
230 my $self = shift;
231 my $args = shift;
232
233 my $dsn = $self->{locator}->{dbi}->{dsn};
234 #my $dsn = $self->{dbi}->{dsn};
235
236 $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
237
238 my $ok;
239 # TODO: is this DBI->connect okay here like it is? regarding errors.....???
240 if ( my $dbh = DBI->connect($dsn, '', '', {
241 PrintError => 0,
242 } ) ) {
243
244 return unless $self->_initSchema();
245
246 $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );
247 $dbh->disconnect();
248 }
249 return $ok;
250 }
251
252 sub retreatSchema {
253
254 my $self = shift;
255 my $dsn = $self->{locator}->{dbi}->{dsn};
256 #my $dsn = $self->{dbi}->{dsn};
257
258 $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
259
260 my $ok;
261 if ( my $dbh = DBI->connect($dsn, '', '', {
262 #PrintError => 0,
263 #RaiseError => 0,
264 } ) ) {
265
266 return unless $self->_initSchema();
267
268 #use Data::Dumper; print Dumper($self);
269 $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
270
271 $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );
272 $ok = 2; # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status
273 # idea: test this by checking for count of tables in database -
274 # problem with this: there may be some left not having been included to the schema
275 $dbh->disconnect();
276 }
277 return $ok;
278 }
279
280 sub rebuildDbAndSchema {
281 my $self = shift;
282 $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );
283 my @results;
284
285 # sum up results (bool (0/1)) in array
286 push @results, $self->retreatSchema();
287 push @results, $self->{dataStorageLayer}->dropDb();
288 push @results, $self->{dataStorageLayer}->createDb();
289 push @results, $self->deploySchema();
290
291 # scan array for "bad ones"
292 my $res = 1;
293 map {
294 $res = 0 if (!$_);
295 } @results;
296
297 return $res;
298 }
299
300 1;

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