1 |
cvsjoko |
1.1 |
################################# |
2 |
|
|
# |
3 |
joko |
1.5 |
# $Id: Tangram.pm,v 1.4 2002/10/25 11:44:44 joko Exp $ |
4 |
joko |
1.2 |
# |
5 |
|
|
# $Log: Tangram.pm,v $ |
6 |
joko |
1.5 |
# Revision 1.4 2002/10/25 11:44:44 joko |
7 |
|
|
# + sub _initSchema |
8 |
|
|
# + sub existsChildNode |
9 |
|
|
# + sub testIntegrity |
10 |
|
|
# + sub rebuildDbAndSchema |
11 |
|
|
# |
12 |
joko |
1.4 |
# Revision 1.3 2002/10/17 03:56:55 joko |
13 |
|
|
# + bugfix: trapped eval error |
14 |
|
|
# |
15 |
joko |
1.3 |
# 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 |
joko |
1.2 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
22 |
|
|
# + new |
23 |
cvsjoko |
1.1 |
# |
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 |
joko |
1.2 |
sub getNewPerlObjectByPkgName { |
46 |
|
|
my $pkgname = shift; |
47 |
|
|
my $args = shift; |
48 |
joko |
1.3 |
$logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" ); |
49 |
joko |
1.2 |
my $evstring = "use $pkgname;"; |
50 |
|
|
eval($evstring); |
51 |
joko |
1.5 |
$@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval $evstring: " . $@ ); |
52 |
joko |
1.2 |
return $pkgname->new($args); |
53 |
|
|
} |
54 |
|
|
|
55 |
joko |
1.4 |
sub _initSchema { |
56 |
|
|
my $self = shift; |
57 |
|
|
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
58 |
|
|
#if (!$self->{schema_tangram}) { |
59 |
joko |
1.5 |
my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
60 |
joko |
1.4 |
$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 |
cvsjoko |
1.1 |
sub connect { |
70 |
|
|
|
71 |
|
|
my $self = shift; |
72 |
|
|
|
73 |
|
|
my $dsn = shift; |
74 |
joko |
1.5 |
$dsn ||= $self->{locator}->{dbi}->{dsn}; |
75 |
cvsjoko |
1.1 |
|
76 |
joko |
1.2 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
77 |
cvsjoko |
1.1 |
|
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 |
joko |
1.4 |
return unless $self->_initSchema(); |
88 |
|
|
|
89 |
joko |
1.2 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
90 |
|
|
$self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); |
91 |
cvsjoko |
1.1 |
|
92 |
|
|
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
93 |
|
|
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
94 |
|
|
#$self->_configureUnderlyingStorage; |
95 |
|
|
$self->configureCOREHANDLE(); |
96 |
|
|
|
97 |
joko |
1.5 |
$self->{locator}->{status}->{connected} = 1; |
98 |
|
|
|
99 |
joko |
1.4 |
return 1; |
100 |
|
|
|
101 |
cvsjoko |
1.1 |
} |
102 |
|
|
|
103 |
|
|
sub getChildNodes { |
104 |
|
|
|
105 |
|
|
my $self = shift; |
106 |
|
|
my @nodes; |
107 |
|
|
|
108 |
joko |
1.4 |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
109 |
|
|
|
110 |
cvsjoko |
1.1 |
# 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 |
joko |
1.5 |
@nodes = @{$storage->getChildNodes()}; |
115 |
cvsjoko |
1.1 |
#$storage->_configureCOREHANDLE(); |
116 |
joko |
1.5 |
#print "getchildnodes\n"; |
117 |
|
|
#print Dumper($self); |
118 |
cvsjoko |
1.1 |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
119 |
|
|
|
120 |
|
|
$storage->disconnect(); |
121 |
|
|
|
122 |
joko |
1.4 |
$self->{meta}->{childnodes} = \@nodes; |
123 |
|
|
|
124 |
cvsjoko |
1.1 |
return \@nodes; |
125 |
|
|
|
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
joko |
1.4 |
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 |
joko |
1.5 |
#print Dumper($self->{meta}->{childnodes}); |
135 |
joko |
1.4 |
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 |
joko |
1.5 |
$self->{locator}->{status}->{integrity} = 1; |
158 |
joko |
1.4 |
return 1; |
159 |
|
|
|
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
|
163 |
cvsjoko |
1.1 |
sub _getSubLayerHandle { |
164 |
|
|
|
165 |
|
|
my $self = shift; |
166 |
|
|
|
167 |
joko |
1.4 |
$logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" ); |
168 |
|
|
|
169 |
cvsjoko |
1.1 |
#print Dumper($self); |
170 |
joko |
1.5 |
|
171 |
cvsjoko |
1.1 |
# hack, make more generic! |
172 |
joko |
1.4 |
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 |
joko |
1.5 |
my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } ); |
176 |
joko |
1.4 |
$self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } ); |
177 |
cvsjoko |
1.1 |
#$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 |
joko |
1.4 |
return $self->{dataStorageLayer}; |
184 |
cvsjoko |
1.1 |
|
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
sub _configureUnderlyingStorage { |
188 |
|
|
|
189 |
|
|
my $self = shift; |
190 |
joko |
1.4 |
|
191 |
|
|
$logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" ); |
192 |
|
|
|
193 |
cvsjoko |
1.1 |
$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 |
joko |
1.4 |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
210 |
cvsjoko |
1.1 |
|
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 |
joko |
1.2 |
} |
228 |
|
|
|
229 |
|
|
sub deploySchema { |
230 |
|
|
my $self = shift; |
231 |
joko |
1.5 |
my $args = shift; |
232 |
|
|
|
233 |
|
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
234 |
|
|
#my $dsn = $self->{dbi}->{dsn}; |
235 |
joko |
1.4 |
|
236 |
|
|
$logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); |
237 |
|
|
|
238 |
joko |
1.2 |
my $ok; |
239 |
joko |
1.5 |
# TODO: is this DBI->connect okay here like it is? regarding errors.....??? |
240 |
joko |
1.2 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
241 |
|
|
PrintError => 0, |
242 |
|
|
} ) ) { |
243 |
joko |
1.4 |
|
244 |
|
|
return unless $self->_initSchema(); |
245 |
|
|
|
246 |
|
|
$ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh ); |
247 |
joko |
1.2 |
$dbh->disconnect(); |
248 |
|
|
} |
249 |
|
|
return $ok; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
sub retreatSchema { |
253 |
joko |
1.4 |
|
254 |
joko |
1.2 |
my $self = shift; |
255 |
joko |
1.5 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
256 |
|
|
#my $dsn = $self->{dbi}->{dsn}; |
257 |
joko |
1.4 |
|
258 |
|
|
$logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); |
259 |
|
|
|
260 |
joko |
1.2 |
my $ok; |
261 |
|
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
262 |
joko |
1.4 |
#PrintError => 0, |
263 |
|
|
#RaiseError => 0, |
264 |
joko |
1.2 |
} ) ) { |
265 |
joko |
1.4 |
|
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 |
joko |
1.2 |
$dbh->disconnect(); |
276 |
|
|
} |
277 |
|
|
return $ok; |
278 |
joko |
1.4 |
} |
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 |
cvsjoko |
1.1 |
} |
299 |
|
|
|
300 |
|
|
1; |