actually implement AND
authorIan Beckwith <ianb@erislabs.net>
Wed, 6 Oct 2010 17:50:37 +0000 (18:50 +0100)
committerIan Beckwith <ianb@erislabs.net>
Wed, 6 Oct 2010 17:50:37 +0000 (18:50 +0100)
lib/ID3FS/DB.pm

index 3c4f468..6424e8c 100644 (file)
@@ -393,6 +393,44 @@ sub filename
 
 sub tags_subselect
 {
+    return shift->tags_subselect_and(@_);
+}
+
+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)
+    {
+       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++;
+    }
+    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_or
+{
     my($self,@constraints)=@_;
     my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
     my @tags=@$tags;
@@ -402,10 +440,11 @@ sub tags_subselect
             "\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n");
     my @orclauses=();
     my @andclauses=();
+    # FIXME: and / or?
     if(@tags)
     {
-       push(@orclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
-       push(@orclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )");
+       push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
+       push(@andclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )");
     }
     for my $pair (@tags_vals)
     {