fix id3fs-index -l
[id3fs.git] / lib / ID3FS / Path.pm
index 202d6ad..012cba6 100644 (file)
@@ -12,10 +12,14 @@ use ID3FS::Path::Node;
 
 our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL,
      $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST,
-     $STATE_FILE)=(0..7);
+     $STATE_FILE, $STATE_ALL)=(0..8);
 
 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
 
+our $PATH_ALLTRACKS="TRACKS";
+our $PATH_NOARTIST="NOARTIST";
+our $PATH_NOALBUM="NOALBUM";
+
 sub new
 {
     my $proto=shift;
@@ -26,6 +30,9 @@ sub new
     $self->{elements}=[];
     $self->{db}=shift;
     $self->{path}=shift;
+    $self->{verbose}=shift;
+    $self->{path} =~ s/\/\//\//g; # drop doubled slashes
+
     $self->parse();
 #    print "STATE: ", $self->state(), "\n";
     return $self;
@@ -69,10 +76,15 @@ sub dirents
 {
     my($self)=@_;
     my @dents=();
+    my @fents=();
     my $state=$self->state();
 #    print "DIRENTS: STATE: $state\n";
 #    print "DIRENTS: FILE: $self->{path}\n";
-    if($state==$STATE_TAG || $state==$STATE_TAGVAL)
+    if($state==$STATE_ALL)
+    {
+       @dents=($self->artists(), $PATH_ALLTRACKS, $PATH_NOARTIST);
+    }
+    elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
     {
        my $tag=$self->tail();
        if($state==$STATE_TAG &&
@@ -84,12 +96,12 @@ sub dirents
        }
        else
        {
-           @dents=(qw(AND OR TRACKS NOARTIST),
-                   $self->artists());
+           @dents=($self->artists(), qw(AND OR), $PATH_ALLTRACKS, $PATH_NOARTIST);
        }
     }
     elsif($state==$STATE_BOOLEAN)
     {
+       @dents=$self->tags();
        my $parent=$self->tail();
        unless(defined($parent) &&
               ref($parent) eq "ID3FS::PathElement::Boolean" &&
@@ -97,25 +109,24 @@ sub dirents
        {
            push(@dents, "NOT");
        }
-       push(@dents, $self->tags());
     }
     elsif($state==$STATE_ROOT)
     {
-       @dents=(qw(ALL NOT), $self->tags());
+       @dents=($self->tags(), qw(ALL NOT));
     }
     elsif($state==$STATE_ALBUMS)
     {
-       @dents=(qw(TRACKS NOALBUM),$self->albums());
+       @dents=($self->albums(), $PATH_ALLTRACKS, $PATH_NOALBUM);
     }
     elsif($state==$STATE_TRACKLIST)
     {
-       @dents=$self->tracks();
+       @fents=$self->tracks();
     }
     else
     {
        print "DIRENTS: UNHANDLED STATE: $state\n";
     }
-    return(@dents);
+    return(\@dents, \@fents);
 }
 
 sub parse
@@ -129,7 +140,11 @@ sub parse
     my @parts=@{$self->{components}};
     my($tag, $tagval);
     $self->{elements}=[];
-    while(my $name=shift @parts)
+    $self->{bare_not}=0;
+    $self->{in_all}=0;
+    my $root_not=0;
+    my $tags_seen=0;
+    while(defined(my $name=shift @parts))
     {
 #      print "NAME: $name\n";
        my $state=$self->state();
@@ -143,10 +158,12 @@ sub parse
 #          print "SM: ROOT: $name\n";
            if($name eq "ALL")
            {
-               $self->state($STATE_TAG);
+               $self->{in_all}=1;
+               $self->state($STATE_ALL);
            }
            elsif($name eq "NOT")
            {
+               $root_not=1;
                push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
                $self->state($STATE_BOOLEAN);
            }
@@ -156,6 +173,7 @@ sub parse
                if($tag)
                {
                    push(@{$self->{elements}}, $tag);
+                   $tags_seen++;
                    $self->state($STATE_TAG);
                }
                else
@@ -186,11 +204,11 @@ sub parse
                    $self->state($STATE_INVALID);
                }
            }
-           elsif($name eq "TRACKS")
+           elsif($name eq $PATH_ALLTRACKS)
            {
                $self->state($STATE_TRACKLIST);
            }
-           elsif($name eq "NOARTIST")
+           elsif($name eq $PATH_NOARTIST)
            {
                $self->state($STATE_TRACKLIST);
            }
@@ -240,6 +258,7 @@ sub parse
                if($tag)
                {
                    push(@{$self->{elements}}, $tag);
+                   $tags_seen++;
                    $self->state($STATE_TAG);
                }
                else
@@ -251,11 +270,11 @@ sub parse
        elsif($state==$STATE_ALBUMS)
        {
 #          print "SM: ALBUM: $name\n";
-           if($name eq "TRACKS")
+           if($name eq $PATH_ALLTRACKS)
            {
                $self->state($STATE_TRACKLIST);
            }
-           elsif($name eq "NOALBUM")
+           elsif($name eq $PATH_NOALBUM)
            {
                $self->state($STATE_TRACKLIST);
            }
@@ -293,12 +312,43 @@ sub parse
            # Can't have anything after a filename
            $self->state($STATE_INVALID);
        }
+       elsif($state==$STATE_ALL)
+       {
+           if($name eq $PATH_ALLTRACKS)
+           {
+               $self->state($STATE_TRACKLIST);
+           }
+           elsif($name eq $PATH_NOARTIST)
+           {
+               # FIXME
+               $self->state($STATE_TRACKLIST);
+           }
+           else
+           {
+               my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name);
+               if($artist)
+               {
+                   push(@{$self->{elements}}, $artist);
+                   $self->state($STATE_ALBUMS);
+               }
+               else
+               {
+                   $self->state($STATE_INVALID);
+               }
+           }
+       }
        else
        {
            print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
            $self->state($STATE_INVALID);
        }
     }
