$self->{type}=shift;
$self->{name}=shift;
$self->{parents_id}=shift;
+ $self->{table}=1;
+ $self->{join}=undef;
if($self->{type} != $TYPE_BOOL)
{
my $table='';
sub type { return shift->set("type", shift); }
sub id { return shift->set("id", shift); }
sub parents_id { return shift->set("parents_id", shift); }
+sub table { return shift->set("table", shift); }
+sub join { return shift->set("join", shift); }
sub to_sql
{
- my($self, $hasvals, $not, @joins)=@_;
+ my($self, $hasvals, $not)=@_;
$not=0 unless(defined($not));
- my @outjoins=();
- unless(@joins)
- {
- @outjoins = @joins = ("INNER");
- }
+ $hasvals=0 unless(defined($hasvals));
my $str='';
if($self->type() != $TYPE_BOOL)
{
- print $self->{id}, " = ", $self->{name}, "\n";
- $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
+ $str .= "t" . $self->table() . ".id='" . $self->{id} . "'";
if($not && !$hasvals)
{
- $str = "(" . $str . " AND fxt" . scalar(@joins) . ".files_id IS NULL)";
+ $str = "(" . $str . " AND fxt" . $self->table() . ".files_id IS NULL)";
}
- return ($str, @outjoins);
+ return ($str);
}
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);
+ return ("") unless($left || $right);
+ my $leftstr = $left->to_sql($hasvals, $not) if($left);
my $op=$self->name();
- if(defined($op))
- {
- # if we are ANDing add an inner join
- 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
-# unless($right && $right->name() && $right->name() eq "NOT")
-# {
- push(@joins, "INNER");
- push(@outjoins, "INNER");
-# }
- }
- elsif($op eq "NOT")
- {
- $not=1;
- # 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);
+ $not=1 if(defined($op) && ($op eq "NOT"));
+ my $rightstr = $right->to_sql($hasvals, $not) if($right);
$str = $leftstr;
$str .= " $op " if($op && !$not);
$str .= $rightstr;
$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($str);
}
sub used_tags