preprocess elements, calculating joins
[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 = 1;
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=qw(INNER);
374     my $table=1;
375     my $lastop=undef;
376     return () unless(@elements);
377     while(my $thing=shift @elements)
378     {
379         if($thing->type() == $TYPE_BOOL)
380         {
381             my $op=$thing->name();
382             if($op eq "AND")
383             {
384                 $table++;
385                 push(@joins, "INNER");
386             }
387             elsif($op eq "NOT")
388             {
389                 unless($lastop && $lastop eq "AND")
390                 {
391                     push(@joins, "LEFT");
392                     $table++;
393                 }
394             }
395             elsif($op eq "OR")
396             {
397                 if($lastop && $lastop eq "NOT")
398                 {
399                     $table++;
400                     push(@joins, "INNER");
401                 }
402             }
403             $lastop=$op;
404         }
405         else
406         {
407             # skip tags with values
408             if(@elements && $elements[0]->type() == $TYPE_TAG)
409             {
410                 $thing=shift(@elements);
411             }
412             $thing->table($table);
413         }
414     }
415     return @joins;
416 }
417
418 sub state
419 {
420     my($self, $newstate)=@_;
421     if(defined($newstate))
422     {
423         $self->{state}=$newstate;
424         $self->{curtagdepth}++ if($newstate == $STATE_TAG);
425     }
426     return $self->{state};
427 }
428
429 # link up precedence-sorted list into a binary tree
430 sub elements_to_tree
431 {
432     my($self, $elements)=@_;
433     return undef unless(@$elements);
434     my ($left, $right, $op)=(undef, undef, undef);
435     my $thing=pop @$elements;
436     if($self->is($TYPE_BOOL, $thing))
437     {
438         $right=$self->elements_to_tree($elements);
439         if($thing->{name} ne "NOT")
440         {
441             $left=$self->elements_to_tree($elements);
442         }
443         $thing->left($left);
444         $thing->right($right);
445     }
446     return $thing;
447 }
448
449 # Dijkstra's shunting-yard algorithm
450 sub sort_elements
451 {
452     my ($self, @input)=@_;
453     my @opstack=();
454     my @output=();
455     while(my $thing = shift @input)
456     {
457         if($self->is($TYPE_TAG, $thing))
458         {
459             # Handle tag values by dropping parent
460             if(@input && $self->is($TYPE_TAG, $input[0]))
461             {
462                 $thing=shift @input;
463             }
464             push(@output, $thing);
465         }
466         elsif($self->is($TYPE_BOOL, $thing))
467         {
468             while(@opstack &&
469                   ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
470             {
471                 push(@output, pop(@opstack));
472             }
473             push(@opstack, $thing);
474         }
475     }
476     while(@opstack)
477     {
478         push(@output, pop(@opstack));
479     }
480     return @output;
481 }
482
483 sub used_tags
484 {
485     my($self)=@_;
486     return() unless(defined($self->{tagtree}));
487     return($self->{tagtree}->used_tags());
488 }
489
490 sub expecting_values
491 {
492     my($self)=@_;
493     my $tail=$self->tail();
494     if($self->is($TYPE_TAG, $tail))
495     {
496         return($self->{db}->tag_has_values($tail->id()));
497     }
498 }
499
500 sub trailing_tag_id
501 {
502     my($self)=@_;
503     my $tail=$self->tail();
504     if($self->is($TYPE_TAG, $tail))
505     {
506         return($tail->id());
507     }
508     return undef;
509 }
510
511 sub trailing_tag_parent
512 {
513     my($self)=@_;
514     my $tail=$self->tail();
515     if($self->is($TYPE_TAG, $tail))
516     {
517         return($tail->{parents_id});
518     }
519     return undef;
520 }
521
522 sub tail
523 {
524     my($self)=@_;
525     return($self->{elements}->[$#{$self->{elements}}]);
526 }
527
528 sub is
529 {
530     my($self, $type, $thing)=@_;
531     return 0 unless($thing);
532     return 0 unless($thing->type());
533     return 1 if($type == $thing->type());
534     return 0;
535 }
536
537 # the one before last
538 sub tail_parent
539 {
540     my($self)=@_;
541     return($self->{elements}->[($#{$self->{elements}}) - 1]);
542 }
543
544 ######################################################################
545
546 sub tags
547 {
548     my($self)=@_;
549     if(!$self->{tagtree}) # / or /NOT
550     {
551         my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
552         return($self->{db}->cmd_firstcol($sql));
553     }
554     my $hasvals=$self->expecting_values();
555     my $sql="SELECT tags.name FROM ";
556     if($self->want_all_tags())
557     {
558         $sql .= "files_x_tags\n";
559     }
560     else
561     {
562         $sql .= ("(\n" .
563                  $self->tags_subselect() .
564                  ") AS subselect\n" .
565                  "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
566     }
567     $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
568     my @andclauses=();
569     my $id=$self->trailing_tag_id();
570
571     my $parentclause= "tags.parents_id='";
572     $parentclause .= $id if($hasvals);
573     $parentclause .= "'";
574     push(@andclauses, $parentclause);
575
576     my @used=$self->used_tags();
577     if(@used)
578     {
579         push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
580     }
581     if(@andclauses)
582     {
583         $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
584     }
585
586     $sql .= "GROUP BY tags.name;";
587     print "SQL(TAGS): $sql\n" if($self->{verbose});
588     my @tagnames=$self->{db}->cmd_firstcol($sql);
589     print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
590     return(@tagnames);
591 }
592
593 sub artists
594 {
595     my($self)=@_;
596     if(!@{$self->{elements}}) # /ALL
597     {
598         my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
599         return($self->{db}->cmd_firstcol($sql));
600     }
601     my $sql=$self->sql_start("artists.name");
602     $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
603              "WHERE artists.name != ''\n" .
604              "GROUP BY artists.name;");
605     print "SQL(ARTISTS): $sql\n" if($self->{verbose});
606     my @tagnames=$self->{db}->cmd_firstcol($sql);
607     print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
608     return(@tagnames);
609 }
610
611 sub albums
612 {
613     my($self)=@_;
614     my $tail=$self->tail();
615     if($self->is($TYPE_ARTIST, $tail))
616     {
617         return $self->artist_albums($tail->id());
618     }
619     my $sql=$self->sql_start("albums.name");
620     $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
621              "WHERE albums.name != ''\n" .
622              "GROUP BY albums.name;");
623     print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
624     my @names=$self->{db}->cmd_firstcol($sql);
625     print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
626     return(@names);
627 }
628
629 sub artist_albums
630 {
631     my($self, $artist_id)=@_;
632     my $sql=$self->sql_start("albums.name");
633     $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
634              "INNER JOIN artists ON artists.id=files.artists_id\n" .
635              "WHERE artists.id=? and albums.name <> ''\n" .
636              "GROUP BY albums.name\n");
637     print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
638     my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
639     print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
640     return(@albums);
641 }
642
643 sub artist_tracks
644 {
645     my($self, $artist_id)=@_;
646     my $sql=$self->sql_start("files.name");
647     $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
648              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
649              "WHERE artists.id=? AND albums.name=''\n" .
650              "GROUP BY files.name\n");
651     print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
652     my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
653     print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
654     return(@names);
655 }
656
657 sub album_tracks
658 {
659     my($self, $artist_id, $album_id)=@_;
660     my $sql=("SELECT files.name FROM files\n" .
661              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
662              "INNER JOIN artists ON artists.id=files.artists_id\n" .
663              "WHERE artists.id=? AND albums.id=?\n" .
664              "GROUP BY files.name\n");
665     print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
666     my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
667     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
668     return(@names);
669 }
670
671 sub tracks
672 {
673     my($self)=@_;
674     my $tail=$self->tail();
675     if($self->is($TYPE_ARTIST, $tail))
676     {
677         return $self->artist_tracks($tail->id());
678     }
679     elsif($self->is($TYPE_ALBUM, $tail))
680     {
681         my $artist_id=0;
682         my $artist=$self->tail_parent();
683         if($self->is($TYPE_ARTIST, $artist))
684         {
685             # should always happen
686             $artist_id=$artist->id();
687         }
688         return $self->album_tracks($artist_id, $tail->id());
689     }
690     my $sql=$self->sql_start("files.name");
691     $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
692     if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
693     {
694         $sql .= "WHERE artists.name =''\n";
695     }
696     $sql .= "GROUP BY files.name;";
697     print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
698     my @names=$self->{db}->cmd_firstcol($sql);
699     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
700     return(@names);
701 }
702
703 sub filename
704 {
705     my($self, $mountpoint)=@_;
706     my $tail=$self->tail();
707     if($self->is($TYPE_FILE, $tail))
708     {
709         my $id=$tail->id();
710         my $sql=("SELECT paths.name, files.name FROM files\n" .
711                  "INNER JOIN paths ON files.paths_id=paths.id\n" .
712                  "WHERE files.id=?\n" .
713                  "GROUP BY paths.name, files.name");
714         print "FILENAME SQL: $sql\n" if($self->{verbose});
715         my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
716         return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
717     }
718     # should never happen
719     return "ERROR";
720 }
721
722 sub tags_subselect
723 {
724     my($self)=@_;
725     my $hasvals=$self->expecting_values();
726     my $tree=$self->{tagtree};
727 #    use Data::Dumper;
728 #    print Dumper $tree;
729     my ($sqlclause, @joins)=$tree->to_sql($hasvals) if($tree);
730     my $sql="\tSELECT fxt1.files_id FROM tags t1";
731     my @crosses=();
732     my @inners=();
733     @joins=@{$self->{joins}};
734     for(my $i=0; $i <= $#joins; $i++)
735     {
736         my $cnt=$i+1;
737         my $join=$joins[$i];
738         my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
739                    "t${cnt}.id=fxt${cnt}.tags_id");
740         if($i > 0)
741         {
742             push(@crosses, "CROSS JOIN tags t$cnt");
743             $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
744         }
745         push(@inners, $inner);
746     }
747     $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
748     $sql .= ("\n" . join("\n", @inners)) if(@inners);
749     $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
750     $sql .= "\n\tGROUP BY fxt1.files_id\n";
751     return $sql;
752 }
753
754 sub sql_start
755 {
756     my($self, $tables)=@_;
757     my $sql="SELECT $tables FROM ";
758     if($self->{in_all})
759     {
760         $sql .= "files\n";
761     }
762     else
763     {
764         $sql .= ("(\n" .
765                  $self->tags_subselect() .
766                  ") AS subselect\n" .
767                  "INNER JOIN files ON subselect.files_id=files.id\n");
768     }
769     return $sql;
770 }
771
772 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
773 # filtering tags properly requires up to four levels of recursion
774 # (tag/tagval/AND/NOT) and is too slow
775 sub filter
776 {
777     my($self, @dirs)=@_;
778     return(@dirs) unless($ENABLE_FILTER);
779     my $base=$self->{path};
780     my @outdirs=();
781     for my $dir (@dirs)
782     {
783         push(@outdirs, $dir) unless($self->empty("$base/$dir"));
784     }
785     return(@outdirs);
786 }
787
788 sub empty
789 {
790     my($self, $dir)=@_;
791     my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
792                               ($self->{maxtagdepth} - $self->{curtagdepth}));
793     return 1 unless($path->isvalid());
794     my($subdirs,$subfiles)=$path->dirents();
795     return 0 if(@$subfiles || @$subdirs);
796     return 1;
797 }
798
799 # if path is .../OR/ or .../OR/NOT or .../AND/NOT
800 sub want_all_tags
801 {
802     my($self)=@_;
803     my $tail=$self->tail();
804     return 0 unless($tail);
805     my $parent=$self->tail_parent();
806     my $parent_valid = ($parent && $parent->type() == $TYPE_BOOL);
807     if($tail->type() == $TYPE_BOOL)
808     {
809         return 1 if($tail->name() eq "OR");
810         return 0 unless($tail->name() eq "NOT");
811         return 0 unless($parent_valid);
812         return 1 if($parent->name() eq "OR");
813         return 1 if($parent->name() eq "AND");
814     }
815     elsif($tail->type() == $TYPE_TAG)
816     {
817         return 0 unless($parent_valid);
818         return 1 if($parent->name() eq "NOT");
819     }
820     return 0;
821 }
822
823 1;