+
+    if($root_not && ($tags_seen < 2))
+    {
+       $self->{bare_not}=1;
+    }
+
     # remove trailing boolean
     my @elements=@{$self->{elements}};
     while(@elements && ref($elements[$#elements]) eq "ID3FS::PathElement::Boolean")
@@ -311,8 +361,9 @@ sub parse
     if($self->{tagtree})
     {
        ($self->{sqlconditions},
-        $self->{andsneeded}) = $self->{tagtree}->to_sql();
-#      print("SQL CONDITION(", $self->{andsneeded}, "): ",
+        $self->{joins}) = $self->{tagtree}->to_sql();
+#      print "TREE: ",  $self->{tagtree}->print(), "\n";
+#      print("SQL CONDITION(", scalar(@{$self->{joins}}), "): ",
 #            $self->{sqlconditions}, "\n");
 #      use Data::Dumper;
 #      print Dumper $self->{tagtree};
@@ -359,6 +410,11 @@ sub sort_elements
     {
        if(ref($thing) eq "ID3FS::PathElement::Tag")
        {
+           # Handle tag values by dropping parent
+           if(@input && ref($input[0]) eq "ID3FS::PathElement::Tag")
+           {
+               $thing=shift @input;
+           }
            push(@output, $thing);
        }
        elsif(ref($thing) eq "ID3FS::PathElement::Boolean")
@@ -383,12 +439,11 @@ sub sort_elements
 sub used_tags
 {
     my($self)=@_;
-    print "TAGTREE UNDEF\n" unless(defined($self->{tagtree}));
-    return undef unless(defined($self->{tagtree}));
+    return() unless(defined($self->{tagtree}));
     return($self->{tagtree}->used_tags());
 }
 
-sub tag_has_values
+sub expecting_values
 {
     my($self)=@_;
     my $tail=$self->tail();
@@ -438,32 +493,32 @@ sub tail_parent
 sub tags
 {
     my($self)=@_;
-    if(!@{$self->{elements}}) # /
+    if(!$self->{tagtree}) # / or /NOT  # FIXME: /ALL too?
     {
        my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
        return($self->{db}->cmd_firstcol($sql));
     }
-    my $hasvals=$self->tag_has_values();
+    my $hasvals=$self->expecting_values();
     my $parent=$self->trailing_tag_parent();
-    print "THASVALS: $hasvals\n";
-    print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
+#    print "THASVALS: $hasvals\n";
+#    print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
     my @ids=();
     my $sql=("SELECT tags.name FROM (\n" .
             $self->tags_subselect() .
             ") AS subselect\n" .
             "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
             "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n");
-    my (@allused)=$self->used_tags();
+    my @allused=$self->used_tags();
     my @used=grep { ref($_) ne "ARRAY"; } @allused;
     my @used_with_vals=grep { ref($_) eq "ARRAY"; } @allused;
-    print "tags(): USED: ", join(", ", @used), "\n";
-    print "tags(): USED_WITH_VALS: ", join(", ", map { "[".$_->[0]. ", ".$_->[1]."]";} @used_with_vals), "\n";
+#    print "tags(): USED: ", join(", ", @used), "\n";
+#    print "tags(): USED_WITH_VALS: ", join(", ", map { "[".$_->[0]. ", ".$_->[1]."]";} @used_with_vals), "\n";
     my @orclauses=();
     my @andclauses=();
     my $id=$self->trailing_tag_id();
     if($hasvals)
     {
-       print "HAS_VALUES\n";
+#      print "HAS_VALUES\n";
        my @values=map { "'".$_->[1]."'"; } grep { $_->[0] == $id; } @used_with_vals;
        my $clause="(tags.parents_id='$id'";
        if(@values)
@@ -471,18 +526,18 @@ sub tags
            $clause .= " AND tags.id NOT IN (" . join(', ', @values) . ")";
        }
        $clause .= ")";
-       push(@orclauses, $clause);
+       push(@andclauses, $clause);
     }
     else
     {
-       print "HASNT VALUES\n";;
+#      print "HASNT VALUES\n";;
        if(@used)
        {
-           push(@orclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))");
+           push(@andclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))");
        }
        for my $pair (@used_with_vals)
        {
-           push(@orclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))");
+           push(@andclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))");
        }
     }
 
@@ -500,35 +555,25 @@ sub tags
 
     if(@orclauses)
     {
-       push(@andclauses, join(' OR ', @orclauses));
+       push(@andclauses, '( ' . join(' OR ', @orclauses) . ' )');
     }
     if(@andclauses)
     {
        $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
     }
     $sql .= "GROUP BY tags.name;";
-    print "SQL: $sql\n";
+    print "SQL(TAGS): $sql\n" if($self->{verbose});
     my @tagnames=$self->{db}->cmd_firstcol($sql);
-    print "SUBNAMES: ", join(', ', @tagnames), "\n";
+    print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
     return(@tagnames);
 }
 
