de8d63bc8e2567878708ab89183c370c36c52e6f
[id3fs.git] / lib / ID3FS / Path.pm
1 # id3fs - a FUSE-based filesystem for browsing audio metadata
2 # Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package ID3FS::Path;
18
19 use strict;
20 use warnings;
21 use ID3FS::Path::Node;
22
23 our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_BOOLEAN,
24      $STATE_ALBUMS, $STATE_TRACKLIST, $STATE_FILE, $STATE_ALL)=(0..7);
25
26 # operator precedence
27 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
28
29 our $PATH_ALLTRACKS= "TRACKS";
30 our $PATH_NOARTIST = "NOARTIST";
31 our $PATH_NOALBUM  = "NOALBUM";
32
33 our $ENABLE_FILTER = 0;
34
35 sub new
36 {
37     my $proto=shift;
38     my $class=ref($proto) || $proto;
39     my $self={};
40     bless($self,$class);
41
42     $self->{elements}=[];
43     $self->{db}=shift;
44     $self->{path}=shift;
45     $self->{verbose}=shift;
46     $self->{maxtagdepth}=shift;
47     $self->{curtagdepth}=0;
48     $self->{path} =~ s/\/\//\//g; # drop doubled slashes
49
50     $self->parse();
51 #    print "STATE: ", $self->state(), "\n";
52     return $self;
53 }
54
55 sub isdir
56 {
57     my($self)=@_;
58     if(($self->state() == $STATE_FILE) ||
59        ($self->state() == $STATE_INVALID))
60     {
61         return 0;
62     }
63     return 1;
64 }
65
66 sub isfile
67 {
68     my($self)=@_;
69     return($self->state() == $STATE_FILE);
70 }
71
72 sub isvalid
73 {
74     my($self)=@_;
75     return($self->state() != $STATE_INVALID);
76 }
77
78 sub dest
79 {
80     my($self, $mountpoint)=@_;
81     if($self->state() == $STATE_FILE)
82     {
83         return $self->filename($mountpoint);
84     }
85     return "ERROR"; #should never happen?
86 }
87
88 sub dirents
89 {
90     my($self)=@_;
91     my @dents=();
92     my @fents=();
93     my $state=$self->state();
94 #    print "DIRENTS: STATE: $state\n";
95 #    print "DIRENTS: FILE: $self->{path}\n";
96     if($state==$STATE_ALL)
97     {
98         @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
99     }
100     elsif($state==$STATE_TAG)
101     {
102         my $tail=$self->tail();
103         if($self->is($TYPE_TAG, $tail) && $self->{db}->tag_has_values($tail->id()))
104         {
105             @dents=$self->tags();
106         }
107         else
108         {
109             if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth}))
110             {
111                 @dents=qw(AND OR);
112             }
113             push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
114         }
115     }
116     elsif($state==$STATE_BOOLEAN)
117     {
118         my $parent=$self->tail();
119         unless($self->is($TYPE_BOOL, $parent) && $parent->name() eq "NOT")
120         {
121             @dents=("NOT");
122         }
123         push(@dents,$self->tags());
124     }
125     elsif($state==$STATE_ROOT)
126     {
127         @dents=(qw(ALL NOT), $self->tags());
128     }
129     elsif($state==$STATE_ALBUMS)
130     {
131         @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
132     }
133     elsif($state==$STATE_TRACKLIST)
134     {
135         @fents=$self->tracks();
136     }
137     else
138     {
139         print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n";
140     }
141     return(\@dents, \@fents);
142 }
143
144 # State Machine Of Doom
145 sub parse
146 {
147     my($self)=@_;
148     $self->state($STATE_ROOT);
149     return if($self->{path} eq "/");
150     @{$self->{components}}=split(/\//, $self->{path});
151     shift @{$self->{components}}; # drop empty field before leading /
152 #    print "PATH: $self->{path}\n";
153     my @parts=@{$self->{components}};
154     my($tag, $tagval);
155     $self->{elements}=[];
156     $self->{in_all}=0;
157     my $root_not=0;
158     my $tags_seen=0;
159     while(defined(my $name=shift @parts))
160     {
161 #       print "NAME: $name\n";
162         my $state=$self->state();
163         if($state==$STATE_INVALID)
164         {
165 #           print "SM: INVALID: $name\n";
166             return;
167         }
168         elsif($state==$STATE_ROOT)
169         {
170 #           print "SM: ROOT: $name\n";
171             if($name eq "ALL")
172             {
173                 $self->{in_all}=1;
174                 $self->state($STATE_ALL);
175             }
176             elsif($name eq "NOT")
177             {
178                 $root_not=1;
179                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
180                 $self->state($STATE_BOOLEAN);
181             }
182             else
183             {
184                 $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
185                 if($tag)
186                 {
187                     push(@{$self->{elements}}, $tag);
188                     $tags_seen++;
189                     $self->state($STATE_TAG);
190                 }
191                 else
192                 {
193                     $self->state($STATE_INVALID);
194                 }
195             }
196         }
197         elsif($state==$STATE_TAG)
198         {
199             my $tag=$self->tail();
200 #           print "SM: TAG/TAGVAL($state): $name\n";
201             if($self->is($TYPE_TAG, $tag) && $self->{db}->tag_has_values($tag->id()))
202             {
203 #               print "Parsing: parent: $tag->id()\n";
204                 my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
205                 if(defined($tagval))
206                 {
207                     # stay in tag state
208                     push(@{$self->{elements}}, $tagval);
209                 }
210                 else
211                 {
212                     $self->state($STATE_INVALID);
213                 }
214             }
215             elsif($name eq $PATH_ALLTRACKS)
216             {
217                 $self->state($STATE_TRACKLIST);
218             }
219             elsif($name eq $PATH_NOARTIST)
220             {
221                 $self->state($STATE_TRACKLIST);
222             }
223             elsif($name eq "AND")
224             {
225                 $self->state($STATE_BOOLEAN);
226                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
227             }
228             elsif($name eq "OR")
229             {
230                 $self->state($STATE_BOOLEAN);
231                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
232             }
233             else
234             {
235                 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
236                 if($artist)
237                 {
238                     push(@{$self->{elements}}, $artist);
239                     $self->state($STATE_ALBUMS);
240                 }
241                 else
242                 {
243                     $self->state($STATE_INVALID);
244                 }
245             }
246         }
247         elsif($state==$STATE_BOOLEAN)
248         {
249 #           print "SM: BOOLEAN: $name\n";
250             my $parent=$self->tail();
251             my $allownot=1;
252             if($self->is($TYPE_BOOL, $parent) &&
253                $parent->name() eq "NOT")
254             {
255                 $allownot=0;
256             }
257             if($allownot && $name eq "NOT")
258             {
259                 $self->state($STATE_BOOLEAN);
260                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
261             }
262             else
263             {
264                 my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
265                 if($tag)
266                 {
267                     push(@{$self->{elements}}, $tag);
268                     $tags_seen++;
269                     $self->state($STATE_TAG);
270                 }
271                 else
272                 {
273                     $self->state($STATE_INVALID);
274                 }
275             }
276         }
277         elsif($state==$STATE_ALBUMS)
278         {
279 #           print "SM: ALBUM: $name\n";
280             if($name eq $PATH_ALLTRACKS)
281             {
282                 $self->state($STATE_TRACKLIST);
283             }
284             elsif($name eq $PATH_NOALBUM)
285             {
286                 $self->state($STATE_TRACKLIST);
287             }
288             else
289             {
290                 my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
291                 if($album)
292                 {
293                     push(@{$self->{elements}}, $album);
294                     $self->state($STATE_TRACKLIST);
295                 }
296                 else
297                 {
298                     $self->state($STATE_INVALID);
299                 }
300             }
301         }
302         elsif($state==$STATE_TRACKLIST)
303         {
304 #           print "SM: TRACKLIST: $name\n";
305             my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
306             if($track)
307             {
308                 push(@{$self->{elements}}, $track);
309                 $self->state($STATE_FILE);
310             }
311             else
312             {
313                 $self->state($STATE_INVALID);
314             }
315         }
316         elsif($state==$STATE_FILE)
317         {
318 #           print "SM: FILE: $name\n";
319             # Can't have anything after a filename
320             $self->state($STATE_INVALID);
321         }
322         elsif($state==$STATE_ALL)
323         {
324             if($name eq $PATH_ALLTRACKS)
325             {
326                 $self->state($STATE_TRACKLIST);
327             }
328             elsif($name eq $PATH_NOARTIST)
329             {
330                 $self->state($STATE_TRACKLIST);
331             }
332             else
333             {
334                 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
335                 if($artist)
336                 {
337                     push(@{$self->{elements}}, $artist);
338                     $self->state($STATE_ALBUMS);
339                 }
340                 else
341                 {
342                     $self->state($STATE_INVALID);
343                 }
344             }
345         }
346         else
347         {
348             print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
349             $self->state($STATE_INVALID);
350         }
351     }
352
353     my @elements=@{$self->{elements}};
354     # remove trailing boolean
355     while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
356     {
357         pop @elements;
358     }
359 #    print "\nELEMENTS: ", join(' ', map { $_->name(); } @elements), "\n";
360     my @joins=$self->number_joins(@elements);
361     @joins=qw(INNER) unless(@joins);
362     $self->{joins}=\@joins;
363 #    print "AFTER: ", join(' ', map { $_->name() . "(" . $_->{table} . ")"; } @elements), "\n";
364 #    print "JOINS: ", join(', ', @joins), "\n";
365     # sort elements by precedence
366     @elements=$self->sort_elements(@elements);
367     $self->{tagtree}=$self->elements_to_tree(\@elements);
368 }
369
370 sub number_joins
371 {
372     my($self, @elements)=@_;
373     my @joins=("INNER");
374     my $table=1;
375     my $nextjoin=undef;
376     my $lastop=undef;
377     return (@joins) unless(@elements);
378     while(my $node=shift @elements)
379     {
380         if($node->type() == $TYPE_BOOL)
381         {
382             my $op=$node->name();
383             if($op eq "AND")
384             {
385                 $nextjoin="INNER";
386             }
387             elsif($op eq "NOT")
388             {
389                 $nextjoin="LEFT";
390             }
391             elsif($op eq "OR")
392             {
393                 # NOT/foo/OR needs an extra join
394                 $nextjoin="INNER" if($lastop && $lastop eq "NOT");
395             }
396             $lastop=$op;
397         }
398         else
399         {
400             if(@elements)
401             {
402                 # if tag has a value, eat the tag, shifting to the value
403                 if($elements[0]->type() == $TYPE_TAG)
404                 {
405                     $node->hasvals(1);
406                     $node=shift(@elements);
407                 }
408                 else
409                 {
410                     $node->hasvals(0);
411                 }
412             }
413             elsif($self->{db}->tag_has_values($node->id()))
414             {
415                 # if the expression ends in a tag that has a value
416                 # (ie we have the tag and want the value)
417                 # use an INNER join even if we were in a NOT
418                 $nextjoin="INNER" if($nextjoin);
419             }
420             if($nextjoin)
421             {
422                 $table++;
423                 push(@joins, $nextjoin);
424                 $nextjoin=undef;
425             }
426             $node->table($table);
427         }
428     }
429     return @joins;
430 }
431
432 sub state
433 {
434     my($self, $newstate)=@_;
435     if(defined($newstate))
436     {
437         $self->{state}=$newstate;
438         $self->{curtagdepth}++ if($newstate == $STATE_TAG);
439     }
440     return $self->{state};
441 }
442
443 # link up precedence-sorted list into a binary tree
444 sub elements_to_tree
445 {
446     my($self, $elements)=@_;
447     return undef unless(@$elements);
448     my ($left, $right, $op)=(undef, undef, undef);
449     my $node=pop @$elements;
450     if($self->is($TYPE_BOOL, $node))
451     {
452         $right=$self->elements_to_tree($elements);
453         if($node->name() ne "NOT")
454         {
455             $left=$self->elements_to_tree($elements);
456         }
457         $node->left($left);
458         $node->right($right);
459     }
460     return $node;
461 }
462
463 # Dijkstra's shunting-yard algorithm
464 sub sort_elements
465 {
466     my ($self, @input)=@_;
467     my @opstack=();
468     my @output=();
469     while(my $node = shift @input)
470     {
471         if($self->is($TYPE_TAG, $node))
472         {
473             # Handle tag values by dropping parent
474             if(@input && $self->is($TYPE_TAG, $input[0]))
475             {
476                 $node=shift @input;
477             }
478             push(@output, $node);
479         }
480         elsif($self->is($TYPE_BOOL, $node))
481         {
482             while(@opstack &&
483                   ($priorities{$node->name()} <= $priorities{$opstack[$#opstack]->name()}))
484             {
485                 push(@output, pop(@opstack));
486             }
487             push(@opstack, $node);
488         }
489     }
490     while(@opstack)
491     {
492         push(@output, pop(@opstack));
493     }
494     return @output;
495 }
496
497 sub used_tags
498 {
499     my($self)=@_;
500     return() unless(defined($self->{tagtree}));
501     return($self->{tagtree}->used_tags());
502 }
503
504 sub expecting_values
505 {
506     my($self)=@_;
507     my $tail=$self->tail();
508     if($self->is($TYPE_TAG, $tail))
509     {
510         return($self->{db}->tag_has_values($tail->id()));
511     }
512 }
513
514 sub trailing_tag_id
515 {
516     my($self)=@_;
517     my $tail=$self->tail();
518     if($self->is($TYPE_TAG, $tail))
519     {
520         return($tail->id());
521     }
522     return undef;
523 }
524
525 sub trailing_tag_parent
526 {
527     my($self)=@_;
528     my $tail=$self->tail();
529     if($self->is($TYPE_TAG, $tail))
530     {
531         return($tail->{parents_id});
532     }
533     return undef;
534 }
535
536 sub tail
537 {
538     my($self)=@_;
539     return($self->{elements}->[$#{$self->{elements}}]);
540 }
541
542 sub is
543 {
544     my($self, $type, $node)=@_;
545     return 0 unless($node);
546     return 0 unless($node->type());
547     return 1 if($type == $node->type());
548     return 0;
549 }
550
551 # the one before last
552 sub tail_parent
553 {
554     my($self)=@_;
555     return($self->{elements}->[($#{$self->{elements}}) - 1]);
556 }
557
558 ######################################################################
559
560 sub tags
561 {
562     my($self)=@_;
563     if(!$self->{tagtree}) # / or /NOT
564     {
565         my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
566         return($self->{db}->cmd_firstcol($sql));
567     }
568     my $sql="SELECT tags.name FROM ";
569     if($self->want_all_tags())
570     {
571         $sql .= "files_x_tags\n";
572     }
573     else
574     {
575         $sql .= ("(\n" .
576                  $self->tags_subselect() .
577                  ") AS subselect\n" .
578                  "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
579     }
580     $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
581     my @andclauses=();
582     my $id=$self->trailing_tag_id();
583
584     my $parentclause= "tags.parents_id='";
585     $parentclause .= $id if($self->expecting_values());
586     $parentclause .= "'";
587     push(@andclauses, $parentclause);
588
589     my @used=$self->used_tags();
590     if(@used)
591     {
592         push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
593     }
594     if(@andclauses)
595     {
596         $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
597     }
598
599     $sql .= "GROUP BY tags.name;";
600     print "SQL(TAGS): $sql\n" if($self->{verbose});
601     my @tagnames=$self->{db}->cmd_firstcol($sql);
602     print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
603     return(@tagnames);
604 }
605
606 sub artists
607 {
608     my($self)=@_;
609     if(!@{$self->{elements}}) # /ALL
610     {
611         my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
612         return($self->{db}->cmd_firstcol($sql));
613     }
614     my $sql=$self->sql_start("artists.name");
615     $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
616              "WHERE artists.name != ''\n" .
617              "GROUP BY artists.name;");
618     print "SQL(ARTISTS): $sql\n" if($self->{verbose});
619     my @tagnames=$self->{db}->cmd_firstcol($sql);
620     print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
621     return(@tagnames);
622 }
623
624 sub albums
625 {
626     my($self)=@_;
627     my $tail=$self->tail();
628     if($self->is($TYPE_ARTIST, $tail))
629     {
630         return $self->artist_albums($tail->id());
631     }
632     my $sql=$self->sql_start("albums.name");
633     $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
634              "WHERE albums.name != ''\n" .
635              "GROUP BY albums.name;");
636     print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
637     my @names=$self->{db}->cmd_firstcol($sql);
638     print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
639     return(@names);
640 }
641
642 sub artist_albums
643 {
644     my($self, $artist_id)=@_;
645     my $sql=$self->sql_start("albums.name");
646     $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
647              "INNER JOIN artists ON artists.id=files.artists_id\n" .
648              "WHERE artists.id=? and albums.name <> ''\n" .
649              "GROUP BY albums.name\n");
650     print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
651     my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
652     print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
653     return(@albums);
654 }
655
656 sub artist_tracks
657 {
658     my($self, $artist_id)=@_;
659     my $sql=$self->sql_start("files.name");
660     $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
661              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
662              "WHERE artists.id=? AND albums.name=''\n" .
663              "GROUP BY files.name\n");
664     print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
665     my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
666     print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
667     return(@names);
668 }
669
670 sub album_tracks
671 {
672     my($self, $artist_id, $album_id)=@_;
673     my $sql=("SELECT files.name FROM files\n" .
674              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
675              "INNER JOIN artists ON artists.id=files.artists_id\n" .
676              "WHERE artists.id=? AND albums.id=?\n" .
677              "GROUP BY files.name\n");
678     print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
679     my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
680     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
681     return(@names);
682 }
683
684 sub tracks
685 {
686     my($self)=@_;
687     my $tail=$self->tail();
688     if($self->is($TYPE_ARTIST, $tail))
689     {
690         return $self->artist_tracks($tail->id());
691     }
692     elsif($self->is($TYPE_ALBUM, $tail))
693     {
694         my $artist_id=0;
695         my $artist=$self->tail_parent();
696         if($self->is($TYPE_ARTIST, $artist))
697         {
698             # should always happen
699             $artist_id=$artist->id();
700         }
701         return $self->album_tracks($artist_id, $tail->id());
702     }
703     my $sql=$self->sql_start("files.name");
704     $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
705     if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
706     {
707         $sql .= "WHERE artists.name =''\n";
708     }
709     $sql .= "GROUP BY files.name;";
710     print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
711     my @names=$self->{db}->cmd_firstcol($sql);
712     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
713     return(@names);
714 }
715
716 sub filename
717 {
718     my($self, $mountpoint)=@_;
719     my $tail=$self->tail();
720     if($self->is($TYPE_FILE, $tail))
721     {
722         my $id=$tail->id();
723         my $sql=("SELECT paths.name, files.name FROM files\n" .
724                  "INNER JOIN paths ON files.paths_id=paths.id\n" .
725                  "WHERE files.id=?\n" .
726                  "GROUP BY paths.name, files.name");
727         print "FILENAME SQL: $sql\n" if($self->{verbose});
728         my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
729         return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
730     }
731     # should never happen
732     return "ERROR";
733 }
734
735 sub tags_subselect
736 {
737     my($self)=@_;
738     my $tree=$self->{tagtree};
739 #    use Data::Dumper;
740 #    print Dumper $tree;
741     my ($sqlclause, @joins)=$tree->to_sql() if($tree);
742     my $sql="\tSELECT fxt1.files_id FROM tags t1";
743     my @crosses=();
744     my @inners=();
745     @joins=@{$self->{joins}};
746     for(my $i=0; $i <= $#joins; $i++)
747     {
748         my $cnt=$i+1;
749         my $join=$joins[$i];
750         my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
751                    "t${cnt}.id=fxt${cnt}.tags_id");
752         if($i > 0)
753         {
754             push(@crosses, "CROSS JOIN tags t$cnt");
755             $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
756         }
757         push(@inners, $inner);
758     }
759     $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
760     $sql .= ("\n" . join("\n", @inners)) if(@inners);
761     $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
762     $sql .= "\n\tGROUP BY fxt1.files_id\n";
763     return $sql;
764 }
765
766 sub sql_start
767 {
768     my($self, $tables)=@_;
769     my $sql="SELECT $tables FROM ";
770     if($self->{in_all})
771     {
772         $sql .= "files\n";
773     }
774     else
775     {
776         $sql .= ("(\n" .
777                  $self->tags_subselect() .
778                  ") AS subselect\n" .
779                  "INNER JOIN files ON subselect.files_id=files.id\n");
780     }
781     return $sql;
782 }
783
784 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
785 # filtering tags properly requires up to four levels of recursion
786 # (tag/tagval/AND/NOT) and is too slow
787 sub filter
788 {
789     my($self, @dirs)=@_;
790     return(@dirs) unless($ENABLE_FILTER);
791     my $base=$self->{path};
792     my @outdirs=();
793     for my $dir (@dirs)
794     {
795         push(@outdirs, $dir) unless($self->empty("$base/$dir"));
796     }
797     return(@outdirs);
798 }
799
800 sub empty
801 {
802     my($self, $dir)=@_;
803     my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
804                               ($self->{maxtagdepth} - $self->{curtagdepth}));
805     return 1 unless($path->isvalid());
806     my($subdirs,$subfiles)=$path->dirents();
807     return 0 if(@$subfiles || @$subdirs);
808     return 1;
809 }
810
811 # if path is .../OR/ or .../OR/NOT or .../AND/NOT
812 sub want_all_tags
813 {
814     my($self)=@_;
815     my $tail=$self->tail();
816     return 0 unless($tail);
817     my $parent=$self->tail_parent();
818     my $parent_valid = ($parent && $parent->type() == $TYPE_BOOL);
819     if($tail->type() == $TYPE_BOOL)
820     {
821         return 1 if($tail->name() eq "OR");
822         return 0 unless($tail->name() eq "NOT");
823         return 0 unless($parent_valid);
824         return 1 if($parent->name() eq "OR");
825         return 1 if($parent->name() eq "AND");
826     }
827     elsif($tail->type() == $TYPE_TAG)
828     {
829         return 0 unless($parent_valid);
830         return 1 if($parent->name() eq "NOT");
831     }
832     return 0;
833 }
834
835 1;