From: Peter Rabbitson Date: Tue, 9 Mar 2010 12:09:53 +0000 (+0000) Subject: Extra nesting test and fix X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=07936978f41bd6a48f2f8eaf38361a2e36ca71bd;p=scpubgit%2FQ-Branch.git Extra nesting test and fix --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 94b4bb2..9191010 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -655,7 +655,6 @@ sub _where_hashpair_HASHREF { $logic ||= 'and'; local $self->{_nested_func_lhs} = $self->{_nested_func_lhs}; - $self->{_nested_func_lhs} ||= $k; my ($all_sql, @all_bind); @@ -714,8 +713,17 @@ sub _where_hashpair_HASHREF { }, FALLBACK => sub { # CASE: col => {op/func => $stuff} + + # if we are starting to nest and the first func is not a cmp op + # assume equality + my $prefix; + unless ($self->{_nested_func_lhs}) { + $self->{_nested_func_lhs} = $k; + $prefix = $self->{cmp} unless $op =~ $self->{cmp_ops}; + } + ($sql, @bind) = $self->_where_func_generic ($op, $val); - $sql = join ' ', $self->_convert($self->_quote($k)), $sql; + $sql = join ' ', $self->_convert($self->_quote($k)), $prefix||(), $sql; }, }); } diff --git a/t/01generate.t b/t/01generate.t index be50c5a..9dbc504 100644 --- a/t/01generate.t +++ b/t/01generate.t @@ -231,10 +231,10 @@ my @tests = ( #26 { func => 'select', - args => ['test', '*', {priority => [ -and => {'!=', 2}, {'!=', 1} ]}], - stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority != ? ) ) )', - stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` != ? ) ) )', - bind => [qw(2 1)], + args => ['test', '*', {priority => [ -and => {'!=', 2}, { -not_like => '3%'} ]}], + stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority NOT LIKE ? ) ) )', + stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` NOT LIKE ? ) ) )', + bind => [qw(2 3%)], }, #27 { @@ -584,6 +584,14 @@ my @tests = ( stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id', bind => [qw/1 2 3 4 5/], }, + { + 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']], + }, );