Commit | Line | Data |
0bf8a8c4 |
1 | use strict; |
2 | use warnings; |
3 | |
ec376489 |
4 | use Test::More tests => 16; |
0bf8a8c4 |
5 | use Test::Differences; |
6 | |
7 | use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); |
8 | |
9 | my $sqla = SQL::Abstract->create(1); |
10 | |
11 | is $sqla->dispatch( |
1b85673a |
12 | { -type => 'expr', |
13 | op => '>', |
14 | args => [ |
627dcb62 |
15 | {-type => identifier => elements => [qw/me id/] }, |
1b85673a |
16 | { -type => 'value', value => 500 } |
17 | ] |
18 | } |
19 | ), "me.id > ?", |
9d7d0694 |
20 | "simple expr clause"; |
0bf8a8c4 |
21 | |
a464be15 |
22 | is $sqla->dispatch( |
23 | { -type => 'expr', |
24 | op => '>', |
25 | args => [ |
627dcb62 |
26 | {-type => 'identifier', elements => [qw/me id/]}, |
a464be15 |
27 | {-type => 'value', value => 500 } |
0bf8a8c4 |
28 | ] |
a464be15 |
29 | } |
30 | ), "me.id > ?", |
31 | "simple expr clause"; |
32 | |
e7996b3a |
33 | my $cols = [ |
34 | { -type => 'expr', |
35 | op => '>', |
36 | args => [ |
627dcb62 |
37 | {-type => 'identifier', elements => [qw/me id/]}, |
e7996b3a |
38 | {-type => 'value', value => 500 } |
39 | ] |
40 | }, |
41 | { -type => 'expr', |
42 | op => '==', |
43 | args => [ |
627dcb62 |
44 | {-type => 'identifier', elements => [qw/me name/]}, |
e7996b3a |
45 | {-type => 'value', value => '200' } |
46 | ] |
47 | }, |
48 | ]; |
49 | |
a464be15 |
50 | eq_or_diff( [ SQL::Abstract->generate( |
51 | { -ast_version => 1, |
52 | -type => 'expr', |
53 | op => 'and', |
e7996b3a |
54 | args => $cols, |
a464be15 |
55 | } |
0bf8a8c4 |
56 | ) ], |
a464be15 |
57 | [ "me.id > ? AND me.name = ?", |
0bf8a8c4 |
58 | [ 500, |
59 | '200' |
60 | ] |
61 | ], |
62 | "Where with binds" |
63 | ); |
64 | |
65 | |
66 | is $sqla->dispatch( |
e7996b3a |
67 | { -type => 'expr', op => 'or', args => $cols } |
68 | ), "me.id > ? OR me.name = ?", |
9d7d0694 |
69 | "expr clause (simple or)"; |
0bf8a8c4 |
70 | |
71 | |
72 | is $sqla->dispatch( |
e7996b3a |
73 | { -type => 'expr', op => 'or', |
74 | args => [ |
75 | { -type => 'expr', op => '==', |
627dcb62 |
76 | args => [ {-type => 'identifier', elements => [qw/me name/] }, {-type => 'value', value => 500 } ] |
e7996b3a |
77 | }, |
78 | { -type => 'expr', op => 'or', args => $cols } |
79 | ] |
80 | } |
81 | ), "me.name = ? OR me.id > ? OR me.name = ?", |
9d7d0694 |
82 | "expr clause (nested or)"; |
0bf8a8c4 |
83 | |
84 | is $sqla->dispatch( |
e7996b3a |
85 | { -type => 'expr', op => 'or', |
86 | args => [ |
87 | { -type => 'expr', op => '==', |
627dcb62 |
88 | args => [ {-type => 'identifier', elements => [qw/me name/] }, {-type => 'value', value => 500 } ] |
e7996b3a |
89 | }, |
90 | { -type => 'expr', op => 'and', args => $cols } |
91 | ] |
92 | } |
39f7dc30 |
93 | ), "me.name = ? OR (me.id > ? AND me.name = ?)", |
9d7d0694 |
94 | "expr clause (inner and)"; |
0bf8a8c4 |
95 | |
96 | is $sqla->dispatch( |
9d7d0694 |
97 | { -type => 'expr', op => 'and', args => [ |
98 | { -type => 'expr', op => '==', args => [ |
627dcb62 |
99 | {-type => 'identifier', elements => [qw/me id/] }, {-type => 'value', value => 200 } |
9d7d0694 |
100 | ], |
101 | }, |
102 | { -type => 'expr', op => 'and', args => $cols } |
103 | ] |
104 | } |
105 | ), "me.id = ? AND me.id > ? AND me.name = ?", |
106 | "expr clause (nested and)"; |
0bf8a8c4 |
107 | |
108 | |
109 | is $sqla->dispatch( |
9d7d0694 |
110 | { -type => 'expr', op => 'and', args => [ |
111 | { -type => 'expr', op => '==', args => [ |
627dcb62 |
112 | {-type => 'identifier', elements => [qw/me id/] }, {-type => 'value', value => 200 } |
9d7d0694 |
113 | ], |
114 | }, |
115 | { -type => 'expr', op => 'or', args => $cols } |
116 | ] |
117 | } |
118 | ), "me.id = ? AND (me.id > ? OR me.name = ?)", |
119 | "expr clause (inner or)"; |
120 | |
121 | is $sqla->dispatch( |
122 | { -type => 'expr', op => 'in', args => [ ] } |
123 | ), "0 = 1", "emtpy -in"; |
124 | |
125 | is $sqla->dispatch( |
126 | { -type => 'expr', |
127 | op => 'in', |
627dcb62 |
128 | args => [ { -type => 'identifier', elements => ['foo'] } ], |
9d7d0694 |
129 | } |
130 | ), "0 = 1", "emtpy -in"; |
0bf8a8c4 |
131 | |
132 | eq_or_diff( |
133 | [SQL::Abstract->generate( |
9d7d0694 |
134 | { -ast_version => 1, |
135 | -type => 'expr', |
136 | op => 'and', |
137 | args => [ |
138 | { -type => 'expr', |
139 | op => 'in', |
140 | args => [ |
627dcb62 |
141 | {-type => 'identifier', elements => [qw/me id/] }, |
9d7d0694 |
142 | {-type => 'value', value => 100 }, |
143 | {-type => 'value', value => 200 }, |
144 | {-type => 'value', value => 300 }, |
145 | ] |
146 | } |
0bf8a8c4 |
147 | ] |
9d7d0694 |
148 | } |
0bf8a8c4 |
149 | ) ], |
150 | |
9d7d0694 |
151 | [ "me.id IN (?, ?, ?)", |
152 | [ 100, 200, 300 ] |
0bf8a8c4 |
153 | ], |
154 | |
9d7d0694 |
155 | "IN expression"); |
0bf8a8c4 |
156 | |
157 | eq_or_diff( |
158 | [SQL::Abstract->generate( |
9d7d0694 |
159 | { -ast_version => 1, |
160 | -type => 'expr', |
161 | op => 'and', |
162 | args => [ |
163 | { -type => 'expr', |
164 | op => 'not_in', |
165 | args => [ |
627dcb62 |
166 | {-type => 'identifier', elements => [qw/me id/] }, |
9d7d0694 |
167 | {-type => 'value', value => 100 }, |
168 | {-type => 'value', value => 200 }, |
169 | {-type => 'value', value => 300 }, |
170 | ] |
171 | } |
0bf8a8c4 |
172 | ] |
9d7d0694 |
173 | } |
0bf8a8c4 |
174 | ) ], |
175 | |
9d7d0694 |
176 | [ "me.id NOT IN (?, ?, ?)", |
177 | [ 100, 200, 300 ] |
0bf8a8c4 |
178 | ], |
179 | |
9d7d0694 |
180 | "NOT IN clause"); |
db861e66 |
181 | |
182 | |
183 | is $sqla->dispatch( |
184 | { -type => 'expr', |
185 | op => 'like', |
186 | args => [ |
627dcb62 |
187 | {-type => identifier => elements => [qw/me id/] }, |
db861e66 |
188 | { -type => 'value', value => 500 } |
189 | ] |
190 | } |
191 | ), "me.id LIKE ?", |
192 | "LIKE expr clause"; |
ec376489 |
193 | |
194 | |
195 | is $sqla->dispatch( |
196 | { -type => 'expr', |
197 | op => '==', |
198 | args => [ |
627dcb62 |
199 | {-type => identifier => elements => [qw/me id/] }, |
ec376489 |
200 | { -type => 'value', value => undef } |
201 | ] |
202 | } |
203 | ), "me.id IS NULL", |
204 | "== undef"; |
205 | |
206 | |
207 | is $sqla->dispatch( |
208 | { -type => 'expr', |
209 | op => '!=', |
210 | args => [ |
627dcb62 |
211 | {-type => identifier => elements => [qw/me id/] }, |
ec376489 |
212 | { -type => 'value', value => undef } |
213 | ] |
214 | } |
215 | ), "me.id IS NOT NULL", |
216 | "!= undef"; |