From: Ash Berlin Date: Mon, 30 Mar 2009 08:51:45 +0000 (+0100) Subject: Fucntion support X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e6a33ce3c4ae795fa8975928640309e545940413;hp=f94aef7f1ad8b1634a80225ee2e47b75f3ea7a45;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Fucntion support --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 34d340e..2c00f26 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -147,6 +147,8 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { method _list(AST $ast) { + return "" unless $ast->{args}; + my @items = is_ArrayRef($ast->{args}) ? @{$ast->{args}} : $ast->{args}; @@ -227,7 +229,12 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { croak "'$op' is not a valid AST type in an expression with " . dump($ast) if $ast->{-type} ne 'expr'; - croak "'$op' is not a valid operator in an expression with " . dump($ast); + # This is an attempt to do some form of validation on function names. This + # might end up being a bad thing. + croak "'$op' is not a valid operator in an expression with " . dump($ast) + if $op =~ /\W/; + + return $self->_generic_function_op($ast); } @@ -241,6 +248,12 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { ); } + method _generic_function_op(AST $ast) { + my $op = $ast->{op}; + + return "$op(" . $self->_list($ast) . ")"; + } + method _in(AST $ast) { my ($field,@values) = @{$ast->{args}}; @@ -256,9 +269,6 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { ")"; } - method _generic_func(ArrayRef $ast) { - } - # '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 new file mode 100644 index 0000000..29d7dee --- /dev/null +++ b/t/101_expr_funcitons.t @@ -0,0 +1,39 @@ + +use strict; +use warnings; + +use Test::More tests => 3; +use Test::Differences; + +use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); + +my $sqla = SQL::Abstract->create(1); + +is $sqla->dispatch( + { -type => 'expr', + op => '==', + args => [ + { -type => 'expr', + op => 'ROUND', + args => [ + {-type => name => args => [qw/me id/] }, + ] + }, + { -type => 'expr', + op => 'ROUND', + args => [ + { -type => 'value', value => 500 } + ] + }, + ] + } +), "ROUND(me.id) = ROUND(?)", + "simple expr clause"; + +is $sqla->dispatch( + { -type => 'expr', + op => 'last_insert_id', + } +), "last_insert_id()", + "last_insert_id"; +