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