my $op=$self->name();
if(defined($op))
{
- my $left_is_not =($left && $left->name() && $left->name() eq "NOT");
- my $right_is_not=($right && $right->name() && $right->name() eq "NOT");
my $join=undef;
- # if we are ANDing, add an inner join
+ # if we are ANDing add an inner join
# also if we are NOTing, but we are looking for a tag *value*
if($op eq "AND")
{
# if right child is a NOT, we don't need extra join/brackets
# NOT will do the same and we will end up with an extra one
- $join= "INNER" unless($right_is_not);
+ $join= "INNER" unless($right && $right->name() && $right->name() eq "NOT");
}
elsif($op eq "NOT")
{
$not=1;
- # as above - if right child is a NOT, we don't need extra join/brackets
- # NOT will do the same and we will end up with an extra one
- $join = ($hasvals ? "INNER" : "LEFT") unless($right_is_not);
+ $join = ($hasvals ? "INNER" : "LEFT");
}
elsif($op eq "OR")
{
- # if left child is a NOT, we need an extra (inner) join
- # unless right child is also a NOT
- $join="INNER" if($left_is_not && ! $right_is_not)
+ # if the rightmost part of the left sub-expression ends in
+ # NOT, then we need an extra join. This doesn't apply if
+ # (as above) the righthand expression is a NOT.
+ $join="INNER" if(($self->right_ends_in_not($left)) &&
+ !($right && $right->name() && $right->name() eq "NOT"));
}
if($join)
{
return $self->id();
}
+sub right_ends_in_not
+{
+ my($self, $node)=@_;
+ return 0 unless($node);
+ my $right=$node->right();
+ if($right && $right->type() == $TYPE_BOOL)
+ {
+ return $self->right_ends_in_not($right);
+ }
+ my $op=$node->name();
+ return 0 unless($op);
+ return 1 if($op eq "NOT");
+ return 0;
+}
+
1;