From: Norbert Buchmuller Date: Sun, 15 Feb 2009 15:39:26 +0000 (+0000) Subject: Applied patch from Zbigniew Lukasiak (with slight modifications) to accept "col ... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8a0d798a7a265eec82275bb0b67e620bb85d27ab;p=scpubgit%2FQ-Branch.git Applied patch from Zbigniew Lukasiak (with slight modifications) to accept "col => { -in => \[$sql, @bind] }". --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 46d2a4f..fcee0f9 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -697,25 +697,36 @@ sub _where_field_IN { # backwards compatibility : if scalar, force into an arrayref $vals = [$vals] if defined $vals && ! ref $vals; - ref $vals eq 'ARRAY' - or puke "special op 'in' requires an arrayref"; - my ($label) = $self->_convert($self->_quote($k)); my ($placeholder) = $self->_convert('?'); - my $and = $self->_sqlcase('and'); $op = $self->_sqlcase($op); - if (@$vals) { # nonempty list - my $placeholders = join ", ", (($placeholder) x @$vals); - my $sql = "$label $op ( $placeholders )"; - my @bind = $self->_bindtype($k, @$vals); + my ($sql, @bind) = $self->_SWITCH_refkind($vals, { + ARRAYREF => sub { # list of choices + if (@$vals) { # nonempty list + my $placeholders = join ", ", (($placeholder) x @$vals); + my $sql = "$label $op ( $placeholders )"; + my @bind = $self->_bindtype($k, @$vals); - return ($sql, @bind); - } - else { # empty list : some databases won't understand "IN ()", so DWIM - my $sql = ($op =~ /\bnot\b/i) ? $self->{sqltrue} : $self->{sqlfalse}; - return ($sql); - } + return ($sql, @bind); + } + else { # empty list : some databases won't understand "IN ()", so DWIM + my $sql = ($op =~ /\bnot\b/i) ? $self->{sqltrue} : $self->{sqlfalse}; + return ($sql); + } + }, + + ARRAYREFREF => sub { # literal SQL with bind + my ($sql, @bind) = @$$vals; + return ("$label $op ( $sql )", @bind); + }, + + FALLBACK => sub { + puke "special op 'in' requires an arrayref (or arrayref-ref)"; + }, + }); + + return ($sql, @bind); } diff --git a/t/07subqueries.t b/t/07subqueries.t index 85f973e..3c79842 100644 --- a/t/07subqueries.t +++ b/t/07subqueries.t @@ -77,6 +77,19 @@ push @tests, { bind => [10, 20, '%son%'], }; +#6 +($sub_stmt, @sub_bind) = ("SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?", + 100, "foo%"); +$where = { + foo => 1234, + bar => { -in => \[$sub_stmt => @sub_bind] }, + }; +push @tests, { + where => $where, + stmt => " WHERE ( bar IN (SELECT c1 FROM t1 WHERE c2 < ? AND c3 LIKE ?) AND foo = ? )", + bind => [100, "foo%", 1234], +}; + plan tests => scalar(@tests);