From: Nigel Metheringham Date: Fri, 4 Sep 2009 14:34:55 +0000 (+0000) Subject: Extended -bool handling to handle more types of parameters X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ef03f1bca876c331c55c602cc941e2e76ecca24b;p=scpubgit%2FQ-Branch.git Extended -bool handling to handle more types of parameters --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 5936443..3ad246b 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -553,14 +553,35 @@ sub _where_op_NEST { sub _where_op_BOOL { my ($self, $op, $v) = @_; - my $prefix = ($op =~ /\bnot\b/i) ? 'NOT ' : ''; + my ( $prefix, $suffix ) = ( $op =~ /\bnot\b/i ) + ? ( '(NOT ', ')' ) + : ( '', '' ); $self->_SWITCH_refkind($v, { + ARRAYREF => sub { + my ( $sql, @bind ) = $self->_where_ARRAYREF($v, ''); + return ( ($prefix . $sql . $suffix), @bind ); + }, + + ARRAYREFREF => sub { + my ( $sql, @bind ) = @{ ${$v} }; + return ( ($prefix . $sql . $suffix), @bind ); + }, + + HASHREF => sub { + my ( $sql, @bind ) = $self->_where_HASHREF($v); + return ( ($prefix . $sql . $suffix), @bind ); + }, + SCALARREF => sub { # literal SQL - return ($prefix . $$v); + return ($prefix . $$v . $suffix); }, SCALAR => sub { # interpreted as SQL column - return ($prefix . $self->_convert($self->_quote($v))); + return ($prefix . $self->_convert($self->_quote($v)) . $suffix); + }, + + UNDEF => sub { + puke "-$op => undef not supported"; }, }); } diff --git a/t/02where.t b/t/02where.t index c612c49..774fc80 100644 --- a/t/02where.t +++ b/t/02where.t @@ -255,16 +255,52 @@ my @handle_tests = ( { where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] }, - stmt => " WHERE NOT foo AND NOT bar", + stmt => " WHERE (NOT foo) AND (NOT bar)", bind => [], }, { where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] }, - stmt => " WHERE NOT foo OR NOT bar", + stmt => " WHERE (NOT foo) OR (NOT bar)", bind => [], }, + { + where => { -bool => \['function(?)', 20] }, + stmt => " WHERE function(?)", + bind => [20], + }, + + { + where => { -not_bool => \['function(?)', 20] }, + stmt => " WHERE NOT function(?)", + bind => [20], + }, + + { + where => { -bool => { a => 1, b => 2} }, + stmt => " WHERE a = ? AND b = ?", + bind => [1, 2], + }, + + { + where => { -bool => [ a => 1, b => 2] }, + stmt => " WHERE a = ? OR b = ?", + bind => [1, 2], + }, + + { + where => { -not_bool => { a => 1, b => 2} }, + stmt => " WHERE NOT (a = ? AND b = ?)", + bind => [1, 2], + }, + + { + where => { -not_bool => [ a => 1, b => 2] }, + stmt => " WHERE NOT ( a = ? OR b = ? )", + bind => [1, 2], + }, + ); plan tests => ( @handle_tests * 2 ) + 1; @@ -275,7 +311,7 @@ for my $case (@handle_tests) { my($stmt, @bind); lives_ok (sub { ($stmt, @bind) = $sql->where($case->{where}, $case->{order}); - is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind}) + is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind}, $stmt) || diag "Search term:\n" . Dumper $case->{where}; }); }