use tagtree in queries
[id3fs.git] / lib / ID3FS / DB.pm
index 50d9043..309df56 100644 (file)
@@ -192,38 +192,17 @@ sub tags
     my @ids=();
 
     my $sql=("SELECT t2.name FROM (\n" .
-            $self->tags_subselect(@constraints) .
+            $self->tags_subselect($path) .
             ") AS subselect\n" .
             "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
             "INNER JOIN tags t2 ON files_x_tags.tags_id=t2.id\n");
-    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
-    my @tags=@$tags;
-    my @tags_vals=@$tags_vals;;
-    my @orclauses=();
-    my @andclauses=();
-    use Data::Dumper;
-#    print "TAGS: ", Dumper \@tags;
-#    print "VALS: ", Dumper \@tags_vals;
-
-    push(@andclauses, "( t2.parents_id=" . (defined($parent) ? $parent : "''") . " )");
-    if(@tags)
-    {
-       push(@orclauses, "( t2.id NOT IN ( " . join(', ', @tags) ." ) )");
-    }
-    for my $pair (@tags_vals)
+    my @used=$path->used_tags();
+    print "tags(): USED: ", join(", ", @used), "\n";
+    if(@used)
     {
-       my($tag, $val)=@$pair;
-#      push(@orclauses, "( NOT ( t2.parents_id=$tag AND t2.id=$val ) )");
-       push(@andclauses, "( NOT ( t2.id=$tag ) )");
-    }
-    if(@orclauses)
-    {
-       push(@andclauses, join("\n\tOR ", @orclauses));
-    }
-    if(@andclauses)
-    {
-       $sql .= "\tWHERE\n\t\t";
-       $sql .= join("\n\tAND ", @andclauses) . "\n";
+       $sql .= "WHERE t2.id NOT IN (";
+       $sql .= join(', ', @used);
+       $sql .= ")\n";
     }
     $sql .= "GROUP BY t2.name;";
     print "SQL: $sql\n";
@@ -256,7 +235,7 @@ sub artists
     }
     my @ids=();
     my $sql=("SELECT artists.name FROM (\n" .
-            $self->tags_subselect(@constraints) .
+            $self->tags_subselect($path) .
             ") AS subselect\n" .
             "INNER JOIN files ON subselect.files_id=files.id\n" .
             "INNER JOIN artists ON files.artists_id=artists.id\n" .
@@ -280,7 +259,7 @@ sub albums
     }
     my $sql=("SELECT albums.name\n" .
             "\tFROM (\n" .
-            $self->tags_subselect(@constraints) .
+            $self->tags_subselect($path) .
             "\t) AS subselect\n" .
             "INNER JOIN files ON subselect.files_id=files.id\n" .
             "INNER JOIN albums ON files.albums_id=albums.id\n" .
@@ -297,7 +276,7 @@ sub artist_albums
     my($self, $artist_id, $path)=@_;
     my @constraints=@{$path->{elements}};
     my $sql=("SELECT albums.name FROM (\n" .
-            $self->tags_subselect(@constraints) .
+            $self->tags_subselect($path) .
             "\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" .
@@ -316,7 +295,7 @@ sub artist_tracks
     my($self, $artist_id, $path)=@_;
     my @constraints=@{$path->{elements}};
     my $sql=("SELECT files.name FROM (\n" .
-            $self->tags_subselect(@constraints) .
+            $self->tags_subselect($path) .
             "\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" .
@@ -368,7 +347,7 @@ sub tracks
 
     my $sql=("SELECT files.name\n" .
             "\tFROM (\n" .
-            $self->tags_subselect(@constraints) .
+            $self->tags_subselect($path) .
             "\t) AS subselect\n" .
             "INNER JOIN files ON files.id=subselect.files_id\n" .
             "GROUP BY files.name;");
@@ -400,10 +379,21 @@ sub filename
 
 sub tags_subselect
 {
-    return shift->tags_subselect_and(@_);
+    my($self, $path)=@_;
+    my $tree=$path->{tagtree};
+    my ($sqlclause, $joinsneeded)=$tree->to_sql();
+    print "SQL($joinsneeded): $sqlclause\n";
+    my $sql="\tSELECT fxt1.files_id FROM files_x_tags fxt1\n";
+    for(my $i=2; $i <= $joinsneeded; $i++)
+    {
+       $sql .= ("\tINNER JOIN files_x_tags fxt$i ON " .
+                "fxt1.files_id=fxt${i}.files_id\n");
+    }
+    $sql .= "\tWHERE $sqlclause\n";
+    $sql .= "\tGROUP BY fxt${joinsneeded}.files_id\n";
+    return $sql;
 }
 
-
 sub tags_subselect_and_not
 {
     my($self,@constraints)=@_;