if($self->type() != $TYPE_BOOL)
{
+ print $self->{id}, " = ", $self->{name}, "\n";
$str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
if($not && !$hasvals)
{
my $left=$self->left();
my $right=$self->right();
return ("", @outjoins) unless($left || $right);
+
my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left);
push(@joins, @leftjoins);
push(@outjoins, @leftjoins);
+
my $op=$self->name();
- print "op: $op type: ", $self->type(), " not: $not\n";
if(defined($op))
{
- # if we are ANDing, add an inner join
- # also if we are NOTing, but we are looking for a tag *value*
+ # if we are ANDing add an inner join
if($op eq "AND")
{
- print "AND\n";
- # hack - if right child is a NOT, we don't need extra join/brackets
+ # 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
- unless($right && $right->name() && $right->name() eq "NOT")
- {
+# unless($right && $right->name() && $right->name() eq "NOT")
+# {
push(@joins, "INNER");
push(@outjoins, "INNER");
- }
+# }
}
elsif($op eq "NOT")
{
- print "NOT (was $not)\n";
$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
- unless($right && $right->name() && $right->name() eq "NOT")
- {
- if($hasvals)
- {
- push(@joins, "INNER");
- push(@outjoins, "INNER");
- }
- else
- {
- push(@joins, "LEFT");
- push(@outjoins, "LEFT");
- }
- }
- }
- elsif($op eq "OR")
- {
- print "OR\n";
- # if left child is a NOT, we need an extra (inner) join
- # unless right child is also a NOT
- if(($left && $left->name() && $left->name() eq "NOT") &&
- !($right && $right->name() && $right->name() eq "NOT"))
- {
- push(@joins, "INNER");
- push(@outjoins, "INNER");
- }
+ # return an extra join, but don't pass it down to $right->to_sql
+ # if we are looking for a tag *value*, use INNER join rather than LEFT
+ push(@outjoins, ($hasvals ? "INNER" : "LEFT"));
}
}
my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right);
push(@outjoins, @rightjoins);
-# print "LEFT (", scalar(@leftjoins), "): $leftstr\n";
-# print "RIGHT (", scalar(@rightjoins), "): $rightstr\n";
- $str=$leftstr;
+ $str = $leftstr;
$str .= " $op " if($op && !$not);
$str .= $rightstr;
- if($op && $left && $right)
- {
- $str="(" . $str . ")";
- }
-# print "STR: $str\n";
-# my @all=(@joins, @rightjoins);
-# print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n";
+ $str=("(" . $str . ")") if($op && $left && $right);
+# print "LEFT (", scalar(@$leftjoins), "): ";
+# print $leftstr if $leftstr;
+# print "\n";
+# print "OP: $op\n" if $op;
+# print "RIGHT (", scalar(@$rightjoins), "): ";
+# print $rightstr if $rightstr;
+# print "\n";
+ print "OUT(", scalar(@outjoins), "): $str\n";
return($str, @outjoins);
}
return $self->id();
}
+# does the bottom right-most expression end in a NOT?
+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;