sanify alias/SELECT list rendering
[dbsrgits/Data-Query.git] / t / sql.t
CommitLineData
515523bc 1use strictures 1;
2use Test::More qw(no_plan);
3
4use Devel::Dwarn;
5use Data::Query::Renderer::SQL::Naive;
9c8fc055 6use Data::Query::ExprHelpers qw(perl_scalar_value);
515523bc 7
8BEGIN { require 't/expr.include' }
9
10my $rend = Data::Query::Renderer::SQL::Naive->new({ quote_chars => [ "'" ] });
11
3a1bb3a0 12sub binding { map perl_scalar_value($_), @_ }
13
515523bc 14sub expr_sql_is (&;@) {
15 my $sub = shift;
16 @_
2cf0bb42 17 ? is_deeply($rend->render(_run_expr($sub)->{expr}), @_)
18 : ::Dwarn($rend->render(_run_expr($sub)->{expr}));
515523bc 19}
20
21expr_sql_is { $_->foo }
22 [ 'foo' ],
23 "Simple identifier -> SQL";
24
25expr_sql_is { $_->group }
26 [ q{'group'} ],
27 "Simple identifier needing quoting -> SQL";
28
29expr_sql_is { $_->foo->group }
30 [ q{foo.'group'} ],
31 "Complex identifier -> SQL";
32
33expr_sql_is { $_->foo == 1 }
3a1bb3a0 34 [ "foo = ?", binding(1) ],
515523bc 35 "Simple expression -> SQL";
36
37expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") }
3a1bb3a0 38 [ "( foo = ? AND bar = ? )", binding(1, "foo") ],
515523bc 39 "Compound expression -> SQL";
40
41
42expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") & ($_->baz > 3) }
3a1bb3a0 43 [ "( foo = ? AND bar = ? AND baz > ? )", binding(1, "foo", 3) ],
515523bc 44 "Flatten expression ok";
45
46expr_sql_is { !$_->foo }
47 [ "NOT foo" ],
48 "Unary expression ok";
9c8fc055 49
50expr_sql_is { SELECT { $_->foo } }
51 [ "SELECT foo" ],
52 "Simple identifier";
53
54expr_sql_is { SELECT { $_->foo, 1 } }
55 # the extra space here is a little icky but Naive's _flatten_structure
56 # will need rewriting to fix it - commit bits available if you do it first
bdb576cb 57 [ "SELECT foo, ?", binding(1) ],
9c8fc055 58 "Identifier and literal";
a0ab336b 59
60expr_sql_is { SELECT { $_->foo => AS("foom"), 1 } }
bdb576cb 61 [ "SELECT foo AS foom, ?", binding(1) ],
a0ab336b 62 "AS with parens";
63
64expr_sql_is { SELECT { $_->foo => AS "foom", 1 } }
bdb576cb 65 [ "SELECT foo AS foom, ?", binding(1) ],
a0ab336b 66 "AS without parens";