disable precedence warnings for &
[dbsrgits/Data-Query.git] / t / expr-helpers.t
1 use strictures 1;
2 use Test::More qw(no_plan);
3
4 use Devel::Dwarn;
5 use Data::Query::Renderer::SQL::Naive;
6 use Data::Query::ExprHelpers;
7
8 my $rend = Data::Query::Renderer::SQL::Naive->new({ quote_chars => [ "'" ] });
9
10 sub binding { map perl_scalar_value($_), @_ }
11
12 sub dq_sql_is {
13   my $expr = shift;
14 #::Dwarn($expr); return;
15   my $rendered = $rend->render($expr);
16   is_deeply($rendered, @_);
17 }
18
19 dq_sql_is
20   Select([ Identifier('*') ], Identifier('foo')),
21   ['SELECT * FROM foo'],
22   'simple select';
23
24 {
25   # should these both be allowed, and result in the same SQL?
26
27   dq_sql_is
28     Join(
29       Select([ Identifier('*') ], Identifier('foo')),
30       Identifier('bar'),
31       perl_operator('==', Identifier('foo', 'x'), Identifier('bar', 'y')),
32     ),
33     ['SELECT * FROM foo JOIN bar ON foo.x = bar.y'],
34     'join on with dots (Join Select)';
35
36   dq_sql_is
37     Select(
38       [ Identifier('*') ],
39       Join(
40         Identifier('foo'),
41         Identifier('bar'),
42         perl_operator('==', Identifier('foo', 'x'), Identifier('bar', 'y')),
43       ),
44     ),
45     ['SELECT * FROM foo JOIN bar ON foo.x = bar.y'],
46     'join on with dots (Select Join)';
47 }
48
49 dq_sql_is
50   Where(
51     perl_operator(
52       '==',
53       Identifier('x'),
54       Literal('SQL', '?', [ binding(1) ],),
55     ),
56     Select([ Identifier('*') ], Identifier('foo')),
57   ),
58   ['SELECT * FROM foo WHERE x = ?', binding(1)],
59   'simple select with where and bind';
60
61 dq_sql_is
62   Group (
63     [ Identifier('x') ],
64     Select([ Identifier('*') ], Identifier('foo')),
65   ),
66   ['SELECT * FROM foo GROUP BY x'],
67   'simple group by';
68
69 dq_sql_is
70   Select(
71     [ Identifier('*') ],
72     Join(
73       Identifier('foo'),
74       Identifier('bar'),
75       perl_operator('==', Identifier('foo', 'x'), Identifier('bar', 'y')),
76       'left outer',
77     ),
78   ),
79   ['SELECT * FROM foo LEFT OUTER JOIN bar ON foo.x = bar.y'],
80   'left outer join';
81
82 dq_sql_is
83   Select(
84     [ Operator({ 'SQL.Naive' => 'apply' }, [ Identifier('COUNT'), Identifier('*') ]) ],
85     Identifier('foo'),
86   ),
87   ['SELECT COUNT( * ) FROM foo'],
88   'count';
89
90 dq_sql_is
91   Where(
92     Operator({ 'SQL.Naive' => 'BETWEEN' }, [
93       Identifier('x'),
94       map Literal('SQL', '?', [ binding($_) ]), 1, 2
95     ]),
96     Select([ Identifier('*') ], Identifier('foo')),
97   ),
98   [ 'SELECT * FROM foo WHERE ( x BETWEEN ? AND ? )', map binding($_), 1, 2 ],
99   'between';
100
101 dq_sql_is
102   Select([ map Identifier($_), qw(a b) ], Identifier('foo')),
103   ['SELECT a, b FROM foo'],
104   'select columns';