From: Ash Berlin Date: Fri, 3 Apr 2009 22:40:51 +0000 (+0100) Subject: Add Compat->AST tests for the complex test from compat/00new.t X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0073ca4362c48f62d13fecd453039afea9146919;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Add Compat->AST tests for the complex test from compat/00new.t --- diff --git a/lib/SQL/Abstract/Compat.pm b/lib/SQL/Abstract/Compat.pm index e45f50f..419fb52 100644 --- a/lib/SQL/Abstract/Compat.pm +++ b/lib/SQL/Abstract/Compat.pm @@ -104,7 +104,6 @@ class SQL::Abstract::Compat { method tablespec(Str|ArrayRef|ScalarRef $from) { return $self->mk_name(0, $from) - if is_Str($from); } method recurse_where(WhereType $ast, LogicEnum $logic?) returns (AST) { @@ -218,10 +217,7 @@ class SQL::Abstract::Compat { my $ast = { -type => 'expr', - - # Handle e => { '!=', [qw(f g)] }. - # SQLA treats this as a 'DWIM' since e != f AND e != g doesn't make sense - op => $op eq '!=' ? 'or' : 'and', + op => 'or', args => [ map { $self->field($key, $_) } @{$value} ] diff --git a/t/compat/ast/01.t b/t/compat/ast/01.t index 037d5f2..0fb36ee 100644 --- a/t/compat/ast/01.t +++ b/t/compat/ast/01.t @@ -7,7 +7,7 @@ use SQLADumperSort; use SQL::Abstract::Compat; -use Test::More tests => 13; +use Test::More tests => 17; use Test::Differences; ok(my $visitor = SQL::Abstract::Compat->new); @@ -165,6 +165,16 @@ eq_or_diff $visitor->convert('UPPER'); +my $ticket_or_eq = { + -type => 'expr', + op => 'or', + args => [ + field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(11))), + field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(12))), + field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(13))), + ] +}; + eq_or_diff $visitor->select_ast( 'test', '*', [ { ticket => [11, 12, 13] } ] @@ -172,51 +182,147 @@ eq_or_diff { -type => 'select', columns => [ { -type => 'name', args => ['*'] } ], tablespec => { -type => 'name', args => ['test'] }, - where => - { -type => 'expr', op => 'or', args => [ - field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(11))), - field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(12))), - field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(13))), - ] } + where => $ticket_or_eq }, "Complex AST with convert('UPPER')"; -=for comment +my $hostname_and_ticket = { + -type => 'expr', + op => 'and', + args => [ + field_op_value( upper(mk_name('hostname')), + in => [ map { + upper(mk_value($_)) + } qw/ntf avd bvd 123/ ] + ), + $ticket_or_eq, + ] +}; + +eq_or_diff + $visitor->select_ast( + 'test', '*', [ { ticket => [11, 12, 13], + hostname => { in => ['ntf', 'avd', 'bvd', '123'] } + } + ] + ), + { -type => 'select', + columns => [ { -type => 'name', args => ['*'] } ], + tablespec => { -type => 'name', args => ['test'] }, + where => $hostname_and_ticket + }, + "Complex AST mixing arrays+hashes with convert('UPPER')"; + +my $tack_between = { + -type => 'expr', + op => 'between', + args => [ + upper(mk_name('tack')), + upper(mk_value('tick')), + upper(mk_value('tock')), + ] +}; + +eq_or_diff + $visitor->select_ast( + 'test', '*', [ { ticket => [11, 12, 13], + hostname => { in => ['ntf', 'avd', 'bvd', '123'] } + }, + { tack => { between => [qw/tick tock/] } } + ] + ), + { -type => 'select', + columns => [ { -type => 'name', args => ['*'] } ], + tablespec => { -type => 'name', args => ['test'] }, + where => { + -type => 'expr', + op => 'or', + args => [ + $hostname_and_ticket, + $tack_between, + ] + } + }, + "Complex AST mixing [ {a => [1,2],b => 3}, { c => 4 }]"; + +my $a_or_eq = { + -type => 'expr', + op => 'or', + args => [ map { + { -type => 'expr', op => '==', args => [ upper(mk_name('a')), upper(mk_value($_)) ] } + } qw/b c d/ ] +}; + +my $e_ne = { + -type => 'expr', + op => 'or', + args => [ map { + { -type => 'expr', op => '!=', args => [ upper(mk_name('e')), upper(mk_value($_)) ] } + } qw/f g/ ] +}; + +eq_or_diff + $visitor->select_ast( + 'test', '*', [ { ticket => [11, 12, 13], + hostname => { in => ['ntf', 'avd', 'bvd', '123'] } + }, + { tack => { between => [qw/tick tock/] } }, + { a => [qw/b c d/], + e => { '!=', [qw(f g)] }, + } + ] + ), + { -type => 'select', + columns => [ { -type => 'name', args => ['*'] } ], + tablespec => { -type => 'name', args => ['test'] }, + where => { + -type => 'expr', + op => 'or', + args => [ + $hostname_and_ticket, + $tack_between, + { -type => 'expr', op => 'and', args => [ $a_or_eq, $e_ne ] } + ] + } + }, + "Complex AST mixing [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] } } ]"; + + eq_or_diff $visitor->select_ast( 'test', '*', [ { ticket => [11, 12, 13], hostname => { in => ['ntf', 'avd', 'bvd', '123'] } }, - #{ tack => { between => [qw/tick tock/] } }, - #{ a => [qw/b c d/], - # e => { '!=', [qw(f g)] }, - # q => { 'not in', [14..20] } - #} + { tack => { between => [qw/tick tock/] } }, + { a => [qw/b c d/], + e => { '!=', [qw(f g)] }, + q => { 'not in', [14..20] } + } ] ), { -type => 'select', columns => [ { -type => 'name', args => ['*'] } ], tablespec => { -type => 'name', args => ['test'] }, - where => - { -type => 'expr', op => 'or', args => [ - { -type => 'expr', op => 'and', args => [ - field_op_value( upper(mk_name('hostname')), - in => [ - upper(mk_value('nft')), - upper(mk_value('avd')), - upper(mk_value('bvd')), - upper(mk_value('123')), - ] - ), - { -type => 'expr', op => 'or', args => [ - field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(11))), - field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(12))), - field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(13))), - ] } + where => { + -type => 'expr', + op => 'or', + args => [ + $hostname_and_ticket, + $tack_between, + { -type => 'expr', op => 'and', args => [ + $a_or_eq, + $e_ne, + { -type => 'expr', + op => 'not_in', + args => [ + upper(mk_name('q')), + map { upper(mk_value($_)) } 14..20 + ] + } ] } - ] } + ] + } }, - "Complex AST with convert('UPPER')"; -=cut + "Complex AST [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] }, q => {'not in' => [10,11] } } ]"; sub field_op_value { my ($field, $op, $value) = @_;