2 use Test::More qw(no_plan);
4 use Scalar::Util qw(blessed);
6 use Data::Query::Renderer::SQL::Naive;
7 use Data::Query::ExprHelpers qw(perl_scalar_value);
8 use Data::Query::ExprDeclare qw(SELECT AS expr);
10 my $rend = Data::Query::Renderer::SQL::Naive->new({ quote_chars => [ "'" ] });
12 sub binding { map perl_scalar_value($_), @_ }
14 sub expr_sql_is (&;@) {
16 my $e = Data::Query::ExprDeclare::_run_expr($sub);
17 $e = blessed($e) ? $e->{expr} : $e;
19 ? is_deeply($rend->render($e), @_)
20 : ::Dwarn($rend->render($e));
23 expr_sql_is { $_->foo }
25 "Simple identifier -> SQL";
27 expr_sql_is { $_->group }
29 "Simple identifier needing quoting -> SQL";
31 expr_sql_is { $_->foo->group }
33 "Complex identifier -> SQL";
35 expr_sql_is { $_->foo == 1 }
36 [ "foo = ?", binding(1) ],
37 "Simple expression -> SQL";
39 expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") }
40 [ "( foo = ? AND bar = ? )", binding(1, "foo") ],
41 "Compound expression -> SQL";
44 expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") & ($_->baz > 3) }
45 [ "( foo = ? AND bar = ? AND baz > ? )", binding(1, "foo", 3) ],
46 "Flatten expression ok";
48 expr_sql_is { !$_->foo } # XXX revisit this why are the parens here
50 "Unary expression ok";
52 expr_sql_is { $_->foo != undef }
53 [ "foo IS NOT NULL" ],
54 "!= undef => IS NOT NULL";
56 expr_sql_is { $_->foo == undef }
57 [ "( NOT foo IS NOT NULL )" ],
58 "== undef => ( NOT foo IS NOT NULL )";
60 expr_sql_is { SELECT { $_->foo } }
64 expr_sql_is { SELECT { $_->foo, 1 } }
65 # the extra space here is a little icky but Naive's _flatten_structure
66 # will need rewriting to fix it - commit bits available if you do it first
67 [ "SELECT foo, ?", binding(1) ],
68 "Identifier and literal";
70 expr_sql_is { SELECT { $_->foo => AS("foom"), 1 } }
71 [ "SELECT foo AS foom, ?", binding(1) ],
74 expr_sql_is { SELECT { $_->foo => AS "foom", 1 } }
75 [ "SELECT foo AS foom, ?", binding(1) ],