-sub tag_values
-{
-    my($self, $tagid)=@_;
-    my $sql=("SELECT DISTINCT name FROM tags\n" .
-            "WHERE parents_id=?");
-    my @tags=$self->{db}->cmd_firstcol($sql, $tagid);
-    @tags=map { length($_) ? $_ : "NOVALUE"; } @tags;
-    return @tags;
-}
-
 sub artists
 {
     my($self)=@_;
     if(!@{$self->{elements}}) # /ALL
     {
-       my $sql="SELECT DISTINCT name FROM artists;";
+       my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
        return($self->{db}->cmd_firstcol($sql));
     }
     my @ids=();
@@ -537,10 +582,11 @@ sub artists
             ") AS subselect\n" .
             "INNER JOIN files ON subselect.files_id=files.id\n" .
             "INNER JOIN artists ON files.artists_id=artists.id\n" .
+            "WHERE artists.name != ''\n" .
             "GROUP BY artists.name;");
-    print "SQL: $sql\n";
+    print "SQL(ARTISTS): $sql\n" if($self->{verbose});
     my @tagnames=$self->{db}->cmd_firstcol($sql);
-    print "ARTISTS: ", join(', ', @tagnames), "\n";
+    print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
     return(@tagnames);
 }
 
@@ -554,64 +600,89 @@ sub albums
     {
        return $self->artist_albums($tail->{id});
     }
