tweak number_joins
[id3fs.git] / lib / ID3FS / Path.pm
index 7107ac7..e54c7e2 100644 (file)
@@ -1,17 +1,36 @@
+# id3fs - a FUSE-based filesystem for browsing audio metadata
+# Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 package ID3FS::Path;
 
 use strict;
 use warnings;
-use ID3FS::PathElement::Artist;
-use ID3FS::PathElement::Album;
-use ID3FS::PathElement::Boolean;
-use ID3FS::PathElement::File;
-use ID3FS::PathElement::Tag;
-use ID3FS::PathElement::Tagval;
+use ID3FS::Path::Node;
+
+our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_BOOLEAN,
+     $STATE_ALBUMS, $STATE_TRACKLIST, $STATE_FILE, $STATE_ALL)=(0..7);
+
+# operator precedence
+our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
+
+our $PATH_ALLTRACKS= "TRACKS";
+our $PATH_NOARTIST = "NOARTIST";
+our $PATH_NOALBUM  = "NOALBUM";
 
-our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL,
-     $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST,
-     $STATE_FILE)=(0..7);
+our $ENABLE_FILTER = 0;
 
 sub new
 {
@@ -20,8 +39,14 @@ sub new
     my $self={};
     bless($self,$class);
 
+    $self->{elements}=[];
     $self->{db}=shift;
     $self->{path}=shift;
+    $self->{verbose}=shift;
+    $self->{maxtagdepth}=shift;
+    $self->{curtagdepth}=0;
+    $self->{path} =~ s/\/\//\//g; # drop doubled slashes
+
     $self->parse();
 #    print "STATE: ", $self->state(), "\n";
     return $self;
@@ -41,8 +66,7 @@ sub isdir
 sub isfile
 {
     my($self)=@_;
-    return 1 if($self->state() == $STATE_FILE);
-    return 0;
+    return($self->state() == $STATE_FILE);
 }
 
 sub isvalid
@@ -53,10 +77,10 @@ sub isvalid
 
 sub dest
 {
-    my($self)=@_;
+    my($self, $mountpoint)=@_;
     if($self->state() == $STATE_FILE)
     {
-       return $self->{db}->filename(@{$self->{elements}});
+       return $self->filename($mountpoint);
     }
     return "ERROR"; #should never happen?
 }
@@ -65,59 +89,74 @@ sub dirents
 {
     my($self)=@_;
     my @dents=();
+    my @fents=();
     my $state=$self->state();
 #    print "DIRENTS: STATE: $state\n";
-    if($state==$STATE_TAG || $state==$STATE_TAGVAL)
+#    print "DIRENTS: FILE: $self->{path}\n";
+    if($state==$STATE_ALL)
     {
-       my $tag=$self->{elements}->[$#{$self->{elements}}];
-       if($state==$STATE_TAG &&
-          defined($tag) &&
-          ref($tag) eq "ID3FS::PathElement::Tag" &&
-          $self->{db}->tag_has_values($tag->{id}))
+       @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
+    }
+    elsif($state==$STATE_TAG)
+    {
+       my $tail=$self->tail();
+       if($self->is($TYPE_TAG, $tail) && $self->{db}->tag_has_values($tail->id()))
        {
-           @dents=$self->{db}->tag_values($tag->{id});
+           @dents=$self->tags();
        }
        else
        {
-           @dents=(qw(AND ALLTRACKS), # FIXME: NOT and OR
-                   $self->{db}->artists(@{$self->{elements}}));
+           if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth}))
+           {
+               @dents=qw(AND OR);
+           }
+           push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
        }
     }
     elsif($state==$STATE_BOOLEAN)
     {
-       @dents=$self->{db}->tags(@{$self->{elements}});
+       my $parent=$self->tail();
+       unless($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT")
+       {
+           @dents=("NOT");
+       }
+       push(@dents,$self->tags());
     }
     elsif($state==$STATE_ROOT)
     {
-       @dents=("ALL", $self->{db}->tags(@{$self->{elements}}));
+       @dents=(qw(ALL NOT), $self->tags());
     }
     elsif($state==$STATE_ALBUMS)
     {
-       @dents=("TRACKS", $self->{db}->albums(@{$self->{elements}}));
+       @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
     }
     elsif($state==$STATE_TRACKLIST)
     {
-       @dents=$self->{db}->tracks(@{$self->{elements}});
+       @fents=$self->tracks();
     }
     else
     {
-       print "DIRENTS: UNHANDLED STATE: $state\n";
+       print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n";
     }
-    return(@dents);
+    return(\@dents, \@fents);
 }
 
