+sub number_joins
+{
+ my($self, @elements)=@_;
+ my @joins=("INNER");
+ my $table=1;
+ my $nextjoin=undef;
+ my $lastop=undef;
+ return (@joins) unless(@elements);
+ while(my $node=shift @elements)
+ {
+ if($node->type() == $TYPE_BOOL)
+ {
+ my $op=$node->name();
+ if($op eq "AND")
+ {
+ $nextjoin="INNER";
+ }
+ elsif($op eq "NOT")
+ {
+ $nextjoin="LEFT";
+ }
+ elsif($op eq "OR")
+ {
+ # NOT/foo/OR needs an extra join
+ $nextjoin="INNER" if($lastop && $lastop eq "NOT");
+ }
+ $lastop=$op;
+ }
+ else
+ {
+ if(@elements)
+ {
+ # if tag has a value, eat the tag, shifting to the value
+ if($elements[0]->type() == $TYPE_TAG)
+ {
+ $node->hasvals(1);
+ $node=shift(@elements);
+ }
+ else
+ {
+ $node->hasvals(0);
+ }
+ }
+ elsif($self->{db}->tag_has_values($node->id()))
+ {
+ # if the expression ends in a tag that has a value
+ # (ie we have the tag and want the value)
+ # use an INNER join even if we were in a NOT
+ $nextjoin="INNER" if($nextjoin);
+ }
+ if($nextjoin)
+ {
+ $table++;
+ push(@joins, $nextjoin);
+ $nextjoin=undef;
+ }
+ $node->table($table);
+ }
+ }
+ return @joins;
+}
+