-    my $sql=("SELECT albums.name\n" .
-            "\tFROM (\n" .
-            $self->tags_subselect() .
-            "\t) AS subselect\n" .
-            "INNER JOIN files ON subselect.files_id=files.id\n" .
-            "INNER JOIN albums ON files.albums_id=albums.id\n" .
-            "GROUP BY albums.name;");
-    print "SQL(ALBUMS): \n$sql\n";
+    my $sql;
+    if($self->{in_all})
+    {
+       $sql="SELECT name FROM albums";
+    }
+    else
+    {
+       $sql=("SELECT albums.name\n" .
+             "\tFROM (\n" .
+             $self->tags_subselect() .
+             "\t) AS subselect\n" .
+             "INNER JOIN files ON subselect.files_id=files.id\n" .
+             "INNER JOIN albums ON files.albums_id=albums.id\n" .
+             "WHERE albums.name != ''\n" .
+             "GROUP BY albums.name;");
+    }
+    print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
     my @names=$self->{db}->cmd_firstcol($sql);
-    print "ALBUMS: ", join(', ', @names), "\n";
+    print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
     return(@names);
 }
 
 sub artist_albums
 {
     my($self, $artist_id)=@_;
-    my $sql=("SELECT albums.name FROM (\n" .
-            $self->tags_subselect() .
-            "\t) AS subselect\n" .
-            "INNER JOIN files ON subselect.files_id=files.id\n" .
-            "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
-            "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
-            "WHERE artists.id=? and albums.name <> ''\n\t" .
+    my $sql="SELECT albums.name FROM ";
+    if($self->{in_all})
+    {
+       $sql .= "files\n";
+    }
+    else
+    {
+       $sql .= ("(\n" .
+                $self->tags_subselect() .
+                ") AS subselect\n" .
+                "INNER JOIN files ON subselect.files_id=files.id\n");
+    }
+    $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
+            "INNER JOIN artists ON artists.id=files.artists_id\n" .
+            "WHERE artists.id=? and albums.name <> ''\n" .
             "GROUP BY albums.name\n");
-    print "ARTIST_ALBUMS SQL: $sql\n";
+    print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
     my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
-    print "ALBUMS: ", join(', ', @albums), "\n";
+    print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
     return(@albums);
 }
 
 sub artist_tracks
 {
     my($self, $artist_id)=@_;
-    my $sql=("SELECT files.name FROM (\n" .
-            $self->tags_subselect() .
-            "\t) AS subselect\n" .
-            "INNER JOIN files ON subselect.files_id=files.id\n" .
-            "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
-            "INNER JOIN albums  ON albums.id=files.albums_id\n\t" .
-            "WHERE artists.id=? AND albums.name=''\n\t" .
+    my $sql="SELECT files.name FROM ";
+    if($self->{in_all})
+    {
+       $sql .= "files\n";
+    }
+    else
+    {
+       $sql .= ("(\n" .
+                $self->tags_subselect() .
+                "\t) AS subselect\n" .
+                "INNER JOIN files ON subselect.files_id=files.id\n");
+    }
+    $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
+            "INNER JOIN albums  ON albums.id=files.albums_id\n" .
+            "WHERE artists.id=? AND albums.name=''\n" .
             "GROUP BY files.name\n");
-    print "ARTIST_TRACKS SQL: $sql\n";
+    print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
     my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
-    print "ARTISTTRACKS: ", join(', ', @names), "\n";
+    print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
     return(@names);
 }
 
 sub album_tracks
 {
     my($self, $artist_id, $album_id)=@_;
-    my $sql=("SELECT files.name FROM files\n\t" .
-            "INNER JOIN albums  ON albums.id=files.albums_id\n\t" .
-            "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
-            "WHERE artists.id=? AND albums.id=?\n\t" .
+    my $sql=("SELECT files.name FROM files\n" .
+            "INNER JOIN albums  ON albums.id=files.albums_id\n" .
+            "INNER JOIN artists ON artists.id=files.artists_id\n" .
+            "WHERE artists.id=? AND albums.id=?\n" .
             "GROUP BY files.name\n");
-    print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n";
+    print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
     my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
-    print "TRACKS: ", join(', ', @names), "\n";
+    print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
     return(@names);
 }
 
@@ -635,16 +706,27 @@ sub tracks
        }
        return $self->album_tracks($artist_id, $tail->{id});
     }