+# State Machine Of Doom
 sub parse
 {
     my($self)=@_;
+    $self->state($STATE_ROOT);
+    return if($self->{path} eq "/");
     @{$self->{components}}=split(/\//, $self->{path});
     shift @{$self->{components}}; # drop empty field before leading /
 #    print "PATH: $self->{path}\n";
-    $self->state($STATE_ROOT);
-    return if($self->{path} eq "/");
     my @parts=@{$self->{components}};
     my($tag, $tagval);
     $self->{elements}=[];
-    while(my $name=shift @parts)
+    $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();
@@ -131,14 +170,22 @@ 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::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
+               $self->state($STATE_BOOLEAN);
            }
            else
            {
-               $tag=ID3FS::PathElement::Tag->new($self->{db}, $name);
+               $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
                if($tag)
                {
                    push(@{$self->{elements}}, $tag);
+                   $tags_seen++;
                    $self->state($STATE_TAG);
                }
                else
@@ -147,19 +194,16 @@ sub parse
                }
            }
        }
-       elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
+       elsif($state==$STATE_TAG)
        {
+           my $tag=$self->tail();
 #          print "SM: TAG/TAGVAL($state): $name\n";
-           my $tag=$self->{elements}->[$#{$self->{elements}}];
-           if($state==$STATE_TAG &&
-              defined($tag) &&
-              ref($tag) eq "ID3FS::PathElement::Tag" &&
-              $self->{db}->tag_has_values($tag->{id}))
+           if($self->is($TYPE_TAG, $tag) && $self->{db}->tag_has_values($tag->id()))
            {
-               my $tagval=ID3FS::PathElement::Tagval->new($name);
+#              print "Parsing: parent: $tag->id()\n";
+               my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
                if(defined($tagval))
                {
-                   $self->state($STATE_TAGVAL);
                    # stay in tag state
                    push(@{$self->{elements}}, $tagval);
                }
@@ -168,28 +212,27 @@ sub parse
                    $self->state($STATE_INVALID);
                }
            }
-           elsif($name eq "ALLTRACKS")
+           elsif($name eq $PATH_ALLTRACKS)
+           {
+               $self->state($STATE_TRACKLIST);
+           }
+           elsif($name eq $PATH_NOARTIST)
            {
                $self->state($STATE_TRACKLIST);
            }
            elsif($name eq "AND")
            {
                $self->state($STATE_BOOLEAN);
-#              push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($name));
+               push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
            }
            elsif($name eq "OR")
            {
                $self->state($STATE_BOOLEAN);
-#              push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($name));
-           }
-           elsif($name eq "NOT")
-           {
-               $self->state($STATE_TAG);
-#              push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($name));
+               push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
            }
            else
            {
-               my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name);
+               my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
                if($artist)
                {
                    push(@{$self->{elements}}, $artist);
@@ -204,27 +247,47 @@ sub parse
        elsif($state==$STATE_BOOLEAN)
        {
 #          print "SM: BOOLEAN: $name\n";
-           my $tag=ID3FS::PathElement::Tag->new($self->{db}, $name);
-           if($tag)
+           my $parent=$self->tail();
+           my $allownot=1;
+           if($self->is($TYPE_BOOL, $parent) &&
+              $parent->{name} eq "NOT")
            {
-               push(@{$self->{elements}}, $tag);
-               $self->state($STATE_TAG);
-               }
+               $allownot=0;
+           }
+           if($allownot && $name eq "NOT")
+           {
+               $self->state($STATE_BOOLEAN);
+               push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
+           }
            else
            {
-               $self->state($STATE_INVALID);
+               my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
+               if($tag)
+               {
+                   push(@{$self->{elements}}, $tag);
+                   $tags_seen++;
+                   $self->state($STATE_TAG);
+               }
+               else
+               {
+                   $self->state($STATE_INVALID);
+               }
            }
        }
        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 $PATH_NOALBUM)
            {
                $self->state($STATE_TRACKLIST);
            }
            else
            {
-               my $album=ID3FS::PathElement::Album->new($self->{db}, $name);
+               my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
                if($album)
                {
                    push(@{$self->{elements}}, $album);
@@ -239,8 +302,7 @@ sub parse
        elsif($state==$STATE_TRACKLIST)
        {
 #          print "SM: TRACKLIST: $name\n";
-           my $track=ID3FS::PathElement::File->new($self->{db}, $name);
-           push(@{$self->{elements}}, $track);
+           my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
            if($track)
            {
                push(@{$self->{elements}}, $track);
@@ -257,19 +319,511 @@ 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)
+           {
+               $self->state($STATE_TRACKLIST);
+           }
+           else
+           {
+               my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $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);
        }
     }
+
+    my @elements=@{$self->{elements}};
+    # remove trailing boolean
+    while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
+    {
+       pop @elements;
+    }
+#    print "\nELEMENTS: ", join(' ', map { $_->name(); } @elements), "\n";
+    my @joins=$self->number_joins(@elements);
+    @joins=qw(INNER) unless(@joins);
+    $self->{joins}=\@joins;
+#    print "AFTER: ", join(' ', map { $_->name() . "(" . $_->{table} . ")"; } @elements), "\n";
+#    print "JOINS: ", join(', ', @joins), "\n";
+    # sort elements by precedence
+    @elements=$self->sort_elements(@elements);
+    $self->{tagtree}=$self->elements_to_tree(\@elements);
+}
+
+sub number_joins
+{
+    my($self, @elements)=@_;
+    my @joins=("INNER");
+    my $table=1;
+    my $nextjoin=undef;
+    my $lastop=undef;
+    return (@joins) unless(@elements);
+    while(my $thing=shift @elements)
+    {
+       if($thing->type() == $TYPE_BOOL)
+       {
+           my $op=$thing->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
+               $thing=shift(@elements) if($elements[0]->type() == $TYPE_TAG);
+           }
+           elsif($self->{db}->tag_has_values($thing->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);
+       }
+    }
+    return @joins;
 }
 
 sub state
 {
     my($self, $newstate)=@_;
-    $self->{state}=$newstate if(defined($newstate));
+    if(defined($newstate))
+    {
+       $self->{state}=$newstate;
+       $self->{curtagdepth}++ if($newstate == $STATE_TAG);
+    }
     return $self->{state};
 }
 
