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