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 |
# Revision 1.38 2003/05/13 16:38:38 joko |
16 |
# problems with "tied" on 5.6.1/win32 |
# problems with "tied" on 5.6.1/win32 |
17 |
# |
# |
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 ); |
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 |
|
|
549 |
|
my $orm_filter_tmp = undef; |
550 |
|
# was: |
551 |
# TODO: is_op? |
# TODO: is_op? |
552 |
# dispatch un-common operators if exists |
# dispatch un-common operators if exists |
553 |
if($filter->{op} eq "ref") { |
if ($filter->{op} eq "ref") { |
554 |
push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'"; |
# 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 { |
} else { |
558 |
# 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 |
559 |
push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; |
#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: |
|
#@results = $self->{COREHANDLE}->select($object_set, $tfilter); |
|
583 |
|
|
584 |
# is: |
# 2.b. sorting [new as of 2003-06-17] |
585 |
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
if ($in->{sorting}) { |
586 |
my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; |
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 |
#print "eval: $evalstring", "\n"; |
# 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 |
# get filtered list/set |
#my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args); |
607 |
@results = eval($evalstring); |
#return $cursor; |
608 |
die $@ if $@; |
#print Dumper(@results); |
609 |
|
|
610 |
|
# is: |
611 |
|
# HACK: build eval-string (sorry) to get filtered list - please give advice here |
612 |
|
#my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');'; |
613 |
|
#print "eval: $evalstring", "\n"; |
614 |
|
# get filtered list/set |
615 |
|
#@results = eval($evalstring); |
616 |
|
#die $@ if $@; |
617 |
|
|
618 |
|
=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; |
759 |
|
|
760 |
if ($crud eq 'RETRIEVE') { |
if ($crud eq 'RETRIEVE') { |
761 |
|
|
762 |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}); |
my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting}); |
763 |
|
#return $list; |
764 |
|
return $self->createResult($list); |
765 |
|
|
766 |
#return $self->createSet($object); |
#return $self->createSet($object); |
767 |
#return $self->createSet($list); |
#return $self->createSet($list); |
768 |
return $self->createSet(@$list); |
return $self->createSet(@$list); |