/[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.34 - (show annotations)
Fri Apr 11 01:18:53 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.33: +26 -8 lines
sendQuery:
+ introduced crud action 'DELETE'

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

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