1 |
############################################ |
2 |
# |
3 |
# $Id: Tangram.pm,v 1.35 2003/04/19 16:09:48 jonen Exp $ |
4 |
# |
5 |
# $Log: Tangram.pm,v $ |
6 |
# Revision 1.35 2003/04/19 16:09:48 jonen |
7 |
# + added operator dispatching (currently for getting ref-type) at 'getListFiltered' |
8 |
# |
9 |
# Revision 1.34 2003/04/11 01:18:53 joko |
10 |
# sendQuery: |
11 |
# + introduced crud action 'DELETE' |
12 |
# |
13 |
# Revision 1.33 2003/04/09 06:07:43 joko |
14 |
# revamped 'sub sendQuery' |
15 |
# |
16 |
# Revision 1.32 2003/04/08 22:52:22 joko |
17 |
# modified 'querySchema': better behaviour regarding filtering result |
18 |
# |
19 |
# Revision 1.31 2003/04/05 21:24:09 joko |
20 |
# modified 'sub getChildNodes': now contains code from 'querySchema' |
21 |
# |
22 |
# Revision 1.30 2003/03/27 15:31:14 joko |
23 |
# fixes to modules regarding new namespace(s) below Data::Mungle::* |
24 |
# |
25 |
# Revision 1.29 2003/02/21 01:47:18 joko |
26 |
# purged old code |
27 |
# minor cosmetics |
28 |
# |
29 |
# Revision 1.28 2003/02/20 20:20:26 joko |
30 |
# tried to get auto-disconnect working again - failed with that |
31 |
# |
32 |
# Revision 1.27 2003/01/31 06:30:59 joko |
33 |
# + enabled 'sendQuery' |
34 |
# |
35 |
# Revision 1.26 2003/01/30 22:29:47 joko |
36 |
# + fixed module usage (removed dependency on 'libp.pm') |
37 |
# |
38 |
# Revision 1.25 2003/01/19 02:30:05 joko |
39 |
# + fix: modified call to '_initSchema' |
40 |
# |
41 |
# Revision 1.24 2002/12/22 14:13:01 joko |
42 |
# + sub dropDb |
43 |
# |
44 |
# Revision 1.23 2002/12/19 16:31:53 joko |
45 |
# +- renamed sub to 'rebuildDb' |
46 |
# |
47 |
# Revision 1.22 2002/12/18 22:28:16 jonen |
48 |
# + added extended logging at 'getObjectByGuid()' |
49 |
# |
50 |
# Revision 1.21 2002/12/16 22:20:49 jonen |
51 |
# + fixed bug at 'getObjectByGuid()' |
52 |
# |
53 |
# Revision 1.20 2002/12/16 20:49:17 jonen |
54 |
# + added sub 'getObjectByGuid()' |
55 |
# + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()' |
56 |
# |
57 |
# Revision 1.19 2002/12/16 06:46:09 joko |
58 |
# + attempt to introduce a generic '_patchSchema' - cancelled! |
59 |
# |
60 |
# Revision 1.18 2002/12/13 21:48:07 joko |
61 |
# + fix to 'sub sendQuery' |
62 |
# |
63 |
# Revision 1.17 2002/12/12 02:51:09 joko |
64 |
# + cosmetics |
65 |
# |
66 |
# Revision 1.16 2002/12/11 06:54:10 joko |
67 |
# + fix: encapsulated object-loading inside an 'eval' |
68 |
# |
69 |
# Revision 1.15 2002/12/05 13:55:21 joko |
70 |
# + now utilizing 'expand' instead of 'var_deref' |
71 |
# + played around with having fresh-objects - no progress.... |
72 |
# |
73 |
# Revision 1.14 2002/12/05 09:40:30 jonen |
74 |
# + added option->{destroy} at getObject for unloading all instance |
75 |
# |
76 |
# Revision 1.13 2002/12/05 07:59:04 joko |
77 |
# + now using Tie::SecureHash as a base for the COREHANDLE |
78 |
# + former public COREHANDLE becomes private _COREHANDLE now |
79 |
# + sub getCOREHANDLE |
80 |
# |
81 |
# Revision 1.12 2002/12/04 11:34:49 joko |
82 |
# - $schema_tangram doesn't have to be in class? |
83 |
# |
84 |
# Revision 1.11 2002/12/04 08:54:08 jonen |
85 |
# + untested bugfix: undef($object) after transform to hash at getObjectAsHash |
86 |
# |
87 |
# Revision 1.10 2002/12/03 15:53:23 joko |
88 |
# + small bugfix regarding object hierarchy |
89 |
# |
90 |
# Revision 1.9 2002/12/03 05:29:40 joko |
91 |
# + sub getObject |
92 |
# + sub getObjectAsHash |
93 |
# |
94 |
# Revision 1.8 2002/12/01 22:25:51 joko |
95 |
# + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode |
96 |
# |
97 |
# Revision 1.7 2002/12/01 04:46:19 joko |
98 |
# + sub eraseAll |
99 |
# |
100 |
# Revision 1.6 2002/11/29 05:02:30 joko |
101 |
# - sub getNewPerlObjectByPkgName (moved to libp.pm) |
102 |
# + sub getMetaInfo |
103 |
# - sub existsChildNode (moved to Abstract.pm) |
104 |
# + sub getListUnfiltered |
105 |
# + sub getListFiltered |
106 |
# + sub createCursor |
107 |
# + sub createSet |
108 |
# + sub sendQuery |
109 |
# |
110 |
# Revision 1.5 2002/11/17 06:35:18 joko |
111 |
# + locator metadata can now be reached via ->{locator} |
112 |
# - getChildNodes is now wrapped via COREHANDLE |
113 |
# |
114 |
# Revision 1.4 2002/10/25 11:44:44 joko |
115 |
# + sub _initSchema |
116 |
# + sub existsChildNode |
117 |
# + sub testIntegrity |
118 |
# + sub rebuildDbAndSchema |
119 |
# |
120 |
# Revision 1.3 2002/10/17 03:56:55 joko |
121 |
# + bugfix: trapped eval error |
122 |
# |
123 |
# Revision 1.2 2002/10/17 00:10:05 joko |
124 |
# + removed dependency from tsobj.pm, schema is now independent |
125 |
# + sub getNewPerlObjectByPkgName |
126 |
# + sub deploySchema |
127 |
# + sub retreatSchema |
128 |
# |
129 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
130 |
# + new |
131 |
# |
132 |
############################################ |
133 |
|
134 |
|
135 |
package Data::Storage::Handler::Tangram; |
136 |
|
137 |
use strict; |
138 |
use warnings; |
139 |
|
140 |
use base ("Data::Storage::Handler"); |
141 |
use base ("Data::Storage::Handler::Abstract"); |
142 |
|
143 |
|
144 |
use Data::Dumper; |
145 |
use Tangram; |
146 |
|
147 |
use DesignPattern::Object; |
148 |
use Data::Storage::Result::Tangram; |
149 |
use Data::Mungle::Compare::Struct qw( isEmpty ); |
150 |
use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to ); |
151 |
|
152 |
# get logger instance |
153 |
my $logger = Log::Dispatch::Config->instance; |
154 |
|
155 |
|
156 |
# this holds the complete instantiated schema from tangram |
157 |
my $schema_tangram; |
158 |
|
159 |
sub getMetaInfo { |
160 |
my $self = shift; |
161 |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
162 |
return { |
163 |
'disconnectMethod' => 'disconnect', |
164 |
}; |
165 |
} |
166 |
|
167 |
sub _initSchema { |
168 |
my $self = shift; |
169 |
$logger->debug( __PACKAGE__ . "->_initSchema()" ); |
170 |
#if (!$schema_tangram) { |
171 |
#my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
172 |
my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); |
173 |
$schema_tangram = $obj->getSchema(); |
174 |
#} |
175 |
if (!$schema_tangram) { |
176 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
177 |
return 0; |
178 |
} |
179 |
#$self->_patchSchema(); |
180 |
return 1; |
181 |
} |
182 |
|
183 |
sub _patchSchema { |
184 |
my $self = shift; |
185 |
foreach (keys %{$schema_tangram->{classes}}) { |
186 |
next if $schema_tangram->{classes}->{$_}->{abstract}; |
187 |
#next if ($_ ne 'TsBankAccount'); |
188 |
#$_ ne 'AbstractAccount' && |
189 |
print "class: $_", "\n"; |
190 |
#print Dumper($schema_tangram->{classes}->{$_}); |
191 |
# create new string property named 'guid' |
192 |
my $tstring = Tangram::String->new(); |
193 |
$tstring->{name} = $tstring->{col} = 'guid'; |
194 |
# inject property into schema |
195 |
#$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring; |
196 |
print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}); |
197 |
} |
198 |
} |
199 |
|
200 |
sub connect { |
201 |
|
202 |
my $self = shift; |
203 |
|
204 |
my $dsn = shift; |
205 |
|
206 |
#print Dumper($self); |
207 |
#exit; |
208 |
|
209 |
# TODO: re-enable |
210 |
$dsn ||= $self->{locator}->{dbi}->{dsn}; |
211 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
212 |
|
213 |
#my $storage = Tangram::Relational->connect( $schema, $dsn ); |
214 |
#my $storage = Tangram::mysql->connect( $schema, $dsn ); |
215 |
#$storage = Tangram::Relational->connect( Project->schema, $dsn ); |
216 |
|
217 |
# if (!testDsn($dsn)) { |
218 |
# croak("Database at \"$dsn\" is not available"); |
219 |
# return; |
220 |
# } |
221 |
|
222 |
#return unless $self->_initSchema(); |
223 |
$self->_initSchema(); |
224 |
|
225 |
# create the main tangram storage object |
226 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
227 |
$self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn ); |
228 |
|
229 |
#print "connect", "\n"; |
230 |
#my $core = $self->{_COREHANDLE}; |
231 |
#print Dumper($core); |
232 |
|
233 |
# some attempts for configuring the wrapped underlying dbi..... |
234 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); |
235 |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
236 |
#$self->_configureUnderlyingStorage; |
237 |
|
238 |
# ..... encapsulation wins! |
239 |
$self->configureCOREHANDLE(); |
240 |
|
241 |
$self->{locator}->{status}->{connected} = 1; |
242 |
|
243 |
return 1; |
244 |
|
245 |
} |
246 |
|
247 |
sub getChildNodes { |
248 |
|
249 |
my $self = shift; |
250 |
my $mode = shift; |
251 |
my $filter = shift; |
252 |
|
253 |
$mode ||= 'core'; |
254 |
$filter ||= 'all'; |
255 |
|
256 |
$logger->debug( __PACKAGE__ . "->getChildNodes($mode)" ); |
257 |
|
258 |
if ($mode eq 'core') { |
259 |
|
260 |
my @nodes; |
261 |
|
262 |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
263 |
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
264 |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
265 |
|
266 |
# todo: should we retrieve information from the schema here |
267 |
# rather than poorly getting table names from underlying dbi? |
268 |
my $storage = $self->_getSubLayerHandle(); |
269 |
@nodes = @{$storage->getChildNodes()}; |
270 |
#$storage->_configureCOREHANDLE(); |
271 |
#print "getchildnodes\n"; |
272 |
#print Dumper($self); |
273 |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
274 |
|
275 |
# TODO: REVIEW |
276 |
#$storage->disconnect(); |
277 |
|
278 |
$self->{meta}->{childnodes} = \@nodes; |
279 |
|
280 |
return \@nodes; |
281 |
|
282 |
} elsif ($mode eq 'root') { |
283 |
|
284 |
# FIXME: this will return *all* known classes to 'Class::Tangram', |
285 |
# which might not be what you expect since more than one instance |
286 |
# of Tangram may be in memory and Class::Tangram seems to |
287 |
# offer no methods to determine this or filter its result(s) according |
288 |
# to a specific database. |
289 |
my @object_names = Class::Tangram::known_classes(); |
290 |
my @concret_names; |
291 |
my $o_cnt; |
292 |
foreach (sort @object_names) { |
293 |
push @concret_names, $_ if (!Class::Tangram::class_is_abstract($_)); |
294 |
$o_cnt++; |
295 |
} |
296 |
|
297 |
if ($filter eq 'all') { |
298 |
return \@object_names; |
299 |
} elsif ($filter eq 'concrete') { |
300 |
return \@concret_names; |
301 |
} |
302 |
|
303 |
} |
304 |
|
305 |
|
306 |
} |
307 |
|
308 |
sub testIntegrity { |
309 |
|
310 |
my $self = shift; |
311 |
|
312 |
$logger->debug( __PACKAGE__ . "->testIntegrity()" ); |
313 |
|
314 |
# 1st test: are there tables? |
315 |
if (!$self->getChildNodes()) { |
316 |
$logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" ); |
317 |
return; |
318 |
} |
319 |
|
320 |
# 2nd test: is there a table named "Tangram"? |
321 |
if (!$self->existsChildNode("Tangram")) { |
322 |
$logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" ); |
323 |
return; |
324 |
} |
325 |
|
326 |
$self->{locator}->{status}->{integrity} = 1; |
327 |
return 1; |
328 |
|
329 |
} |
330 |
|
331 |
|
332 |
sub _getSubLayerHandle { |
333 |
|
334 |
my $self = shift; |
335 |
|
336 |
$logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" ); |
337 |
|
338 |
#print Dumper($self); |
339 |
|
340 |
# hack, make more generic! |
341 |
if (!$self->{dataStorageLayer}) { |
342 |
$logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" ); |
343 |
#my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} ); |
344 |
my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } ); |
345 |
$self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } ); |
346 |
#$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE(); |
347 |
#$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE(); |
348 |
} |
349 |
|
350 |
#print Dumper($self->{STORAGE_UNDER_THE_HOOD}); |
351 |
|
352 |
return $self->{dataStorageLayer}; |
353 |
|
354 |
} |
355 |
|
356 |
sub _configureUnderlyingStorage { |
357 |
|
358 |
my $self = shift; |
359 |
|
360 |
$logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" ); |
361 |
|
362 |
$self->_configureCOREHANDLE_DBI(); |
363 |
return; |
364 |
|
365 |
foreach my $key (keys %{$self->{dbi}}) { |
366 |
my $val = $self->{dbi}->{$key}; |
367 |
print "entry: $key; $val", "\n"; |
368 |
$self->{_COREHANDLE}->{db}->{$key} = $val; |
369 |
} |
370 |
#print Dumper($self->{COREHANDLE}->{db}); |
371 |
} |
372 |
|
373 |
|
374 |
sub configureCOREHANDLE { |
375 |
|
376 |
my $self = shift; |
377 |
|
378 |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
379 |
|
380 |
#my $subLayer = $self->_getSubLayerHandle(); |
381 |
#print Dumper($self); |
382 |
#exit; |
383 |
|
384 |
# apply configured modifications |
385 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
386 |
$self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
387 |
} |
388 |
if (exists $self->{dbi}->{RaiseError}) { |
389 |
$self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; |
390 |
} |
391 |
if (exists $self->{dbi}->{PrintError}) { |
392 |
$self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; |
393 |
} |
394 |
if (exists $self->{dbi}->{HandleError}) { |
395 |
$self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; |
396 |
} |
397 |
|
398 |
} |
399 |
|
400 |
sub deploySchema { |
401 |
my $self = shift; |
402 |
my $args = shift; |
403 |
|
404 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
405 |
|
406 |
$logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); |
407 |
|
408 |
my $ok; |
409 |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
410 |
return unless $self->_initSchema(); |
411 |
$ok = Tangram::Relational->deploy($schema_tangram, $dbh ); |
412 |
$dbh->disconnect(); |
413 |
} |
414 |
return $ok; |
415 |
} |
416 |
|
417 |
sub retreatSchema { |
418 |
|
419 |
my $self = shift; |
420 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
421 |
|
422 |
$logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); |
423 |
|
424 |
my $ok; |
425 |
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
426 |
|
427 |
return unless $self->_initSchema(); |
428 |
|
429 |
#use Data::Dumper; print Dumper($self); |
430 |
$self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); |
431 |
|
432 |
$ok = Tangram::Relational->retreat($schema_tangram, $dbh ); |
433 |
|
434 |
# answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because... |
435 |
# - ... Tangram::Relational->retreat doesn't seem to return a valid status |
436 |
# - possible improvement: |
437 |
# - test this by checking for count of tables in database |
438 |
# - problem with this: there may be some left not having been included to the schema |
439 |
# - maybe better: use "->getChildNodes"? |
440 |
$ok = 2; |
441 |
|
442 |
$dbh->disconnect(); |
443 |
|
444 |
} |
445 |
return $ok; |
446 |
} |
447 |
|
448 |
sub rebuildDb { |
449 |
my $self = shift; |
450 |
$logger->info( __PACKAGE__ . "->rebuildDb()" ); |
451 |
my @results; |
452 |
|
453 |
# sum up results (bool (0/1)) in array |
454 |
push @results, $self->retreatSchema(); |
455 |
push @results, $self->{dataStorageLayer}->dropDb(); |
456 |
push @results, $self->{dataStorageLayer}->createDb(); |
457 |
push @results, $self->deploySchema(); |
458 |
|
459 |
# scan array for "bad ones" |
460 |
my $res = 1; |
461 |
map { |
462 |
$res = 0 if (!$_); |
463 |
} @results; |
464 |
|
465 |
return $res; |
466 |
} |
467 |
|
468 |
sub getListUnfiltered { |
469 |
my $self = shift; |
470 |
my $nodename = shift; |
471 |
my @results; |
472 |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
473 |
# get set of objects from odbms by object name |
474 |
my $object_set = $self->{_COREHANDLE}->remote($nodename); |
475 |
@results = $self->{_COREHANDLE}->select($object_set); |
476 |
return \@results; |
477 |
} |
478 |
|
479 |
sub getListFiltered { |
480 |
my $self = shift; |
481 |
|
482 |
# redirect to unfiltered mode |
483 |
#return $self->getListUnfiltered(@_); |
484 |
|
485 |
my $nodename = shift; |
486 |
my $filters = shift; |
487 |
my @results; |
488 |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
489 |
|
490 |
#print Dumper($filters); |
491 |
|
492 |
my @tfilters; |
493 |
|
494 |
foreach my $filter (@$filters) { |
495 |
|
496 |
# get filter - TODO: for each filter |
497 |
#my $filter = $filters->[0]; |
498 |
|
499 |
# build filter |
500 |
my $lexpr = $filter->{key}; |
501 |
#my $op = $filter->{op}; |
502 |
my $op = '='; |
503 |
my $rexpr = $filter->{val}; |
504 |
my $tight = 100; |
505 |
|
506 |
# my $tfilter = Tangram::Filter->new( |
507 |
# expr => "t1.$lexpr $op '$rexpr'", |
508 |
# tight => $tight, |
509 |
# objects => $objects, |
510 |
# ); |
511 |
|
512 |
# TODO: is_op? |
513 |
# dispatch un-common operators if exists |
514 |
if($filter->{op} eq "ref") { |
515 |
push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
516 |
} else { |
517 |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
518 |
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
519 |
} |
520 |
|
521 |
} |
522 |
|
523 |
my $tfilter = join(' & ', @tfilters); |
524 |
|
525 |
# get set of objects from odbms by object name |
526 |
my $remote = $self->{_COREHANDLE}->remote($nodename); |
527 |
|
528 |
# was: |
529 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
530 |
|
531 |
# is: |
532 |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
533 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
534 |
|
535 |
#print "eval: $evalstring", "\n"; |
536 |
|
537 |
# get filtered list/set |
538 |
@results = eval($evalstring); |
539 |
die $@ if $@; |
540 |
|
541 |
return \@results; |
542 |
} |
543 |
|
544 |
sub createCursor { |
545 |
my $self = shift; |
546 |
my $node = shift; |
547 |
my $cmdHandle = $self->{_COREHANDLE}->cursor($node); |
548 |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); |
549 |
return $result; |
550 |
} |
551 |
|
552 |
sub createSet { |
553 |
my $self = shift; |
554 |
#print "-" x 80, "\n"; |
555 |
#print Dumper(@_); |
556 |
my @objects = @_; |
557 |
my $rh = Set::Object->new(); |
558 |
foreach (@objects) { |
559 |
if (!isEmpty($_)) { |
560 |
#print Dumper($_); |
561 |
$rh->insert($_); |
562 |
} |
563 |
} |
564 |
#print Dumper($rh->members()); |
565 |
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
566 |
return $result; |
567 |
} |
568 |
|
569 |
sub sendQuery { |
570 |
my $self = shift; |
571 |
my $query = shift; |
572 |
|
573 |
#print Dumper($query); |
574 |
|
575 |
# type = ITEM|LIST|TRANSPARENT |
576 |
my $type = ''; |
577 |
# mode = OID|SPECIAL |
578 |
my $mode = ''; |
579 |
my $ident = ''; |
580 |
my $crud = ''; |
581 |
|
582 |
|
583 |
# dispatch type and mode |
584 |
|
585 |
# defaults - 1 |
586 |
if ($query->{options}) { |
587 |
$crud = $query->{options}->{crud}; |
588 |
$crud ||= $query->{options}->{action}; |
589 |
} |
590 |
|
591 |
# defaults - 2 |
592 |
$type ||= 'TRANSPARENT'; |
593 |
$crud ||= 'RETRIEVE'; |
594 |
|
595 |
if ($query->{options}->{OID}) { |
596 |
$type = 'ITEM'; |
597 |
$mode = 'OID'; |
598 |
$ident = $query->{options}->{OID}; |
599 |
|
600 |
} elsif (my $guid = $query->{options}->{GUID}) { |
601 |
$type = 'TRANSPARENT'; |
602 |
$query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; |
603 |
|
604 |
# if operator is different (dispatcher for 'getListFiltered') |
605 |
} elsif (my $op = $query->{options}->{op}) { |
606 |
$type = 'TRANSPARENT'; |
607 |
$query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ]; |
608 |
|
609 |
} |
610 |
|
611 |
# HACK: special case: querying by id does not translate into a common tangram query |
612 |
# just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync! |
613 |
if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) { |
614 |
$type = 'ITEM'; |
615 |
$mode = 'SPECIAL.SYNC'; |
616 |
$ident = $query->{criterias}->[0]->{val}; |
617 |
} |
618 |
|
619 |
|
620 |
# execute query |
621 |
my $result; |
622 |
|
623 |
if ($type eq 'ITEM' && $ident) { |
624 |
|
625 |
if ($mode eq 'OID') { |
626 |
# TODO: review this case! |
627 |
$result = $self->getObject($ident, $query->{options}); |
628 |
|
629 |
} elsif ($mode eq 'SPECIAL.SYNC') { |
630 |
|
631 |
# V1 - failed |
632 |
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
633 |
|
634 |
# hmm.... |
635 |
#return $self->createSet() if $ident == 5; |
636 |
|
637 |
# Unload single object before doing any further operations to |
638 |
# expect a "fresh" object from orm when performing the next calls. |
639 |
$self->{_COREHANDLE}->unload($ident); |
640 |
|
641 |
# Load object from orm. |
642 |
my $object = $self->{_COREHANDLE}->load($ident); |
643 |
|
644 |
# determine object identifier (OID) |
645 |
my $oid = $self->{_COREHANDLE}->id($object); |
646 |
|
647 |
# encapsulate into result/response container and return this one |
648 |
$result = $self->createSet($object); |
649 |
|
650 |
# debugging |
651 |
#$result = $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
652 |
|
653 |
} |
654 |
|
655 |
|
656 |
} elsif ($type eq 'TRANSPARENT') { |
657 |
|
658 |
if ($crud eq 'RETRIEVE') { |
659 |
|
660 |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
661 |
#return $self->createSet($object); |
662 |
#return $self->createSet($list); |
663 |
return $self->createSet(@$list); |
664 |
|
665 |
#die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
666 |
|
667 |
# try a generic tangram query here |
668 |
# TODO: try to place an oql on top of that (search.cpan.org!) |
669 |
my @crits; |
670 |
foreach (@{$query->{criterias}}) { |
671 |
my $op = ''; |
672 |
$op = '=' if lc $_->{op} eq 'eq'; |
673 |
push @crits, "$_->{key}$op'$_->{val}'"; |
674 |
} |
675 |
my $subnodes = {}; |
676 |
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
677 |
# HACK: this is hardcoded ;( expand possibilities! |
678 |
#my $crit = join(' AND ', @crits); |
679 |
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
680 |
#return $self->sendCommand($sql); |
681 |
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
682 |
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
683 |
$result = $self->createCursor($query->{node}); |
684 |
|
685 |
} elsif ($crud eq 'UPDATE') { |
686 |
|
687 |
# Patch current query to be a loader (e.g. change action, remove payload) ... |
688 |
my $childquery = deep_copy($query); |
689 |
$childquery->{options}->{crud} = 'RETRIEVE'; |
690 |
delete $childquery->{payload}; |
691 |
|
692 |
# ... to use it to fetch a fresh object using ourselves (sendQuery). |
693 |
my $cursor = $self->sendQuery($childquery); |
694 |
my $status = $cursor->getStatus(); |
695 |
my $object = $cursor->getNextEntry(); |
696 |
|
697 |
# Merge values and apply value modifiers. |
698 |
my $options = { utf8 => 1, php => 1 }; |
699 |
merge_to($object, $query->{payload}, $options); |
700 |
|
701 |
# Execute update operation at orm. |
702 |
$self->update($object); |
703 |
|
704 |
} elsif ($crud eq 'DELETE') { |
705 |
|
706 |
# Patch current query to be a loader (e.g. change action) ... |
707 |
my $childquery = deep_copy($query); |
708 |
$childquery->{options}->{crud} = 'RETRIEVE'; |
709 |
|
710 |
# ... to use it to fetch a fresh object using ourselves (sendQuery). |
711 |
my $cursor = $self->sendQuery($childquery); |
712 |
my $status = $cursor->getStatus(); |
713 |
my $object = $cursor->getNextEntry(); |
714 |
|
715 |
$self->erase($object); |
716 |
|
717 |
} elsif ($crud eq 'CREATE') { |
718 |
|
719 |
my $nodename = $query->{node}; |
720 |
my $newnode = $self->createNode($nodename); |
721 |
my $id = $self->{_COREHANDLE}->insert($newnode); |
722 |
|
723 |
print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n"; |
724 |
|
725 |
return $newnode; |
726 |
|
727 |
} |
728 |
|
729 |
} |
730 |
|
731 |
return $result; |
732 |
|
733 |
} |
734 |
|
735 |
sub eraseAll { |
736 |
my $self = shift; |
737 |
my $classname = shift; |
738 |
my $remote = $self->{_COREHANDLE}->remote($classname); |
739 |
my @objs = $self->{_COREHANDLE}->select($remote); |
740 |
$self->{_COREHANDLE}->erase(@objs); |
741 |
} |
742 |
|
743 |
sub createDb { |
744 |
my $self = shift; |
745 |
my $storage = $self->_getSubLayerHandle(); |
746 |
return $storage->createDb(); |
747 |
} |
748 |
|
749 |
sub getObject { |
750 |
my $self = shift; |
751 |
my $oid = shift; |
752 |
my $options = shift; |
753 |
|
754 |
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
755 |
# unload($oid) will only unload object, not deep object hashes |
756 |
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
757 |
|
758 |
# TODO: review this |
759 |
#if (!$self->{COREHANDLE}) { return; } |
760 |
|
761 |
# TODO: review this |
762 |
my $object = eval('$self->{_COREHANDLE}->load($oid);'); |
763 |
print $@, "\n" if $@; |
764 |
|
765 |
return $object if $object; |
766 |
} |
767 |
|
768 |
sub getObjectByGuid_old { |
769 |
my $self = shift; |
770 |
my $guid = shift; |
771 |
my $options = shift; |
772 |
|
773 |
# Guid and Classname is needed |
774 |
if(!$guid || !$options->{classname}) { |
775 |
$logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" ); |
776 |
return; |
777 |
} |
778 |
|
779 |
# TODO: create a deep_unload method (currently _all_ objects are unloaded) |
780 |
# unload($oid) will only unload object, not deep object hashes |
781 |
$self->{_COREHANDLE}->unload() if ($options->{destroy}); |
782 |
|
783 |
# search for object with given Classname and Guid |
784 |
my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname}); |
785 |
my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid); |
786 |
|
787 |
# we searched for global unique identifer of some object, |
788 |
# so I think we can trust there would be only one result |
789 |
if($result[0]) { |
790 |
return $result[0]; |
791 |
} else { |
792 |
$logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" ); |
793 |
return; |
794 |
} |
795 |
|
796 |
} |
797 |
|
798 |
sub getObjectAsHash_old { |
799 |
my $self = shift; |
800 |
my $oid = shift; |
801 |
my $options = shift; |
802 |
my $obj; |
803 |
|
804 |
if($options->{guid}) { |
805 |
$obj = $self->getObjectByGuid($oid, $options); |
806 |
} else { |
807 |
$obj = $self->getObject($oid, $options); |
808 |
} |
809 |
|
810 |
# build options (a callback to unload autovivified objects) for 'expand' |
811 |
# TODO: use $logger to write to debug here! |
812 |
my $cb; # = sub {}; |
813 |
|
814 |
# deactivated way to get rid of used instances, if requested |
815 |
=pod |
816 |
if ($options->{destroy}) { |
817 |
$options->{cb}->{destroy} = sub { |
818 |
print "================ DESTROY", "\n"; |
819 |
my $object = shift; |
820 |
#print Dumper($object); |
821 |
$self->{_COREHANDLE}->unload($object); |
822 |
#undef($object); |
823 |
}; |
824 |
} |
825 |
=cut |
826 |
|
827 |
my $hash = expand($obj, $options); |
828 |
|
829 |
# old (unsuccessful) attempts to get rid of used instances, if requested |
830 |
|
831 |
# V1: |
832 |
#$options->{cb}->{destroy}->($obj); |
833 |
#$self->{_COREHANDLE}->unload($obj); |
834 |
|
835 |
# V2: |
836 |
#$obj->clear_refs; |
837 |
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
838 |
#$obj->DESTROY; |
839 |
#undef($obj) if($options->{destroy}); |
840 |
|
841 |
return $hash; |
842 |
} |
843 |
|
844 |
sub getSchema { |
845 |
return $schema_tangram; |
846 |
} |
847 |
|
848 |
sub getCOREHANDLE { |
849 |
my $self = shift; |
850 |
return $self->{_COREHANDLE}; |
851 |
} |
852 |
|
853 |
sub dropDb { |
854 |
my $self = shift; |
855 |
my $storage = $self->_getSubLayerHandle(); |
856 |
return $storage->dropDb(); |
857 |
} |
858 |
|
859 |
sub testAvailability { |
860 |
my $self = shift; |
861 |
my $storage = $self->_getSubLayerHandle(); |
862 |
return $storage->testAvailability(); |
863 |
} |
864 |
|
865 |
sub disconnect2 { |
866 |
my $self = shift; |
867 |
my $storage = $self->_getSubLayerHandle(); |
868 |
print "DISC!", "\n"; |
869 |
|
870 |
my $storage_ll = $storage->{_COREHANDLE}; |
871 |
$storage_ll->disconnect(); |
872 |
|
873 |
print Dumper($storage); |
874 |
exit; |
875 |
|
876 |
#$self->{_COREHANDLE} |
877 |
#$storage->disconnect(); |
878 |
$self->{dataStorageLayer}->disconnect(); |
879 |
} |
880 |
|
881 |
|
882 |
sub createNode { |
883 |
my $self = shift; |
884 |
my $classname = shift; |
885 |
|
886 |
my $obj = $classname->new(); |
887 |
|
888 |
my $attr_options = Class::Tangram::attribute_options($classname); |
889 |
#print "Attribute Options: " . Dumper($attr_options); |
890 |
|
891 |
my $attr_types = Class::Tangram::attribute_types($classname); |
892 |
#print "Attribute Types: " . Dumper($attr_types); |
893 |
|
894 |
foreach(keys %{$attr_types}) { |
895 |
if($attr_types->{$_} eq 'string') { |
896 |
$obj->{$_} = ''; |
897 |
} elsif($attr_types->{$_} eq 'int') { |
898 |
$obj->{$_} = 0; |
899 |
} elsif($attr_types->{$_} eq 'real') { |
900 |
$obj->{$_} = 0; |
901 |
} elsif($attr_types->{$_} eq 'rawdatetime') { |
902 |
$obj->{$_} = '0000-00-00 00:00:00'; |
903 |
} elsif($attr_types->{$_} eq 'ref') { |
904 |
if($attr_options->{$_}->{class}) { |
905 |
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
906 |
} else { |
907 |
#$obj->{$_} = undef(); |
908 |
} |
909 |
} elsif($attr_types->{$_} eq 'iarray') { |
910 |
$obj->{$_} = [ ]; |
911 |
} elsif($attr_types->{$_} eq 'hash') { |
912 |
$obj->{$_} = { }; |
913 |
} elsif($attr_types->{$_} eq 'flat_hash') { |
914 |
$obj->{$_} = { }; |
915 |
} |
916 |
} |
917 |
|
918 |
#print "New Object: " . Dumper($obj); |
919 |
|
920 |
return $obj; |
921 |
} |
922 |
|
923 |
|
924 |
sub insertChildNode { |
925 |
my $self = shift; |
926 |
my $child_entry = shift; |
927 |
my $query_args = shift; |
928 |
|
929 |
my $core = $self->{_COREHANDLE}; |
930 |
my $nodename = $query_args->{nodename}; |
931 |
|
932 |
# get parent object |
933 |
my $query = { |
934 |
node => $query_args->{parent}->{nodename}, |
935 |
options => { GUID => $query_args->{parent}->{guid}, }, |
936 |
}; |
937 |
my $cursor = $self->sendQuery($query); |
938 |
my $parent = $cursor->getNextEntry(); |
939 |
|
940 |
# debug |
941 |
#print "Parent_org: " . Dumper($parent); |
942 |
|
943 |
# Create child node object if isn't already done |
944 |
# ($child_entry have to be the class name then...) |
945 |
if(!ref($child_entry)) { |
946 |
$child_entry = $self->createNode($child_entry); |
947 |
# it could be insert 'manually' or will be insert 'transparent' if parent will be updated |
948 |
#$core->insert($child_entry); |
949 |
#print "Create child object [$nodename]: " . Dumper($child_entry); |
950 |
} |
951 |
|
952 |
# get reference of tied node |
953 |
my $tied_node = tied $parent->{$nodename}; |
954 |
|
955 |
# insert/change child entry at parent |
956 |
#print "reference: " . ref($parent->{$nodename}) . "\n"; |
957 |
if(ref($parent->{$nodename}) eq 'ARRAY') { |
958 |
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type, |
959 |
# so a 'PUSH' is not implemented (which could be then done transparently) |
960 |
my $array = $tied_node->FETCH; |
961 |
push @$array, $child_entry; |
962 |
$tied_node->STORE($array); |
963 |
# node will be normaly untied at 'STORE' |
964 |
if(tied $parent->{$nodename}) { print "already tied !!\n"; } |
965 |
else { undef $tied_node; } |
966 |
} |
967 |
elsif(ref($parent->{$nodename}) eq 'HASH') { |
968 |
if(my $key = $query_args->{hash_key}) { |
969 |
# same problem as with 'ARRAY': |
970 |
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type. |
971 |
my $hash = $tied_node->FETCH; |
972 |
$hash->{$key} = $child_entry; |
973 |
$tied_node->STORE($hash); |
974 |
# node will be normaly untied at 'STORE' |
975 |
if(tied $parent->{$nodename}) { print "already tied !!\n"; } |
976 |
else { undef $tied_node; } |
977 |
} else { |
978 |
print "ERROR: No HASH KEY given, so not able to insert hash entry!"; |
979 |
} |
980 |
} |
981 |
else { |
982 |
$parent->{$nodename} = $child_entry; |
983 |
} |
984 |
|
985 |
# debug |
986 |
#print "Parent_new: " . Dumper($parent); |
987 |
|
988 |
# save parent |
989 |
$core->update($parent); |
990 |
|
991 |
# debug |
992 |
#print "Saved Parent: ". Dumper($parent); |
993 |
|
994 |
return $child_entry; |
995 |
} |
996 |
|
997 |
|
998 |
1; |
999 |
__END__ |