/ALL fixes
authorIan Beckwith <ianb@erislabs.net>
Fri, 15 Oct 2010 00:51:16 +0000 (01:51 +0100)
committerIan Beckwith <ianb@erislabs.net>
Fri, 15 Oct 2010 00:51:16 +0000 (01:51 +0100)
lib/ID3FS/Path.pm

index 39aab39..4898aeb 100644 (file)
@@ -133,6 +133,7 @@ sub parse
     my($tag, $tagval);
     $self->{elements}=[];
     $self->{bare_not}=0;
+    $self->{in_all}=0;
     my $root_not=0;
     my $tags_seen=0;
     while(defined(my $name=shift @parts))
@@ -149,6 +150,7 @@ sub parse
 #          print "SM: ROOT: $name\n";
            if($name eq "ALL")
            {
+               $self->{in_all}=1;
                $self->state($STATE_ALL);
            }
            elsif($name eq "NOT")
@@ -600,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";
@@ -617,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);
@@ -634,11 +652,19 @@ 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" .
+    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");
@@ -651,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);
@@ -682,18 +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" .
-            "INNER JOIN artists ON files.artists_id=artists.id\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: $sql\n";
+    print "TRACKS SQL($self->{path}): $sql\n";
     my @names=$self->{db}->cmd_firstcol($sql);
     print "TRACKS: ", join(', ', @names), "\n";
     return(@names);
@@ -727,6 +760,10 @@ sub tags_subselect
     {
        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();