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