From: Matt S Trout Date: Mon, 30 Sep 2019 17:15:56 +0000 (+0000) Subject: require explicit restoration of old unop behaviour except for special ops X-Git-Tag: v2.000000~3^2~90 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=336bff329cb76b5338e1c40524a2dc025837ba94;p=dbsrgits%2FSQL-Abstract.git require explicit restoration of old unop behaviour except for special ops --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 523e24b..401f38f 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -989,23 +989,26 @@ sub _expand_hashpair_op { my $op = $self->_normalize_op($k); - { # Old SQLA compat + my $wsop = join(' ', split '_', $op); - my $op = join(' ', split '_', $op); + my $is_special = List::Util::first { $wsop =~ $_->{regex} } + @{$self->{special_ops}}; + + { # Old SQLA compat # the old special op system requires illegality for top-level use if ( (our $Expand_Depth) == 1 and ( - List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}} + $is_special or ( $self->{disable_old_special_ops} - and List::Util::first { $op =~ $_->{regex} } @BUILTIN_SPECIAL_OPS + and List::Util::first { $wsop =~ $_->{regex} } @BUILTIN_SPECIAL_OPS ) ) ) { - puke "Illegal use of top-level '-$op'" + puke "Illegal use of top-level '-$wsop'" } } @@ -1037,27 +1040,22 @@ sub _expand_hashpair_op { } } - my $type = ( - $self->{unknown_unop_always_func} && !$self->{render_op}{$op} - ? -func - : -op - ); + my $type = $is_special || $self->{render_op}{$op} ? -op : -func; - { # Old SQLA compat + if ($self->{restore_old_unop_handling}) { + + # Old SQLA compat if ( ref($v) eq 'HASH' and keys %$v == 1 and (keys %$v)[0] =~ /^-/ + and not $self->{render_op}{$op} + and not $is_special ) { - $type = ( - ( - (List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}) - or $self->{render_op}{$op} - ) - ? -op - : -func - ) + $type = -func; + } else { + $type = -op; } } @@ -1569,8 +1567,14 @@ sub _render_unop_paren { sub _render_unop_prefix { my ($self, $op, $v) = @_; + my $op_sql = $self->{restore_old_unop_handling} + ? $self->_sqlcase($op) + : { -keyword => $op }; return $self->join_query_parts(' ', - { -keyword => \$op }, $v->[0] + ($self->{restore_old_unop_handling} + ? $self->_sqlcase($op) + : { -keyword => \$op }), + $v->[0] ); } diff --git a/t/01generate.t b/t/01generate.t index 7ab35b2..85a667a 100644 --- a/t/01generate.t +++ b/t/01generate.t @@ -454,6 +454,14 @@ 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 ? )", + bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']], + }, + { + func => 'update', + new => {bindtype => 'columns',restore_old_unop_handling => 1}, + 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 ? )", bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']], @@ -537,6 +545,14 @@ my @tests = ( func => 'select', new => {bindtype => 'columns'}, args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ], + stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN(?) ) ) ) )', + stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN(?) ) ) ) )', + bind => [[Y => 'x']], + }, + { + func => 'select', + new => {bindtype => 'columns',restore_old_unop_handling => 1}, + args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ], stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )', stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )', bind => [[Y => 'x']], diff --git a/t/02where.t b/t/02where.t index d1e2ee6..204a8fd 100644 --- a/t/02where.t +++ b/t/02where.t @@ -335,12 +335,12 @@ my @handle_tests = ( # Op against random functions (these two are oracle-specific) { where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } }, - stmt => " WHERE ( timestamp != TRUNC (YEAR sysdate) )", + stmt => " WHERE ( timestamp != TRUNC(YEAR(sysdate)) )", bind => [], }, { where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } }, - stmt => " WHERE ( timestamp >= TO_DATE ? )", + stmt => " WHERE ( timestamp >= TO_DATE(?) )", bind => ['2009-12-21 00:00:00'], }, diff --git a/t/05in_between.t b/t/05in_between.t index 5bcbe55..ae66b90 100644 --- a/t/05in_between.t +++ b/t/05in_between.t @@ -93,6 +93,26 @@ my @in_between_tests = ( ] }, }, stmt => "WHERE ( + ( start0 BETWEEN ? AND UPPER(?) ) + AND ( start1 BETWEEN ? AND ? ) + AND ( start2 BETWEEN lower(x) AND upper(y) ) + AND ( start3 BETWEEN lower(x) AND upper(?) ) + )", + bind => [1, 2, 1, 2, 'stuff'], + test => '-between POD test', + }, + { + args => { restore_old_unop_handling => 1 }, + where => { + start0 => { -between => [ 1, { -upper => 2 } ] }, + start1 => { -between => \["? AND ?", 1, 2] }, + start2 => { -between => \"lower(x) AND upper(y)" }, + start3 => { -between => [ + \"lower(x)", + \["upper(?)", 'stuff' ], + ] }, + }, + stmt => "WHERE ( ( start0 BETWEEN ? AND UPPER ? ) AND ( start1 BETWEEN ? AND ? ) AND ( start2 BETWEEN lower(x) AND upper(y) ) @@ -113,6 +133,32 @@ my @in_between_tests = ( ] }, }, stmt => "WHERE ( + ( start0 BETWEEN ? AND UPPER(?) ) + AND ( start1 BETWEEN ? AND ? ) + AND ( start2 BETWEEN lower(x) AND upper(y) ) + AND ( start3 BETWEEN lower(x) AND upper(?) ) + )", + bind => [ + [ start0 => 1 ], + [ start0 => 2 ], + [ start1 => 1 ], + [ start1 => 2 ], + [ start3 => 'stuff' ], + ], + test => '-between POD test', + }, + { + args => { restore_old_unop_handling => 1, bindtype => 'columns' }, + where => { + start0 => { -between => [ 1, { -upper => 2 } ] }, + start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] }, + start2 => { -between => \"lower(x) AND upper(y)" }, + start3 => { -between => [ + \"lower(x)", + \["upper(?)", [ start3 => 'stuff'] ], + ] }, + }, + stmt => "WHERE ( ( start0 BETWEEN ? AND UPPER ? ) AND ( start1 BETWEEN ? AND ? ) AND ( start2 BETWEEN lower(x) AND upper(y) ) @@ -204,6 +250,13 @@ my @in_between_tests = ( { where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } }, + stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER(?) ) )", + bind => [qw/A c/], + test => '-in with an array of function array refs with args', + }, + { + args => { restore_old_unop_handling => 1 }, + where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } }, stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )", bind => [qw/A c/], test => '-in with an array of function array refs with args',