-
-    my $sql=("SELECT files.name\n" .
-            "\tFROM (\n" .
-            $self->tags_subselect() .
-            "\t) AS subselect\n" .
-            "INNER JOIN files ON files.id=subselect.files_id\n" .
-            "GROUP BY files.name;");
-    print "SQL: $sql\n";
+    my $sql="SELECT files.name FROM ";
+    if($self->{in_all})
+    {
+       $sql .= "files\n";
+    }
+    else
+    {
+       $sql .= ("(\n" .
+                $self->tags_subselect() .
+                ") AS subselect\n" .
+                "INNER JOIN files ON files.id=subselect.files_id\n");
+    }
+    $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
+    if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
+    {
+       $sql .= "WHERE artists.name =''\n";
+    }
+    $sql .= "GROUP BY files.name;";
+    print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
     my @names=$self->{db}->cmd_firstcol($sql);
-    print "TRACKS: ", join(', ', @names), "\n";
+    print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
     return(@names);
 }
 
@@ -659,7 +741,7 @@ sub filename
                 "INNER JOIN paths ON files.paths_id=paths.id\n" .
                 "WHERE files.id=?\n" .
                 "GROUP BY paths.name, files.name");
-       print "FILENAME SQL: $sql\n";
+       print "FILENAME SQL: $sql\n" if($self->{verbose});
        my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
        my $id3fs_path=join('/', map { $_->{name}; }  @{$self->{elements}});
        return($self->{db}->relativise($path, $name, $mountpoint, $id3fs_path));
@@ -670,8 +752,18 @@ sub filename
 sub tags_subselect
 {
     my($self)=@_;
+    my $hasvals=$self->expecting_values();
+    # we need to specially handle a bare /NOT/tag with no other clauses,
+    # using a simple WHERE id !='tagid' instead of a LEFT JOIN
+    if($self->{bare_not})
+    {
+       return $self->bare_not_subselect();
+    }
+    if($self->{in_all})
+    {
+       return "\tSELECT id FROM files AS files_id\n";
+    }
     my $tree=$self->{tagtree};
-    my $hasvals=$self->tag_has_values();
     my $parent=$self->trailing_tag_parent();
 
 #    print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n";
@@ -680,23 +772,25 @@ sub tags_subselect
     if($hasvals)
     {
        $tag=$self->trailing_tag_id();
-       print "Trailing id: $tag\n";
+#      print "Trailing id: $tag\n";
     }
-    my ($sqlclause, $joinsneeded)=(undef, 1);
-    ($sqlclause, $joinsneeded) = $tree->to_sql($tag) if($tree);
-#    print "SQL($joinsneeded): $sqlclause\n";
+    my ($sqlclause, @joins)=(undef, ());
+    ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
+#    print "SQL(" . scalar(@joins) .": $sqlclause\n";
     my $sql="\tSELECT fxt1.files_id FROM tags t1";
     my @crosses=();
     my @inners=();
 #    $joinsneeded++ if($tag);
-    for(my $i=1; $i <= $joinsneeded; $i++)
+    for(my $i=0; $i <= $#joins; $i++)
     {
-       my $inner=("\tINNER JOIN files_x_tags fxt$i ON " .
-                  "t${i}.id=fxt${i}.tags_id");
-       if($i > 1)
+       my $cnt=$i+1;
+       my $join=$joins[$i];
+       my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
+                  "t${cnt}.id=fxt${cnt}.tags_id");
+       if($i > 0)
        {
-           push(@crosses, "CROSS JOIN tags t$i");
-           $inner .= " AND fxt1.files_id=fxt${i}.files_id";
+           push(@crosses, "CROSS JOIN tags t$cnt");
+           $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
        }
        push(@inners, $inner);
     }
@@ -711,108 +805,25 @@ sub tags_subselect
     return $sql;
 }
 
