From: Ash Berlin Date: Tue, 3 Mar 2009 09:14:48 +0000 (+0000) Subject: Return binds when called as class method X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5bf8c024d27e974336be6ece34f1d4f0cf998fce;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Return binds when called as class method --- diff --git a/Makefile.PL b/Makefile.PL index b208fbb..3937cd5 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -9,5 +9,6 @@ requires 'MooseX::Method::Signatures' => '0.10'; requires 'MooseX::Declare' => '0.07'; test_requires 'Test::More'; +test_requires 'Test::Differences'; WriteAll; diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index e835c6e..d1dc021 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -55,21 +55,25 @@ class SQL::Abstract { 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) { diff --git a/t/001_basic.t b/t/001_basic.t index d3aecc3..224c81b 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -2,13 +2,15 @@ use strict; 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/], @@ -17,43 +19,49 @@ is SQL::Abstract->generate( ), "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' ] ], @@ -61,7 +69,7 @@ is SQL::Abstract->generate( ), "WHERE me.id > ? OR me.name = ?", "where clause"; -is SQL::Abstract->generate( +is $sqla->generate( [ -where => -or => [ '>', [-name => qw/me id/], [-value => 500 ] ], [ -or => @@ -71,7 +79,7 @@ is SQL::Abstract->generate( ] ), "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 => @@ -81,7 +89,7 @@ is SQL::Abstract->generate( ] ), "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 => @@ -92,7 +100,7 @@ is SQL::Abstract->generate( ), "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 =>