/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.35 - (show annotations)
Sat Apr 19 16:09:48 2003 UTC (21 years, 2 months ago) by jonen
Branch: MAIN
Changes since 1.34: +20 -5 lines
+ added operator dispatching (currently for getting ref-type) at 'getListFiltered'

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed