From: Peter Rabbitson Date: Sun, 5 Dec 2010 20:02:29 +0000 (+0100) Subject: Fix stupid assumption in parenthesis unroller X-Git-Tag: v1.72~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6e9a377bd3915ce41ee6a7d5e8db20928f852379;p=dbsrgits%2FSQL-Abstract.git Fix stupid assumption in parenthesis unroller --- diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm index 17454dc..864451a 100644 --- a/lib/SQL/Abstract/Tree.pm +++ b/lib/SQL/Abstract/Tree.pm @@ -527,6 +527,7 @@ my @unrollable_ops = ( 'GROUP \s+ BY', 'HAVING', 'ORDER \s+ BY', + 'I?LIKE', ); my $unrollable_ops_re = join ' | ', @unrollable_ops; $unrollable_ops_re = qr/$unrollable_ops_re/xi; @@ -573,11 +574,14 @@ sub _parenthesis_unroll { } # only *ONE* LITERAL or placeholder element + # as an AND/OR/NOT argument elsif ( @{$child->[1]} == 1 && ( $child->[1][0][0] eq 'LITERAL' or $child->[1][0][0] eq 'PLACEHOLDER' + ) && ( + $ast->[0] eq 'AND' or $ast->[0] eq 'OR' or $ast->[0] eq 'NOT' ) ) { push @children, $child->[1][0]; diff --git a/t/01generate.t b/t/01generate.t index 828bfdd..5855f80 100644 --- a/t/01generate.t +++ b/t/01generate.t @@ -442,8 +442,8 @@ my @tests = ( func => 'update', new => {bindtype => 'columns'}, args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}], - stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER( ? ) WHERE ( a BETWEEN ? AND ? )", - stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ( ? ) WHERE ( `a` BETWEEN ? AND ? )", + stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER ? WHERE ( a BETWEEN ? AND ? )", + stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ? WHERE ( `a` BETWEEN ? AND ? )", bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']], }, { diff --git a/t/10test.t b/t/10test.t index aa140d4..4d14014 100644 --- a/t/10test.t +++ b/t/10test.t @@ -643,33 +643,31 @@ my @sql_tests = ( 'SELECT foo FROM bar ()', ] }, - # single ? of unknown funcs can unroll - # (think ...LIKE ?...) { - equal => 1, + equal => 0, statements => [ - 'SELECT foo FROM bar WHERE bar > foo ?', - 'SELECT foo FROM bar WHERE bar > foo (?)', - 'SELECT foo FROM bar WHERE bar > foo( ? )', + 'SELECT COUNT * FROM foo', + 'SELECT COUNT( * ) FROM foo', ] }, + # single ? of unknown funcs do not unroll unless + # explicitly allowed (e.g. Like) { - equal => 1, + equal => 0, statements => [ - 'SELECT foo FROM bar WHERE bar > (foo ?)', - 'SELECT foo FROM bar WHERE bar > (foo( ? ))', - 'SELECT foo FROM bar WHERE bar > (( foo (?) ))', + 'SELECT foo FROM bar WHERE bar > foo ?', + 'SELECT foo FROM bar WHERE bar > foo( ? )', ] }, { equal => 1, statements => [ - 'SELECT foo FROM bar WHERE bar foo ?', - 'SELECT foo FROM bar WHERE bar foo (?)', - 'SELECT foo FROM bar WHERE bar foo( (?))', + 'SELECT foo FROM bar WHERE bar LIKE ?', + 'SELECT foo FROM bar WHERE bar LiKe (?)', + 'SELECT foo FROM bar WHERE bar lIkE( (?))', ] }, - # not so about multival + # test multival { equal => 0, statements => [