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