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