X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FPath.pm;h=4898aebf82eb77eb0e7f3d0e369c3de045cf0147;hb=a15e80578a92a5f74b7dc5c0aa00b5df6dc4bc69;hp=68c0ab89dd293ab388a93ee34ea7f543a8cbc9fa;hpb=97b9ef0d0c66f3c3d995d74b82f93613e2c81dd4;p=id3fs.git diff --git a/lib/ID3FS/Path.pm b/lib/ID3FS/Path.pm index 68c0ab8..4898aeb 100644 --- a/lib/ID3FS/Path.pm +++ b/lib/ID3FS/Path.pm @@ -132,7 +132,11 @@ sub parse my @parts=@{$self->{components}}; my($tag, $tagval); $self->{elements}=[]; - while(my $name=shift @parts) + $self->{bare_not}=0; + $self->{in_all}=0; + my $root_not=0; + my $tags_seen=0; + while(defined(my $name=shift @parts)) { # print "NAME: $name\n"; my $state=$self->state(); @@ -146,10 +150,12 @@ sub parse # print "SM: ROOT: $name\n"; if($name eq "ALL") { + $self->{in_all}=1; $self->state($STATE_ALL); } elsif($name eq "NOT") { + $root_not=1; push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name)); $self->state($STATE_BOOLEAN); } @@ -159,6 +165,7 @@ sub parse if($tag) { push(@{$self->{elements}}, $tag); + $tags_seen++; $self->state($STATE_TAG); } else @@ -243,6 +250,7 @@ sub parse if($tag) { push(@{$self->{elements}}, $tag); + $tags_seen++; $self->state($STATE_TAG); } else @@ -326,6 +334,13 @@ sub parse $self->state($STATE_INVALID); } } + + print "ROOT_NOT: $root_not TAGS_SEEN: $tags_seen\n"; + 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") @@ -338,9 +353,9 @@ sub parse if($self->{tagtree}) { ($self->{sqlconditions}, - $self->{andsneeded}) = $self->{tagtree}->to_sql(); + $self->{joins}) = $self->{tagtree}->to_sql(); # print "TREE: ", $self->{tagtree}->print(), "\n"; -# print("SQL CONDITION(", $self->{andsneeded}, "): ", +# print("SQL CONDITION(", scalar(@{$self->{joins}}), "): ", # $self->{sqlconditions}, "\n"); # use Data::Dumper; # print Dumper $self->{tagtree}; @@ -416,8 +431,7 @@ sub sort_elements sub used_tags { my($self)=@_; - print "TAGTREE UNDEF\n" unless(defined($self->{tagtree})); - return undef unless(defined($self->{tagtree})); + return() unless(defined($self->{tagtree})); return($self->{tagtree}->used_tags()); } @@ -471,7 +485,7 @@ sub tail_parent sub tags { my($self)=@_; - if(!@{$self->{elements}}) # / + if(!$self->{tagtree}) # / or /NOT { my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';"; return($self->{db}->cmd_firstcol($sql)); @@ -486,7 +500,7 @@ sub tags ") 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 @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"; @@ -540,7 +554,7 @@ sub tags $sql .= "WHERE " . join(' AND ', @andclauses) . "\n"; } $sql .= "GROUP BY tags.name;"; - print "SQL: $sql\n"; + print "SQL(TAGS): $sql\n"; my @tagnames=$self->{db}->cmd_firstcol($sql); print "SUBNAMES: ", join(', ', @tagnames), "\n"; return(@tagnames); @@ -572,7 +586,7 @@ sub artists "INNER JOIN artists ON files.artists_id=artists.id\n" . "WHERE artists.name != ''\n" . "GROUP BY artists.name;"); - print "SQL: $sql\n"; + print "SQL(ARTISTS): $sql\n"; my @tagnames=$self->{db}->cmd_firstcol($sql); print "ARTISTS: ", join(', ', @tagnames), "\n"; return(@tagnames); @@ -588,14 +602,22 @@ sub albums { return $self->artist_albums($tail->{id}); } - my $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;"); + 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 @names=$self->{db}->cmd_firstcol($sql); print "ALBUMS: ", join(', ', @names), "\n"; @@ -605,13 +627,21 @@ sub albums sub artist_albums { my($self, $artist_id)=@_; - my $sql=("SELECT albums.name FROM (\n" . - $self->tags_subselect() . - "\t) AS subselect\n" . - "INNER JOIN files ON subselect.files_id=files.id\n" . - "INNER JOIN albums ON albums.id=files.albums_id\n\t" . - "INNER JOIN artists ON artists.id=files.artists_id\n\t" . - "WHERE artists.id=? and albums.name <> ''\n\t" . + 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"); + } + $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"; my @albums=$self->{db}->cmd_firstcol($sql, $artist_id); @@ -622,13 +652,21 @@ sub artist_albums sub artist_tracks { my($self, $artist_id)=@_; - my $sql=("SELECT files.name FROM (\n" . - $self->tags_subselect() . - "\t) AS subselect\n" . - "INNER JOIN files ON subselect.files_id=files.id\n" . - "INNER JOIN artists ON artists.id=files.artists_id\n\t" . - "INNER JOIN albums ON albums.id=files.albums_id\n\t" . - "WHERE artists.id=? AND albums.name=''\n\t" . + 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"); + } + $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"; my @names=$self->{db}->cmd_firstcol($sql, $artist_id); @@ -639,10 +677,10 @@ sub artist_tracks sub album_tracks { my($self, $artist_id, $album_id)=@_; - my $sql=("SELECT files.name FROM files\n\t" . - "INNER JOIN albums ON albums.id=files.albums_id\n\t" . - "INNER JOIN artists ON artists.id=files.artists_id\n\t" . - "WHERE artists.id=? AND albums.id=?\n\t" . + my $sql=("SELECT files.name FROM files\n" . + "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.id=?\n" . "GROUP BY files.name\n"); print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n"; my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id); @@ -670,14 +708,25 @@ sub tracks } return $self->album_tracks($artist_id, $tail->{id}); } - - my $sql=("SELECT files.name\n" . - "\tFROM (\n" . - $self->tags_subselect() . - "\t) AS subselect\n" . - "INNER JOIN files ON files.id=subselect.files_id\n" . - "GROUP BY files.name;"); - print "SQL: $sql\n"; + 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"); + } + $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n"; + if($self->{components}->[$#{$self->{components}}] eq "NOARTIST") + { + $sql .= "WHERE artists.name =''\n"; + } + $sql .= "GROUP BY files.name;"; + print "TRACKS SQL($self->{path}): $sql\n"; my @names=$self->{db}->cmd_firstcol($sql); print "TRACKS: ", join(', ', @names), "\n"; return(@names); @@ -705,6 +754,16 @@ sub filename sub tags_subselect { my($self)=@_; + # 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 $hasvals=$self->tag_has_values(); my $parent=$self->trailing_tag_parent(); @@ -717,21 +776,23 @@ sub tags_subselect $tag=$self->trailing_tag_id(); # print "Trailing id: $tag\n"; } - my ($sqlclause, $joinsneeded)=(undef, 1); - ($sqlclause, $joinsneeded) = $tree->to_sql($tag) if($tree); -# print "SQL($joinsneeded): $sqlclause\n"; + my ($sqlclause, @joins)=(undef, ()); + ($sqlclause, @joins) = $tree->to_sql() if($tree); +# print "SQL(" . scalar(@joins) .": $sqlclause\n"; my $sql="\tSELECT fxt1.files_id FROM tags t1"; my @crosses=(); my @inners=(); # $joinsneeded++ if($tag); - for(my $i=1; $i <= $joinsneeded; $i++) + for(my $i=0; $i <= $#joins; $i++) { - my $inner=("\tINNER JOIN files_x_tags fxt$i ON " . - "t${i}.id=fxt${i}.tags_id"); - if($i > 1) + my $cnt=$i+1; + my $join=$joins[$i]; + my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " . + "t${cnt}.id=fxt${cnt}.tags_id"); + if($i > 0) { - push(@crosses, "CROSS JOIN tags t$i"); - $inner .= " AND fxt1.files_id=fxt${i}.files_id"; + push(@crosses, "CROSS JOIN tags t$cnt"); + $inner .= " AND fxt1.files_id=fxt${cnt}.files_id"; } push(@inners, $inner); } @@ -746,6 +807,27 @@ sub tags_subselect return $sql; } +sub bare_not_subselect +{ + 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) + { + $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 .= "\n\t\tGROUP BY fxt1.files_id\n\t)\n"; + return($sql); +} + sub tags_subselect_and_not { my($self,@constraints)=@_;