-sub tags_subselect_and_not
+sub bare_not_subselect
 {
-    my($self,@constraints)=@_;
-    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
-    my @tags=@$tags;
-    my @tags_vals=@$tags_vals;;
-    my $cnt=1;
-    my @andclauses=();
-    my $sql='';
-    for my $tag (@tags)
-    {
-       if($cnt == 1)
-       {
-           $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n";
-           push(@andclauses, "\t\tfxt${cnt}.tags_id=$tag");
-       }
-       else
-       {
-           $sql .= ("\tLEFT JOIN files_x_tags fxt$cnt ON fxt" .
-                ($cnt-1) . ".files_id=fxt${cnt}.files_id\n");
-           push(@andclauses, "\t\tfxt${cnt}.tags_id IS NULL");
-       }
-       print "AND: @andclauses\n";
-       $cnt++;
-    }
-    if(@andclauses)
-    {
-       $sql .= "\tWHERE\n\t\t";
-       $sql .= join(" AND\n\t\t", @andclauses) . "\n";
-    }
-    $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n";
-    return $sql;
-}
-
-
-sub tags_subselect_and
-{
-    my($self,@constraints)=@_;
-    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
-    my @tags=@$tags;
-    my @tags_vals=@$tags_vals;;
-    my $cnt=1;
-    my @andclauses=();
-    my $sql='';
-    for my $tag (@tags)
-    {
-       if($cnt == 1)
-       {
-           $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n";
-       }
-       else
-       {
-           $sql .= ("\tINNER JOIN files_x_tags fxt$cnt ON fxt" .
-                ($cnt-1) . ".files_id=fxt${cnt}.files_id\n");
-       }
-       push(@andclauses, "\t\tfxt${cnt}.tags_id = $tag");
-       print "AND: @andclauses\n";
-       $cnt++;
-    }
-    if(@andclauses)
-    {
-       $sql .= "\tWHERE\n\t\t";
-       $sql .= join(" AND\n\t\t", @andclauses) . "\n";
-    }
-    $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n";
-    return $sql;
-}
-
-sub tags_subselect_or
-{
-    my($self,@constraints)=@_;
-    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
-    my @tags=@$tags;
-    my @tags_vals=@$tags_vals;;
-
-    my $sql=("\tSELECT files_x_tags.files_id FROM tags t1\n" .
-            "\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n");
-    my @orclauses=();
-    my @andclauses=();
-    # FIXME: and / or?
-    if(@tags)
-    {
-       push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
-       push(@andclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )");
-    }
-    for my $pair (@tags_vals)
-    {
-       my($tag, $val)=@$pair;
-       push(@orclauses, "( t1.parents_id=$tag AND t1.id=$val )");
-    }
-#    push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
-    if(@orclauses)
+    my($self)=@_;
+    my @tags=grep { ref($_) eq "ID3FS::PathElement::Tag"; } @{$self->{elements}};
+    my $sql=("\tSELECT f1.id AS files_id FROM files f1 WHERE f1.id NOT IN (\n" .
+            "\t\tSELECT fxt1.files_id FROM tags t1\n" .
+            "\t\tINNER JOIN files_x_tags fxt1 ON t1.id=fxt1.tags_id\n" .
+            "\t\tWHERE ");
+    if(scalar(@tags) > 1)
     {
-       push(@andclauses, join("\n\t\tOR ", @orclauses));
+       $sql .= ("(t1.parents_id='" . $tags[0]->{id} . "' AND t1.id='" .
+                $tags[1]->{id} . "')");
     }
-    if(@andclauses)
+    else
     {
-       $sql .= "\tWHERE\n\t\t";
-       $sql .= join("\n\t\tAND ", @andclauses) . "\n";
+       $sql .= ("(t1.parents_id='' AND t1.id='" . $tags[0]->{id} . "')");
     }
-    $sql .= "\tGROUP BY files_x_tags.files_id\n";
-    return $sql;
+    $sql .= "\n\t\tGROUP BY fxt1.files_id\n\t)\n";
+    return($sql);
 }
 
 sub constraints_tag_list
