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";
},
});
}
{
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;
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};
});
}