/[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.4 - (show annotations)
Fri Oct 25 11:44:44 2002 UTC (21 years, 8 months ago) by joko
Branch: MAIN
Changes since 1.3: +114 -13 lines
+ sub _initSchema
+ sub existsChildNode
+ sub testIntegrity
+ sub rebuildDbAndSchema

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

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