X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FPath.pm;h=de8d63bc8e2567878708ab89183c370c36c52e6f;hb=a461a5fca9a1ffc9e54c978423214c2fa59431e5;hp=60ea5de43b498e1fe30c64dcbf81cb11be0b1977;hpb=d37f9e03d8acde3100e40874535f860eaa15582b;p=id3fs.git diff --git a/lib/ID3FS/Path.pm b/lib/ID3FS/Path.pm index 60ea5de..de8d63b 100644 --- a/lib/ID3FS/Path.pm +++ b/lib/ID3FS/Path.pm @@ -1,24 +1,36 @@ +# id3fs - a FUSE-based filesystem for browsing audio metadata +# Copyright (C) 2010 Ian Beckwith +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + package ID3FS::Path; use strict; use warnings; -use ID3FS::PathElement::Artist; -use ID3FS::PathElement::Album; -use ID3FS::PathElement::Boolean; -use ID3FS::PathElement::File; -use ID3FS::PathElement::Tag; -use ID3FS::PathElement::Tagval; use ID3FS::Path::Node; -our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL, - $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST, - $STATE_FILE, $STATE_ALL)=(0..8); +our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_BOOLEAN, + $STATE_ALBUMS, $STATE_TRACKLIST, $STATE_FILE, $STATE_ALL)=(0..7); +# operator precedence our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 ); -our $PATH_ALLTRACKS="TRACKS"; -our $PATH_NOARTIST="NOARTIST"; -our $PATH_NOALBUM="NOALBUM"; +our $PATH_ALLTRACKS= "TRACKS"; +our $PATH_NOARTIST = "NOARTIST"; +our $PATH_NOALBUM = "NOALBUM"; + +our $ENABLE_FILTER = 0; sub new { @@ -31,6 +43,8 @@ sub new $self->{db}=shift; $self->{path}=shift; $self->{verbose}=shift; + $self->{maxtagdepth}=shift; + $self->{curtagdepth}=0; $self->{path} =~ s/\/\//\//g; # drop doubled slashes $self->parse(); @@ -52,8 +66,7 @@ sub isdir sub isfile { my($self)=@_; - return 1 if($self->state() == $STATE_FILE); - return 0; + return($self->state() == $STATE_FILE); } sub isvalid @@ -67,7 +80,7 @@ sub dest my($self, $mountpoint)=@_; if($self->state() == $STATE_FILE) { - return $self->filename($mountpoint, $self); + return $self->filename($mountpoint); } return "ERROR"; #should never happen? } @@ -82,41 +95,40 @@ sub dirents # print "DIRENTS: FILE: $self->{path}\n"; if($state==$STATE_ALL) { - @dents=($self->artists(), $PATH_ALLTRACKS, $PATH_NOARTIST); + @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists()); } - elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) + elsif($state==$STATE_TAG) { - my $tag=$self->tail(); - if($state==$STATE_TAG && - defined($tag) && - ref($tag) eq "ID3FS::PathElement::Tag" && - $self->{db}->tag_has_values($tag->{id})) + my $tail=$self->tail(); + if($self->is($TYPE_TAG, $tail) && $self->{db}->tag_has_values($tail->id())) { @dents=$self->tags(); } else { - @dents=($self->artists(), qw(AND OR), $PATH_ALLTRACKS, $PATH_NOARTIST); + if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth})) + { + @dents=qw(AND OR); + } + push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists()); } } elsif($state==$STATE_BOOLEAN) { - @dents=$self->tags(); my $parent=$self->tail(); - unless(defined($parent) && - ref($parent) eq "ID3FS::PathElement::Boolean" && - $parent->{name} eq "NOT") + unless($self->is($TYPE_BOOL, $parent) && $parent->name() eq "NOT") { - push(@dents, "NOT"); + @dents=("NOT"); } + push(@dents,$self->tags()); } elsif($state==$STATE_ROOT) { - @dents=($self->tags(), qw(ALL NOT)); + @dents=(qw(ALL NOT), $self->tags()); } elsif($state==$STATE_ALBUMS) { - @dents=($self->albums(), $PATH_ALLTRACKS, $PATH_NOALBUM); + @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums()); } elsif($state==$STATE_TRACKLIST) { @@ -124,23 +136,23 @@ sub dirents } else { - print "DIRENTS: UNHANDLED STATE: $state\n"; + print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n"; } return(\@dents, \@fents); } +# State Machine Of Doom sub parse { my($self)=@_; + $self->state($STATE_ROOT); + return if($self->{path} eq "/"); @{$self->{components}}=split(/\//, $self->{path}); shift @{$self->{components}}; # drop empty field before leading / # print "PATH: $self->{path}\n"; - $self->state($STATE_ROOT); - return if($self->{path} eq "/"); my @parts=@{$self->{components}}; my($tag, $tagval); $self->{elements}=[]; - $self->{bare_not}=0; $self->{in_all}=0; my $root_not=0; my $tags_seen=0; @@ -164,12 +176,12 @@ sub parse elsif($name eq "NOT") { $root_not=1; - push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); $self->state($STATE_BOOLEAN); } else { - $tag=ID3FS::PathElement::Tag->new($self->{db}, $name); + $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -182,20 +194,16 @@ sub parse } } } - elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) + elsif($state==$STATE_TAG) { -# print "SM: TAG/TAGVAL($state): $name\n"; my $tag=$self->tail(); - if($state==$STATE_TAG && - defined($tag) && - ref($tag) eq "ID3FS::PathElement::Tag" && - $self->{db}->tag_has_values($tag->{id})) +# print "SM: TAG/TAGVAL($state): $name\n"; + if($self->is($TYPE_TAG, $tag) && $self->{db}->tag_has_values($tag->id())) { -# print "Parsing: parent: $tag->{id}\n"; - my $tagval=ID3FS::PathElement::Tag->new($self->{db}, $name, $tag->{id}); +# print "Parsing: parent: $tag->id()\n"; + my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id()); if(defined($tagval)) { - $self->state($STATE_TAGVAL); # stay in tag state push(@{$self->{elements}}, $tagval); } @@ -215,16 +223,16 @@ sub parse elsif($name eq "AND") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } elsif($name eq "OR") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } else { - my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name); + my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -241,20 +249,19 @@ sub parse # print "SM: BOOLEAN: $name\n"; my $parent=$self->tail(); my $allownot=1; - if(defined($parent) && - ref($parent) eq "ID3FS::PathElement::Boolean" && - $parent->{name} eq "NOT") + if($self->is($TYPE_BOOL, $parent) && + $parent->name() eq "NOT") { $allownot=0; } if($allownot && $name eq "NOT") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } else { - my $tag=ID3FS::PathElement::Tag->new($self->{db}, $name); + my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -280,7 +287,7 @@ sub parse } else { - my $album=ID3FS::PathElement::Album->new($self->{db}, $name); + my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name); if($album) { push(@{$self->{elements}}, $album); @@ -295,7 +302,7 @@ sub parse elsif($state==$STATE_TRACKLIST) { # print "SM: TRACKLIST: $name\n"; - my $track=ID3FS::PathElement::File->new($self->{db}, $name); + my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name); if($track) { push(@{$self->{elements}}, $track); @@ -320,12 +327,11 @@ sub parse } elsif($name eq $PATH_NOARTIST) { - # FIXME $self->state($STATE_TRACKLIST); } else { - my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name); + my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -344,59 +350,114 @@ sub parse } } - if($root_not && ($tags_seen < 2)) - { - $self->{bare_not}=1; - } - - # remove trailing boolean my @elements=@{$self->{elements}}; - while(@elements && ref($elements[$#elements]) eq "ID3FS::PathElement::Boolean") + # remove trailing boolean + while(@elements && $self->is($TYPE_BOOL, $elements[$#elements])) { pop @elements; } +# print "\nELEMENTS: ", join(' ', map { $_->name(); } @elements), "\n"; + my @joins=$self->number_joins(@elements); + @joins=qw(INNER) unless(@joins); + $self->{joins}=\@joins; +# print "AFTER: ", join(' ', map { $_->name() . "(" . $_->{table} . ")"; } @elements), "\n"; +# print "JOINS: ", join(', ', @joins), "\n"; # sort elements by precedence @elements=$self->sort_elements(@elements); $self->{tagtree}=$self->elements_to_tree(\@elements); - if($self->{tagtree}) +} + +sub number_joins +{ + my($self, @elements)=@_; + my @joins=("INNER"); + my $table=1; + my $nextjoin=undef; + my $lastop=undef; + return (@joins) unless(@elements); + while(my $node=shift @elements) { - ($self->{sqlconditions}, - $self->{joins}) = $self->{tagtree}->to_sql(); -# print "TREE: ", $self->{tagtree}->print(), "\n"; -# print("SQL CONDITION(", scalar(@{$self->{joins}}), "): ", -# $self->{sqlconditions}, "\n"); -# use Data::Dumper; -# print Dumper $self->{tagtree}; + if($node->type() == $TYPE_BOOL) + { + my $op=$node->name(); + if($op eq "AND") + { + $nextjoin="INNER"; + } + elsif($op eq "NOT") + { + $nextjoin="LEFT"; + } + elsif($op eq "OR") + { + # NOT/foo/OR needs an extra join + $nextjoin="INNER" if($lastop && $lastop eq "NOT"); + } + $lastop=$op; + } + else + { + if(@elements) + { + # if tag has a value, eat the tag, shifting to the value + if($elements[0]->type() == $TYPE_TAG) + { + $node->hasvals(1); + $node=shift(@elements); + } + else + { + $node->hasvals(0); + } + } + elsif($self->{db}->tag_has_values($node->id())) + { + # if the expression ends in a tag that has a value + # (ie we have the tag and want the value) + # use an INNER join even if we were in a NOT + $nextjoin="INNER" if($nextjoin); + } + if($nextjoin) + { + $table++; + push(@joins, $nextjoin); + $nextjoin=undef; + } + $node->table($table); + } } + return @joins; } sub state { my($self, $newstate)=@_; - $self->{state}=$newstate if(defined($newstate)); + if(defined($newstate)) + { + $self->{state}=$newstate; + $self->{curtagdepth}++ if($newstate == $STATE_TAG); + } return $self->{state}; } +# link up precedence-sorted list into a binary tree sub elements_to_tree { my($self, $elements)=@_; return undef unless(@$elements); my ($left, $right, $op)=(undef, undef, undef); - my $thing=pop @$elements; - if(ref($thing) eq "ID3FS::PathElement::Boolean") + my $node=pop @$elements; + if($self->is($TYPE_BOOL, $node)) { - my $op=$thing; $right=$self->elements_to_tree($elements); - if($op->{name} ne "NOT") + if($node->name() ne "NOT") { $left=$self->elements_to_tree($elements); } - return ID3FS::Path::Node->new($left, $op, $right); - } - else - { - return ID3FS::Path::Node->new($thing); + $node->left($left); + $node->right($right); } + return $node; } # Dijkstra's shunting-yard algorithm @@ -405,34 +466,31 @@ sub sort_elements my ($self, @input)=@_; my @opstack=(); my @output=(); -# print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n"; - while(my $thing = shift @input) + while(my $node = shift @input) { - if(ref($thing) eq "ID3FS::PathElement::Tag") + if($self->is($TYPE_TAG, $node)) { # Handle tag values by dropping parent - if(@input && ref($input[0]) eq "ID3FS::PathElement::Tag") + if(@input && $self->is($TYPE_TAG, $input[0])) { - $thing=shift @input; + $node=shift @input; } - push(@output, $thing); + push(@output, $node); } - elsif(ref($thing) eq "ID3FS::PathElement::Boolean") + elsif($self->is($TYPE_BOOL, $node)) { - # bool while(@opstack && - ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}})) + ($priorities{$node->name()} <= $priorities{$opstack[$#opstack]->name()})) { push(@output, pop(@opstack)); } - push(@opstack, $thing); + push(@opstack, $node); } } while(@opstack) { push(@output, pop(@opstack)); } -# print "STACK: ", join(', ', map { $_->{name}; } @output), "\n"; return @output; } @@ -443,13 +501,13 @@ sub used_tags return($self->{tagtree}->used_tags()); } -sub tag_has_values +sub expecting_values { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is($TYPE_TAG, $tail)) { - return($self->{db}->tag_has_values($tail->{id})); + return($self->{db}->tag_has_values($tail->id())); } } @@ -457,9 +515,9 @@ sub trailing_tag_id { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is($TYPE_TAG, $tail)) { - return($tail->{id}); + return($tail->id()); } return undef; } @@ -468,7 +526,7 @@ sub trailing_tag_parent { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is($TYPE_TAG, $tail)) { return($tail->{parents_id}); } @@ -481,6 +539,15 @@ sub tail return($self->{elements}->[$#{$self->{elements}}]); } +sub is +{ + my($self, $type, $node)=@_; + return 0 unless($node); + return 0 unless($node->type()); + return 1 if($type == $node->type()); + return 0; +} + # the one before last sub tail_parent { @@ -493,74 +560,42 @@ sub tail_parent sub tags { my($self)=@_; - if(!$self->{tagtree}) # / or /NOT # FIXME: /ALL too? + if(!$self->{tagtree}) # / or /NOT { my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';"; return($self->{db}->cmd_firstcol($sql)); } - my $hasvals=$self->tag_has_values(); - my $parent=$self->trailing_tag_parent(); -# print "THASVALS: $hasvals\n"; -# print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n"; - my @ids=(); - my $sql=("SELECT tags.name FROM (\n" . - $self->tags_subselect() . - ") AS subselect\n" . - "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" . - "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n"); - my @allused=$self->used_tags(); - my @used=grep { ref($_) ne "ARRAY"; } @allused; - my @used_with_vals=grep { ref($_) eq "ARRAY"; } @allused; -# print "tags(): USED: ", join(", ", @used), "\n"; -# print "tags(): USED_WITH_VALS: ", join(", ", map { "[".$_->[0]. ", ".$_->[1]."]";} @used_with_vals), "\n"; - my @orclauses=(); - my @andclauses=(); - my $id=$self->trailing_tag_id(); - if($hasvals) + my $sql="SELECT tags.name FROM "; + if($self->want_all_tags()) { -# print "HAS_VALUES\n"; - my @values=map { "'".$_->[1]."'"; } grep { $_->[0] == $id; } @used_with_vals; - my $clause="(tags.parents_id='$id'"; - if(@values) - { - $clause .= " AND tags.id NOT IN (" . join(', ', @values) . ")"; - } - $clause .= ")"; - push(@andclauses, $clause); + $sql .= "files_x_tags\n"; } else { -# print "HASNT VALUES\n";; - if(@used) - { - push(@andclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))"); - } - for my $pair (@used_with_vals) - { - push(@andclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))"); - } + $sql .= ("(\n" . + $self->tags_subselect() . + ") AS subselect\n" . + "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n"); } + $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n"; + my @andclauses=(); + my $id=$self->trailing_tag_id(); - my $parentclause= "(tags.parents_id='"; - if($hasvals) - { - $parentclause .= $id; - } - elsif($parent) - { - $parentclause .= $parent; - } - $parentclause .= "')"; + my $parentclause= "tags.parents_id='"; + $parentclause .= $id if($self->expecting_values()); + $parentclause .= "'"; push(@andclauses, $parentclause); - if(@orclauses) + my @used=$self->used_tags(); + if(@used) { - push(@andclauses, '( ' . join(' OR ', @orclauses) . ' )'); + push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")"); } if(@andclauses) { $sql .= "WHERE " . join(' AND ', @andclauses) . "\n"; } + $sql .= "GROUP BY tags.name;"; print "SQL(TAGS): $sql\n" if($self->{verbose}); my @tagnames=$self->{db}->cmd_firstcol($sql); @@ -568,16 +603,6 @@ sub tags return(@tagnames); } -sub tag_values -{ - my($self, $tagid)=@_; - my $sql=("SELECT DISTINCT name FROM tags\n" . - "WHERE parents_id=?"); - my @tags=$self->{db}->cmd_firstcol($sql, $tagid); - @tags=map { length($_) ? $_ : "NOVALUE"; } @tags; - return @tags; -} - sub artists { my($self)=@_; @@ -586,12 +611,8 @@ sub artists my $sql="SELECT DISTINCT name FROM artists WHERE name!='';"; return($self->{db}->cmd_firstcol($sql)); } - my @ids=(); - my $sql=("SELECT artists.name FROM (\n" . - $self->tags_subselect() . - ") AS subselect\n" . - "INNER JOIN files ON subselect.files_id=files.id\n" . - "INNER JOIN artists ON files.artists_id=artists.id\n" . + my $sql=$self->sql_start("artists.name"); + $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" . "WHERE artists.name != ''\n" . "GROUP BY artists.name;"); print "SQL(ARTISTS): $sql\n" if($self->{verbose}); @@ -603,29 +624,15 @@ sub artists sub albums { my($self)=@_; - my @ids=(); my $tail=$self->tail(); - # FIXME: rework PathElements - if(ref($tail) eq "ID3FS::PathElement::Artist") - { - return $self->artist_albums($tail->{id}); - } - my $sql; - if($self->{in_all}) + if($self->is($TYPE_ARTIST, $tail)) { - $sql="SELECT name FROM albums"; - } - else - { - $sql=("SELECT albums.name\n" . - "\tFROM (\n" . - $self->tags_subselect() . - "\t) AS subselect\n" . - "INNER JOIN files ON subselect.files_id=files.id\n" . - "INNER JOIN albums ON files.albums_id=albums.id\n" . - "WHERE albums.name != ''\n" . - "GROUP BY albums.name;"); + return $self->artist_albums($tail->id()); } + my $sql=$self->sql_start("albums.name"); + $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" . + "WHERE albums.name != ''\n" . + "GROUP BY albums.name;"); print "SQL(ALBUMS): \n$sql\n" if($self->{verbose}); my @names=$self->{db}->cmd_firstcol($sql); print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose}); @@ -635,18 +642,7 @@ sub albums sub artist_albums { my($self, $artist_id)=@_; - my $sql="SELECT albums.name FROM "; - if($self->{in_all}) - { - $sql .= "files\n"; - } - else - { - $sql .= ("(\n" . - $self->tags_subselect() . - ") AS subselect\n" . - "INNER JOIN files ON subselect.files_id=files.id\n"); - } + my $sql=$self->sql_start("albums.name"); $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" . "INNER JOIN artists ON artists.id=files.artists_id\n" . "WHERE artists.id=? and albums.name <> ''\n" . @@ -660,18 +656,7 @@ sub artist_albums sub artist_tracks { my($self, $artist_id)=@_; - my $sql="SELECT files.name FROM "; - if($self->{in_all}) - { - $sql .= "files\n"; - } - else - { - $sql .= ("(\n" . - $self->tags_subselect() . - "\t) AS subselect\n" . - "INNER JOIN files ON subselect.files_id=files.id\n"); - } + my $sql=$self->sql_start("files.name"); $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" . "INNER JOIN albums ON albums.id=files.albums_id\n" . "WHERE artists.id=? AND albums.name=''\n" . @@ -699,35 +684,23 @@ sub album_tracks sub tracks { my($self)=@_; - # FIXME: rework PathElements my $tail=$self->tail(); - if(ref($tail) eq "ID3FS::PathElement::Artist") + if($self->is($TYPE_ARTIST, $tail)) { - return $self->artist_tracks($tail->{id}); + return $self->artist_tracks($tail->id()); } - elsif(ref($tail) eq "ID3FS::PathElement::Album") + elsif($self->is($TYPE_ALBUM, $tail)) { my $artist_id=0; my $artist=$self->tail_parent(); - if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist")) + if($self->is($TYPE_ARTIST, $artist)) { # should always happen - $artist_id=$artist->{id}; + $artist_id=$artist->id(); } - return $self->album_tracks($artist_id, $tail->{id}); - } - my $sql="SELECT files.name FROM "; - if($self->{in_all}) - { - $sql .= "files\n"; - } - else - { - $sql .= ("(\n" . - $self->tags_subselect() . - ") AS subselect\n" . - "INNER JOIN files ON files.id=subselect.files_id\n"); + return $self->album_tracks($artist_id, $tail->id()); } + my $sql=$self->sql_start("files.name"); $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n"; if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST) { @@ -744,53 +717,32 @@ sub filename { my($self, $mountpoint)=@_; my $tail=$self->tail(); - if(ref($tail) eq "ID3FS::PathElement::File") + if($self->is($TYPE_FILE, $tail)) { - my $id=$tail->{id}; + my $id=$tail->id(); my $sql=("SELECT paths.name, files.name FROM files\n" . "INNER JOIN paths ON files.paths_id=paths.id\n" . "WHERE files.id=?\n" . "GROUP BY paths.name, files.name"); print "FILENAME SQL: $sql\n" if($self->{verbose}); my ($path, $name)=$self->{db}->cmd_onerow($sql, $id); - my $id3fs_path=join('/', map { $_->{name}; } @{$self->{elements}}); - return($self->{db}->relativise($path, $name, $mountpoint, $id3fs_path)); + return($self->{db}->relativise($path, $name, $mountpoint, $self->{path})); } - die("DB::filename: unhandled case\n"); #FIXME + # should never happen + return "ERROR"; } sub tags_subselect { my($self)=@_; - my $hasvals=$self->tag_has_values(); - # we need to specially handle a bare /NOT/tag with no other clauses, - # using a simple WHERE id !='tagid' instead of a LEFT JOIN - if($self->{bare_not}) - { - return $self->bare_not_subselect(); - } - if($self->{in_all}) - { - return "\tSELECT id FROM files AS files_id\n"; - } my $tree=$self->{tagtree}; - my $parent=$self->trailing_tag_parent(); - -# print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n"; -# print "TREE: ", $tree->print(), "\n"; - my $tag=undef; - if($hasvals) - { - $tag=$self->trailing_tag_id(); -# print "Trailing id: $tag\n"; - } - my ($sqlclause, @joins)=(undef, ()); - ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree); -# print "SQL(" . scalar(@joins) .": $sqlclause\n"; +# use Data::Dumper; +# print Dumper $tree; + my ($sqlclause, @joins)=$tree->to_sql() if($tree); my $sql="\tSELECT fxt1.files_id FROM tags t1"; my @crosses=(); my @inners=(); -# $joinsneeded++ if($tag); + @joins=@{$self->{joins}}; for(my $i=0; $i <= $#joins; $i++) { my $cnt=$i+1; @@ -807,178 +759,75 @@ sub tags_subselect $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses); $sql .= ("\n" . join("\n", @inners)) if(@inners); $sql .= "\n\tWHERE $sqlclause" if($sqlclause); -# if($tag) -# { -# $sql .= " AND t${joinsneeded}.parents_id='$tag'"; -# } $sql .= "\n\tGROUP BY fxt1.files_id\n"; return $sql; } -sub bare_not_subselect +sub sql_start { - my($self)=@_; - my @tags=grep { ref($_) eq "ID3FS::PathElement::Tag"; } @{$self->{elements}}; - my $sql=("\tSELECT f1.id AS files_id FROM files f1 WHERE f1.id NOT IN (\n" . - "\t\tSELECT fxt1.files_id FROM tags t1\n" . - "\t\tINNER JOIN files_x_tags fxt1 ON t1.id=fxt1.tags_id\n" . - "\t\tWHERE "); - if(scalar(@tags) > 1) + my($self, $tables)=@_; + my $sql="SELECT $tables FROM "; + if($self->{in_all}) { - $sql .= ("(t1.parents_id='" . $tags[0]->{id} . "' AND t1.id='" . - $tags[1]->{id} . "')"); + $sql .= "files\n"; } else { - $sql .= ("(t1.parents_id='' AND t1.id='" . $tags[0]->{id} . "')"); - } - $sql .= "\n\t\tGROUP BY fxt1.files_id\n\t)\n"; - return($sql); -} - -sub constraints_tag_list -{ - my($self, @constraints)=@_; - my $lasttag=undef; - my @tags=(); - my @tags_vals=(); - for my $constraint (@constraints) - { -# print ref($constraint), ": ", $constraint->{name}, "\n"; - if(ref($constraint) eq "ID3FS::PathElement::Tag") - { - if(defined($lasttag)) - { -# print "TAGVAL\n"; - push(@tags_vals, [$lasttag, $constraint->{id}]) if defined($constraint->{id}); - $lasttag=undef; - } - elsif($self->tag_has_values($constraint->{id})) - { -# print "HASVALUES\n"; - $lasttag=$constraint->{id} if defined($constraint->{id}); - } - else - { -# print "NOVALUES\n"; - push(@tags, $constraint->{id}) if(defined($constraint->{id})); - } - } - } - unless($self->{db}->{postgres}) - { - @tags=map{ "\"$_\""; } @tags; - @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals); - $lasttag="\"$lasttag\"" if defined($lasttag); - } - return(\@tags, \@tags_vals, $lasttag); -} - - -sub bare_tags -{ - my($self)=@_; - my $sql=("SELECT tags.name FROM tags\n" . - "WHERE tags.parents_id=''\n" . - "GROUP BY tags.name\n"); - my @names=$self->{db}->cmd_firstcol($sql); - return (@names); -} - -sub tags_with_values -{ - # FIXME: only shows one level of tag depth - my($self)=@_; - my $sql=("SELECT p.name, t.name FROM tags t\n" . - "INNER JOIN tags p ON t.parents_id=p.id\n" . - "GROUP BY p.name, t.name\n"); -# print "SQL: $sql\n"; - my $result=$self->{db}->cmd_rows($sql); - my $tags={}; - for my $pair (@$result) - { - push(@{$tags->{$pair->[0]}}, $pair->[1]); + $sql .= ("(\n" . + $self->tags_subselect() . + ") AS subselect\n" . + "INNER JOIN files ON subselect.files_id=files.id\n"); } - return $tags; + return $sql; } - +# we just filter $ALLTRACKS, $NOARTIST and $NOALBUM +# filtering tags properly requires up to four levels of recursion +# (tag/tagval/AND/NOT) and is too slow sub filter { my($self, @dirs)=@_; + return(@dirs) unless($ENABLE_FILTER); my $base=$self->{path}; my @outdirs=(); - # depth 4 to allow for tag/tagval/AND/NOT - my $maxdepth=4; for my $dir (@dirs) { -# print "\nFILTER (",$self->state(), "): $base / $dir\n"; - if($self->empty("$base/$dir", $maxdepth)) - { -# print "empty: $base / $dir\n"; - } - else - { -# print "non-empty, accepting: $base / $dir\n"; - push(@outdirs, $dir); - } + push(@outdirs, $dir) unless($self->empty("$base/$dir")); } return(@outdirs); } sub empty { - my($self, $dir, $maxdepth)=@_; - return 0 unless($maxdepth); -# print "testing($maxdepth): $dir\n"; - my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose}); -# print "PATH INVALID\n" unless($path->isvalid()); + my($self, $dir)=@_; + my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose}, + ($self->{maxtagdepth} - $self->{curtagdepth})); return 1 unless($path->isvalid()); my($subdirs,$subfiles)=$path->dirents(); -# print "SUBDENTS: ", join(", ", @$subdirs, @$subfiles), "\n"; -# print("SUBFILES: ", join(', ', @$subfiles), "\n") if(@$subfiles); - return 0 if(@$subfiles); - for my $subdir (@$subdirs) - { -# print "SUBSUB $dir/$subdir\n"; - if(1) #$self->dir_is_special($subdir)) - { - if($self->empty("$dir/$subdir", ($maxdepth-1))) - { -# print "EMPTY: $dir / $subdir\n"; - } - else - { -# print "NONEMPTY: $dir / $subdir\n"; - return 0; - } - } - else - { - return 0; - } -# return 0 if($self->nonempty("$dir/$subdir", ($maxdepth-1))); - } + return 0 if(@$subfiles || @$subdirs); return 1; } -sub tagname_has_values -{ - my($self, $name)=@_; - my $id=$self->{db}->lookup_id("tags", $name); - return 0 unless($id); - return $self->{db}->tag_has_values($id); - -} - -sub dir_is_special +# if path is .../OR/ or .../OR/NOT or .../AND/NOT +sub want_all_tags { - my($self, $dir)=@_; - if((grep { $_ eq $dir; } - qw(AND OR NOT), $PATH_ALLTRACKS, $PATH_NOARTIST, $PATH_NOALBUM) || - ($self->tagname_has_values($dir))) + my($self)=@_; + my $tail=$self->tail(); + return 0 unless($tail); + my $parent=$self->tail_parent(); + my $parent_valid = ($parent && $parent->type() == $TYPE_BOOL); + if($tail->type() == $TYPE_BOOL) + { + return 1 if($tail->name() eq "OR"); + return 0 unless($tail->name() eq "NOT"); + return 0 unless($parent_valid); + return 1 if($parent->name() eq "OR"); + return 1 if($parent->name() eq "AND"); + } + elsif($tail->type() == $TYPE_TAG) { - return 1; + return 0 unless($parent_valid); + return 1 if($parent->name() eq "NOT"); } return 0; }