has binds => (
isa => ArrayRef,
+ is => 'ro',
default => sub { [ ] },
metaclass => 'Collection::Array',
provides => {
push => 'add_bind',
- get => 'binds'
+ clear => '_clear_binds',
}
);
method generate (Object|ClassName $self: ArrayRef $ast) {
- $self = $self->new unless blessed($self);
+ my $class_meth = !blessed($self);
+ $self = $self->new if $class_meth;
local $_ = $ast->[0];
s/^-/_/g or croak "Unknown type tag '$_'";
my $meth = $self->can($_) || \&_generic_func;
- return $meth->($self, $ast);
+ return $class_meth
+ ? ($meth->($self, $ast), $self->binds)
+ : $meth->($self, $ast);
}
method _select(ArrayRef $ast) {
use warnings;
use Test::More tests => 14;
+use Test::Differences;
use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
-is SQL::Abstract->generate( [ -name => qw/me id/]), "me.id",
+my $sqla = SQL::Abstract->new;
+is $sqla->generate( [ -name => qw/me id/]), "me.id",
"Simple name generator";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -list =>
[ -name => qw/me id/],
[ -name => qw/me foo bar/],
), "me.id, me.foo.bar, bar",
"List generator";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -alias => [ -name => qw/me id/], "foobar", ]
), "me.id AS foobar",
"Alias generator";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -order_by => [ -name => qw/me date/ ] ]
), "ORDER BY me.date";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -order_by =>
[ -name => qw/me date/ ],
[ -name => qw/me foobar/ ],
]
), "ORDER BY me.date, me.foobar";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -order_by => [ -desc => [ -name => qw/me date/ ] ] ]
), "ORDER BY me.date DESC";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -where =>
[ '>', [-name => qw/me id/], [-value => 500 ] ]
]
), "WHERE me.id > ?", "where clause";
-
-is SQL::Abstract->generate(
- [ -where =>
- [ '>', [-name => qw/me id/], [-value => 500 ] ],
- [ '==', [-name => qw/me name/], [-value => '200' ] ]
- ]
-), "WHERE me.id > ? AND me.name = ?", "where clause";
-
-
-is SQL::Abstract->generate(
+eq_or_diff( [ SQL::Abstract->generate(
+ [ -where =>
+ [ '>', [-name => qw/me id/], [-value => 500 ] ],
+ [ '==', [-name => qw/me name/], [-value => '200' ] ]
+ ]
+ ) ],
+ [ "WHERE me.id > ? AND me.name = ?",
+ [ 500,
+ '200'
+ ]
+ ],
+ "Where with binds"
+);
+
+
+is $sqla->generate(
[ -where => -or =>
[ '>', [-name => qw/me id/], [-value => 500 ] ],
[ '==', [-name => qw/me name/], [-value => '200' ] ],
), "WHERE me.id > ? OR me.name = ?", "where clause";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -where => -or =>
[ '>', [-name => qw/me id/], [-value => 500 ] ],
[ -or =>
]
), "WHERE me.id > ? OR me.name = ? OR me.name = ?", "where clause";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -where => -or =>
[ '==', [-name => qw/me id/], [-value => 500 ] ],
[ -and =>
]
), "WHERE me.id = ? OR me.name > ? AND me.name < ?", "where clause";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -where => -and =>
[ '==', [-name => qw/me id/], [-value => 500 ] ],
[ -and =>
), "WHERE me.id = ? AND me.name > ? AND me.name < ?", "where clause";
-is SQL::Abstract->generate(
+is $sqla->generate(
[ -where => -and =>
[ '==', [-name => qw/me id/], [-value => 500 ] ],
[ -or =>