From: Ash Berlin Date: Sat, 4 Apr 2009 23:20:53 +0000 (+0100) Subject: Start working on update clause X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d4656fcf868ce52c9689c1036628e04993eb5e59;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Start working on update clause --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index a87fc5c..54b4172 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -80,6 +80,15 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return join(' ', @output); } + method _update(AST $ast) { + + for (qw/columns values tablespec/) { + confess "'$_' is required in update AST with " . dump ($ast) + unless exists $ast->{$_}; + } + + } + method _join(HashRef $ast) { # TODO: Validate join type diff --git a/t/202_update.t b/t/202_update.t new file mode 100644 index 0000000..f6544bc --- /dev/null +++ b/t/202_update.t @@ -0,0 +1,33 @@ +use strict; +use warnings; + +use Test::More tests => 5; +use Test::Differences; + +use FindBin; +use lib "$FindBin::Bin/lib"; +use Test::SQL::Abstract::Util qw/ + mk_name + mk_value + mk_alias + :dumper_sort +/; + +use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); + +my $sqla = SQL::Abstract->create(1); + +is $sqla->dispatch( + { -type => 'update', + tablespec => mk_name('test'), + columns => [ + mk_name(qw/me id/), + mk_name(qw/hostname/), + ], + values => [ + mk_expr('+', mk_name(qw/me id/), mk_value(5)), + mk_value('localhost'), + ] + } +), "UPDATE test SET me.id = me.id + 5, hostnameme = localhost" + "update clause"; diff --git a/t/900_errors.t b/t/900_errors.t index f885a89..b86eda0 100644 --- a/t/900_errors.t +++ b/t/900_errors.t @@ -1,9 +1,17 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 6; use Test::Exception; +use FindBin; +use lib "$FindBin::Bin/lib"; +use Test::SQL::Abstract::Util qw/ + mk_name + mk_value + mk_alias +/; + use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); my $sqla = SQL::Abstract->create(1); @@ -25,6 +33,7 @@ throws_ok { ) } qr/^'~' is not a valid operator in an expression/; +{ local $TODO = "Work out how to get nice errors for these"; throws_ok { @@ -39,3 +48,16 @@ throws_ok { ) } qr/foobar/, "alias: iden instead of ident"; +} + +throws_ok { + $sqla->dispatch( + { -type => 'update', + tablespec => mk_name('test'), + columns => [ + mk_name(qw/me id/), + mk_alias(mk_name(qw/foo id/) ,'foo_id') + ] + } + ) +} qr/^'values' is required in update AST/, "Invalid clause in update" diff --git a/t/compat/ast/01.t b/t/compat/ast/01.t index fe1b384..4b1d316 100644 --- a/t/compat/ast/01.t +++ b/t/compat/ast/01.t @@ -6,6 +6,7 @@ use lib "$FindBin::Bin/../../lib"; use Test::SQL::Abstract::Util qw/ mk_name mk_value + mk_expr field_op_value :dumper_sort /; @@ -330,15 +331,6 @@ eq_or_diff "Complex AST [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] }, q => {'not in' => [10,11] } } ]"; -sub upper { expr(UPPER => @_) } +sub upper { mk_expr(UPPER => @_) } -sub expr { - my ($op, @args) = @_; - - return { - -type => 'expr', - op => $op, - args => [@args] - }; -} diff --git a/t/lib/Test/SQL/Abstract/Util.pm b/t/lib/Test/SQL/Abstract/Util.pm index 4455c29..598ddac 100644 --- a/t/lib/Test/SQL/Abstract/Util.pm +++ b/t/lib/Test/SQL/Abstract/Util.pm @@ -8,6 +8,7 @@ use Sub::Exporter -setup => { mk_name mk_value mk_alias + mk_expr field_op_value /], groups => [ @@ -63,6 +64,15 @@ sub mk_value { return { -type => 'value', value => $_[0] } } +sub mk_expr { + my ($op, @args) = @_; + + return { + -type => 'expr', + op => $op, + args => [@args] + }; +} sub field_op_value { my ($field, $op, $value) = @_;