From: Ian Beckwith Date: Wed, 6 Oct 2010 17:50:37 +0000 (+0100) Subject: actually implement AND X-Git-Tag: debian/1.0-1~124 X-Git-Url: http://erislabs.net/gitweb/?p=id3fs.git;a=commitdiff_plain;h=833a8e7fc8037e933dd944a456e817beba1c4522 actually implement AND --- 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) {