We can't overload defined(), so this is the only way to do it.
We're not using IS NULL, since the converter can only hande 1:1
mapping between ops.
});
}
}
- qw(+ - * / % ** << >> . < > == != lt le gt ge eq ne),
+ qw(+ - * / % ** << >> . < > lt le gt ge ),
# since 'and' and 'or' aren't operators we borrow the bitwise ops
[ '&' => 'and' ], [ '|' => 'or' ],
),
+
+ # equality operators (need undef maping)
+ (map {
+ my ($overload, $as) = ref($_) ? @$_ : ($_, $_);
+ $overload => sub {
+ Data::Query::ExprBuilder->new({
+ expr => grep(!defined, @_[0,1])
+ ? (map { $overload =~ /==|eq/ ? perl_operator(not => $_) : $_ }
+ perl_operator(defined => map { defined($_) ? $_->{expr} : () } @_[0,1]))
+ : perl_operator(
+ $as,
+ map {
+ (Scalar::Util::blessed($_)
+ && $_->isa('Data::Query::ExprBuilder'))
+ ? $_->{expr}
+ : perl_scalar_value($_)
+ # we're called with ($left, $right, 0) or ($right, $left, 1)
+ } $_[2] ? @_[1,0] : @_[0,1]
+ )
+ });
+ }
+ }
+ qw(== != eq ne)
+
+ ),
# unsupported
(map {
my $op = $_;
$_ eq '==' and return '=';
$_ eq 'eq' and return '=';
$_ eq '!' and return 'NOT';
+ $_ eq 'defined' and return 'IS NOT NULL';
}
return uc $perl_op; # hope!
}
args => [ expr { $_->foo } ],
},
'! ok';
+
+expr_is { $_->foo != undef }
+ {
+ type => DQ_OPERATOR,
+ operator => { Perl => 'defined' },
+ args => [ expr { $_->foo } ],
+ },
+ '!= undef => defined';
+
+expr_is { $_->foo ne undef }
+ {
+ type => DQ_OPERATOR,
+ operator => { Perl => 'defined' },
+ args => [ expr { $_->foo } ],
+ },
+ 'ne undef => defined';
+
+expr_is { $_->foo == undef }
+ expr { !($_->foo != undef) },
+ '== undef => not defined';
+
+expr_is { $_->foo eq undef }
+ expr { !($_->foo ne undef) },
+ 'eq undef => not defined';
[ "( NOT foo )" ],
"Unary expression ok";
+expr_sql_is { $_->foo != undef }
+ [ "foo IS NOT NULL" ],
+ "!= undef => IS NOT NULL";
+
+expr_sql_is { $_->foo == undef }
+ [ "( NOT foo IS NOT NULL )" ],
+ "== undef => ( NOT foo IS NOT NULL )";
+
expr_sql_is { SELECT { $_->foo } }
[ "SELECT foo" ],
"Simple identifier";
expr_sql_is { SELECT { $_->foo => AS "foom", 1 } }
[ "SELECT foo AS foom, ?", binding(1) ],
"AS without parens";
+