@@ -853,39 +864,76 @@ sub constraints_tag_list
     return(\@tags, \@tags_vals, $lasttag);
 }
 
-
-sub bare_tags
+sub filter
 {
-    my($self)=@_;
-    my $sql=("SELECT tags.name FROM tags\n" .
-            "WHERE tags.parents_id=''\n" .
-            "GROUP BY tags.name\n");
-    my @names=$self->{db}->cmd_firstcol($sql);
-    return (@names);
+    my($self, @dirs)=@_;
+    my $base=$self->{path};
+    my @outdirs=();
+    # depth 4 to allow for tag/tagval/AND/NOT
+    my $maxdepth=4;
+    for my $dir (@dirs)
+    {
+#      print "\nFILTER (",$self->state(), "): $base / $dir\n";
+       if($self->empty("$base/$dir", $maxdepth))
+       {
+#          print "empty: $base / $dir\n";
+       }
+       else
+       {
+#          print "non-empty, accepting: $base / $dir\n";
+           push(@outdirs, $dir);
+       }
+    }
+    return(@outdirs);
 }
 
-sub tags_with_values
+sub empty
 {
-    # FIXME: only shows one level of tag depth
-    my($self)=@_;
-    my $sql=("SELECT p.name, t.name  FROM tags t\n" .
-            "INNER JOIN tags p ON t.parents_id=p.id\n" .
-            "GROUP BY p.name, t.name\n");
-    print "SQL: $sql\n";
-    my $result=$self->{db}->cmd_rows($sql);
-    my $tags={};
-    for my $pair (@$result)
+    my($self, $dir, $maxdepth)=@_;
+    return 0 unless($maxdepth);
+#    print "testing($maxdepth): $dir\n";
+    my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose});
+#    print "PATH INVALID\n" unless($path->isvalid());
+    return 1 unless($path->isvalid());
+    my($subdirs,$subfiles)=$path->dirents();
+#    print "SUBDENTS: ", join(", ", @$subdirs, @$subfiles), "\n";
+#    print("SUBFILES: ", join(', ', @$subfiles), "\n") if(@$subfiles);
+    return 0 if(@$subfiles);
+    for my $subdir (@$subdirs)
     {
-       push(@{$tags->{$pair->[0]}}, $pair->[1]);
+#      print "SUBSUB $dir/$subdir\n";
+       if(1) #$self->dir_is_special($subdir))
+       {
+           if($self->empty("$dir/$subdir", ($maxdepth-1)))
+           {
+#              print "EMPTY: $dir / $subdir\n";
+           }
+           else
+           {
+#              print "NONEMPTY: $dir / $subdir\n";
+               return 0;
+           }
+       }
+       else
+       {
+           return 0;
+       }
+#      return 0 if($self->nonempty("$dir/$subdir", ($maxdepth-1)));
     }
-    return $tags;
+    return 1;
 }
 
-sub lookup_id
+sub dir_is_special
 {
-    my($self, $table, $name)=@_;
-    my($id)=$self->{db}->cmd_onerow("SELECT id FROM $table where name=?", $name);
-    return $id;
+    my($self, $dir)=@_;
+    my $id=$self->{db}->lookup_id("tags", $dir);
+    if((grep { $_ eq $dir; } (qw(AND OR NOT), $PATH_ALLTRACKS,
+                             $PATH_NOARTIST, $PATH_NOALBUM)) ||
+       ($id && $self->{db}->tag_has_values($id)))
+    {
+       return 1;
+    }
+    return 0;
 }
 
 1;