handle /NOT/foo/OR/bar
[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_TAGVAL,
24      $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST,
25      $STATE_FILE, $STATE_ALL)=(0..8);
26
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 || $state==$STATE_TAGVAL)
101     {
102         if($state==$STATE_TAG && $self->at("tag") &&
103            $self->{db}->tag_has_values($self->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("boolean", $parent) &&
120                $parent->{name} eq "NOT")
121         {
122             @dents=("NOT");
123         }
124         push(@dents,$self->tags());
125     }
126     elsif($state==$STATE_ROOT)
127     {
128         @dents=(qw(ALL NOT), $self->tags());
129     }
130     elsif($state==$STATE_ALBUMS)
131     {
132         @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
133     }
134     elsif($state==$STATE_TRACKLIST)
135     {
136         @fents=$self->tracks();
137     }
138     else
139     {
140         print "DIRENTS: UNHANDLED STATE: $state\n";
141     }
142     return(\@dents, \@fents);
143 }
144
145 sub parse
146 {
147     my($self)=@_;
148     @{$self->{components}}=split(/\//, $self->{path});
149     shift @{$self->{components}}; # drop empty field before leading /
150 #    print "PATH: $self->{path}\n";
151     $self->state($STATE_ROOT);
152     return if($self->{path} eq "/");
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}, "boolean", $name));
180                 $self->state($STATE_BOOLEAN);
181             }
182             else
183             {
184                 $tag=ID3FS::Path::Node->new($self->{db},"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 || $state==$STATE_TAGVAL)
198         {
199             my $tag=$self->tail();
200 #           print "SM: TAG/TAGVAL($state): $name\n";
201             if($state==$STATE_TAG && $self->is("tag", $tag) &&
202                $self->{db}->tag_has_values($tag->id()))
203             {
204 #               print "Parsing: parent: $tag->id()\n";
205                 my $tagval=ID3FS::Path::Node->new($self->{db}, "tag", $name, $tag->id());
206                 if(defined($tagval))
207                 {
208                     $self->state($STATE_TAGVAL);
209                     # stay in tag state
210                     push(@{$self->{elements}}, $tagval);
211                 }
212                 else
213                 {
214                     $self->state($STATE_INVALID);
215                 }
216             }
217             elsif($name eq $PATH_ALLTRACKS)
218             {
219                 $self->state($STATE_TRACKLIST);
220             }
221             elsif($name eq $PATH_NOARTIST)
222             {
223                 $self->state($STATE_TRACKLIST);
224             }
225             elsif($name eq "AND")
226             {
227                 $self->state($STATE_BOOLEAN);
228                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
229             }
230             elsif($name eq "OR")
231             {
232                 $self->state($STATE_BOOLEAN);
233                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
234             }
235             else
236             {
237                 my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
238                 if($artist)
239                 {
240                     push(@{$self->{elements}}, $artist);
241                     $self->state($STATE_ALBUMS);
242                 }
243                 else
244                 {
245                     $self->state($STATE_INVALID);
246                 }
247             }
248         }
249         elsif($state==$STATE_BOOLEAN)
250         {
251 #           print "SM: BOOLEAN: $name\n";
252             my $parent=$self->tail();
253             my $allownot=1;
254             if($self->is("boolean", $parent) &&
255                $parent->{name} eq "NOT")
256             {
257                 $allownot=0;
258             }
259             if($allownot && $name eq "NOT")
260             {
261                 $self->state($STATE_BOOLEAN);
262                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
263             }
264             else
265             {
266                 my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name);
267                 if($tag)
268                 {
269                     push(@{$self->{elements}}, $tag);
270                     $tags_seen++;
271                     $self->state($STATE_TAG);
272                 }
273                 else
274                 {
275                     $self->state($STATE_INVALID);
276                 }
277             }
278         }
279         elsif($state==$STATE_ALBUMS)
280         {
281 #           print "SM: ALBUM: $name\n";
282             if($name eq $PATH_ALLTRACKS)
283             {
284                 $self->state($STATE_TRACKLIST);
285             }
286             elsif($name eq $PATH_NOALBUM)
287             {
288                 $self->state($STATE_TRACKLIST);
289             }
290             else
291             {
292                 my $album=ID3FS::Path::Node->new($self->{db}, "album", $name);
293                 if($album)
294                 {
295                     push(@{$self->{elements}}, $album);
296                     $self->state($STATE_TRACKLIST);
297                 }
298                 else
299                 {
300                     $self->state($STATE_INVALID);
301                 }
302             }
303         }
304         elsif($state==$STATE_TRACKLIST)
305         {
306 #           print "SM: TRACKLIST: $name\n";
307             my $track=ID3FS::Path::Node->new($self->{db}, "file", $name);
308             if($track)
309             {
310                 push(@{$self->{elements}}, $track);
311                 $self->state($STATE_FILE);
312             }
313             else
314             {
315                 $self->state($STATE_INVALID);
316             }
317         }
318         elsif($state==$STATE_FILE)
319         {
320 #           print "SM: FILE: $name\n";
321             # Can't have anything after a filename
322             $self->state($STATE_INVALID);
323         }
324         elsif($state==$STATE_ALL)
325         {
326             if($name eq $PATH_ALLTRACKS)
327             {
328                 $self->state($STATE_TRACKLIST);
329             }
330             elsif($name eq $PATH_NOARTIST)
331             {
332                 $self->state($STATE_TRACKLIST);
333             }
334             else
335             {
336                 my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
337                 if($artist)
338                 {
339                     push(@{$self->{elements}}, $artist);
340                     $self->state($STATE_ALBUMS);
341                 }
342                 else
343                 {
344                     $self->state($STATE_INVALID);
345                 }
346             }
347         }
348         else
349         {
350             print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
351             $self->state($STATE_INVALID);
352         }
353     }
354
355     # remove trailing boolean
356     my @elements=@{$self->{elements}};
357     while(@elements && $self->is("boolean", $elements[$#elements]))
358     {
359         pop @elements;
360     }
361     # sort elements by precedence
362     @elements=$self->sort_elements(@elements);
363     $self->{tagtree}=$self->elements_to_tree(\@elements);
364     if($self->{tagtree})
365     {
366 #       use Data::Dumper;
367 #       print Dumper $self->{tagtree};
368 #       my ($conditions, @joins)=$self->{tagtree}->to_sql();
369 #       print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n";
370     }
371 }
372
373 sub state
374 {
375     my($self, $newstate)=@_;
376     if(defined($newstate))
377     {
378         $self->{state}=$newstate;
379         $self->{curtagdepth}++ if($newstate == $STATE_TAG);
380     }
381     return $self->{state};
382 }
383
384 sub elements_to_tree
385 {
386     my($self, $elements)=@_;
387     return undef unless(@$elements);
388     my ($left, $right, $op)=(undef, undef, undef);
389     my $thing=pop @$elements;
390     if($self->is("boolean", $thing))
391     {
392         $right=$self->elements_to_tree($elements);
393         if($thing->{name} ne "NOT")
394         {
395             $left=$self->elements_to_tree($elements);
396         }
397         $thing->left($left);
398         $thing->right($right);
399     }
400     return $thing;
401 }
402
403 # Dijkstra's shunting-yard algorithm
404 sub sort_elements
405 {
406     my ($self, @input)=@_;
407     my @opstack=();
408     my @output=();
409 #    print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
410     while(my $thing = shift @input)
411     {
412         if($self->is("tag", $thing))
413         {
414             # Handle tag values by dropping parent
415             if(@input && $self->is("tag", $input[0]))
416             {
417                 $thing=shift @input;
418             }
419             push(@output, $thing);
420         }
421         elsif($self->is("boolean", $thing))
422         {
423             # bool
424             while(@opstack &&
425                   ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
426             {
427                 push(@output, pop(@opstack));
428             }
429             push(@opstack, $thing);
430         }
431     }
432     while(@opstack)
433     {
434         push(@output, pop(@opstack));
435     }
436 #    print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
437     return @output;
438 }
439
440 sub used_tags
441 {
442     my($self)=@_;
443     return() unless(defined($self->{tagtree}));
444     return($self->{tagtree}->used_tags());
445 }
446
447 sub expecting_values
448 {
449     my($self)=@_;
450     my $tail=$self->tail();
451     if($self->is("tag", $tail))
452     {
453         return($self->{db}->tag_has_values($tail->id()));
454     }
455 }
456
457 sub trailing_tag_id
458 {
459     my($self)=@_;
460     my $tail=$self->tail();
461     if($self->is("tag", $tail))
462     {
463         return($tail->id());
464     }
465     return undef;
466 }
467
468 sub trailing_tag_parent
469 {
470     my($self)=@_;
471     my $tail=$self->tail();
472     if($self->is("tag", $tail))
473     {
474         return($tail->{parents_id});
475     }
476     return undef;
477 }
478
479 sub tail
480 {
481     my($self)=@_;
482     return($self->{elements}->[$#{$self->{elements}}]);
483 }
484
485 sub at
486 {
487     my($self, $type)=@_;
488     return($self->is($type, $self->tail()));
489 }
490
491 sub is
492 {
493     my($self, $type, $thing)=@_;
494     return 0 unless($thing);
495     return 0 unless($thing->type());
496     return 1 if($type eq $thing->type());
497     return 0;
498 }
499
500 # the one before last
501 sub tail_parent
502 {
503     my($self)=@_;
504     return($self->{elements}->[($#{$self->{elements}}) - 1]);
505 }
506
507 ######################################################################
508
509 sub tags
510 {
511     my($self)=@_;
512     if(!$self->{tagtree}) # / or /NOT
513     {
514         my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
515         return($self->{db}->cmd_firstcol($sql));
516     }
517     my $hasvals=$self->expecting_values();
518     my $parent=$self->trailing_tag_parent();
519 #    print "THASVALS: $hasvals\n";
520 #    print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
521     my @ids=();
522     my $sql="SELECT tags.name FROM ";
523     if($self->in_or())
524     {
525         $sql .= "files_x_tags\n";
526     }
527     else
528     {
529         $sql .= ("(\n" .
530                  $self->tags_subselect() .
531                  ") AS subselect\n" .
532                  "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
533     }
534     $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
535     my @andclauses=();
536     my $id=$self->trailing_tag_id();
537
538     my $parentclause= "tags.parents_id='";
539     if($hasvals)
540     {
541         $parentclause .= $id;
542     }
543     elsif($parent)
544     {
545         $parentclause .= $parent;
546     }
547     $parentclause .= "'";
548     push(@andclauses, $parentclause);
549
550     my @used=$self->used_tags();
551     if(@used)
552     {
553         push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
554     }
555     if(@andclauses)
556     {
557         $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
558     }
559
560     $sql .= "GROUP BY tags.name;";
561     print "SQL(TAGS): $sql\n" if($self->{verbose});
562     my @tagnames=$self->{db}->cmd_firstcol($sql);
563     print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
564     return(@tagnames);
565 }
566
567 sub artists
568 {
569     my($self)=@_;
570     if(!@{$self->{elements}}) # /ALL
571     {
572         my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
573         return($self->{db}->cmd_firstcol($sql));
574     }
575     my @ids=();
576     my $sql=$self->sql_start("artists.name");
577     $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
578              "WHERE artists.name != ''\n" .
579              "GROUP BY artists.name;");
580     print "SQL(ARTISTS): $sql\n" if($self->{verbose});
581     my @tagnames=$self->{db}->cmd_firstcol($sql);
582     print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
583     return(@tagnames);
584 }
585
586 sub albums
587 {
588     my($self)=@_;
589     my @ids=();
590     my $tail=$self->tail();
591     if($self->is("artist", $tail))
592     {
593         return $self->artist_albums($tail->id());
594     }
595     my $sql=$self->sql_start("albums.name");
596     $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
597              "WHERE albums.name != ''\n" .
598              "GROUP BY albums.name;");
599     print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
600     my @names=$self->{db}->cmd_firstcol($sql);
601     print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
602     return(@names);
603 }
604
605 sub artist_albums
606 {
607     my($self, $artist_id)=@_;
608     my $sql=$self->sql_start("albums.name");
609     $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
610              "INNER JOIN artists ON artists.id=files.artists_id\n" .
611              "WHERE artists.id=? and albums.name <> ''\n" .
612              "GROUP BY albums.name\n");
613     print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
614     my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
615     print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
616     return(@albums);
617 }
618
619 sub artist_tracks
620 {
621     my($self, $artist_id)=@_;
622     my $sql=$self->sql_start("files.name");
623     $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
624              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
625              "WHERE artists.id=? AND albums.name=''\n" .
626              "GROUP BY files.name\n");
627     print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
628     my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
629     print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
630     return(@names);
631 }
632
633 sub album_tracks
634 {
635     my($self, $artist_id, $album_id)=@_;
636     my $sql=("SELECT files.name FROM files\n" .
637              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
638              "INNER JOIN artists ON artists.id=files.artists_id\n" .
639              "WHERE artists.id=? AND albums.id=?\n" .
640              "GROUP BY files.name\n");
641     print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
642     my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
643     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
644     return(@names);
645 }
646
647 sub tracks
648 {
649     my($self)=@_;
650     my $tail=$self->tail();
651     if($self->is("artist", $tail))
652     {
653         return $self->artist_tracks($tail->id());
654     }
655     elsif($self->is("album", $tail))
656     {
657         my $artist_id=0;
658         my $artist=$self->tail_parent();
659         if($self->is("artist", $artist))
660         {
661             # should always happen
662             $artist_id=$artist->id();
663         }
664         return $self->album_tracks($artist_id, $tail->id());
665     }
666     my $sql=$self->sql_start("files.name");
667     $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
668     if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
669     {
670         $sql .= "WHERE artists.name =''\n";
671     }
672     $sql .= "GROUP BY files.name;";
673     print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
674     my @names=$self->{db}->cmd_firstcol($sql);
675     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
676     return(@names);
677 }
678
679 sub filename
680 {
681     my($self, $mountpoint)=@_;
682     my $tail=$self->tail();
683     if($self->is("file", $tail))
684     {
685         my $id=$tail->id();
686         my $sql=("SELECT paths.name, files.name FROM files\n" .
687                  "INNER JOIN paths ON files.paths_id=paths.id\n" .
688                  "WHERE files.id=?\n" .
689                  "GROUP BY paths.name, files.name");
690         print "FILENAME SQL: $sql\n" if($self->{verbose});
691         my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
692         my $id3fs_path=join('/', map { $_->{name}; }  @{$self->{elements}});
693         return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
694     }
695     # should never happen
696     return "ERROR";
697 }
698
699 sub tags_subselect
700 {
701     my($self)=@_;
702     my $hasvals=$self->expecting_values();
703     if($self->{in_all})
704     {
705         # FIXME: is this used?
706         die "IN_ALL CALLED!!\n";
707         return "\tSELECT id FROM files AS files_id\n";
708     }
709     my $tree=$self->{tagtree};
710     # FIXME
711     print "UNDEF!!\n" unless($self->{tagtree});
712     my $parent=$self->trailing_tag_parent();
713
714 #    print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n";
715 #    print "TREE: ", $tree->print(), "\n";
716     my $tag=undef;
717     if($hasvals)
718     {
719         $tag=$self->trailing_tag_id();
720 #       print "Trailing id: $tag\n";
721     }
722     my ($sqlclause, @joins)=(undef, ());
723     ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
724 #    print "SQL(" . scalar(@joins) ."): $sqlclause\n";
725     my $sql="\tSELECT fxt1.files_id FROM tags t1";
726     my @crosses=();
727     my @inners=();
728 #    $joinsneeded++ if($tag);
729     for(my $i=0; $i <= $#joins; $i++)
730     {
731         my $cnt=$i+1;
732         my $join=$joins[$i];
733         my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
734                    "t${cnt}.id=fxt${cnt}.tags_id");
735         if($i > 0)
736         {
737             push(@crosses, "CROSS JOIN tags t$cnt");
738             $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
739         }
740         push(@inners, $inner);
741     }
742     $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
743     $sql .= ("\n" . join("\n", @inners)) if(@inners);
744     $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
745 #    if($tag)
746 #    {
747 #       $sql .= " AND t${joinsneeded}.parents_id='$tag'";
748 #    }
749     $sql .= "\n\tGROUP BY fxt1.files_id\n";
750     return $sql;
751 }
752
753 sub sql_start
754 {
755     my($self, $tables)=@_;
756     my $sql="SELECT $tables FROM ";
757     if($self->{in_all})
758     {
759         $sql .= "files\n";
760     }
761     else
762     {
763         $sql .= ("(\n" .
764                  $self->tags_subselect() .
765                  ") AS subselect\n" .
766                  "INNER JOIN files ON subselect.files_id=files.id\n");
767     }
768     return $sql;
769 }
770
771 # FIXME: remove
772 sub constraints_tag_list
773 {
774     my($self, @constraints)=@_;
775     my $lasttag=undef;
776     my @tags=();
777     my @tags_vals=();
778     for my $constraint (@constraints)
779     {
780 #       print ref($constraint), ": ", $constraint->{name}, "\n";
781         if($self->is("tag", $constraint))
782         {
783             if(defined($lasttag))
784             {
785 #               print "TAGVAL\n";
786                 push(@tags_vals, [$lasttag, $constraint->id()]) if defined($constraint->id());
787                 $lasttag=undef;
788             }
789             elsif($self->tag_has_values($constraint->id()))
790             {
791 #               print "HASVALUES\n";
792                 $lasttag=$constraint->id() if defined($constraint->id());
793             }
794             else
795             {
796 #               print "NOVALUES\n";
797                 push(@tags, $constraint->id()) if(defined($constraint->id()));
798             }
799         }
800     }
801     @tags=map{ "\"$_\""; } @tags;
802     @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals);
803     $lasttag="\"$lasttag\"" if defined($lasttag);
804     return(\@tags, \@tags_vals, $lasttag);
805 }
806
807 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
808 # filtering tags properly requires up to four levels of recursion
809 # (tag/tagval/AND/NOT) and is too slow
810 sub filter
811 {
812     my($self, @dirs)=@_;
813     return(@dirs) unless($ENABLE_FILTER);
814     my $base=$self->{path};
815     my @outdirs=();
816     for my $dir (@dirs)
817     {
818 #       print "\nFILTER (",$self->state(), "): $base / $dir\n";
819         if($self->empty("$base/$dir"))
820         {
821 #           print "empty: $base / $dir\n";
822         }
823         else
824         {
825 #           print "non-empty, accepting: $base / $dir\n";
826             push(@outdirs, $dir);
827         }
828     }
829     return(@outdirs);
830 }
831
832 sub empty
833 {
834     my($self, $dir)=@_;
835     my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
836                               ($self->{maxtagdepth} - $self->{curtagdepth}));
837     return 1 unless($path->isvalid());
838     my($subdirs,$subfiles)=$path->dirents();
839     return 0 if(@$subfiles || @$subdirs);
840     return 1;
841 }
842
843 # if path is .../OR/ or .../OR/NOT
844 sub in_or
845 {
846     my($self)=@_;
847     my $tail=$self->tail();
848     return 0 unless($tail);
849     return 0 unless($tail->type() eq "boolean");
850     return 1 if($tail->name() eq "OR");
851     return 0 unless($tail->name() eq "NOT");
852     my $parent=$self->tail_parent();
853     return 0 unless($parent);
854     return 0 unless($parent->type() eq "boolean");
855     return 1 if($parent->name() eq "OR");
856     return 0;
857 }
858
859 1;