use list of join types instead of count
[id3fs.git] / lib / ID3FS / Path.pm
index 0559463..d3a99a4 100644 (file)
@@ -12,7 +12,7 @@ use ID3FS::Path::Node;
 
 our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL,
      $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST,
-     $STATE_FILE)=(0..7);
+     $STATE_FILE, $STATE_ALL)=(0..8);
 
 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
 
@@ -72,7 +72,11 @@ sub dirents
     my $state=$self->state();
 #    print "DIRENTS: STATE: $state\n";
 #    print "DIRENTS: FILE: $self->{path}\n";
-    if($state==$STATE_TAG || $state==$STATE_TAGVAL)
+    if($state==$STATE_ALL)
+    {
+       push(@dents, qw(TRACKS NOARTIST), $self->artists());
+    }
+    elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
     {
        my $tag=$self->tail();
        if($state==$STATE_TAG &&
@@ -84,8 +88,7 @@ sub dirents
        }
        else
        {
-           @dents=(qw(AND OR TRACKS NOARTIST),
-                   $self->artists());
+           push(@dents, qw(AND OR TRACKS NOARTIST), $self->artists());
        }
     }
     elsif($state==$STATE_BOOLEAN)
@@ -129,7 +132,7 @@ sub parse
     my @parts=@{$self->{components}};
     my($tag, $tagval);
     $self->{elements}=[];
-    while(my $name=shift @parts)
+    while(defined(my $name=shift @parts))
     {
 #      print "NAME: $name\n";
        my $state=$self->state();
@@ -143,7 +146,7 @@ sub parse
 #          print "SM: ROOT: $name\n";
            if($name eq "ALL")
            {
-               $self->state($STATE_TAG);
+               $self->state($STATE_ALL);
            }
            elsif($name eq "NOT")
            {
@@ -293,6 +296,30 @@ sub parse
            # Can't have anything after a filename
            $self->state($STATE_INVALID);
        }
+       elsif($state==$STATE_ALL)
+       {
+           if($name eq "TRACKS")
+           {
+               $self->state($STATE_TRACKLIST);
+           }
+           elsif($name eq "NOARTIST")
+           {
+               $self->state($STATE_TRACKLIST);
+           }
+           else
+           {
+               my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name);
+               if($artist)
+               {
+                   push(@{$self->{elements}}, $artist);
+                   $self->state($STATE_ALBUMS);
+               }
+               else
+               {
+                   $self->state($STATE_INVALID);
+               }
+           }
+       }
        else
        {
            print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
@@ -311,9 +338,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};
@@ -477,18 +504,18 @@ sub tags
            $clause .= " AND tags.id NOT IN (" . join(', ', @values) . ")";
        }
        $clause .= ")";
-       push(@orclauses, $clause);
+       push(@andclauses, $clause);
     }
     else
     {
 #      print "HASNT VALUES\n";;
        if(@used)
        {
-           push(@orclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))");
+           push(@andclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))");
        }
        for my $pair (@used_with_vals)
        {
-           push(@orclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))");
+           push(@andclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))");
        }
     }
 
@@ -513,7 +540,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);
@@ -534,7 +561,7 @@ sub artists
     my($self)=@_;
     if(!@{$self->{elements}}) # /ALL
     {
-       my $sql="SELECT DISTINCT name FROM artists;";
+       my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
        return($self->{db}->cmd_firstcol($sql));
     }
     my @ids=();
@@ -543,8 +570,9 @@ sub artists
             ") AS subselect\n" .
             "INNER JOIN files ON subselect.files_id=files.id\n" .
             "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);
@@ -566,6 +594,7 @@ sub albums
             "\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);
@@ -597,9 +626,9 @@ sub artist_tracks
             $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" .
+            "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);
@@ -641,14 +670,18 @@ 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";
+            "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";
     my @names=$self->{db}->cmd_firstcol($sql);
     print "TRACKS: ", join(', ', @names), "\n";
     return(@names);
@@ -688,21 +721,25 @@ 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($tag) if($tree);
+    @joins=("INNER") unless(@joins);
+    print "JOINS: ", scalar(@joins), "\n";
+#    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);
     }
@@ -887,11 +924,4 @@ sub tags_with_values
     return $tags;
 }
 
-sub lookup_id
-{
-    my($self, $table, $name)=@_;
-    my($id)=$self->{db}->cmd_onerow("SELECT id FROM $table where name=?", $name);
-    return $id;
-}
-
 1;