From: Ash Berlin Date: Mon, 30 Mar 2009 19:38:56 +0000 (+0100) Subject: Simple between support X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FSQL-Abstract-2.0-ish.git;a=commitdiff_plain;h=1f4bd99c989eec23be495216f51da03aac83f206 Simple between support --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 2c00f26..53db579 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -19,6 +19,8 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { %{super()}, in => $self->can('_in'), not_in => $self->can('_in'), + between => $self->can('_between'), + not_between => $self->can('_between'), and => $self->can('_recurse_where'), or => $self->can('_recurse_where'), map { +"$_" => $self->can("_$_") } qw/ @@ -269,6 +271,20 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { ")"; } + method _between(AST $ast) { + + my ($field,@values) = @{$ast->{args}}; + + my $not = ($ast->{op} =~ /^not_/) ? " NOT" : ""; + croak "between requires 3 arguments: " . dump($ast) + unless @values == 2; + + return $self->_expr($field) . + $not . + " BETWEEN " . + join(" AND ", map { $self->dispatch($_) } @values ); + } + # 'constants' that are portable across DBs method _false($ast?) { "0 = 1" } method _true($ast?) { "1 = 1" } diff --git a/t/101_expr_funcitons.t b/t/101_expr_funcitons.t index 29d7dee..13ec805 100644 --- a/t/101_expr_funcitons.t +++ b/t/101_expr_funcitons.t @@ -2,7 +2,7 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Differences; use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); @@ -37,3 +37,15 @@ is $sqla->dispatch( ), "last_insert_id()", "last_insert_id"; +is $sqla->dispatch( + { -type => 'expr', + op => 'between', + args => [ + {-type => name => args => [qw/me id/] }, + { -type => 'value', value => 500 }, + { -type => 'value', value => 599 }, + ], + } +), "me.id BETWEEN ? AND ?", + "between"; +