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