3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.47 2004/08/31 14:26:08 jonen |
7 |
|
# updated |
8 |
|
# |
9 |
|
# Revision 1.46 2004/05/06 12:54:34 jonen |
10 |
|
# + bugfix related to multiple select-'filter' |
11 |
|
# |
12 |
|
# Revision 1.45 2003/12/14 01:48:36 jonen |
13 |
|
# small HACK at _insertChildNode: some special Childnodes should not be created because existing objects have to be selected! |
14 |
|
# TODO: make this more generic, e.g. implement a special flag at Schema |
15 |
|
# |
16 |
|
# Revision 1.44 2003/12/04 01:01:50 joko |
17 |
|
# + sendQuery now returns result even on crud=UPDATE |
18 |
|
# |
19 |
|
# Revision 1.43 2003/07/02 11:07:12 jonen |
20 |
|
# re-activate filtering of results *after* results are fetched from tangram |
21 |
|
# (needed for e.g. UserManagment) |
22 |
|
# |
23 |
|
# Revision 1.42 2003/07/01 23:24:17 joko |
24 |
|
# now using package before using function |
25 |
|
# |
26 |
|
# Revision 1.41 2003/06/29 02:03:45 joko |
27 |
|
# fix:? initialize schema on startup |
28 |
|
# |
29 |
|
# Revision 1.40 2003/06/25 22:57:54 joko |
30 |
|
# major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting" |
31 |
|
# |
32 |
# Revision 1.39 2003/06/06 11:40:40 jonen |
# Revision 1.39 2003/06/06 11:40:40 jonen |
33 |
# fixed bug at 'getFilteredList' |
# fixed bug at 'getFilteredList' |
34 |
# |
# |
186 |
|
|
187 |
use Data::Dumper; |
use Data::Dumper; |
188 |
use Tangram; |
use Tangram; |
189 |
|
use Class::Tangram; |
190 |
|
|
191 |
use DesignPattern::Object; |
use DesignPattern::Object; |
192 |
use Data::Storage::Result::Tangram; |
use Data::Storage::Result::Tangram; |
196 |
# get logger instance |
# get logger instance |
197 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
198 |
|
|
199 |
|
#$Tangram::TRACE = *STDOUT; |
200 |
|
|
201 |
# this holds the complete instantiated schema from tangram |
# this holds the complete instantiated schema from tangram |
202 |
my $schema_tangram; |
my $schema_tangram; |
215 |
#if (!$schema_tangram) { |
#if (!$schema_tangram) { |
216 |
#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} } ); |
217 |
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} } ); |
218 |
$schema_tangram = $obj->getSchema(); |
$schema_tangram = $obj->getSchema() if $obj; |
219 |
#} |
#} |
220 |
if (!$schema_tangram) { |
if (!$schema_tangram) { |
221 |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); |
$logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." ); |
222 |
return 0; |
return 0; |
223 |
} |
} |
224 |
#$self->_patchSchema(); |
#$self->_patchSchema(); |
264 |
# return; |
# return; |
265 |
# } |
# } |
266 |
|
|
267 |
#return unless $self->_initSchema(); |
return unless $self->_initSchema(); |
268 |
$self->_initSchema(); |
#$self->_initSchema(); |
269 |
|
|
270 |
# create the main tangram storage object |
# create the main tangram storage object |
271 |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
#$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); |
527 |
# redirect to unfiltered mode |
# redirect to unfiltered mode |
528 |
#return $self->getListUnfiltered(@_); |
#return $self->getListUnfiltered(@_); |
529 |
|
|
530 |
my $nodename = shift; |
my $in = {}; |
531 |
my $filters = shift; |
$in->{nodename} = shift; |
532 |
|
$in->{filters} = shift; |
533 |
|
$in->{sorting} = shift; |
534 |
|
|
535 |
my @results; |
my @results; |
536 |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); |
$logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" ); |
537 |
|
|
538 |
#print Dumper($filters); |
#print "Filter_payload: " . Dumper($in->{filters}) . "\n"; |
539 |
|
|
540 |
|
# 1. "Remote Object Handle" - get set of objects from odbms by object name |
541 |
|
my $remote = $self->{_COREHANDLE}->remote($in->{nodename}); |
542 |
|
|
543 |
|
# 2. Transfer $in to $orm_query |
544 |
|
my $orm_query = {}; |
545 |
|
|
546 |
|
# 2.a. Filters |
547 |
my @tfilters; |
my @tfilters; |
548 |
|
my $orm_filter = undef; |
549 |
|
|
550 |
foreach my $filter (@$filters) { |
foreach my $filter (@{$in->{filters}}) { |
551 |
|
|
552 |
# get filter - TODO: for each filter |
# get filter - TODO: for each filter |
553 |
#my $filter = $filters->[0]; |
#my $filter = $filters->[0]; |
554 |
|
|
555 |
# build filter |
# 1. The parts of a filter source entry |
556 |
my $lexpr = $filter->{key}; |
my $lexpr = $filter->{key}; |
557 |
#my $op = $filter->{op}; |
#my $op = $filter->{op}; |
558 |
my $op = '='; |
my $op = '='; |
559 |
my $rexpr = $filter->{val}; |
my $rexpr = $filter->{val}; |
560 |
my $tight = 100; |
my $tight = 100; |
561 |
|
|
562 |
|
# 2. Build filter target entry |
563 |
|
|
564 |
# my $tfilter = Tangram::Filter->new( |
# Test 1 - didn't work out! |
565 |
# expr => "t1.$lexpr $op '$rexpr'", |
# my $tfilter = Tangram::Filter->new( |
566 |
# tight => $tight, |
# expr => "t1.$lexpr $op '$rexpr'", |
567 |
# objects => $objects, |
# tight => $tight, |
568 |
# ); |
# objects => $objects, |
569 |
|
# ); |
570 |
|
|
571 |
|
my $orm_filter_tmp = undef; |
572 |
|
# was: |
573 |
# TODO: is_op? |
# TODO: is_op? |
574 |
# dispatch un-common operators if exists |
# dispatch un-common operators if exists |
575 |
if($filter->{op} eq "ref") { |
if ($filter->{op} eq "ref") { |
576 |
# do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query |
# do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query |
577 |
#print "Filter->op eq 'ref'.\n"; |
#print "Filter->op eq 'ref'.\n"; |
578 |
#push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
#push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
579 |
} else { |
} else { |
580 |
# 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 |
581 |
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
582 |
|
# better: calculate expression right here! |
583 |
|
#push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
584 |
|
|
585 |
|
#print "key: ", $filter->{key}, "\n"; |
586 |
|
|
587 |
|
my $left = $remote->{$filter->{key}}; |
588 |
|
#my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" ); |
589 |
|
my $right = $filter->{val}; |
590 |
|
my $op = $filter->{op}; |
591 |
|
$orm_filter_tmp = $left->$op($right); |
592 |
} |
} |
593 |
|
|
594 |
} |
if (!ref($orm_filter)) { |
595 |
|
$orm_filter = $orm_filter_tmp; |
596 |
|
} else { |
597 |
|
$orm_filter = $orm_filter->and($orm_filter_tmp); |
598 |
|
} |
599 |
|
|
600 |
my $tfilter = join(' & ', @tfilters); |
} |
601 |
|
|
602 |
# get set of objects from odbms by object name |
$orm_query->{filter} = $orm_filter; |
|
my $remote = $self->{_COREHANDLE}->remote($nodename); |
|
603 |
|
|
604 |
|
# debug point: |
605 |
|
#print "Filter: " . Dumper($orm_query->{filter}) . "\n"; |
606 |
|
|
607 |
# was: |
# was: |
608 |
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
|
609 |
|
# 2.b. sorting [new as of 2003-06-17] |
610 |
|
if ($in->{sorting}) { |
611 |
|
my $sorting_rules = []; |
612 |
|
my $sorting_order = 'ASC'; |
613 |
|
foreach my $sorting_column (keys %{$in->{sorting}}) { |
614 |
|
$sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column}; |
615 |
|
push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column ); |
616 |
|
} |
617 |
|
$orm_query->{order} = $sorting_rules; |
618 |
|
if ($sorting_order eq 'DESC') { |
619 |
|
$orm_query->{desc} = 1; |
620 |
|
} |
621 |
|
} |
622 |
|
|
623 |
|
|
624 |
|
# 3. build and issue query - return result as Tangram::Cursor |
625 |
|
|
626 |
|
# coerce hash into array (This is valid in Perl) |
627 |
|
my @orm_query_args = %$orm_query; |
628 |
|
# send query (arguments) to orm |
629 |
|
@results = $self->{_COREHANDLE}->select($remote, @orm_query_args); |
630 |
|
|
631 |
|
#my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args); |
632 |
|
#return $cursor; |
633 |
|
#print Dumper(@results); |
634 |
|
|
635 |
# is: |
# is: |
636 |
# 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 |
637 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
#my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');'; |
638 |
|
#print "eval: $evalstring", "\n"; |
639 |
#print "eval: $evalstring", "\n"; |
# get filtered list/set |
640 |
|
#@results = eval($evalstring); |
641 |
# get filtered list/set |
#die $@ if $@; |
|
@results = eval($evalstring); |
|
|
die $@ if $@; |
|
|
|
|
642 |
|
|
643 |
# filter results |
# filter results - NEEDED for e.g. UserManagment !! |
644 |
if($filters->[0]->{op} eq "ref") { |
if ($in->{filters}->[0]->{op} && ($in->{filters}->[0]->{op} eq "ref")) { |
645 |
#print "Filter->op eq 'ref'.\n"; |
#print "Filter->op eq 'ref'.\n"; |
646 |
my $att_name = $filters->[0]->{key}; |
my $att_name = $in->{filters}->[0]->{key}; |
647 |
my $att_val = $filters->[0]->{val}; |
my $att_val = $in->{filters}->[0]->{val}; |
648 |
my @filtered; |
my @filtered; |
649 |
foreach(@results) { |
foreach(@results) { |
650 |
if(ref($_->{$att_name}) eq $att_val) { |
if(ref($_->{$att_name}) eq $att_val) { |
684 |
return $result; |
return $result; |
685 |
} |
} |
686 |
|
|
687 |
|
sub createResult { |
688 |
|
my $self = shift; |
689 |
|
my $rh = shift; |
690 |
|
my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); |
691 |
|
return $result; |
692 |
|
} |
693 |
|
|
694 |
sub sendQuery { |
sub sendQuery { |
695 |
my $self = shift; |
my $self = shift; |
696 |
my $query = shift; |
my $query = shift; |
782 |
|
|
783 |
if ($crud eq 'RETRIEVE') { |
if ($crud eq 'RETRIEVE') { |
784 |
|
|
785 |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting}); |
786 |
|
#return $list; |
787 |
|
return $self->createResult($list); |
788 |
|
|
789 |
#return $self->createSet($object); |
#return $self->createSet($object); |
790 |
#return $self->createSet($list); |
#return $self->createSet($list); |
791 |
return $self->createSet(@$list); |
return $self->createSet(@$list); |
826 |
my $options = { utf8 => 1, php => 1 }; |
my $options = { utf8 => 1, php => 1 }; |
827 |
merge_to($object, $query->{payload}, $options); |
merge_to($object, $query->{payload}, $options); |
828 |
|
|
829 |
|
#print Dumper($object); |
830 |
|
|
831 |
# Execute update operation at orm. |
# Execute update operation at orm. |
832 |
$self->update($object); |
$self->update($object); |
833 |
|
$result = $self->createResult([ $object ]); |
834 |
|
|
835 |
} elsif ($crud eq 'DELETE') { |
} elsif ($crud eq 'DELETE') { |
836 |
|
|
844 |
my $object = $cursor->getNextEntry(); |
my $object = $cursor->getNextEntry(); |
845 |
|
|
846 |
$self->erase($object); |
$self->erase($object); |
847 |
|
$self->unload($object); |
848 |
|
|
849 |
} elsif ($crud eq 'CREATE') { |
} elsif ($crud eq 'CREATE') { |
850 |
|
|
1034 |
$obj->{$_} = '0000-00-00 00:00:00'; |
$obj->{$_} = '0000-00-00 00:00:00'; |
1035 |
} elsif($attr_types->{$_} eq 'ref') { |
} elsif($attr_types->{$_} eq 'ref') { |
1036 |
if($attr_options->{$_}->{class}) { |
if($attr_options->{$_}->{class}) { |
1037 |
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
# HACK!!! |
1038 |
|
# STANDALONE Objects (objects which make sense to instanciat alone) should not created automaticly |
1039 |
|
# because they maybe exists and should only be SETTED not CREATED! |
1040 |
|
# TODO: Create a flag at the scheme for that reason! |
1041 |
|
# (e.g child_node => 1 for child-nodes only like e.g. UserData) |
1042 |
|
if($attr_options->{$_}->{class} eq 'NetPerson' || $attr_options->{$_}->{class} eq 'Event' || $attr_options->{$_}->{class} eq 'BetRule') { |
1043 |
|
#$obj->{$_} = undef(); |
1044 |
|
} else { |
1045 |
|
$obj->{$_} = $self->createNode($attr_options->{$_}->{class}); |
1046 |
|
} |
1047 |
} else { |
} else { |
1048 |
#$obj->{$_} = undef(); |
#$obj->{$_} = undef(); |
1049 |
} |
} |