From: Ash Berlin Date: Mon, 30 Mar 2009 08:59:43 +0000 (+0100) Subject: Handle field convertor X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c7e5fddfe5fdca506f7fdec3614ce5c5890e1a09;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Handle field convertor --- diff --git a/lib/SQL/Abstract/Compat.pm b/lib/SQL/Abstract/Compat.pm index 09b586a..5c7313a 100644 --- a/lib/SQL/Abstract/Compat.pm +++ b/lib/SQL/Abstract/Compat.pm @@ -41,6 +41,12 @@ class SQL::Abstract::Compat { '=' => '==', ); + has convert => ( + is => 'rw', + isa => 'Str', + predicate => 'has_field_convertor' + ); + method select(Str|ArrayRef|ScalarRef $from, ArrayRef|Str $fields, WhereType $where?, WhereType $order?) @@ -49,7 +55,7 @@ class SQL::Abstract::Compat { -type => 'select', columns => [ map { - $self->mk_name($_) + $self->mk_name(0, $_) } ( is_Str($fields) ? $fields : @$fields ) ], tablespec => $self->tablespec($from) @@ -80,12 +86,17 @@ class SQL::Abstract::Compat { } sub mk_name { - shift; - return { -type => 'name', args => [ @_ ] }; + my ($self, $use_convert) = (shift,shift); + my $ast = { -type => 'name', args => [ @_ ] }; + + return $ast + unless $use_convert && $self->has_field_convertor; + + return $self->apply_convert($ast); } method tablespec(Str|ArrayRef|ScalarRef $from) { - return $self->mk_name($from) + return $self->mk_name(0, $from) if is_Str($from); } @@ -161,7 +172,7 @@ class SQL::Abstract::Compat { -type => 'expr', op => $op, args => [ - { -type => 'name', args => [$key] } + $self->mk_name(1, $key) ], }; @@ -212,12 +223,22 @@ class SQL::Abstract::Compat { } method value($value) returns (AST) { - return { -type => 'value', value => $value } + return $self->apply_convert( { -type => 'value', value => $value }) if is_Str($value); confess "Don't know how to handle terminal value " . dump($value); } + method apply_convert(AST $ast) { + return $ast unless $self->has_field_convertor; + + return { + -type => 'expr', + op => $self->convert, + args => [ $ast ] + }; + } + } diff --git a/t/compat/00new.t b/t/compat/00new.t index 083b7dd..b1cd6c8 100644 --- a/t/compat/00new.t +++ b/t/compat/00new.t @@ -48,8 +48,6 @@ my @handle_tests = ( args => {cmp => "like"}, stmt => 'SELECT * FROM test WHERE ( a LIKE ? AND b LIKE ? )' }, -); -my @foo = ( #7 { args => {logic => "or", cmp => "like"}, @@ -60,21 +58,25 @@ my @foo = ( }, #8 { + todo => 'lower', args => {case => "lower"}, stmt => 'select * from test where ( a = ? and b = ? )' }, #9 { + todo => 'lower', args => {case => "lower", cmp => "="}, stmt => 'select * from test where ( a = ? and b = ? )' }, #10 { + todo => 'lower', args => {case => "lower", cmp => "like"}, stmt => 'select * from test where ( a like ? and b like ? )' }, #11 { + todo => 'lower', args => {case => "lower", convert => "lower", cmp => "like"}, stmt => 'select * from test where ( lower(a) like lower(?) and lower(b) like lower(?) )' }, @@ -85,6 +87,7 @@ my @foo = ( }, #13 { + todo => 'lower', args => {convert => "lower"}, stmt => 'SELECT * FROM test WHERE ( ( LOWER(ticket) = LOWER(?) ) OR ( LOWER(hostname) = LOWER(?) ) OR ( LOWER(taco) = LOWER(?) ) OR ( LOWER(salami) = LOWER(?) ) )', where => [ { ticket => 11 }, { hostname => 11 }, { taco => 'salad' }, { salami => 'punch' } ], @@ -112,6 +115,7 @@ for (@handle_tests) { my $where = $_->{where} || { a => 4, b => 0}; my($stmt, @bind) = $sql->select('test', '*', $where); + local $TODO = $_->{todo}; # LDNOTE: this original test suite from NWIGER did no comparisons # on @bind values, just checking if @bind is nonempty.