1 |
################################# |
############################################ |
2 |
# |
# |
3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.31 2003/04/05 21:24:09 joko |
7 |
|
# modified 'sub getChildNodes': now contains code from 'querySchema' |
8 |
|
# |
9 |
|
# Revision 1.30 2003/03/27 15:31:14 joko |
10 |
|
# fixes to modules regarding new namespace(s) below Data::Mungle::* |
11 |
|
# |
12 |
|
# Revision 1.29 2003/02/21 01:47:18 joko |
13 |
|
# purged old code |
14 |
|
# minor cosmetics |
15 |
|
# |
16 |
|
# Revision 1.28 2003/02/20 20:20:26 joko |
17 |
|
# tried to get auto-disconnect working again - failed with that |
18 |
|
# |
19 |
|
# Revision 1.27 2003/01/31 06:30:59 joko |
20 |
|
# + enabled 'sendQuery' |
21 |
|
# |
22 |
|
# Revision 1.26 2003/01/30 22:29:47 joko |
23 |
|
# + fixed module usage (removed dependency on 'libp.pm') |
24 |
|
# |
25 |
|
# Revision 1.25 2003/01/19 02:30:05 joko |
26 |
|
# + fix: modified call to '_initSchema' |
27 |
|
# |
28 |
|
# Revision 1.24 2002/12/22 14:13:01 joko |
29 |
|
# + sub dropDb |
30 |
|
# |
31 |
|
# Revision 1.23 2002/12/19 16:31:53 joko |
32 |
|
# +- renamed sub to 'rebuildDb' |
33 |
|
# |
34 |
|
# Revision 1.22 2002/12/18 22:28:16 jonen |
35 |
|
# + added extended logging at 'getObjectByGuid()' |
36 |
|
# |
37 |
|
# Revision 1.21 2002/12/16 22:20:49 jonen |
38 |
|
# + fixed bug at 'getObjectByGuid()' |
39 |
|
# |
40 |
|
# Revision 1.20 2002/12/16 20:49:17 jonen |
41 |
|
# + added sub 'getObjectByGuid()' |
42 |
|
# + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()' |
43 |
|
# |
44 |
|
# Revision 1.19 2002/12/16 06:46:09 joko |
45 |
|
# + attempt to introduce a generic '_patchSchema' - cancelled! |
46 |
|
# |
47 |
|
# Revision 1.18 2002/12/13 21:48:07 joko |
48 |
|
# + fix to 'sub sendQuery' |
49 |
|
# |
50 |
|
# Revision 1.17 2002/12/12 02:51:09 joko |
51 |
|
# + cosmetics |
52 |
|
# |
53 |
|
# Revision 1.16 2002/12/11 06:54:10 joko |
54 |
|
# + fix: encapsulated object-loading inside an 'eval' |
55 |
|
# |
56 |
|
# Revision 1.15 2002/12/05 13:55:21 joko |
57 |
|
# + now utilizing 'expand' instead of 'var_deref' |
58 |
|
# + played around with having fresh-objects - no progress.... |
59 |
|
# |
60 |
|
# Revision 1.14 2002/12/05 09:40:30 jonen |
61 |
|
# + added option->{destroy} at getObject for unloading all instance |
62 |
|
# |
63 |
|
# Revision 1.13 2002/12/05 07:59:04 joko |
64 |
|
# + now using Tie::SecureHash as a base for the COREHANDLE |
65 |
|
# + former public COREHANDLE becomes private _COREHANDLE now |
66 |
|
# + sub getCOREHANDLE |
67 |
|
# |
68 |
|
# Revision 1.12 2002/12/04 11:34:49 joko |
69 |
|
# - $schema_tangram doesn't have to be in class? |
70 |
|
# |
71 |
|
# Revision 1.11 2002/12/04 08:54:08 jonen |
72 |
|
# + untested bugfix: undef($object) after transform to hash at getObjectAsHash |
73 |
|
# |
74 |
|
# Revision 1.10 2002/12/03 15:53:23 joko |
75 |
|
# + small bugfix regarding object hierarchy |
76 |
|
# |
77 |
|
# Revision 1.9 2002/12/03 05:29:40 joko |
78 |
|
# + sub getObject |
79 |
|
# + sub getObjectAsHash |
80 |
|
# |
81 |
|
# Revision 1.8 2002/12/01 22:25:51 joko |
82 |
|
# + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode |
83 |
|
# |
84 |
|
# Revision 1.7 2002/12/01 04:46:19 joko |
85 |
|
# + sub eraseAll |
86 |
|
# |
87 |
|
# Revision 1.6 2002/11/29 05:02:30 joko |
88 |
|
# - sub getNewPerlObjectByPkgName (moved to libp.pm) |
89 |
|
# + sub getMetaInfo |
90 |
|
# - sub existsChildNode (moved to Abstract.pm) |
91 |
|
# + sub getListUnfiltered |
92 |
|
# + sub getListFiltered |
93 |
|
# + sub createCursor |
94 |
|
# + sub createSet |
95 |
|
# + sub sendQuery |
96 |
|
# |
97 |
|
# Revision 1.5 2002/11/17 06:35:18 joko |
98 |
|
# + locator metadata can now be reached via ->{locator} |
99 |
|
# - getChildNodes is now wrapped via COREHANDLE |
100 |
|
# |
101 |
|
# Revision 1.4 2002/10/25 11:44:44 joko |
102 |
|
# + sub _initSchema |
103 |
|
# + sub existsChildNode |
104 |
|
# + sub testIntegrity |
105 |
|
# + sub rebuildDbAndSchema |
106 |
|
# |
107 |
|
# Revision 1.3 2002/10/17 03:56:55 joko |
108 |
|
# + bugfix: trapped eval error |
109 |
|
# |
110 |
# Revision 1.2 2002/10/17 00:10:05 joko |
# Revision 1.2 2002/10/17 00:10:05 joko |
111 |
# + removed dependency from tsobj.pm, schema is now independent |
# + removed dependency from tsobj.pm, schema is now independent |
112 |
# + sub getNewPerlObjectByPkgName |
# + sub getNewPerlObjectByPkgName |
116 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
117 |
# + new |
# + new |
118 |
# |
# |
119 |
# |
############################################ |
120 |
################################# |
|
121 |
|
|
122 |
package Data::Storage::Handler::Tangram; |
package Data::Storage::Handler::Tangram; |
123 |
|
|
124 |
use strict; |
use strict; |
125 |
use warnings; |
use warnings; |
126 |
|
|
127 |
|
use base ("Data::Storage::Handler"); |
128 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
129 |
|
|
130 |
use Tangram; |
|
131 |
use Data::Dumper; |
use Data::Dumper; |
132 |
|
use Tangram; |
133 |
|
|
134 |
|
use DesignPattern::Object; |
135 |
|
use Data::Storage::Result::Tangram; |
136 |
|
use Data::Mungle::Compare::Struct qw( isEmpty ); |
137 |
|
use Data::Mungle::Transform::Deep qw( expand ); |
138 |
|
|
139 |
# get logger instance |
# get logger instance |
140 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
141 |
|
|
142 |
|
|
143 |
our $metainfo = { |
# this holds the complete instantiated schema from tangram |
144 |
'disconnectMethod' => 'disconnect', |
my $schema_tangram; |
|
}; |
|
145 |
|
|
146 |
sub getNewPerlObjectByPkgName { |
sub getMetaInfo { |
147 |
my $pkgname = shift; |
my $self = shift; |
148 |
my $args = shift; |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
149 |
my $evstring = "use $pkgname;"; |
return { |
150 |
eval($evstring); |
'disconnectMethod' => 'disconnect', |
151 |
return $pkgname->new($args); |
}; |
152 |
|
} |
153 |
|
|
154 |
|
sub _initSchema { |
155 |
|
my $self = shift; |
156 |
|
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
157 |
|
#if (!$schema_tangram) { |
158 |
|
#my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
159 |
|
my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); |
160 |
|
$schema_tangram = $obj->getSchema(); |
161 |
|
#} |
162 |
|
if (!$schema_tangram) { |
163 |
|
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
164 |
|
return 0; |
165 |
|
} |
166 |
|
#$self->_patchSchema(); |
167 |
|
return 1; |
168 |
|
} |
169 |
|
|
170 |
|
sub _patchSchema { |
171 |
|
my $self = shift; |
172 |
|
foreach (keys %{$schema_tangram->{classes}}) { |
173 |
|
next if $schema_tangram->{classes}->{$_}->{abstract}; |
174 |
|
#next if ($_ ne 'TsBankAccount'); |
175 |
|
#$_ ne 'AbstractAccount' && |
176 |
|
print "class: $_", "\n"; |
177 |
|
#print Dumper($schema_tangram->{classes}->{$_}); |
178 |
|
# create new string property named 'guid' |
179 |
|
my $tstring = Tangram::String->new(); |
180 |
|
$tstring->{name} = $tstring->{col} = 'guid'; |
181 |
|
# inject property into schema |
182 |
|
#$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring; |
183 |
|
print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}); |
184 |
|
} |
185 |
} |
} |
186 |
|
|
187 |
sub connect { |
sub connect { |
189 |
my $self = shift; |
my $self = shift; |
190 |
|
|
191 |
my $dsn = shift; |
my $dsn = shift; |
192 |
$dsn ||= $self->{dbi}->{dsn}; |
|
193 |
|
#print Dumper($self); |
194 |
|
#exit; |
195 |
|
|
196 |
|
# TODO: re-enable |
197 |
|
$dsn ||= $self->{locator}->{dbi}->{dsn}; |
198 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
199 |
|
|
200 |
#my $storage = Tangram::Relational->connect( $schema, $dsn ); |
#my $storage = Tangram::Relational->connect( $schema, $dsn ); |
206 |
# return; |
# return; |
207 |
# } |
# } |
208 |
|
|
209 |
my $obj = getNewPerlObjectByPkgName($self->{schema}, { EXPORT_OBJECTS => $self->{classnames} } ); |
#return unless $self->_initSchema(); |
210 |
$self->{schema_tangram} = $obj->getSchema(); |
$self->_initSchema(); |
211 |
|
|
212 |
|
# create the main tangram storage object |
213 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
214 |
$self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); |
$self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn ); |
215 |
|
|
216 |
|
#print "connect", "\n"; |
217 |
|
#my $core = $self->{_COREHANDLE}; |
218 |
|
#print Dumper($core); |
219 |
|
|
220 |
|
# some attempts for configuring the wrapped underlying dbi..... |
221 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
222 |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
223 |
#$self->_configureUnderlyingStorage; |
#$self->_configureUnderlyingStorage; |
224 |
|
|
225 |
|
# ..... encapsulation wins! |
226 |
$self->configureCOREHANDLE(); |
$self->configureCOREHANDLE(); |
227 |
|
|
228 |
|
$self->{locator}->{status}->{connected} = 1; |
229 |
|
|
230 |
|
return 1; |
231 |
|
|
232 |
} |
} |
233 |
|
|
234 |
sub getChildNodes { |
sub getChildNodes { |
235 |
|
|
236 |
my $self = shift; |
my $self = shift; |
237 |
my @nodes; |
my $mode = shift; |
238 |
|
$mode ||= 'core'; |
239 |
|
|
240 |
|
$logger->debug( __PACKAGE__ . "->getChildNodes($mode)" ); |
241 |
|
|
242 |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
if ($mode eq 'core') { |
|
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
|
|
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
|
|
my $storage = $self->_getSubLayerHandle(); |
|
|
#$storage->_configureCOREHANDLE(); |
|
243 |
|
|
244 |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
my @nodes; |
245 |
if (my $result = $storage->sendCommand( 'SHOW TABLES;' ) ) { |
|
246 |
while ( my $row = $result->_getNextEntry() ) { |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
247 |
push @nodes, $row; |
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
248 |
|
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
249 |
|
|
250 |
|
# todo: should we retrieve information from the schema here |
251 |
|
# rather than poorly getting table names from underlying dbi? |
252 |
|
my $storage = $self->_getSubLayerHandle(); |
253 |
|
@nodes = @{$storage->getChildNodes()}; |
254 |
|
#$storage->_configureCOREHANDLE(); |
255 |
|
#print "getchildnodes\n"; |
256 |
|
#print Dumper($self); |
257 |
|
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
258 |
|
|
259 |
|
# TODO: REVIEW |
260 |
|
#$storage->disconnect(); |
261 |
|
|
262 |
|
$self->{meta}->{childnodes} = \@nodes; |
263 |
|
|
264 |
|
return \@nodes; |
265 |
|
|
266 |
|
} elsif ($mode eq 'root') { |
267 |
|
|
268 |
|
# FIXME: this will return *all* known classes to 'Class::Tangram', |
269 |
|
# which might not be what you expect since more than one instance |
270 |
|
# of Tangram may be in memory and Class::Tangram seems to |
271 |
|
# offer no methods to determine this or filter its result(s) according |
272 |
|
# to a specific database. |
273 |
|
my @object_names = Class::Tangram::known_classes(); |
274 |
|
my @concret_names; |
275 |
|
my $o_cnt; |
276 |
|
foreach (sort @object_names) { |
277 |
|
push @concret_names, $_ if (!Class::Tangram::class_is_abstract($_)); |
278 |
|
$o_cnt++; |
279 |
} |
} |
|
} |
|
280 |
|
|
281 |
$storage->disconnect(); |
my $result = { |
282 |
|
all => \@object_names, |
283 |
|
concrete => \@concret_names, |
284 |
|
}; |
285 |
|
return $result; |
286 |
|
|
287 |
|
} |
288 |
|
|
289 |
return \@nodes; |
|
290 |
|
} |
291 |
|
|
292 |
|
sub testIntegrity { |
293 |
|
|
294 |
|
my $self = shift; |
295 |
|
|
296 |
|
$logger->debug( __PACKAGE__ . "->testIntegrity()" ); |
297 |
|
|
298 |
|
# 1st test: are there tables? |
299 |
|
if (!$self->getChildNodes()) { |
300 |
|
$logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" ); |
301 |
|
return; |
302 |
|
} |
303 |
|
|
304 |
|
# 2nd test: is there a table named "Tangram"? |
305 |
|
if (!$self->existsChildNode("Tangram")) { |
306 |
|
$logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" ); |
307 |
|
return; |
308 |
|
} |
309 |
|
|
310 |
|
$self->{locator}->{status}->{integrity} = 1; |
311 |
|
return 1; |
312 |
|
|
313 |
} |
} |
314 |
|
|
317 |
|
|
318 |
my $self = shift; |
my $self = shift; |
319 |
|
|
320 |
use Data::Dumper; |
$logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" ); |
321 |
|
|
322 |
#print Dumper($self); |
#print Dumper($self); |
323 |
|
|
324 |
# hack, make more generic! |
# hack, make more generic! |
325 |
if (!$self->{STORAGE_SUBLAYER}) { |
if (!$self->{dataStorageLayer}) { |
326 |
my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} ); |
$logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" ); |
327 |
$self->{STORAGE_SUBLAYER} = Data::Storage->new( $loc, { protected => 1 } ); |
#my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} ); |
328 |
|
my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } ); |
329 |
|
$self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } ); |
330 |
#$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE(); |
#$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE(); |
331 |
#$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE(); |
#$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE(); |
332 |
} |
} |
333 |
|
|
334 |
#print Dumper($self->{STORAGE_UNDER_THE_HOOD}); |
#print Dumper($self->{STORAGE_UNDER_THE_HOOD}); |
335 |
|
|
336 |
return $self->{STORAGE_SUBLAYER}; |
return $self->{dataStorageLayer}; |
337 |
|
|
338 |
} |
} |
339 |
|
|
340 |
sub _configureUnderlyingStorage { |
sub _configureUnderlyingStorage { |
341 |
|
|
342 |
my $self = shift; |
my $self = shift; |
343 |
|
|
344 |
|
$logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" ); |
345 |
|
|
346 |
$self->_configureCOREHANDLE_DBI(); |
$self->_configureCOREHANDLE_DBI(); |
347 |
return; |
return; |
348 |
|
|
349 |
foreach my $key (keys %{$self->{dbi}}) { |
foreach my $key (keys %{$self->{dbi}}) { |
350 |
my $val = $self->{dbi}->{$key}; |
my $val = $self->{dbi}->{$key}; |
351 |
print "entry: $key; $val", "\n"; |
print "entry: $key; $val", "\n"; |
352 |
$self->{COREHANDLE}->{db}->{$key} = $val; |
$self->{_COREHANDLE}->{db}->{$key} = $val; |
353 |
} |
} |
354 |
#print Dumper($self->{COREHANDLE}->{db}); |
#print Dumper($self->{COREHANDLE}->{db}); |
355 |
} |
} |
359 |
|
|
360 |
my $self = shift; |
my $self = shift; |
361 |
|
|
362 |
$logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
363 |
|
|
364 |
#my $subLayer = $self->_getSubLayerHandle(); |
#my $subLayer = $self->_getSubLayerHandle(); |
365 |
|
#print Dumper($self); |
366 |
|
#exit; |
367 |
|
|
368 |
# apply configured modifications |
# apply configured modifications |
369 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
370 |
$self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
$self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
371 |
} |
} |
372 |
if (exists $self->{dbi}->{RaiseError}) { |
if (exists $self->{dbi}->{RaiseError}) { |
373 |
$self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
$self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
374 |
} |
} |
375 |
if (exists $self->{dbi}->{PrintError}) { |
if (exists $self->{dbi}->{PrintError}) { |
376 |
$self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
$self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
377 |
} |
} |
378 |
if (exists $self->{dbi}->{HandleError}) { |
if (exists $self->{dbi}->{HandleError}) { |
379 |
$self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
$self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
380 |
} |
} |
381 |
|
|
382 |
} |
} |
383 |
|
|
384 |
sub deploySchema { |
sub deploySchema { |
385 |
my $self = shift; |
my $self = shift; |
386 |
#my $dsn = $self->{locator}->{dbi}->{dsn}; |
my $args = shift; |
387 |
my $dsn = $self->{dbi}->{dsn}; |
|
388 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
389 |
|
|
390 |
|
$logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); |
391 |
|
|
392 |
my $ok; |
my $ok; |
393 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
394 |
PrintError => 0, |
return unless $self->_initSchema(); |
395 |
} ) ) { |
$ok = Tangram::Relational->deploy($schema_tangram, $dbh ); |
|
$ok = Tangram::Relational->deploy($self->{schema}, $dbh ); |
|
396 |
$dbh->disconnect(); |
$dbh->disconnect(); |
397 |
} |
} |
398 |
return $ok; |
return $ok; |
399 |
} |
} |
400 |
|
|
401 |
sub retreatSchema { |
sub retreatSchema { |
402 |
print "retreat\n"; |
|
403 |
my $self = shift; |
my $self = shift; |
404 |
#my $dsn = $self->{locator}->{dbi}->{dsn}; |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
405 |
my $dsn = $self->{dbi}->{dsn}; |
|
406 |
|
$logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); |
407 |
|
|
408 |
my $ok; |
my $ok; |
409 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
410 |
PrintError => 0, |
|
411 |
} ) ) { |
return unless $self->_initSchema(); |
412 |
$ok = Tangram::Relational->retreat($self->{schema}, $dbh ); |
|
413 |
|
#use Data::Dumper; print Dumper($self); |
414 |
|
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
415 |
|
|
416 |
|
$ok = Tangram::Relational->retreat($schema_tangram, $dbh ); |
417 |
|
|
418 |
|
# answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because... |
419 |
|
# - ... Tangram::Relational->retreat doesn't seem to return a valid status |
420 |
|
# - possible improvement: |
421 |
|
# - test this by checking for count of tables in database |
422 |
|
# - problem with this: there may be some left not having been included to the schema |
423 |
|
# - maybe better: use "->getChildNodes"? |
424 |
|
$ok = 2; |
425 |
|
|
426 |
$dbh->disconnect(); |
$dbh->disconnect(); |
427 |
|
|
428 |
} |
} |
429 |
return $ok; |
return $ok; |
430 |
} |
} |
431 |
|
|
432 |
|
sub rebuildDb { |
433 |
|
my $self = shift; |
434 |
|
$logger->info( __PACKAGE__ . "->rebuildDb()" ); |
435 |
|
my @results; |
436 |
|
|
437 |
|
# sum up results (bool (0/1)) in array |
438 |
|
push @results, $self->retreatSchema(); |
439 |
|
push @results, $self->{dataStorageLayer}->dropDb(); |
440 |
|
push @results, $self->{dataStorageLayer}->createDb(); |
441 |
|
push @results, $self->deploySchema(); |
442 |
|
|
443 |
|
# scan array for "bad ones" |
444 |
|
my $res = 1; |
445 |
|
map { |
446 |
|
$res = 0 if (!$_); |
447 |
|
} @results; |
448 |
|
|
449 |
|
return $res; |
450 |
|
} |
451 |
|
|
452 |
|
sub getListUnfiltered { |
453 |
|
my $self = shift; |
454 |
|
my $nodename = shift; |
455 |
|
my @results; |
456 |
|
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
457 |
|
# get set of objects from odbms by object name |
458 |
|
my $object_set = $self->{_COREHANDLE}->remote($nodename); |
459 |
|
@results = $self->{_COREHANDLE}->select($object_set); |
460 |
|
return \@results; |
461 |
|
} |
462 |
|
|
463 |
|
sub getListFiltered { |
464 |
|
my $self = shift; |
465 |
|
|
466 |
|
# redirect to unfiltered mode |
467 |
|
#return $self->getListUnfiltered(@_); |
468 |
|
|
469 |
|
my $nodename = shift; |
470 |
|
my $filters = shift; |
471 |
|
my @results; |
472 |
|
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
473 |
|
|
474 |
|
#print Dumper($filters); |
475 |
|
|
476 |
|
my @tfilters; |
477 |
|
|
478 |
|
foreach my $filter (@$filters) { |
479 |
|
|
480 |
|
# get filter - TODO: for each filter |
481 |
|
#my $filter = $filters->[0]; |
482 |
|
|
483 |
|
# build filter |
484 |
|
my $lexpr = $filter->{key}; |
485 |
|
#my $op = $filter->{op}; |
486 |
|
my $op = '='; |
487 |
|
my $rexpr = $filter->{val}; |
488 |
|
my $tight = 100; |
489 |
|
|
490 |
|
# my $tfilter = Tangram::Filter->new( |
491 |
|
# expr => "t1.$lexpr $op '$rexpr'", |
492 |
|
# tight => $tight, |
493 |
|
# objects => $objects, |
494 |
|
# ); |
495 |
|
|
496 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
497 |
|
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
498 |
|
|
499 |
|
} |
500 |
|
|
501 |
|
my $tfilter = join(' & ', @tfilters); |
502 |
|
|
503 |
|
# get set of objects from odbms by object name |
504 |
|
my $remote = $self->{_COREHANDLE}->remote($nodename); |
505 |
|
|
506 |
|
# was: |
507 |
|
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
508 |
|
|
509 |
|
# is: |
510 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
511 |
|
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
512 |
|
|
513 |
|
#print "eval: $evalstring", "\n"; |
514 |
|
|
515 |
|
# get filtered list/set |
516 |
|
@results = eval($evalstring); |
517 |
|
die $@ if $@; |
518 |
|
|
519 |
|
return \@results; |
520 |
|
} |
521 |
|
|
522 |
|
sub createCursor { |
523 |
|
my $self = shift; |
524 |
|
my $node = shift; |
525 |
|
my $cmdHandle = $self->{_COREHANDLE}->cursor($node); |
526 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
527 |
|
return $result; |
528 |
|
} |
529 |
|
|
530 |
|
sub createSet { |
531 |
|
my $self = shift; |
532 |
|
#print "-" x 80, "\n"; |
533 |
|
#print Dumper(@_); |
534 |
|
my @objects = @_; |
535 |
|
my $rh = Set::Object->new(); |
536 |
|
foreach (@objects) { |
537 |
|
if (!isEmpty($_)) { |
538 |
|
#print Dumper($_); |
539 |
|
$rh->insert($_); |
540 |
|
} |
541 |
|
} |
542 |
|
#print Dumper($rh->members()); |
543 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
544 |
|
return $result; |
545 |
|
} |
546 |
|
|
547 |
|
sub sendQuery { |
548 |
|
my $self = shift; |
549 |
|
my $query = shift; |
550 |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
551 |
|
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
552 |
|
|
553 |
|
#print Dumper($query); |
554 |
|
|
555 |
|
# HACK: special case: querying by id does not translate into a common tangram query |
556 |
|
# just load the object by given id(ent) |
557 |
|
if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') { |
558 |
|
#print "LOAD!!!", "\n"; |
559 |
|
#exit; |
560 |
|
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
561 |
|
my $ident = $query->{criterias}->[0]->{val}; |
562 |
|
#print "load obj", "\n"; |
563 |
|
#return $self->createSet() if $ident == 5; |
564 |
|
$self->{_COREHANDLE}->unload($ident); |
565 |
|
my $object = $self->{_COREHANDLE}->load($ident); |
566 |
|
#print "get id", "\n"; |
567 |
|
my $oid = $self->{_COREHANDLE}->id($object); |
568 |
|
#print Dumper($object); |
569 |
|
#print "oid: $oid", "\n"; |
570 |
|
return $self->createSet($object); |
571 |
|
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
572 |
|
} |
573 |
|
|
574 |
|
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
575 |
|
#return $self->createSet($object); |
576 |
|
#return $self->createSet($list); |
577 |
|
return $self->createSet(@$list); |
578 |
|
|
579 |
|
#die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
580 |
|
|
581 |
|
|
582 |
|
|
583 |
|
|
584 |
|
# try a generic tangram query here |
585 |
|
# TODO: try to place an oql on top of that (search.cpan.org!) |
586 |
|
my @crits; |
587 |
|
foreach (@{$query->{criterias}}) { |
588 |
|
my $op = ''; |
589 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
590 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
591 |
|
} |
592 |
|
my $subnodes = {}; |
593 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
594 |
|
# HACK: this is hardcoded ;( expand possibilities! |
595 |
|
#my $crit = join(' AND ', @crits); |
596 |
|
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
597 |
|
#return $self->sendCommand($sql); |
598 |
|
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
599 |
|
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
600 |
|
return $self->createCursor($query->{node}); |
601 |
|
} |
602 |
|
|
603 |
|
sub eraseAll { |
604 |
|
my $self = shift; |
605 |
|
my $classname = shift; |
606 |
|
my $remote = $self->{_COREHANDLE}->remote($classname); |
607 |
|
my @objs = $self->{_COREHANDLE}->select($remote); |
608 |
|
$self->{_COREHANDLE}->erase(@objs); |
609 |
|
} |
610 |
|
|
611 |
|
sub createDb { |
612 |
|
my $self = shift; |
613 |
|
my $storage = $self->_getSubLayerHandle(); |
614 |
|
return $storage->createDb(); |
615 |
|
} |
616 |
|
|
617 |
|
sub getObject { |
618 |
|
my $self = shift; |
619 |
|
my $oid = shift; |
620 |
|
my $options = shift; |
621 |
|
|
622 |
|
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
623 |
|
# unload($oid) will only unload object, not deep object hashes |
624 |
|
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
625 |
|
|
626 |
|
# TODO: review this |
627 |
|
#if (!$self->{COREHANDLE}) { return; } |
628 |
|
|
629 |
|
# TODO: review this |
630 |
|
my $object = eval('$self->{_COREHANDLE}->load($oid);'); |
631 |
|
print $@, "\n" if $@; |
632 |
|
|
633 |
|
return $object if $object; |
634 |
|
} |
635 |
|
|
636 |
|
sub getObjectByGuid { |
637 |
|
my $self = shift; |
638 |
|
my $guid = shift; |
639 |
|
my $options = shift; |
640 |
|
|
641 |
|
# Guid and Classname is needed |
642 |
|
if(!$guid || !$options->{classname}) { |
643 |
|
$logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" ); |
644 |
|
return; |
645 |
|
} |
646 |
|
|
647 |
|
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
648 |
|
# unload($oid) will only unload object, not deep object hashes |
649 |
|
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
650 |
|
|
651 |
|
# search for object with given Classname and Guid |
652 |
|
my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname}); |
653 |
|
my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid); |
654 |
|
|
655 |
|
# we searched for global unique identifer of some object, |
656 |
|
# so I think we can trust there would be only one result |
657 |
|
if($result[0]) { |
658 |
|
return $result[0]; |
659 |
|
} else { |
660 |
|
$logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" ); |
661 |
|
return; |
662 |
|
} |
663 |
|
|
664 |
|
} |
665 |
|
|
666 |
|
sub getObjectAsHash { |
667 |
|
my $self = shift; |
668 |
|
my $oid = shift; |
669 |
|
my $options = shift; |
670 |
|
my $obj; |
671 |
|
|
672 |
|
if($options->{guid}) { |
673 |
|
$obj = $self->getObjectByGuid($oid, $options); |
674 |
|
} else { |
675 |
|
$obj = $self->getObject($oid, $options); |
676 |
|
} |
677 |
|
|
678 |
|
# build options (a callback to unload autovivified objects) for 'expand' |
679 |
|
# TODO: use $logger to write to debug here! |
680 |
|
my $cb; # = sub {}; |
681 |
|
|
682 |
|
# deactivated way to get rid of used instances, if requested |
683 |
|
=pod |
684 |
|
if ($options->{destroy}) { |
685 |
|
$options->{cb}->{destroy} = sub { |
686 |
|
print "================ DESTROY", "\n"; |
687 |
|
my $object = shift; |
688 |
|
#print Dumper($object); |
689 |
|
$self->{_COREHANDLE}->unload($object); |
690 |
|
#undef($object); |
691 |
|
}; |
692 |
|
} |
693 |
|
=cut |
694 |
|
|
695 |
|
my $hash = expand($obj, $options); |
696 |
|
|
697 |
|
# old (unsuccessful) attempts to get rid of used instances, if requested |
698 |
|
|
699 |
|
# V1: |
700 |
|
#$options->{cb}->{destroy}->($obj); |
701 |
|
#$self->{_COREHANDLE}->unload($obj); |
702 |
|
|
703 |
|
# V2: |
704 |
|
#$obj->clear_refs; |
705 |
|
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
706 |
|
#$obj->DESTROY; |
707 |
|
#undef($obj) if($options->{destroy}); |
708 |
|
|
709 |
|
return $hash; |
710 |
|
} |
711 |
|
|
712 |
|
sub getSchema { |
713 |
|
return $schema_tangram; |
714 |
|
} |
715 |
|
|
716 |
|
sub getCOREHANDLE { |
717 |
|
my $self = shift; |
718 |
|
return $self->{_COREHANDLE}; |
719 |
|
} |
720 |
|
|
721 |
|
sub dropDb { |
722 |
|
my $self = shift; |
723 |
|
my $storage = $self->_getSubLayerHandle(); |
724 |
|
return $storage->dropDb(); |
725 |
|
} |
726 |
|
|
727 |
|
sub testAvailability { |
728 |
|
my $self = shift; |
729 |
|
my $storage = $self->_getSubLayerHandle(); |
730 |
|
return $storage->testAvailability(); |
731 |
|
} |
732 |
|
|
733 |
|
sub disconnect2 { |
734 |
|
my $self = shift; |
735 |
|
my $storage = $self->_getSubLayerHandle(); |
736 |
|
print "DISC!", "\n"; |
737 |
|
|
738 |
|
my $storage_ll = $storage->{_COREHANDLE}; |
739 |
|
$storage_ll->disconnect(); |
740 |
|
|
741 |
|
print Dumper($storage); |
742 |
|
exit; |
743 |
|
|
744 |
|
#$self->{_COREHANDLE} |
745 |
|
#$storage->disconnect(); |
746 |
|
$self->{dataStorageLayer}->disconnect(); |
747 |
|
} |
748 |
|
|
749 |
1; |
1; |
750 |
|
__END__ |