factor construction of simple expressions out into ExprHelpers.pm
[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;
6
7BEGIN { require 't/expr.include' }
8
9my $rend = Data::Query::Renderer::SQL::Naive->new({ quote_chars => [ "'" ] });
10
11sub expr_sql_is (&;@) {
12 my $sub = shift;
13 @_
14 ? is_deeply($rend->render(_mk_expr($sub)), @_)
15 : ::Dwarn($rend->render(_mk_expr($sub)));
16}
17
18expr_sql_is { $_->foo }
19 [ 'foo' ],
20 "Simple identifier -> SQL";
21
22expr_sql_is { $_->group }
23 [ q{'group'} ],
24 "Simple identifier needing quoting -> SQL";
25
26expr_sql_is { $_->foo->group }
27 [ q{foo.'group'} ],
28 "Complex identifier -> SQL";
29
30expr_sql_is { $_->foo == 1 }
31 [
32 "foo = ?",
33 {
34 subtype => {
35 Perl => "Scalar"
36 },
37 type => "Value",
38 value => 1
39 }
40 ],
41 "Simple expression -> SQL";
42
43expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") }
44 [
45 "( foo = ? AND bar = ? )",
46 {
47 subtype => {
48 Perl => "Scalar"
49 },
50 type => "Value",
51 value => 1
52 },
53 {
54 subtype => {
55 Perl => "Scalar"
56 },
57 type => "Value",
58 value => "foo"
59 }
60 ],
61 "Compound expression -> SQL";
62
63
64expr_sql_is { ($_->foo == 1) & ($_->bar eq "foo") & ($_->baz > 3) }
65 [
66 "( foo = ? AND bar = ? AND baz > ? )",
67 {
68 subtype => {
69 Perl => "Scalar"
70 },
71 type => "Value",
72 value => 1
73 },
74 {
75 subtype => {
76 Perl => "Scalar"
77 },
78 type => "Value",
79 value => "foo"
80 },
81 {
82 subtype => {
83 Perl => "Scalar"
84 },
85 type => "Value",
86 value => 3
87 }
88 ],
89 "Flatten expression ok";
90
91expr_sql_is { !$_->foo }
92 [ "NOT foo" ],
93 "Unary expression ok";