From: Dagfinn Ilmari Mannsåker Date: Fri, 6 Dec 2013 17:24:22 +0000 (+0000) Subject: Handle undef -> NULL for more operators X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fce4b7a9;p=dbsrgits%2FSQL-Abstract.git Handle undef -> NULL for more operators 'IS NOT' and 'IS' are not documented in legacy SQLA, but do work. --- diff --git a/lib/SQL/Abstract/Converter.pm b/lib/SQL/Abstract/Converter.pm index 2a7d90a..0d7e064 100644 --- a/lib/SQL/Abstract/Converter.pm +++ b/lib/SQL/Abstract/Converter.pm @@ -536,9 +536,9 @@ sub _where_hashpair_to_dq { ); } elsif (!defined($rhs)) { my $null_op = do { - if ($op eq '=' or $op eq 'LIKE') { + if ($op eq '=' or $op eq 'LIKE' or $op eq 'IS') { 'IS NULL' - } elsif ($op eq '!=') { + } elsif ($op eq '!=' or $op eq 'NOT LIKE' or $op eq 'IS NOT') { 'IS NOT NULL' } else { die "Can't do undef -> NULL transform for operator ${op}"; diff --git a/t/01generate.t b/t/01generate.t index 55d3dd8..f876e60 100644 --- a/t/01generate.t +++ b/t/01generate.t @@ -566,6 +566,34 @@ my @tests = ( stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NOT NULL )', bind => [], }, + { + func => 'select', + args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }], + stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )', + stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )', + bind => [], + }, + { + func => 'select', + args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }], + stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL )', + stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL AND `c` IS NOT NULL )', + bind => [], + }, + { + func => 'select', + args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }], + stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )', + stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )', + bind => [], + }, + { + func => 'select', + args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }], + stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL )', + stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL )', + bind => [], + }, ); for my $t (@tests) {