| 3 |
# $Id$ |
# $Id$ |
| 4 |
# |
# |
| 5 |
# $Log$ |
# $Log$ |
| 6 |
|
# Revision 1.48 2004/10/28 11:35:51 jonen |
| 7 |
|
# + bugfix related to select-criterias(filter) |
| 8 |
|
# |
| 9 |
|
# Revision 1.47 2004/08/31 14:26:08 jonen |
| 10 |
|
# updated |
| 11 |
|
# |
| 12 |
|
# Revision 1.46 2004/05/06 12:54:34 jonen |
| 13 |
|
# + bugfix related to multiple select-'filter' |
| 14 |
|
# |
| 15 |
|
# Revision 1.45 2003/12/14 01:48:36 jonen |
| 16 |
|
# small HACK at _insertChildNode: some special Childnodes should not be created because existing objects have to be selected! |
| 17 |
|
# TODO: make this more generic, e.g. implement a special flag at Schema |
| 18 |
|
# |
| 19 |
|
# Revision 1.44 2003/12/04 01:01:50 joko |
| 20 |
|
# + sendQuery now returns result even on crud=UPDATE |
| 21 |
|
# |
| 22 |
|
# Revision 1.43 2003/07/02 11:07:12 jonen |
| 23 |
|
# re-activate filtering of results *after* results are fetched from tangram |
| 24 |
|
# (needed for e.g. UserManagment) |
| 25 |
|
# |
| 26 |
|
# Revision 1.42 2003/07/01 23:24:17 joko |
| 27 |
|
# now using package before using function |
| 28 |
|
# |
| 29 |
|
# Revision 1.41 2003/06/29 02:03:45 joko |
| 30 |
|
# fix:? initialize schema on startup |
| 31 |
|
# |
| 32 |
|
# Revision 1.40 2003/06/25 22:57:54 joko |
| 33 |
|
# major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting" |
| 34 |
|
# |
| 35 |
|
# Revision 1.39 2003/06/06 11:40:40 jonen |
| 36 |
|
# fixed bug at 'getFilteredList' |
| 37 |
|
# |
| 38 |
# Revision 1.38 2003/05/13 16:38:38 joko |
# Revision 1.38 2003/05/13 16:38:38 joko |
| 39 |
# problems with "tied" on 5.6.1/win32 |
# problems with "tied" on 5.6.1/win32 |
| 40 |
# |
# |
| 189 |
|
|
| 190 |
use Data::Dumper; |
use Data::Dumper; |
| 191 |
use Tangram; |
use Tangram; |
| 192 |
|
use Class::Tangram; |
| 193 |
|
|
| 194 |
use DesignPattern::Object; |
use DesignPattern::Object; |
| 195 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
| 199 |
# get logger instance |
# get logger instance |
| 200 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
| 201 |
|
|
| 202 |
|
#$Tangram::TRACE = *STDOUT; |
| 203 |
|
|
| 204 |
# this holds the complete instantiated schema from tangram |
# this holds the complete instantiated schema from tangram |
| 205 |
my $schema_tangram; |
my $schema_tangram; |
| 218 |
#if (!$schema_tangram) { |
#if (!$schema_tangram) { |
| 219 |
#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} } ); |
| 220 |
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} } ); |
| 221 |
$schema_tangram = $obj->getSchema(); |
$schema_tangram = $obj->getSchema() if $obj; |
| 222 |
#} |
#} |
| 223 |
if (!$schema_tangram) { |
if (!$schema_tangram) { |
| 224 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." ); |
| 225 |
return 0; |
return 0; |
| 226 |
} |
} |
| 227 |
#$self->_patchSchema(); |
#$self->_patchSchema(); |
| 267 |
# return; |
# return; |
| 268 |
# } |
# } |
| 269 |
|
|
| 270 |
#return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
| 271 |
$self->_initSchema(); |
#$self->_initSchema(); |
| 272 |
|
|
| 273 |
# create the main tangram storage object |
# create the main tangram storage object |
| 274 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
| 530 |
# redirect to unfiltered mode |
# redirect to unfiltered mode |
| 531 |
#return $self->getListUnfiltered(@_); |
#return $self->getListUnfiltered(@_); |
| 532 |
|
|
| 533 |
my $nodename = shift; |
my $in = {}; |
| 534 |
my $filters = shift; |
$in->{nodename} = shift; |
| 535 |
|
$in->{filters} = shift; |
| 536 |
|
$in->{sorting} = shift; |
| 537 |
|
|
| 538 |
my @results; |
my @results; |
| 539 |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" ); |
| 540 |
|
|
| 541 |
|
#print "Filter_payload: " . Dumper($in->{filters}) . "\n"; |
| 542 |
|
|
| 543 |
#print Dumper($filters); |
# 1. "Remote Object Handle" - get set of objects from odbms by object name |
| 544 |
|
my $remote = $self->{_COREHANDLE}->remote($in->{nodename}); |
| 545 |
|
|
| 546 |
|
# 2. Transfer $in to $orm_query |
| 547 |
|
my $orm_query = {}; |
| 548 |
|
|
| 549 |
|
# 2.a. Filters |
| 550 |
my @tfilters; |
my @tfilters; |
| 551 |
|
my $orm_filter = undef; |
| 552 |
|
|
| 553 |
foreach my $filter (@$filters) { |
foreach my $filter (@{$in->{filters}}) { |
| 554 |
|
|
| 555 |
# get filter - TODO: for each filter |
# get filter - TODO: for each filter |
| 556 |
#my $filter = $filters->[0]; |
#my $filter = $filters->[0]; |
| 557 |
|
|
| 558 |
# build filter |
# 1. The parts of a filter source entry |
| 559 |
my $lexpr = $filter->{key}; |
my $lexpr = $filter->{key}; |
| 560 |
#my $op = $filter->{op}; |
#my $op = $filter->{op}; |
| 561 |
my $op = '='; |
my $op = '='; |
| 562 |
my $rexpr = $filter->{val}; |
my $rexpr = $filter->{val}; |
| 563 |
my $tight = 100; |
my $tight = 100; |
| 564 |
|
|
| 565 |
|
# 2. Build filter target entry |
| 566 |
|
|
| 567 |
# my $tfilter = Tangram::Filter->new( |
# Test 1 - didn't work out! |
| 568 |
# expr => "t1.$lexpr $op '$rexpr'", |
# my $tfilter = Tangram::Filter->new( |
| 569 |
# tight => $tight, |
# expr => "t1.$lexpr $op '$rexpr'", |
| 570 |
# objects => $objects, |
# tight => $tight, |
| 571 |
# ); |
# objects => $objects, |
| 572 |
|
# ); |
| 573 |
|
|
| 574 |
|
my $orm_filter_tmp = undef; |
| 575 |
|
# was: |
| 576 |
# TODO: is_op? |
# TODO: is_op? |
| 577 |
# dispatch un-common operators if exists |
# dispatch un-common operators if exists |
| 578 |
if($filter->{op} eq "ref") { |
if ($filter->{op} eq "ref") { |
| 579 |
push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
# do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query |
| 580 |
|
#print "Filter->op eq 'ref'.\n"; |
| 581 |
|
#push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
| 582 |
} else { |
} else { |
| 583 |
# 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 |
| 584 |
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
| 585 |
|
# better: calculate expression right here! |
| 586 |
|
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
| 587 |
|
|
| 588 |
|
#print "key: ", $filter->{key}, "\n"; |
| 589 |
|
|
| 590 |
|
my $left = $remote->{$filter->{key}}; |
| 591 |
|
#my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" ); |
| 592 |
|
my $right = $filter->{val}; |
| 593 |
|
my $op = $filter->{op}; |
| 594 |
|
$orm_filter_tmp = $left->$op($right); |
| 595 |
} |
} |
| 596 |
|
|
| 597 |
} |
if (!ref($orm_filter)) { |
| 598 |
|
$orm_filter = $orm_filter_tmp; |
| 599 |
|
} else { |
| 600 |
|
$orm_filter = $orm_filter->and($orm_filter_tmp); |
| 601 |
|
} |
| 602 |
|
|
| 603 |
my $tfilter = join(' & ', @tfilters); |
} |
| 604 |
|
|
| 605 |
# get set of objects from odbms by object name |
$orm_query->{filter} = $orm_filter; |
|
my $remote = $self->{_COREHANDLE}->remote($nodename); |
|
| 606 |
|
|
| 607 |
|
# debug point: |
| 608 |
|
#print "Filter: " . Dumper($orm_query->{filter}) . "\n"; |
| 609 |
|
|
| 610 |
# was: |
# was: |
| 611 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
|
| 612 |
|
# 2.b. sorting [new as of 2003-06-17] |
| 613 |
|
if ($in->{sorting}) { |
| 614 |
|
my $sorting_rules = []; |
| 615 |
|
my $sorting_order = 'ASC'; |
| 616 |
|
foreach my $sorting_column (keys %{$in->{sorting}}) { |
| 617 |
|
$sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column}; |
| 618 |
|
push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column ); |
| 619 |
|
} |
| 620 |
|
$orm_query->{order} = $sorting_rules; |
| 621 |
|
if ($sorting_order eq 'DESC') { |
| 622 |
|
$orm_query->{desc} = 1; |
| 623 |
|
} |
| 624 |
|
} |
| 625 |
|
|
| 626 |
|
|
| 627 |
|
# 3. build and issue query - return result as Tangram::Cursor |
| 628 |
|
|
| 629 |
|
# coerce hash into array (This is valid in Perl) |
| 630 |
|
my @orm_query_args = %$orm_query; |
| 631 |
|
# send query (arguments) to orm |
| 632 |
|
@results = $self->{_COREHANDLE}->select($remote, @orm_query_args); |
| 633 |
|
|
| 634 |
|
#my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args); |
| 635 |
|
#return $cursor; |
| 636 |
|
#print Dumper(@results); |
| 637 |
|
|
| 638 |
# is: |
# is: |
| 639 |
# 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 |
| 640 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
#my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');'; |
| 641 |
|
#print "eval: $evalstring", "\n"; |
| 642 |
#print "eval: $evalstring", "\n"; |
# get filtered list/set |
| 643 |
|
#@results = eval($evalstring); |
| 644 |
# get filtered list/set |
#die $@ if $@; |
| 645 |
@results = eval($evalstring); |
|
| 646 |
die $@ if $@; |
# filter results - NEEDED for e.g. UserManagment !! |
| 647 |
|
if ($in->{filters}->[0]->{op} && ($in->{filters}->[0]->{op} eq "ref")) { |
| 648 |
|
#print "Filter->op eq 'ref'.\n"; |
| 649 |
|
my $att_name = $in->{filters}->[0]->{key}; |
| 650 |
|
my $att_val = $in->{filters}->[0]->{val}; |
| 651 |
|
my @filtered; |
| 652 |
|
foreach(@results) { |
| 653 |
|
if(ref($_->{$att_name}) eq $att_val) { |
| 654 |
|
push @filtered, $_; |
| 655 |
|
} |
| 656 |
|
} |
| 657 |
|
@results = @filtered; |
| 658 |
|
} |
| 659 |
|
|
| 660 |
|
#print "results: " . Dumper(\@results); |
| 661 |
|
|
| 662 |
return \@results; |
return \@results; |
| 663 |
} |
} |
| 687 |
return $result; |
return $result; |
| 688 |
} |
} |
| 689 |
|
|
| 690 |
|
sub createResult { |
| 691 |
|
my $self = shift; |
| 692 |
|
my $rh = shift; |
| 693 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
| 694 |
|
return $result; |
| 695 |
|
} |
| 696 |
|
|
| 697 |
sub sendQuery { |
sub sendQuery { |
| 698 |
my $self = shift; |
my $self = shift; |
| 699 |
my $query = shift; |
my $query = shift; |
| 727 |
|
|
| 728 |
} elsif (my $guid = $query->{options}->{GUID}) { |
} elsif (my $guid = $query->{options}->{GUID}) { |
| 729 |
$type = 'TRANSPARENT'; |
$type = 'TRANSPARENT'; |
| 730 |
$query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; |
if(ref($query->{criterias}) eq 'ARRAY') { |
| 731 |
|
push @{$query->{criterias}}, { key => 'guid', op => 'eq', val => $guid }; |
| 732 |
|
} else { |
| 733 |
|
$query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ]; |
| 734 |
|
} |
| 735 |
|
} |
| 736 |
# if operator is different (dispatcher for 'getListFiltered') |
# if operator is different (dispatcher for 'getListFiltered') |
| 737 |
} elsif (my $op = $query->{options}->{op}) { |
if (my $op = $query->{options}->{op}) { |
| 738 |
$type = 'TRANSPARENT'; |
$type = 'TRANSPARENT'; |
| 739 |
$query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ]; |
if(ref($query->{criterias}) eq 'ARRAY') { |
| 740 |
|
push @{$query->{criterias}}, { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} }; |
| 741 |
|
} else { |
| 742 |
|
$query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ]; |
| 743 |
|
} |
| 744 |
} |
} |
| 745 |
|
|
| 746 |
# HACK: special case: querying by id does not translate into a common tangram query |
# HACK: special case: querying by id does not translate into a common tangram query |
| 792 |
|
|
| 793 |
if ($crud eq 'RETRIEVE') { |
if ($crud eq 'RETRIEVE') { |
| 794 |
|
|
| 795 |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting}); |
| 796 |
|
#return $list; |
| 797 |
|
return $self->createResult($list); |
| 798 |
|
|
| 799 |
#return $self->createSet($object); |
#return $self->createSet($object); |
| 800 |
#return $self->createSet($list); |
#return $self->createSet($list); |
| 801 |
return $self->createSet(@$list); |
return $self->createSet(@$list); |
| 836 |
my $options = { utf8 => 1, php => 1 }; |
my $options = { utf8 => 1, php => 1 }; |
| 837 |
merge_to($object, $query->{payload}, $options); |
merge_to($object, $query->{payload}, $options); |
| 838 |
|
|
| 839 |
|
#print Dumper($object); |
| 840 |
|
|
| 841 |
# Execute update operation at orm. |
# Execute update operation at orm. |
| 842 |
$self->update($object); |
$self->update($object); |
| 843 |
|
$result = $self->createResult([ $object ]); |
| 844 |
|
|
| 845 |
} elsif ($crud eq 'DELETE') { |
} elsif ($crud eq 'DELETE') { |
| 846 |
|
|
| 854 |
my $object = $cursor->getNextEntry(); |
my $object = $cursor->getNextEntry(); |
| 855 |
|
|
| 856 |
$self->erase($object); |
$self->erase($object); |
| 857 |
|
$self->unload($object); |
| 858 |
|
|
| 859 |
} elsif ($crud eq 'CREATE') { |
} elsif ($crud eq 'CREATE') { |
| 860 |
|
|
| 1044 |
$obj->{$_} = '0000-00-00 00:00:00'; |
$obj->{$_} = '0000-00-00 00:00:00'; |
| 1045 |
} elsif($attr_types->{$_} eq 'ref') { |
} elsif($attr_types->{$_} eq 'ref') { |
| 1046 |
if($attr_options->{$_}->{class}) { |
if($attr_options->{$_}->{class}) { |
| 1047 |
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
# HACK!!! |
| 1048 |
|
# STANDALONE Objects (objects which make sense to instanciat alone) should not created automaticly |
| 1049 |
|
# because they maybe exists and should only be SETTED not CREATED! |
| 1050 |
|
# TODO: Create a flag at the scheme for that reason! |
| 1051 |
|
# (e.g child_node => 1 for child-nodes only like e.g. UserData) |
| 1052 |
|
if($attr_options->{$_}->{class} eq 'NetPerson' || $attr_options->{$_}->{class} eq 'Event' || $attr_options->{$_}->{class} eq 'BetRule') { |
| 1053 |
|
#$obj->{$_} = undef(); |
| 1054 |
|
} else { |
| 1055 |
|
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
| 1056 |
|
} |
| 1057 |
} else { |
} else { |
| 1058 |
#$obj->{$_} = undef(); |
#$obj->{$_} = undef(); |
| 1059 |
} |
} |