| 3 |
# $Id$ |
# $Id$ |
| 4 |
# |
# |
| 5 |
# $Log$ |
# $Log$ |
| 6 |
|
# Revision 1.41 2003/06/29 02:03:45 joko |
| 7 |
|
# fix:? initialize schema on startup |
| 8 |
|
# |
| 9 |
|
# Revision 1.40 2003/06/25 22:57:54 joko |
| 10 |
|
# major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting" |
| 11 |
|
# |
| 12 |
|
# Revision 1.39 2003/06/06 11:40:40 jonen |
| 13 |
|
# fixed bug at 'getFilteredList' |
| 14 |
|
# |
| 15 |
|
# Revision 1.38 2003/05/13 16:38:38 joko |
| 16 |
|
# problems with "tied" on 5.6.1/win32 |
| 17 |
|
# |
| 18 |
|
# Revision 1.37 2003/05/10 17:37:39 jonen |
| 19 |
|
# corrected last commit |
| 20 |
|
# |
| 21 |
|
# Revision 1.36 2003/05/10 17:31:18 jonen |
| 22 |
|
# + added new functions related to 'create' item |
| 23 |
|
# - createNode() |
| 24 |
|
# # creates non-persistent 'deep dummy filled' object |
| 25 |
|
# - insertChildNode() |
| 26 |
|
# # inserts child node at given parent (child node haven't to exists, |
| 27 |
|
# createNode will be injected transparently) |
| 28 |
|
# |
| 29 |
|
# Revision 1.35 2003/04/19 16:09:48 jonen |
| 30 |
|
# + added operator dispatching (currently for getting ref-type) at 'getListFiltered' |
| 31 |
|
# |
| 32 |
|
# Revision 1.34 2003/04/11 01:18:53 joko |
| 33 |
|
# sendQuery: |
| 34 |
|
# + introduced crud action 'DELETE' |
| 35 |
|
# |
| 36 |
|
# Revision 1.33 2003/04/09 06:07:43 joko |
| 37 |
|
# revamped 'sub sendQuery' |
| 38 |
|
# |
| 39 |
|
# Revision 1.32 2003/04/08 22:52:22 joko |
| 40 |
|
# modified 'querySchema': better behaviour regarding filtering result |
| 41 |
|
# |
| 42 |
|
# Revision 1.31 2003/04/05 21:24:09 joko |
| 43 |
|
# modified 'sub getChildNodes': now contains code from 'querySchema' |
| 44 |
|
# |
| 45 |
|
# Revision 1.30 2003/03/27 15:31:14 joko |
| 46 |
|
# fixes to modules regarding new namespace(s) below Data::Mungle::* |
| 47 |
|
# |
| 48 |
|
# Revision 1.29 2003/02/21 01:47:18 joko |
| 49 |
|
# purged old code |
| 50 |
|
# minor cosmetics |
| 51 |
|
# |
| 52 |
# Revision 1.28 2003/02/20 20:20:26 joko |
# Revision 1.28 2003/02/20 20:20:26 joko |
| 53 |
# tried to get auto-disconnect working again - failed with that |
# tried to get auto-disconnect working again - failed with that |
| 54 |
# |
# |
| 169 |
|
|
| 170 |
use DesignPattern::Object; |
use DesignPattern::Object; |
| 171 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
| 172 |
use Data::Compare::Struct qw( isEmpty ); |
use Data::Mungle::Compare::Struct qw( isEmpty ); |
| 173 |
use Data::Transform::Deep qw( expand ); |
use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to ); |
|
#use Data::Transform::Encode qw( var2utf8 ); |
|
| 174 |
|
|
| 175 |
# get logger instance |
# get logger instance |
| 176 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
| 193 |
#if (!$schema_tangram) { |
#if (!$schema_tangram) { |
| 194 |
#my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
#my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); |
| 195 |
my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); |
my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); |
| 196 |
$schema_tangram = $obj->getSchema(); |
$schema_tangram = $obj->getSchema() if $obj; |
| 197 |
#} |
#} |
| 198 |
if (!$schema_tangram) { |
if (!$schema_tangram) { |
| 199 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." ); |
| 200 |
return 0; |
return 0; |
| 201 |
} |
} |
| 202 |
#$self->_patchSchema(); |
#$self->_patchSchema(); |
| 242 |
# return; |
# return; |
| 243 |
# } |
# } |
| 244 |
|
|
| 245 |
#return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
| 246 |
$self->_initSchema(); |
#$self->_initSchema(); |
| 247 |
|
|
| 248 |
# create the main tangram storage object |
# create the main tangram storage object |
| 249 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
| 270 |
sub getChildNodes { |
sub getChildNodes { |
| 271 |
|
|
| 272 |
my $self = shift; |
my $self = shift; |
| 273 |
my @nodes; |
my $mode = shift; |
| 274 |
|
my $filter = shift; |
| 275 |
|
|
| 276 |
|
$mode ||= 'core'; |
| 277 |
|
$filter ||= 'all'; |
| 278 |
|
|
| 279 |
|
$logger->debug( __PACKAGE__ . "->getChildNodes($mode)" ); |
| 280 |
|
|
| 281 |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
if ($mode eq 'core') { |
| 282 |
|
|
| 283 |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
my @nodes; |
| 284 |
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
|
| 285 |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
# create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE |
| 286 |
|
#my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); |
| 287 |
# todo: should we retrieve information from the schema here |
#my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); |
| 288 |
# rather than poorly getting table names from underlying dbi? |
|
| 289 |
my $storage = $self->_getSubLayerHandle(); |
# todo: should we retrieve information from the schema here |
| 290 |
@nodes = @{$storage->getChildNodes()}; |
# rather than poorly getting table names from underlying dbi? |
| 291 |
#$storage->_configureCOREHANDLE(); |
my $storage = $self->_getSubLayerHandle(); |
| 292 |
#print "getchildnodes\n"; |
@nodes = @{$storage->getChildNodes()}; |
| 293 |
#print Dumper($self); |
#$storage->_configureCOREHANDLE(); |
| 294 |
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
#print "getchildnodes\n"; |
| 295 |
|
#print Dumper($self); |
| 296 |
|
#if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
| 297 |
|
|
| 298 |
|
# TODO: REVIEW |
| 299 |
|
#$storage->disconnect(); |
| 300 |
|
|
| 301 |
|
$self->{meta}->{childnodes} = \@nodes; |
| 302 |
|
|
| 303 |
|
return \@nodes; |
| 304 |
|
|
| 305 |
# TODO: REVIEW |
} elsif ($mode eq 'root') { |
| 306 |
#$storage->disconnect(); |
|
| 307 |
|
# FIXME: this will return *all* known classes to 'Class::Tangram', |
| 308 |
|
# which might not be what you expect since more than one instance |
| 309 |
|
# of Tangram may be in memory and Class::Tangram seems to |
| 310 |
|
# offer no methods to determine this or filter its result(s) according |
| 311 |
|
# to a specific database. |
| 312 |
|
my @object_names = Class::Tangram::known_classes(); |
| 313 |
|
my @concret_names; |
| 314 |
|
my $o_cnt; |
| 315 |
|
foreach (sort @object_names) { |
| 316 |
|
push @concret_names, $_ if (!Class::Tangram::class_is_abstract($_)); |
| 317 |
|
$o_cnt++; |
| 318 |
|
} |
| 319 |
|
|
| 320 |
|
if ($filter eq 'all') { |
| 321 |
|
return \@object_names; |
| 322 |
|
} elsif ($filter eq 'concrete') { |
| 323 |
|
return \@concret_names; |
| 324 |
|
} |
| 325 |
|
|
| 326 |
$self->{meta}->{childnodes} = \@nodes; |
} |
| 327 |
|
|
|
return \@nodes; |
|
| 328 |
|
|
| 329 |
} |
} |
| 330 |
|
|
|
|
|
| 331 |
sub testIntegrity { |
sub testIntegrity { |
| 332 |
|
|
| 333 |
my $self = shift; |
my $self = shift; |
| 505 |
# redirect to unfiltered mode |
# redirect to unfiltered mode |
| 506 |
#return $self->getListUnfiltered(@_); |
#return $self->getListUnfiltered(@_); |
| 507 |
|
|
| 508 |
my $nodename = shift; |
my $in = {}; |
| 509 |
my $filters = shift; |
$in->{nodename} = shift; |
| 510 |
|
$in->{filters} = shift; |
| 511 |
|
$in->{sorting} = shift; |
| 512 |
|
|
| 513 |
my @results; |
my @results; |
| 514 |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" ); |
| 515 |
|
|
| 516 |
|
#print Dumper($filters); |
| 517 |
|
|
| 518 |
#print Dumper($filters); |
# 1. "Remote Object Handle" - get set of objects from odbms by object name |
| 519 |
|
my $remote = $self->{_COREHANDLE}->remote($in->{nodename}); |
| 520 |
|
|
| 521 |
|
# 2. Transfer $in to $orm_query |
| 522 |
|
my $orm_query = {}; |
| 523 |
|
|
| 524 |
|
# 2.a. Filters |
| 525 |
my @tfilters; |
my @tfilters; |
| 526 |
|
my $orm_filter = undef; |
| 527 |
|
|
| 528 |
foreach my $filter (@$filters) { |
foreach my $filter (@{$in->{filters}}) { |
| 529 |
|
|
| 530 |
# get filter - TODO: for each filter |
# get filter - TODO: for each filter |
| 531 |
#my $filter = $filters->[0]; |
#my $filter = $filters->[0]; |
| 532 |
|
|
| 533 |
# build filter |
# 1. The parts of a filter source entry |
| 534 |
my $lexpr = $filter->{key}; |
my $lexpr = $filter->{key}; |
| 535 |
#my $op = $filter->{op}; |
#my $op = $filter->{op}; |
| 536 |
my $op = '='; |
my $op = '='; |
| 537 |
my $rexpr = $filter->{val}; |
my $rexpr = $filter->{val}; |
| 538 |
my $tight = 100; |
my $tight = 100; |
| 539 |
|
|
| 540 |
|
# 2. Build filter target entry |
| 541 |
|
|
| 542 |
# my $tfilter = Tangram::Filter->new( |
# Test 1 - didn't work out! |
| 543 |
# expr => "t1.$lexpr $op '$rexpr'", |
# my $tfilter = Tangram::Filter->new( |
| 544 |
# tight => $tight, |
# expr => "t1.$lexpr $op '$rexpr'", |
| 545 |
# objects => $objects, |
# tight => $tight, |
| 546 |
# ); |
# objects => $objects, |
| 547 |
|
# ); |
| 548 |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
|
| 549 |
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
my $orm_filter_tmp = undef; |
| 550 |
|
# was: |
| 551 |
|
# TODO: is_op? |
| 552 |
|
# dispatch un-common operators if exists |
| 553 |
|
if ($filter->{op} eq "ref") { |
| 554 |
|
# do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query |
| 555 |
|
#print "Filter->op eq 'ref'.\n"; |
| 556 |
|
#push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
| 557 |
|
} else { |
| 558 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
| 559 |
|
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
| 560 |
|
# better: calculate expression right here! |
| 561 |
|
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
| 562 |
|
|
| 563 |
|
#print "key: ", $filter->{key}, "\n"; |
| 564 |
|
|
| 565 |
|
my $left = $remote->{$filter->{key}}; |
| 566 |
|
#my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" ); |
| 567 |
|
my $right = $filter->{val}; |
| 568 |
|
my $op = $filter->{op}; |
| 569 |
|
$orm_filter_tmp = $left->$op($right); |
| 570 |
|
} |
| 571 |
|
|
| 572 |
|
if (not $orm_filter) { |
| 573 |
|
$orm_filter = $orm_filter_tmp; |
| 574 |
|
} else { |
| 575 |
|
$orm_filter = $orm_filter->and($orm_filter_tmp); |
| 576 |
|
} |
| 577 |
|
|
| 578 |
} |
} |
| 579 |
|
|
| 580 |
my $tfilter = join(' & ', @tfilters); |
$orm_query->{filter} = $orm_filter; |
| 581 |
|
|
|
# get set of objects from odbms by object name |
|
|
my $remote = $self->{_COREHANDLE}->remote($nodename); |
|
|
|
|
| 582 |
# was: |
# was: |
| 583 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
|
| 584 |
|
# 2.b. sorting [new as of 2003-06-17] |
| 585 |
|
if ($in->{sorting}) { |
| 586 |
|
my $sorting_rules = []; |
| 587 |
|
my $sorting_order = 'ASC'; |
| 588 |
|
foreach my $sorting_column (keys %{$in->{sorting}}) { |
| 589 |
|
$sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column}; |
| 590 |
|
push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column ); |
| 591 |
|
} |
| 592 |
|
$orm_query->{order} = $sorting_rules; |
| 593 |
|
if ($sorting_order eq 'DESC') { |
| 594 |
|
$orm_query->{desc} = 1; |
| 595 |
|
} |
| 596 |
|
} |
| 597 |
|
|
| 598 |
|
|
| 599 |
|
# 3. build and issue query - return result as Tangram::Cursor |
| 600 |
|
|
| 601 |
|
# coerce hash into array (This is valid in Perl) |
| 602 |
|
my @orm_query_args = %$orm_query; |
| 603 |
|
# send query (arguments) to orm |
| 604 |
|
@results = $self->{_COREHANDLE}->select($remote, @orm_query_args); |
| 605 |
|
|
| 606 |
|
#my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args); |
| 607 |
|
#return $cursor; |
| 608 |
|
#print Dumper(@results); |
| 609 |
|
|
| 610 |
# is: |
# is: |
| 611 |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
| 612 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
#my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');'; |
| 613 |
|
#print "eval: $evalstring", "\n"; |
| 614 |
#print "eval: $evalstring", "\n"; |
# get filtered list/set |
| 615 |
|
#@results = eval($evalstring); |
| 616 |
# get filtered list/set |
#die $@ if $@; |
| 617 |
@results = eval($evalstring); |
|
| 618 |
die $@ if $@; |
=pod |
| 619 |
|
# filter results |
| 620 |
|
if ($filters->[0]->{op} && ($filters->[0]->{op} eq "ref")) { |
| 621 |
|
#print "Filter->op eq 'ref'.\n"; |
| 622 |
|
my $att_name = $filters->[0]->{key}; |
| 623 |
|
my $att_val = $filters->[0]->{val}; |
| 624 |
|
my @filtered; |
| 625 |
|
foreach(@results) { |
| 626 |
|
if(ref($_->{$att_name}) eq $att_val) { |
| 627 |
|
push @filtered, $_; |
| 628 |
|
} |
| 629 |
|
} |
| 630 |
|
@results = @filtered; |
| 631 |
|
} |
| 632 |
|
=cut |
| 633 |
|
|
| 634 |
|
#print "results: " . Dumper(\@results); |
| 635 |
|
|
| 636 |
return \@results; |
return \@results; |
| 637 |
} |
} |
| 661 |
return $result; |
return $result; |
| 662 |
} |
} |
| 663 |
|
|
| 664 |
|
sub createResult { |
| 665 |
|
my $self = shift; |
| 666 |
|
my $rh = shift; |
| 667 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
| 668 |
|
return $result; |
| 669 |
|
} |
| 670 |
|
|
| 671 |
sub sendQuery { |
sub sendQuery { |
| 672 |
my $self = shift; |
my $self = shift; |
| 673 |
my $query = shift; |
my $query = shift; |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
|
|
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
|
| 674 |
|
|
| 675 |
#print Dumper($query); |
#print Dumper($query); |
| 676 |
|
|
| 677 |
# HACK: special case: querying by id does not translate into a common tangram query |
# type = ITEM|LIST|TRANSPARENT |
| 678 |
# just load the object by given id(ent) |
my $type = ''; |
| 679 |
if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') { |
# mode = OID|SPECIAL |
| 680 |
#print "LOAD!!!", "\n"; |
my $mode = ''; |
| 681 |
#exit; |
my $ident = ''; |
| 682 |
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
my $crud = ''; |
| 683 |
my $ident = $query->{criterias}->[0]->{val}; |
|
| 684 |
#print "load obj", "\n"; |
|
| 685 |
#return $self->createSet() if $ident == 5; |
# dispatch type and mode |
| 686 |
$self->{_COREHANDLE}->unload($ident); |
|
| 687 |
my $object = $self->{_COREHANDLE}->load($ident); |
# defaults - 1 |
| 688 |
#print "get id", "\n"; |
if ($query->{options}) { |
| 689 |
my $oid = $self->{_COREHANDLE}->id($object); |
$crud = $query->{options}->{crud}; |
| 690 |
#print Dumper($object); |
$crud ||= $query->{options}->{action}; |
| 691 |
#print "oid: $oid", "\n"; |
} |
| 692 |
return $self->createSet($object); |
|
| 693 |
#return $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
# defaults - 2 |
| 694 |
} |
$type ||= 'TRANSPARENT'; |
| 695 |
|
$crud ||= 'RETRIEVE'; |
| 696 |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
|
| 697 |
#return $self->createSet($object); |
if ($query->{options}->{OID}) { |
| 698 |
#return $self->createSet($list); |
$type = 'ITEM'; |
| 699 |
return $self->createSet(@$list); |
$mode = 'OID'; |
| 700 |
|
$ident = $query->{options}->{OID}; |
| 701 |
#die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
|
| 702 |
|
} elsif (my $guid = $query->{options}->{GUID}) { |
| 703 |
|
$type = 'TRANSPARENT'; |
| 704 |
|
$query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; |
| 705 |
|
|
| 706 |
|
# if operator is different (dispatcher for 'getListFiltered') |
| 707 |
|
} elsif (my $op = $query->{options}->{op}) { |
| 708 |
|
$type = 'TRANSPARENT'; |
| 709 |
|
$query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ]; |
| 710 |
|
|
| 711 |
|
} |
| 712 |
|
|
| 713 |
|
# HACK: special case: querying by id does not translate into a common tangram query |
| 714 |
|
# just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync! |
| 715 |
|
if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) { |
| 716 |
|
$type = 'ITEM'; |
| 717 |
|
$mode = 'SPECIAL.SYNC'; |
| 718 |
|
$ident = $query->{criterias}->[0]->{val}; |
| 719 |
|
} |
| 720 |
|
|
| 721 |
|
|
| 722 |
|
# execute query |
| 723 |
|
my $result; |
| 724 |
|
|
| 725 |
|
if ($type eq 'ITEM' && $ident) { |
| 726 |
|
|
| 727 |
|
if ($mode eq 'OID') { |
| 728 |
|
# TODO: review this case! |
| 729 |
|
$result = $self->getObject($ident, $query->{options}); |
| 730 |
|
|
| 731 |
|
} elsif ($mode eq 'SPECIAL.SYNC') { |
| 732 |
|
|
| 733 |
|
# V1 - failed |
| 734 |
|
#return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); |
| 735 |
|
|
| 736 |
|
# hmm.... |
| 737 |
|
#return $self->createSet() if $ident == 5; |
| 738 |
|
|
| 739 |
|
# Unload single object before doing any further operations to |
| 740 |
|
# expect a "fresh" object from orm when performing the next calls. |
| 741 |
|
$self->{_COREHANDLE}->unload($ident); |
| 742 |
|
|
| 743 |
|
# Load object from orm. |
| 744 |
|
my $object = $self->{_COREHANDLE}->load($ident); |
| 745 |
|
|
| 746 |
|
# determine object identifier (OID) |
| 747 |
|
my $oid = $self->{_COREHANDLE}->id($object); |
| 748 |
|
|
| 749 |
|
# encapsulate into result/response container and return this one |
| 750 |
|
$result = $self->createSet($object); |
| 751 |
|
|
| 752 |
|
# debugging |
| 753 |
|
#$result = $self->createSet( $self->{COREHANDLE}->load('300090018') ); |
| 754 |
|
|
| 755 |
|
} |
| 756 |
|
|
| 757 |
|
|
| 758 |
|
} elsif ($type eq 'TRANSPARENT') { |
| 759 |
|
|
| 760 |
|
if ($crud eq 'RETRIEVE') { |
| 761 |
|
|
| 762 |
|
my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting}); |
| 763 |
|
#return $list; |
| 764 |
|
return $self->createResult($list); |
| 765 |
|
|
| 766 |
|
#return $self->createSet($object); |
| 767 |
|
#return $self->createSet($list); |
| 768 |
|
return $self->createSet(@$list); |
| 769 |
|
|
| 770 |
|
#die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); |
| 771 |
|
|
| 772 |
|
# try a generic tangram query here |
| 773 |
|
# TODO: try to place an oql on top of that (search.cpan.org!) |
| 774 |
|
my @crits; |
| 775 |
|
foreach (@{$query->{criterias}}) { |
| 776 |
|
my $op = ''; |
| 777 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
| 778 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
| 779 |
|
} |
| 780 |
|
my $subnodes = {}; |
| 781 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
| 782 |
|
# HACK: this is hardcoded ;( expand possibilities! |
| 783 |
|
#my $crit = join(' AND ', @crits); |
| 784 |
|
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
| 785 |
|
#return $self->sendCommand($sql); |
| 786 |
|
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
| 787 |
|
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
| 788 |
|
$result = $self->createCursor($query->{node}); |
| 789 |
|
|
| 790 |
|
} elsif ($crud eq 'UPDATE') { |
| 791 |
|
|
| 792 |
|
# Patch current query to be a loader (e.g. change action, remove payload) ... |
| 793 |
|
my $childquery = deep_copy($query); |
| 794 |
|
$childquery->{options}->{crud} = 'RETRIEVE'; |
| 795 |
|
delete $childquery->{payload}; |
| 796 |
|
|
| 797 |
|
# ... to use it to fetch a fresh object using ourselves (sendQuery). |
| 798 |
|
my $cursor = $self->sendQuery($childquery); |
| 799 |
|
my $status = $cursor->getStatus(); |
| 800 |
|
my $object = $cursor->getNextEntry(); |
| 801 |
|
|
| 802 |
|
# Merge values and apply value modifiers. |
| 803 |
|
my $options = { utf8 => 1, php => 1 }; |
| 804 |
|
merge_to($object, $query->{payload}, $options); |
| 805 |
|
|
| 806 |
|
# Execute update operation at orm. |
| 807 |
|
$self->update($object); |
| 808 |
|
|
| 809 |
|
} elsif ($crud eq 'DELETE') { |
| 810 |
|
|
| 811 |
|
# Patch current query to be a loader (e.g. change action) ... |
| 812 |
|
my $childquery = deep_copy($query); |
| 813 |
|
$childquery->{options}->{crud} = 'RETRIEVE'; |
| 814 |
|
|
| 815 |
|
# ... to use it to fetch a fresh object using ourselves (sendQuery). |
| 816 |
|
my $cursor = $self->sendQuery($childquery); |
| 817 |
|
my $status = $cursor->getStatus(); |
| 818 |
|
my $object = $cursor->getNextEntry(); |
| 819 |
|
|
| 820 |
|
$self->erase($object); |
| 821 |
|
|
| 822 |
|
} elsif ($crud eq 'CREATE') { |
| 823 |
|
|
| 824 |
|
my $nodename = $query->{node}; |
| 825 |
|
my $newnode = $self->createNode($nodename); |
| 826 |
|
my $id = $self->{_COREHANDLE}->insert($newnode); |
| 827 |
|
|
| 828 |
|
print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n"; |
| 829 |
|
|
| 830 |
|
return $newnode; |
| 831 |
|
|
| 832 |
|
} |
| 833 |
|
|
| 834 |
|
} |
| 835 |
|
|
| 836 |
|
return $result; |
| 837 |
|
|
|
# try a generic tangram query here |
|
|
# TODO: try to place an oql on top of that (search.cpan.org!) |
|
|
my @crits; |
|
|
foreach (@{$query->{criterias}}) { |
|
|
my $op = ''; |
|
|
$op = '=' if lc $_->{op} eq 'eq'; |
|
|
push @crits, "$_->{key}$op'$_->{val}'"; |
|
|
} |
|
|
my $subnodes = {}; |
|
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
|
|
# HACK: this is hardcoded ;( expand possibilities! |
|
|
#my $crit = join(' AND ', @crits); |
|
|
#my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
|
|
#return $self->sendCommand($sql); |
|
|
#my $h = $self->{COREHANDLE}->remote($query->{node}); |
|
|
#my $res = $self->{COREHANDLE}->select($h, $h->{); |
|
|
return $self->createCursor($query->{node}); |
|
| 838 |
} |
} |
| 839 |
|
|
| 840 |
sub eraseAll { |
sub eraseAll { |
| 870 |
return $object if $object; |
return $object if $object; |
| 871 |
} |
} |
| 872 |
|
|
| 873 |
sub getObjectByGuid { |
sub getObjectByGuid_old { |
| 874 |
my $self = shift; |
my $self = shift; |
| 875 |
my $guid = shift; |
my $guid = shift; |
| 876 |
my $options = shift; |
my $options = shift; |
| 900 |
|
|
| 901 |
} |
} |
| 902 |
|
|
| 903 |
sub getObjectAsHash { |
sub getObjectAsHash_old { |
| 904 |
my $self = shift; |
my $self = shift; |
| 905 |
my $oid = shift; |
my $oid = shift; |
| 906 |
my $options = shift; |
my $options = shift; |
| 915 |
# build options (a callback to unload autovivified objects) for 'expand' |
# build options (a callback to unload autovivified objects) for 'expand' |
| 916 |
# TODO: use $logger to write to debug here! |
# TODO: use $logger to write to debug here! |
| 917 |
my $cb; # = sub {}; |
my $cb; # = sub {}; |
| 918 |
|
|
| 919 |
|
# deactivated way to get rid of used instances, if requested |
| 920 |
=pod |
=pod |
| 921 |
if ($options->{destroy}) { |
if ($options->{destroy}) { |
| 922 |
$options->{cb}->{destroy} = sub { |
$options->{cb}->{destroy} = sub { |
| 923 |
print "================ DESTROY", "\n"; |
print "================ DESTROY", "\n"; |
| 924 |
my $object = shift; |
my $object = shift; |
| 925 |
#print Dumper($object); |
#print Dumper($object); |
| 926 |
$self->{_COREHANDLE}->unload($object); |
$self->{_COREHANDLE}->unload($object); |
| 927 |
#undef($object); |
#undef($object); |
| 928 |
}; |
}; |
| 929 |
} |
} |
| 930 |
=cut |
=cut |
| 931 |
|
|
| 932 |
my $hash = expand($obj, $options); |
my $hash = expand($obj, $options); |
| 933 |
#$options->{cb}->{destroy}->($obj); |
|
| 934 |
#$self->{_COREHANDLE}->unload($obj); |
# old (unsuccessful) attempts to get rid of used instances, if requested |
| 935 |
|
|
| 936 |
# convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML |
# V1: |
| 937 |
# now done in expand |
#$options->{cb}->{destroy}->($obj); |
| 938 |
#var2utf8($hash) if ($options->{utf8}); |
#$self->{_COREHANDLE}->unload($obj); |
| 939 |
|
|
| 940 |
# old (wrong) attempts to get rid of used instances, if requested |
# V2: |
| 941 |
#$obj->clear_refs; |
#$obj->clear_refs; |
| 942 |
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
#$self->{COREHANDLE}->unload($obj) if($options->{destroy}); |
| 943 |
#$obj->DESTROY; |
#$obj->DESTROY; |
| 983 |
$self->{dataStorageLayer}->disconnect(); |
$self->{dataStorageLayer}->disconnect(); |
| 984 |
} |
} |
| 985 |
|
|
| 986 |
|
|
| 987 |
|
sub createNode { |
| 988 |
|
my $self = shift; |
| 989 |
|
my $classname = shift; |
| 990 |
|
|
| 991 |
|
my $obj = $classname->new(); |
| 992 |
|
|
| 993 |
|
my $attr_options = Class::Tangram::attribute_options($classname); |
| 994 |
|
#print "Attribute Options: " . Dumper($attr_options); |
| 995 |
|
|
| 996 |
|
my $attr_types = Class::Tangram::attribute_types($classname); |
| 997 |
|
#print "Attribute Types: " . Dumper($attr_types); |
| 998 |
|
|
| 999 |
|
foreach(keys %{$attr_types}) { |
| 1000 |
|
if($attr_types->{$_} eq 'string') { |
| 1001 |
|
$obj->{$_} = ''; |
| 1002 |
|
} elsif($attr_types->{$_} eq 'int') { |
| 1003 |
|
$obj->{$_} = 0; |
| 1004 |
|
} elsif($attr_types->{$_} eq 'real') { |
| 1005 |
|
$obj->{$_} = 0; |
| 1006 |
|
} elsif($attr_types->{$_} eq 'rawdatetime') { |
| 1007 |
|
$obj->{$_} = '0000-00-00 00:00:00'; |
| 1008 |
|
} elsif($attr_types->{$_} eq 'ref') { |
| 1009 |
|
if($attr_options->{$_}->{class}) { |
| 1010 |
|
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
| 1011 |
|
} else { |
| 1012 |
|
#$obj->{$_} = undef(); |
| 1013 |
|
} |
| 1014 |
|
} elsif($attr_types->{$_} eq 'iarray') { |
| 1015 |
|
$obj->{$_} = [ ]; |
| 1016 |
|
} elsif($attr_types->{$_} eq 'hash') { |
| 1017 |
|
$obj->{$_} = { }; |
| 1018 |
|
} elsif($attr_types->{$_} eq 'flat_hash') { |
| 1019 |
|
$obj->{$_} = { }; |
| 1020 |
|
} |
| 1021 |
|
} |
| 1022 |
|
|
| 1023 |
|
#print "New Object: " . Dumper($obj); |
| 1024 |
|
|
| 1025 |
|
return $obj; |
| 1026 |
|
} |
| 1027 |
|
|
| 1028 |
|
|
| 1029 |
|
sub insertChildNode { |
| 1030 |
|
my $self = shift; |
| 1031 |
|
my $child_entry = shift; |
| 1032 |
|
my $query_args = shift; |
| 1033 |
|
|
| 1034 |
|
my $core = $self->{_COREHANDLE}; |
| 1035 |
|
my $nodename = $query_args->{nodename}; |
| 1036 |
|
|
| 1037 |
|
# get parent object |
| 1038 |
|
my $query = { |
| 1039 |
|
node => $query_args->{parent}->{nodename}, |
| 1040 |
|
options => { GUID => $query_args->{parent}->{guid}, }, |
| 1041 |
|
}; |
| 1042 |
|
my $cursor = $self->sendQuery($query); |
| 1043 |
|
my $parent = $cursor->getNextEntry(); |
| 1044 |
|
|
| 1045 |
|
# debug |
| 1046 |
|
#print "Parent_org: " . Dumper($parent); |
| 1047 |
|
|
| 1048 |
|
# Create child node object if isn't already done |
| 1049 |
|
# ($child_entry have to be the class name then...) |
| 1050 |
|
if(!ref($child_entry)) { |
| 1051 |
|
$child_entry = $self->createNode($child_entry); |
| 1052 |
|
# it could be insert 'manually' or will be insert 'transparent' if parent will be updated |
| 1053 |
|
#$core->insert($child_entry); |
| 1054 |
|
#print "Create child object [$nodename]: " . Dumper($child_entry); |
| 1055 |
|
} |
| 1056 |
|
|
| 1057 |
|
# get reference of tied node (seems, only on Linux node's are tied!!) |
| 1058 |
|
my $tied_node = tied $parent->{$nodename}; |
| 1059 |
|
|
| 1060 |
|
# insert/change child entry at parent |
| 1061 |
|
#print "reference: " . ref($parent->{$nodename}) . "\n"; |
| 1062 |
|
if(ref($parent->{$nodename}) eq 'ARRAY') { |
| 1063 |
|
# (seems, only on Linux node's are tied!!) |
| 1064 |
|
if($tied_node) { |
| 1065 |
|
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type, |
| 1066 |
|
# so a 'PUSH' is not implemented (which could be then done transparently) |
| 1067 |
|
my $array = $tied_node->FETCH; |
| 1068 |
|
push @$array, $child_entry; |
| 1069 |
|
$tied_node->STORE($array); |
| 1070 |
|
# node will be normaly untied at 'STORE' |
| 1071 |
|
if(tied $parent->{$nodename}) { print "already tied !!\n"; } |
| 1072 |
|
else { undef $tied_node; } |
| 1073 |
|
} else { |
| 1074 |
|
push @{$parent->{$nodename}}, $child_entry; |
| 1075 |
|
} |
| 1076 |
|
} |
| 1077 |
|
elsif(ref($parent->{$nodename}) eq 'HASH') { |
| 1078 |
|
if(my $key = $query_args->{hash_key}) { |
| 1079 |
|
# (seems, only on Linux node's are tied!!) |
| 1080 |
|
if($tied_node) { |
| 1081 |
|
# same problem as with 'ARRAY': |
| 1082 |
|
# all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type. |
| 1083 |
|
my $hash = $tied_node->FETCH; |
| 1084 |
|
$hash->{$key} = $child_entry; |
| 1085 |
|
$tied_node->STORE($hash); |
| 1086 |
|
# node will be normaly untied at 'STORE' |
| 1087 |
|
if(tied $parent->{$nodename}) { print "already tied !!\n"; } |
| 1088 |
|
else { undef $tied_node; } |
| 1089 |
|
} else { |
| 1090 |
|
$parent->{$nodename}->{$key} = $child_entry; |
| 1091 |
|
} |
| 1092 |
|
} else { |
| 1093 |
|
print "ERROR: No HASH KEY given, so not able to insert hash entry!"; |
| 1094 |
|
} |
| 1095 |
|
} |
| 1096 |
|
else { |
| 1097 |
|
$parent->{$nodename} = $child_entry; |
| 1098 |
|
} |
| 1099 |
|
|
| 1100 |
|
# debug |
| 1101 |
|
#print "Parent_new: " . Dumper($parent); |
| 1102 |
|
|
| 1103 |
|
# save parent |
| 1104 |
|
$core->update($parent); |
| 1105 |
|
|
| 1106 |
|
# debug |
| 1107 |
|
#print "Saved Parent: ". Dumper($parent); |
| 1108 |
|
|
| 1109 |
|
return $child_entry; |
| 1110 |
|
} |
| 1111 |
|
|
| 1112 |
|
|
| 1113 |
1; |
1; |
| 1114 |
__END__ |
__END__ |