X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FPath.pm;h=49472568a4584406f2f8a43eb4dbbc09fcd4b71e;hb=1c7e55d34956f90d235632995acf1238878db2a3;hp=5555bb26e9d40adc20de536d4a47dad2aa00a5c2;hpb=cef04f30604060d77cc20fd294625bbf1accc5c1;p=id3fs.git diff --git a/lib/ID3FS/Path.pm b/lib/ID3FS/Path.pm index 5555bb2..4947256 100644 --- a/lib/ID3FS/Path.pm +++ b/lib/ID3FS/Path.pm @@ -2,12 +2,6 @@ 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, @@ -16,6 +10,10 @@ our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL, our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 ); +our $PATH_ALLTRACKS="TRACKS"; +our $PATH_NOARTIST="NOARTIST"; +our $PATH_NOALBUM="NOALBUM"; + sub new { my $proto=shift; @@ -26,6 +24,11 @@ sub new $self->{elements}=[]; $self->{db}=shift; $self->{path}=shift; + $self->{verbose}=shift; + $self->{maxtagdepth}=shift; + $self->{curtagdepth}=0; + $self->{path} =~ s/\/\//\//g; # drop doubled slashes + $self->parse(); # print "STATE: ", $self->state(), "\n"; return $self; @@ -45,8 +48,7 @@ sub isdir sub isfile { my($self)=@_; - return 1 if($self->state() == $STATE_FILE); - return 0; + return($self->state() == $STATE_FILE); } sub isvalid @@ -60,7 +62,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? } @@ -75,33 +77,34 @@ sub dirents # print "DIRENTS: FILE: $self->{path}\n"; if($state==$STATE_ALL) { - push(@dents, qw(TRACKS NOARTIST), $self->artists()); + @dents=($PATH_ALLTRACKS, $PATH_NOARTIST, $self->artists()); } elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) { - my $tag=$self->tail(); - if($state==$STATE_TAG && - defined($tag) && - ref($tag) eq "ID3FS::PathElement::Tag" && - $self->{db}->tag_has_values($tag->{id})) + if($state==$STATE_TAG && $self->at("tag") && + $self->{db}->tag_has_values($self->tail()->id())) { @dents=$self->tags(); } else { - push(@dents, qw(AND OR TRACKS NOARTIST), $self->artists()); + if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth})) + { + @dents=qw(AND OR); + } + push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST)); + push(@dents, $self->artists()); } } elsif($state==$STATE_BOOLEAN) { my $parent=$self->tail(); - unless(defined($parent) && - ref($parent) eq "ID3FS::PathElement::Boolean" && + unless($self->is("boolean", $parent) && $parent->{name} eq "NOT") { - push(@dents, "NOT"); + @dents=("NOT"); } - push(@dents, $self->tags()); + push(@dents,$self->tags()); } elsif($state==$STATE_ROOT) { @@ -109,7 +112,7 @@ sub dirents } elsif($state==$STATE_ALBUMS) { - @dents=(qw(TRACKS NOALBUM),$self->albums()); + @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOALBUM), $self->albums()); } elsif($state==$STATE_TRACKLIST) { @@ -157,12 +160,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}, "boolean", $name)); $self->state($STATE_BOOLEAN); } else { - $tag=ID3FS::PathElement::Tag->new($self->{db}, $name); + $tag=ID3FS::Path::Node->new($self->{db},"tag", $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -177,15 +180,13 @@ sub parse } elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) { -# 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($state==$STATE_TAG && $self->is("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}, "tag", $name, $tag->id()); if(defined($tagval)) { $self->state($STATE_TAGVAL); @@ -197,27 +198,27 @@ sub parse $self->state($STATE_INVALID); } } - elsif($name eq "TRACKS") + elsif($name eq $PATH_ALLTRACKS) { $self->state($STATE_TRACKLIST); } - elsif($name eq "NOARTIST") + elsif($name eq $PATH_NOARTIST) { $self->state($STATE_TRACKLIST); } 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}, "boolean", $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}, "boolean", $name)); } else { - my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name); + my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -234,8 +235,7 @@ sub parse # print "SM: BOOLEAN: $name\n"; my $parent=$self->tail(); my $allownot=1; - if(defined($parent) && - ref($parent) eq "ID3FS::PathElement::Boolean" && + if($self->is("boolean", $parent) && $parent->{name} eq "NOT") { $allownot=0; @@ -243,11 +243,11 @@ sub parse 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}, "boolean", $name)); } else { - my $tag=ID3FS::PathElement::Tag->new($self->{db}, $name); + my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -263,17 +263,17 @@ sub parse elsif($state==$STATE_ALBUMS) { # print "SM: ALBUM: $name\n"; - if($name eq "TRACKS") + if($name eq $PATH_ALLTRACKS) { $self->state($STATE_TRACKLIST); } - elsif($name eq "NOALBUM") + elsif($name eq $PATH_NOALBUM) { $self->state($STATE_TRACKLIST); } else { - my $album=ID3FS::PathElement::Album->new($self->{db}, $name); + my $album=ID3FS::Path::Node->new($self->{db}, "album", $name); if($album) { push(@{$self->{elements}}, $album); @@ -288,7 +288,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}, "file", $name); if($track) { push(@{$self->{elements}}, $track); @@ -307,17 +307,17 @@ sub parse } elsif($state==$STATE_ALL) { - if($name eq "TRACKS") + if($name eq $PATH_ALLTRACKS) { $self->state($STATE_TRACKLIST); } - elsif($name eq "NOARTIST") + elsif($name eq $PATH_NOARTIST) { $self->state($STATE_TRACKLIST); } else { - my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name); + my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -343,7 +343,7 @@ sub parse # remove trailing boolean my @elements=@{$self->{elements}}; - while(@elements && ref($elements[$#elements]) eq "ID3FS::PathElement::Boolean") + while(@elements && $self->is("boolean", $elements[$#elements])) { pop @elements; } @@ -352,8 +352,8 @@ sub parse $self->{tagtree}=$self->elements_to_tree(\@elements); if($self->{tagtree}) { - ($self->{sqlconditions}, - $self->{joins}) = $self->{tagtree}->to_sql(); + my ($conditions, @joins)=$self->{tagtree}->to_sql(); +# print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n"; # print "TREE: ", $self->{tagtree}->print(), "\n"; # print("SQL CONDITION(", scalar(@{$self->{joins}}), "): ", # $self->{sqlconditions}, "\n"); @@ -365,7 +365,11 @@ sub parse 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}; } @@ -375,20 +379,17 @@ sub elements_to_tree return undef unless(@$elements); my ($left, $right, $op)=(undef, undef, undef); my $thing=pop @$elements; - if(ref($thing) eq "ID3FS::PathElement::Boolean") + if($self->is("boolean", $thing)) { - my $op=$thing; $right=$self->elements_to_tree($elements); - if($op->{name} ne "NOT") + if($thing->{name} ne "NOT") { $left=$self->elements_to_tree($elements); } - return ID3FS::Path::Node->new($left, $op, $right); - } - else - { - return ID3FS::Path::Node->new($thing); + $thing->left($left); + $thing->right($right); } + return $thing; } # Dijkstra's shunting-yard algorithm @@ -400,16 +401,16 @@ sub sort_elements # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n"; while(my $thing = shift @input) { - if(ref($thing) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $thing)) { # Handle tag values by dropping parent - if(@input && ref($input[0]) eq "ID3FS::PathElement::Tag") + if(@input && $self->is("tag", $input[0])) { $thing=shift @input; } push(@output, $thing); } - elsif(ref($thing) eq "ID3FS::PathElement::Boolean") + elsif($self->is("boolean", $thing)) { # bool while(@opstack && @@ -435,13 +436,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("tag", $tail)) { - return($self->{db}->tag_has_values($tail->{id})); + return($self->{db}->tag_has_values($tail->id())); } } @@ -449,9 +450,9 @@ sub trailing_tag_id { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $tail)) { - return($tail->{id}); + return($tail->id()); } return undef; } @@ -460,7 +461,7 @@ sub trailing_tag_parent { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $tail)) { return($tail->{parents_id}); } @@ -473,6 +474,21 @@ sub tail return($self->{elements}->[$#{$self->{elements}}]); } +sub at +{ + my($self, $type)=@_; + return($self->is($type, $self->tail())); +} + +sub is +{ + my($self, $type, $thing)=@_; + return 0 unless($thing); + return 0 unless($thing->type()); + return 1 if($type eq $thing->type()); + return 0; +} + # the one before last sub tail_parent { @@ -485,12 +501,12 @@ 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 $hasvals=$self->expecting_values(); my $parent=$self->trailing_tag_parent(); # print "THASVALS: $hasvals\n"; # print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n"; @@ -554,22 +570,12 @@ sub tags $sql .= "WHERE " . join(' AND ', @andclauses) . "\n"; } $sql .= "GROUP BY tags.name;"; - print "SQL(TAGS): $sql\n"; + print "SQL(TAGS): $sql\n" if($self->{verbose}); my @tagnames=$self->{db}->cmd_firstcol($sql); - print "SUBNAMES: ", join(', ', @tagnames), "\n"; + print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose}); 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)=@_; @@ -579,16 +585,13 @@ sub artists 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"; + print "SQL(ARTISTS): $sql\n" if($self->{verbose}); my @tagnames=$self->{db}->cmd_firstcol($sql); - print "ARTISTS: ", join(', ', @tagnames), "\n"; + print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose}); return(@tagnames); } @@ -597,80 +600,45 @@ sub albums my($self)=@_; my @ids=(); my $tail=$self->tail(); - # FIXME: rework PathElements - if(ref($tail) eq "ID3FS::PathElement::Artist") + if($self->is("artist", $tail)) { - return $self->artist_albums($tail->{id}); + return $self->artist_albums($tail->id()); } - my $sql; - if($self->{in_all}) - { - $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;"); - } - print "SQL(ALBUMS): \n$sql\n"; + 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"; + print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose}); return(@names); } 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" . "GROUP BY albums.name\n"); - print "ARTIST_ALBUMS SQL: $sql\n"; + print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose}); my @albums=$self->{db}->cmd_firstcol($sql, $artist_id); - print "ALBUMS: ", join(', ', @albums), "\n"; + print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose}); return(@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" . "GROUP BY files.name\n"); - print "ARTIST_TRACKS SQL: $sql\n"; + print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose}); my @names=$self->{db}->cmd_firstcol($sql, $artist_id); - print "ARTISTTRACKS: ", join(', ', @names), "\n"; + print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose}); return(@names); } @@ -682,53 +650,41 @@ sub album_tracks "INNER JOIN artists ON artists.id=files.artists_id\n" . "WHERE artists.id=? AND albums.id=?\n" . "GROUP BY files.name\n"); - print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n"; + print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose}); my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id); - print "TRACKS: ", join(', ', @names), "\n"; + print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose}); return(@names); } sub tracks { my($self)=@_; - # FIXME: rework PathElements my $tail=$self->tail(); - if(ref($tail) eq "ID3FS::PathElement::Artist") + if($self->is("artist", $tail)) { - return $self->artist_tracks($tail->{id}); + return $self->artist_tracks($tail->id()); } - elsif(ref($tail) eq "ID3FS::PathElement::Album") + elsif($self->is("album", $tail)) { my $artist_id=0; my $artist=$self->tail_parent(); - if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist")) + if($self->is("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 "NOARTIST") + if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST) { $sql .= "WHERE artists.name =''\n"; } $sql .= "GROUP BY files.name;"; - print "TRACKS SQL($self->{path}): $sql\n"; + print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose}); my @names=$self->{db}->cmd_firstcol($sql); - print "TRACKS: ", join(', ', @names), "\n"; + print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose}); return(@names); } @@ -736,25 +692,26 @@ sub filename { my($self, $mountpoint)=@_; my $tail=$self->tail(); - if(ref($tail) eq "ID3FS::PathElement::File") + if($self->is("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"; + 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)); } - die("DB::filename: unhandled case\n"); #FIXME + # should never happen + return "ERROR"; } sub tags_subselect { my($self)=@_; - my $hasvals=$self->tag_has_values(); + my $hasvals=$self->expecting_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}) @@ -766,6 +723,9 @@ sub tags_subselect return "\tSELECT id FROM files AS files_id\n"; } my $tree=$self->{tagtree}; + print "UNDEF!!\n" unless($self->{tagtree}); + use Data::Dumper; + print Dumper $tree; my $parent=$self->trailing_tag_parent(); # print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n"; @@ -777,7 +737,7 @@ sub tags_subselect # print "Trailing id: $tag\n"; } my ($sqlclause, @joins)=(undef, ()); - ($sqlclause, @joins) = $tree->to_sql() if($tree); + ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree); # print "SQL(" . scalar(@joins) .": $sqlclause\n"; my $sql="\tSELECT fxt1.files_id FROM tags t1"; my @crosses=(); @@ -810,127 +770,42 @@ sub tags_subselect sub bare_not_subselect { my($self)=@_; - my @tags=grep { ref($_) eq "ID3FS::PathElement::Tag"; } @{$self->{elements}}; + my @tags=grep { $self->is("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) { - $sql .= ("(t1.parents_id='" . $tags[0]->{id} . "' AND t1.id='" . - $tags[1]->{id} . "')"); + $sql .= ("(t1.parents_id='" . $tags[0]->id() . "' AND t1.id='" . + $tags[1]->id() . "')"); } else { - $sql .= ("(t1.parents_id='' AND t1.id='" . $tags[0]->{id} . "')"); + $sql .= ("(t1.parents_id='' AND t1.id='" . $tags[0]->id() . "')"); } $sql .= "\n\t\tGROUP BY fxt1.files_id\n\t)\n"; return($sql); } -sub tags_subselect_and_not +sub sql_start { - my($self,@constraints)=@_; - my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints); - my @tags=@$tags; - my @tags_vals=@$tags_vals;; - my $cnt=1; - my @andclauses=(); - my $sql=''; - for my $tag (@tags) - { - if($cnt == 1) - { - $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n"; - push(@andclauses, "\t\tfxt${cnt}.tags_id=$tag"); - } - else - { - $sql .= ("\tLEFT JOIN files_x_tags fxt$cnt ON fxt" . - ($cnt-1) . ".files_id=fxt${cnt}.files_id\n"); - push(@andclauses, "\t\tfxt${cnt}.tags_id IS NULL"); - } - print "AND: @andclauses\n"; - $cnt++; - } - if(@andclauses) - { - $sql .= "\tWHERE\n\t\t"; - $sql .= join(" AND\n\t\t", @andclauses) . "\n"; - } - $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n"; - return $sql; -} - - -sub tags_subselect_and -{ - my($self,@constraints)=@_; - my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints); - my @tags=@$tags; - my @tags_vals=@$tags_vals;; - my $cnt=1; - my @andclauses=(); - my $sql=''; - for my $tag (@tags) + my($self, $tables)=@_; + my $sql="SELECT $tables FROM "; + if($self->{in_all}) { - if($cnt == 1) - { - $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n"; - } - else - { - $sql .= ("\tINNER JOIN files_x_tags fxt$cnt ON fxt" . - ($cnt-1) . ".files_id=fxt${cnt}.files_id\n"); - } - push(@andclauses, "\t\tfxt${cnt}.tags_id = $tag"); - print "AND: @andclauses\n"; - $cnt++; + $sql .= "files\n"; } - if(@andclauses) + else { - $sql .= "\tWHERE\n\t\t"; - $sql .= join(" AND\n\t\t", @andclauses) . "\n"; + $sql .= ("(\n" . + $self->tags_subselect() . + ") AS subselect\n" . + "INNER JOIN files ON subselect.files_id=files.id\n"); } - $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n"; return $sql; } -sub tags_subselect_or -{ - my($self,@constraints)=@_; - my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints); - my @tags=@$tags; - my @tags_vals=@$tags_vals;; - - my $sql=("\tSELECT files_x_tags.files_id FROM tags t1\n" . - "\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n"); - my @orclauses=(); - my @andclauses=(); - # FIXME: and / or? - if(@tags) - { - push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )"); - push(@andclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )"); - } - for my $pair (@tags_vals) - { - my($tag, $val)=@$pair; - push(@orclauses, "( t1.parents_id=$tag AND t1.id=$val )"); - } -# push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )"); - if(@orclauses) - { - push(@andclauses, join("\n\t\tOR ", @orclauses)); - } - if(@andclauses) - { - $sql .= "\tWHERE\n\t\t"; - $sql .= join("\n\t\tAND ", @andclauses) . "\n"; - } - $sql .= "\tGROUP BY files_x_tags.files_id\n"; - return $sql; -} sub constraints_tag_list { @@ -941,61 +816,65 @@ sub constraints_tag_list for my $constraint (@constraints) { # print ref($constraint), ": ", $constraint->{name}, "\n"; - if(ref($constraint) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $constraint)) { if(defined($lasttag)) { # print "TAGVAL\n"; - push(@tags_vals, [$lasttag, $constraint->{id}]) if defined($constraint->{id}); + push(@tags_vals, [$lasttag, $constraint->id()]) if defined($constraint->id()); $lasttag=undef; } - elsif($self->tag_has_values($constraint->{id})) + elsif($self->tag_has_values($constraint->id())) { # print "HASVALUES\n"; - $lasttag=$constraint->{id} if defined($constraint->{id}); + $lasttag=$constraint->id() if defined($constraint->id()); } else { # print "NOVALUES\n"; - push(@tags, $constraint->{id}) if(defined($constraint->{id})); + 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); - } + @tags=map{ "\"$_\""; } @tags; + @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals); + $lasttag="\"$lasttag\"" if defined($lasttag); return(\@tags, \@tags_vals, $lasttag); } - -sub bare_tags +# 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)=@_; - 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); + my($self, @dirs)=@_; + my $base=$self->{path}; + my @outdirs=(); + for my $dir (@dirs) + { + print "\nFILTER (",$self->state(), "): $base / $dir\n"; + if($self->empty("$base/$dir")) + { + print "empty: $base / $dir\n"; + } + else + { + print "non-empty, accepting: $base / $dir\n"; + push(@outdirs, $dir); + } + } + return(@outdirs); } -sub tags_with_values +sub empty { - # 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]); - } - return $tags; + 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(); + return 0 if(@$subfiles || @$subdirs); + return 1; } 1;