From 833a8e7fc8037e933dd944a456e817beba1c4522 Mon Sep 17 00:00:00 2001 From: Ian Beckwith Date: Wed, 6 Oct 2010 18:50:37 +0100 Subject: [PATCH] actually implement AND --- lib/ID3FS/DB.pm | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm index 3c4f468..6424e8c 100644 --- a/lib/ID3FS/DB.pm +++ b/lib/ID3FS/DB.pm @@ -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) { -- 2.11.0