$self->{type}=shift;
$self->{name}=shift;
$self->{parents_id}=shift;
+ $self->{table}=1;
+ $self->{hasvals}=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 hasvals { return shift->set("hasvals", shift); }
sub to_sql
{
- my($self, $hasvals, $not, @joins)=@_;
+ my($self, $not)=@_;
$not=0 unless(defined($not));
- my @outjoins=();
- unless(@joins)
- {
- @outjoins = @joins = ("INNER");
- }
my $str='';
if($self->type() != $TYPE_BOOL)
{
- $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
- if($not && !$hasvals)
+ $str .= "t" . $self->table() . ".id='" . $self->{id} . "'";
+ if($not && !$self->hasvals())
{
- $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($not) if($left);
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($op eq "AND")
- {
- print "AND\n";
- # hack - 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")
- {
- 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");
- }
- }
- }
- 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;
+ $not=1 if(defined($op) && ($op eq "NOT"));
+ my $rightstr = $right->to_sql($not) if($right);
+ $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";
- return($str, @outjoins);
+ $str=("(" . $str . ")") if($op && $left && $right);
+ return($str);
}
sub used_tags