make hasvals a per-node setting
[id3fs.git] / lib / ID3FS / Path.pm
index 855ea9e..de8d63b 100644 (file)
@@ -30,7 +30,7 @@ our $PATH_ALLTRACKS= "TRACKS";
 our $PATH_NOARTIST = "NOARTIST";
 our $PATH_NOALBUM  = "NOALBUM";
 
-our $ENABLE_FILTER = 1;
+our $ENABLE_FILTER = 0;
 
 sub new
 {
@@ -116,7 +116,7 @@ sub dirents
     elsif($state==$STATE_BOOLEAN)
     {
        my $parent=$self->tail();
-       unless($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT")
+       unless($self->is($TYPE_BOOL, $parent) && $parent->name() eq "NOT")
        {
            @dents=("NOT");
        }
@@ -250,7 +250,7 @@ sub parse
            my $parent=$self->tail();
            my $allownot=1;
            if($self->is($TYPE_BOOL, $parent) &&
-              $parent->{name} eq "NOT")
+              $parent->name() eq "NOT")
            {
                $allownot=0;
            }
@@ -358,7 +358,7 @@ sub parse
     }
 #    print "\nELEMENTS: ", join(' ', map { $_->name(); } @elements), "\n";
     my @joins=$self->number_joins(@elements);
-#    @joins=qw(INNER) unless(@joins);
+    @joins=qw(INNER) unless(@joins);
     $self->{joins}=\@joins;
 #    print "AFTER: ", join(' ', map { $_->name() . "(" . $_->{table} . ")"; } @elements), "\n";
 #    print "JOINS: ", join(', ', @joins), "\n";
@@ -370,46 +370,60 @@ sub parse
 sub number_joins
 {
     my($self, @elements)=@_;
-    my @joins=qw(INNER);
+    my @joins=("INNER");
     my $table=1;
+    my $nextjoin=undef;
     my $lastop=undef;
-    return () unless(@elements);
-    while(my $thing=shift @elements)
+    return (@joins) unless(@elements);
+    while(my $node=shift @elements)
     {
-       if($thing->type() == $TYPE_BOOL)
+       if($node->type() == $TYPE_BOOL)
        {
-           my $op=$thing->name();
+           my $op=$node->name();
            if($op eq "AND")
            {
-               $table++;
-               push(@joins, "INNER");
+               $nextjoin="INNER";
            }
            elsif($op eq "NOT")
            {
-               unless($lastop && $lastop eq "AND")
-               {
-                   push(@joins, "LEFT");
-                   $table++;
-               }
+               $nextjoin="LEFT";
            }
            elsif($op eq "OR")
            {
-               if($lastop && $lastop eq "NOT")
-               {
-                   $table++;
-                   push(@joins, "INNER");
-               }
+               # NOT/foo/OR needs an extra join
+               $nextjoin="INNER" if($lastop && $lastop eq "NOT");
            }
            $lastop=$op;
        }
        else
        {
-           # skip tags with values
-           if(@elements && $elements[0]->type() == $TYPE_TAG)
+           if(@elements)
            {
-               $thing=shift(@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;
            }
-           $thing->table($table);
+           $node->table($table);
        }
     }
     return @joins;
@@ -432,18 +446,18 @@ sub elements_to_tree
     my($self, $elements)=@_;
     return undef unless(@$elements);
     my ($left, $right, $op)=(undef, undef, undef);
-    my $thing=pop @$elements;
-    if($self->is($TYPE_BOOL, $thing))
+    my $node=pop @$elements;
+    if($self->is($TYPE_BOOL, $node))
     {
        $right=$self->elements_to_tree($elements);
-       if($thing->{name} ne "NOT")
+       if($node->name() ne "NOT")
        {
            $left=$self->elements_to_tree($elements);
        }
-       $thing->left($left);
-       $thing->right($right);
+       $node->left($left);
+       $node->right($right);
     }
-    return $thing;
+    return $node;
 }
 
 # Dijkstra's shunting-yard algorithm
@@ -452,25 +466,25 @@ sub sort_elements
     my ($self, @input)=@_;
     my @opstack=();
     my @output=();
-    while(my $thing = shift @input)
+    while(my $node = shift @input)
     {
-       if($self->is($TYPE_TAG, $thing))
+       if($self->is($TYPE_TAG, $node))
        {
            # Handle tag values by dropping parent
            if(@input && $self->is($TYPE_TAG, $input[0]))
            {
-               $thing=shift @input;
+               $node=shift @input;
            }
-           push(@output, $thing);
+           push(@output, $node);
        }
-       elsif($self->is($TYPE_BOOL, $thing))
+       elsif($self->is($TYPE_BOOL, $node))
        {
            while(@opstack &&
-                 ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
+                 ($priorities{$node->name()} <= $priorities{$opstack[$#opstack]->name()}))
            {
                push(@output, pop(@opstack));
            }
-           push(@opstack, $thing);
+           push(@opstack, $node);
        }
     }
     while(@opstack)
@@ -527,10 +541,10 @@ sub tail
 
 sub is
 {
-    my($self, $type, $thing)=@_;
-    return 0 unless($thing);
-    return 0 unless($thing->type());
-    return 1 if($type == $thing->type());
+    my($self, $type, $node)=@_;
+    return 0 unless($node);
+    return 0 unless($node->type());
+    return 1 if($type == $node->type());
     return 0;
 }
 
@@ -551,7 +565,6 @@ sub tags
        my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
        return($self->{db}->cmd_firstcol($sql));
     }
-    my $hasvals=$self->expecting_values();
     my $sql="SELECT tags.name FROM ";
     if($self->want_all_tags())
     {
@@ -569,7 +582,7 @@ sub tags
     my $id=$self->trailing_tag_id();
 
     my $parentclause= "tags.parents_id='";
-    $parentclause .= $id if($hasvals);
+    $parentclause .= $id if($self->expecting_values());
     $parentclause .= "'";
     push(@andclauses, $parentclause);
 
@@ -722,11 +735,10 @@ sub filename
 sub tags_subselect
 {
     my($self)=@_;
-    my $hasvals=$self->expecting_values();
     my $tree=$self->{tagtree};
 #    use Data::Dumper;
 #    print Dumper $tree;
-    my ($sqlclause, @joins)=$tree->to_sql($hasvals) if($tree);
+    my ($sqlclause, @joins)=$tree->to_sql() if($tree);
     my $sql="\tSELECT fxt1.files_id FROM tags t1";
     my @crosses=();
     my @inners=();