+# link up precedence-sorted list into a binary tree
+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))
+    {
+       $right=$self->elements_to_tree($elements);
+       if($thing->{name} ne "NOT")
+       {
+           $left=$self->elements_to_tree($elements);
+       }
+       $thing->left($left);
+       $thing->right($right);
+    }
+    return $thing;
+}
+
+# Dijkstra's shunting-yard algorithm
+sub sort_elements
+{
+    my ($self, @input)=@_;
+    my @opstack=();
+    my @output=();
+    while(my $thing = shift @input)
+    {
+       if($self->is($TYPE_TAG, $thing))
+       {
+           # Handle tag values by dropping parent
+           if(@input && $self->is($TYPE_TAG, $input[0]))
+           {
+               $thing=shift @input;
+           }
+           push(@output, $thing);
+       }
+       elsif($self->is($TYPE_BOOL, $thing))
+       {
+           while(@opstack &&
+                 ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
+           {
+               push(@output, pop(@opstack));
+           }
+           push(@opstack, $thing);
+       }
+    }
+    while(@opstack)
+    {
+       push(@output, pop(@opstack));
+    }
+    return @output;
+}
+
+sub used_tags
+{
+    my($self)=@_;
+    return() unless(defined($self->{tagtree}));
+    return($self->{tagtree}->used_tags());
+}
+
+sub expecting_values
+{
+    my($self)=@_;
+    my $tail=$self->tail();
+    if($self->is($TYPE_TAG, $tail))
+    {
+       return($self->{db}->tag_has_values($tail->id()));
+    }
+}
+
+sub trailing_tag_id
+{
+    my($self)=@_;
+    my $tail=$self->tail();
+    if($self->is($TYPE_TAG, $tail))
+    {
+       return($tail->id());
+    }
+    return undef;
+}
+
+sub trailing_tag_parent
+{
+    my($self)=@_;
+    my $tail=$self->tail();
+    if($self->is($TYPE_TAG, $tail))
+    {
+       return($tail->{parents_id});
+    }
+    return undef;
+}
+
+sub tail
+{
+    my($self)=@_;
+    return($self->{elements}->[$#{$self->{elements}}]);
+}
+
+sub is
+{
+    my($self, $type, $thing)=@_;
+    return 0 unless($thing);
+    return 0 unless($thing->type());
+    return 1 if($type == $thing->type());
+    return 0;
+}
+
+# the one before last
+sub tail_parent
+{
+    my($self)=@_;
+    return($self->{elements}->[($#{$self->{elements}}) - 1]);
+}
+
+######################################################################
+
+sub tags
+{
+    my($self)=@_;
+    if(!$self->{tagtree}) # / or /NOT
+    {
+       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())
+    {
+       $sql .= "files_x_tags\n";
+    }
+    else
+    {
+       $sql .= ("(\n" .
+                $self->tags_subselect() .
+                ") AS subselect\n" .
+                "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
+    }
+    $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
+    my @andclauses=();
+    my $id=$self->trailing_tag_id();
+
+    my $parentclause= "tags.parents_id='";
+    $parentclause .= $id if($hasvals);
+    $parentclause .= "'";
+    push(@andclauses, $parentclause);
+
+    my @used=$self->used_tags();
+    if(@used)
+    {
+       push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
+    }
+    if(@andclauses)
+    {
+       $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
+    }
+
+    $sql .= "GROUP BY tags.name;";
+    print "SQL(TAGS): $sql\n" if($self->{verbose});
+    my @tagnames=$self->{db}->cmd_firstcol($sql);
+    print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
+    return(@tagnames);
+}
+
+sub artists
+{
+    my($self)=@_;
+    if(!@{$self->{elements}}) # /ALL
+    {
+       my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
+       return($self->{db}->cmd_firstcol($sql));
+    }
+    my $sql=$self->sql_start("artists.name");
+    $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
+            "WHERE artists.name != ''\n" .
+            "GROUP BY artists.name;");
+    print "SQL(ARTISTS): $sql\n" if($self->{verbose});
+    my @tagnames=$self->{db}->cmd_firstcol($sql);
+    print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
+    return(@tagnames);
+}
+
+sub albums
+{
+    my($self)=@_;
+    my $tail=$self->tail();
+    if($self->is($TYPE_ARTIST, $tail))
+    {
+       return $self->artist_albums($tail->id());
+    }
+    my $sql=$self->sql_start("albums.name");
+    $sql .= ("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") if($self->{verbose});
+    return(@names);
+}
+
+sub artist_albums
+{
+    my($self, $artist_id)=@_;
+    my $sql=$self->sql_start("albums.name");
+    $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" if($self->{verbose});
+    my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
+    print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
+    return(@albums);
+}
+
+sub artist_tracks
+{
+    my($self, $artist_id)=@_;
+    my $sql=$self->sql_start("files.name");
+    $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" if($self->{verbose});
+    my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
+    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" .
+            "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" if($self->{verbose});
+    my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
+    print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
+    return(@names);
+}
+
+sub tracks
+{
+    my($self)=@_;
+    my $tail=$self->tail();
+    if($self->is($TYPE_ARTIST, $tail))
+    {
+       return $self->artist_tracks($tail->id());
+    }
+    elsif($self->is($TYPE_ALBUM, $tail))
+    {
+       my $artist_id=0;
+       my $artist=$self->tail_parent();
+       if($self->is($TYPE_ARTIST, $artist))
+       {
+           # should always happen
+           $artist_id=$artist->id();
+       }
+       return $self->album_tracks($artist_id, $tail->id());
+    }
+    my $sql=$self->sql_start("files.name");
+    $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") if($self->{verbose});
+    return(@names);
+}
+
+sub filename
+{
+    my($self, $mountpoint)=@_;
+    my $tail=$self->tail();
+    if($self->is($TYPE_FILE, $tail))
+    {
+       my $id=$tail->id();
+       my $sql=("SELECT paths.name, files.name FROM files\n" .
+                "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" if($self->{verbose});
+       my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
+       return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
+    }
+    # should never happen
+    return "ERROR";
+}
+
+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 $sql="\tSELECT fxt1.files_id FROM tags t1";
+    my @crosses=();
+    my @inners=();
+    @joins=@{$self->{joins}};
+    for(my $i=0; $i <= $#joins; $i++)
+    {
+       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$cnt");
+           $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
+       }
+       push(@inners, $inner);
+    }
+    $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
+    $sql .= ("\n" . join("\n", @inners)) if(@inners);
+    $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
+    $sql .= "\n\tGROUP BY fxt1.files_id\n";
+    return $sql;
+}
+
+sub sql_start
+{
+    my($self, $tables)=@_;
+    my $sql="SELECT $tables 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");
+    }
+    return $sql;
+}
+
+# we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
+# filtering tags properly requires up to four levels of recursion
+# (tag/tagval/AND/NOT) and is too slow
+sub filter
+{
+    my($self, @dirs)=@_;
+    return(@dirs) unless($ENABLE_FILTER);
+    my $base=$self->{path};
+    my @outdirs=();
+    for my $dir (@dirs)
+    {
+       push(@outdirs, $dir) unless($self->empty("$base/$dir"));
+    }
+    return(@outdirs);
+}
+
+sub empty
+{
+    my($self, $dir)=@_;
+    my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
+                             ($self->{maxtagdepth} - $self->{curtagdepth}));
+    return 1 unless($path->isvalid());
+    my($subdirs,$subfiles)=$path->dirents();
+    return 0 if(@$subfiles || @$subdirs);
+    return 1;
+}
+
+# if path is .../OR/ or .../OR/NOT or .../AND/NOT
+sub want_all_tags
+{
+    my($self)=@_;
+    my $tail=$self->tail();
+    return 0 unless($tail);
+    my $parent=$self->tail_parent();
+    my $parent_valid = ($parent && $parent->type() == $TYPE_BOOL);
+    if($tail->type() == $TYPE_BOOL)
+    {
+       return 1 if($tail->name() eq "OR");
+       return 0 unless($tail->name() eq "NOT");
+       return 0 unless($parent_valid);
+       return 1 if($parent->name() eq "OR");
+       return 1 if($parent->name() eq "AND");
+    }
+    elsif($tail->type() == $TYPE_TAG)
+    {
+       return 0 unless($parent_valid);
+       return 1 if($parent->name() eq "NOT");
+    }
+    return 0;
+}
+
 1;