foreach ( @{$ast->{args}} ) {
croak "invalid component in where clause: $_" unless is_HashAST($_);
- if ($_->{-type} eq 'expr' && $_->{op} =~ /^-(and|or)$/) {
+ if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
my $sub_prio = $SQL::Abstract::PRIO{$1};
if ($sub_prio <= $prio) {
return $code->($self, $ast);
}
- croak "'$op' is not a valid clause in a where AST"
+ croak "'$op' is not a valid AST type in an expression"
if $op =~ /^-/;
use Devel::PartialDump qw/dump/;
- croak "'$op' is not a valid operator in " . dump($ast);
+ croak "'$op' is not a valid AST type in " . dump($ast);
}
method _in(HashAST $ast) {
- my ($field,$values) = @{$ast->{args}};
+ my ($field,@values) = @{$ast->{args}};
- my $not = ($ast->{op} =~ /^-not/) ? " NOT" : "";
+ my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
- return $self->_false if !defined $values || @$values == 0;
+ return $self->_false unless @values;
return $self->_where_component($field) .
- $not.
+ $not .
" IN (" .
- join(", ", map { $self->dispatch($_) } @$values ) .
+ join(", ", map { $self->dispatch($_) } @values ) .
")";
}
use strict;
use warnings;
-use Test::More tests => 12;
+use Test::More tests => 13;
use Test::Differences;
use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
]
}
), "me.id > ?",
- "simple where clause";
-
-is $sqla->dispatch(
- { -type => 'expr', op => 'in', args => [ ] }
-), "0 = 1", "emtpy -in";
-
-is $sqla->dispatch(
- { -type => 'expr',
- op => 'in',
- args => [ { -type => 'name', args => ['foo'] } ],
- }
-), "0 = 1", "emtpy -in";
+ "simple expr clause";
is $sqla->dispatch(
{ -type => 'expr',
is $sqla->dispatch(
{ -type => 'expr', op => 'or', args => $cols }
), "me.id > ? OR me.name = ?",
- "where clause (simple or)";
+ "expr clause (simple or)";
is $sqla->dispatch(
]
}
), "me.name = ? OR me.id > ? OR me.name = ?",
- "where clause (nested or)";
+ "expr clause (nested or)";
is $sqla->dispatch(
{ -type => 'expr', op => 'or',
]
}
), "me.name = ? OR me.id > ? AND me.name = ?",
- "where clause (inner and)";
+ "expr clause (inner and)";
-__END__
is $sqla->dispatch(
- [ -where => -and =>
- [ '==', [-name => qw/me id/], [-value => 500 ] ],
- [ -and =>
- [ '>', [-name => qw/me name/], [-value => '200' ] ],
- [ '<', [-name => qw/me name/], [-value => '100' ] ]
- ]
- ]
-), "WHERE me.id = ? AND me.name > ? AND me.name < ?",
- "where clause (nested and)";
+ { -type => 'expr', op => 'and', args => [
+ { -type => 'expr', op => '==', args => [
+ {-type => 'name', args => [qw/me id/] }, {-type => 'value', value => 200 }
+ ],
+ },
+ { -type => 'expr', op => 'and', args => $cols }
+ ]
+ }
+), "me.id = ? AND me.id > ? AND me.name = ?",
+ "expr clause (nested and)";
is $sqla->dispatch(
- [ -where => -and =>
- [ '==', [-name => qw/me id/], [-value => 500 ] ],
- [ -or =>
- [ '>', [-name => qw/me name/], [-value => '200' ] ],
- [ '<', [-name => qw/me name/], [-value => '100' ] ]
- ]
- ]
-), "WHERE me.id = ? AND (me.name > ? OR me.name < ?)",
- "where clause (inner or)";
+ { -type => 'expr', op => 'and', args => [
+ { -type => 'expr', op => '==', args => [
+ {-type => 'name', args => [qw/me id/] }, {-type => 'value', value => 200 }
+ ],
+ },
+ { -type => 'expr', op => 'or', args => $cols }
+ ]
+ }
+), "me.id = ? AND (me.id > ? OR me.name = ?)",
+ "expr clause (inner or)";
+
+is $sqla->dispatch(
+ { -type => 'expr', op => 'in', args => [ ] }
+), "0 = 1", "emtpy -in";
+
+is $sqla->dispatch(
+ { -type => 'expr',
+ op => 'in',
+ args => [ { -type => 'name', args => ['foo'] } ],
+ }
+), "0 = 1", "emtpy -in";
eq_or_diff(
[SQL::Abstract->generate(
- [ -ast_version => 1,
- -where =>
- [ -in =>
- [-name => qw/me id/],
- [-value => '100' ],
- [-value => '200' ],
- [-value => '300' ],
+ { -ast_version => 1,
+ -type => 'expr',
+ op => 'and',
+ args => [
+ { -type => 'expr',
+ op => 'in',
+ args => [
+ {-type => 'name', args => [qw/me id/] },
+ {-type => 'value', value => 100 },
+ {-type => 'value', value => 200 },
+ {-type => 'value', value => 300 },
+ ]
+ }
]
- ]
+ }
) ],
- [ "WHERE me.id IN (?, ?, ?)",
- [ qw/100 200 300/]
+ [ "me.id IN (?, ?, ?)",
+ [ 100, 200, 300 ]
],
- "where IN clause");
-
+ "IN expression");
eq_or_diff(
[SQL::Abstract->generate(
- [ -ast_version => 1,
- -where =>
- [ -not_in =>
- [-name => qw/me id/],
- [-value => '100' ],
- [-value => '200' ],
- [-value => '300' ],
+ { -ast_version => 1,
+ -type => 'expr',
+ op => 'and',
+ args => [
+ { -type => 'expr',
+ op => 'not_in',
+ args => [
+ {-type => 'name', args => [qw/me id/] },
+ {-type => 'value', value => 100 },
+ {-type => 'value', value => 200 },
+ {-type => 'value', value => 300 },
+ ]
+ }
]
- ]
+ }
) ],
- [ "WHERE me.id NOT IN (?, ?, ?)",
- [ qw/100 200 300/]
+ [ "me.id NOT IN (?, ?, ?)",
+ [ 100, 200, 300 ]
],
- "where NOT IN clause");
+ "NOT IN clause");