Commit | Line | Data |
41751122 |
1 | #!/usr/bin/perl |
32eab2da |
2 | |
3 | use strict; |
41751122 |
4 | use warnings; |
5 | use Test::More; |
32eab2da |
6 | |
96449e8e |
7 | use FindBin; |
8 | use lib "$FindBin::Bin"; |
9 | use TestSqlAbstract; |
41751122 |
10 | |
96449e8e |
11 | plan tests => 64; |
32eab2da |
12 | |
13 | use SQL::Abstract; |
14 | |
15 | my @tests = ( |
16 | #1 |
17 | { |
18 | func => 'select', |
19 | args => ['test', '*'], |
20 | stmt => 'SELECT * FROM test', |
21 | stmt_q => 'SELECT * FROM `test`', |
22 | bind => [] |
23 | }, |
24 | #2 |
25 | { |
26 | func => 'select', |
27 | args => ['test', [qw(one two three)]], |
28 | stmt => 'SELECT one, two, three FROM test', |
29 | stmt_q => 'SELECT `one`, `two`, `three` FROM `test`', |
30 | bind => [] |
31 | }, |
32 | #3 |
33 | { |
34 | func => 'select', |
35 | args => ['test', '*', { a => 0 }, [qw/boom bada bing/]], |
36 | stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY boom, bada, bing', |
37 | stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `boom`, `bada`, `bing`', |
38 | bind => [0] |
39 | }, |
40 | #4 |
41 | { |
42 | func => 'select', |
43 | args => ['test', '*', [ { a => 5 }, { b => 6 } ]], |
44 | stmt => 'SELECT * FROM test WHERE ( ( a = ? ) OR ( b = ? ) )', |
45 | stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` = ? ) OR ( `b` = ? ) )', |
46 | bind => [5,6] |
47 | }, |
48 | #5 |
49 | { |
50 | func => 'select', |
51 | args => ['test', '*', undef, ['id']], |
52 | stmt => 'SELECT * FROM test ORDER BY id', |
53 | stmt_q => 'SELECT * FROM `test` ORDER BY `id`', |
54 | bind => [] |
55 | }, |
56 | #6 |
57 | { |
58 | func => 'select', |
59 | args => ['test', '*', { a => 'boom' } , ['id']], |
60 | stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY id', |
61 | stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `id`', |
62 | bind => ['boom'] |
63 | }, |
64 | #7 |
65 | { |
66 | func => 'select', |
67 | args => ['test', '*', { a => ['boom', 'bang'] }], |
68 | stmt => 'SELECT * FROM test WHERE ( ( ( a = ? ) OR ( a = ? ) ) )', |
69 | stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `a` = ? ) OR ( `a` = ? ) ) )', |
70 | bind => ['boom', 'bang'] |
71 | }, |
72 | #8 |
73 | { |
74 | func => 'select', |
75 | args => [[qw/test1 test2/], '*', { 'test1.a' => { 'In', ['boom', 'bang'] } }], |
76 | stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a IN ( ?, ? ) )', |
77 | stmt_q => 'SELECT * FROM `test1`, `test2` WHERE ( `test1`.`a` IN ( ?, ? ) )', |
78 | bind => ['boom', 'bang'] |
79 | }, |
80 | #9 |
81 | { |
82 | func => 'select', |
83 | args => ['test', '*', { a => { 'between', ['boom', 'bang'] } }], |
84 | stmt => 'SELECT * FROM test WHERE ( a BETWEEN ? AND ? )', |
85 | stmt_q => 'SELECT * FROM `test` WHERE ( `a` BETWEEN ? AND ? )', |
86 | bind => ['boom', 'bang'] |
87 | }, |
88 | #10 |
89 | { |
90 | func => 'select', |
91 | args => ['test', '*', { a => { '!=', 'boom' } }], |
92 | stmt => 'SELECT * FROM test WHERE ( a != ? )', |
93 | stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )', |
94 | bind => ['boom'] |
95 | }, |
96 | #11 |
97 | { |
98 | func => 'update', |
99 | args => ['test', {a => 'boom'}, {a => undef}], |
100 | stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )', |
101 | stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )', |
102 | bind => ['boom'] |
103 | }, |
104 | #12 |
105 | { |
106 | func => 'update', |
107 | args => ['test', {a => 'boom'}, { a => {'!=', "bang" }} ], |
108 | stmt => 'UPDATE test SET a = ? WHERE ( a != ? )', |
109 | stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` != ? )', |
110 | bind => ['boom', 'bang'] |
111 | }, |
112 | #13 |
113 | { |
114 | func => 'update', |
115 | args => ['test', {'a-funny-flavored-candy' => 'yummy', b => 'oops'}, { a42 => "bang" }], |
116 | stmt => 'UPDATE test SET a-funny-flavored-candy = ?, b = ? WHERE ( a42 = ? )', |
117 | stmt_q => 'UPDATE `test` SET `a-funny-flavored-candy` = ?, `b` = ? WHERE ( `a42` = ? )', |
118 | bind => ['yummy', 'oops', 'bang'] |
119 | }, |
120 | #14 |
121 | { |
122 | func => 'delete', |
123 | args => ['test', {requestor => undef}], |
124 | stmt => 'DELETE FROM test WHERE ( requestor IS NULL )', |
125 | stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )', |
126 | bind => [] |
127 | }, |
128 | #15 |
129 | { |
130 | func => 'delete', |
131 | args => [[qw/test1 test2 test3/], |
132 | { 'test1.field' => \'!= test2.field', |
133 | user => {'!=','nwiger'} }, |
134 | ], |
135 | stmt => 'DELETE FROM test1, test2, test3 WHERE ( test1.field != test2.field AND user != ? )', |
136 | stmt_q => 'DELETE FROM `test1`, `test2`, `test3` WHERE ( `test1`.`field` != test2.field AND `user` != ? )', # test2.field is a literal value, cannnot be quoted. |
137 | bind => ['nwiger'] |
138 | }, |
139 | #16 |
140 | { |
141 | func => 'insert', |
142 | args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}], |
143 | stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)', |
144 | stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)', |
145 | bind => [qw/1 2 3 4 5/], |
146 | }, |
147 | #17 |
148 | { |
149 | func => 'insert', |
150 | args => ['test', [qw/1 2 3 4 5/]], |
151 | stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?)', |
152 | stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?)', |
153 | bind => [qw/1 2 3 4 5/], |
154 | }, |
155 | #18 |
156 | { |
157 | func => 'insert', |
158 | args => ['test', [qw/1 2 3 4 5/, undef]], |
159 | stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)', |
160 | stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)', |
161 | bind => [qw/1 2 3 4 5/, undef], |
162 | }, |
163 | #19 |
164 | { |
165 | func => 'update', |
166 | args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}], |
167 | stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?', |
168 | stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?', |
169 | bind => [qw/1 2 3 4 5/], |
170 | }, |
171 | #20 |
172 | { |
173 | func => 'update', |
174 | args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}], |
175 | stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )', |
176 | stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )', |
177 | bind => [qw/1 2 3 4 5 1 2 3 4 5/], |
178 | }, |
179 | #21 |
180 | { |
181 | func => 'update', |
96449e8e |
182 | args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}], |
32eab2da |
183 | stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )', |
184 | stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )', |
185 | bind => [qw(1 02/02/02 1 2)], |
186 | }, |
187 | #22 |
188 | { |
189 | func => 'insert', |
190 | args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ], |
191 | stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)', |
192 | stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)', |
193 | bind => ['4'], |
194 | }, |
195 | #23 |
196 | { |
197 | func => 'insert', |
198 | new => {bindtype => 'columns'}, |
199 | args => ['test.table', {one => 2, three => 4, five => 6} ], |
200 | stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)', |
201 | stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)', |
202 | bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man... |
203 | }, |
204 | #24 |
205 | { |
206 | func => 'select', |
207 | new => {bindtype => 'columns', case => 'lower'}, |
208 | args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ], |
209 | stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )', |
210 | stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )', |
211 | bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man... |
212 | }, |
213 | #25 |
214 | { |
215 | func => 'update', |
216 | new => {bindtype => 'columns', cmp => 'like'}, |
217 | args => ['testin.table2', {One => 22, Three => 44, FIVE => 66}, |
218 | {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}], |
219 | stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE ' |
220 | . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )', |
221 | stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE ' |
222 | . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )', |
223 | bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'], |
224 | ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']], |
225 | }, |
226 | #26 |
227 | { |
228 | func => 'select', |
229 | args => ['test', '*', {priority => [ -and => {'!=', 2}, {'!=', 1} ]}], |
230 | stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority != ? ) ) )', |
231 | stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` != ? ) ) )', |
232 | bind => [qw(2 1)], |
233 | }, |
234 | #27 |
235 | { |
236 | func => 'select', |
237 | args => ['Yo Momma', '*', { user => 'nwiger', |
238 | -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }], |
239 | stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )', |
240 | stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )', |
241 | bind => [qw(20 ASIA nwiger)], |
242 | }, |
243 | #28 |
244 | { |
245 | func => 'update', |
246 | args => ['taco_punches', { one => 2, three => 4 }, |
247 | { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ], |
248 | tasty => { '!=', [qw(yes YES)] }, |
249 | -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] }, |
250 | ], |
96449e8e |
251 | # LDNOTE : modified the test below, same reasons as #14 in 00where.t |
32eab2da |
252 | stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )' |
96449e8e |
253 | # . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )', |
254 | . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) AND ( tasty != ? ) ) )', |
32eab2da |
255 | stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )' |
96449e8e |
256 | # . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )', |
257 | . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) AND ( `tasty` != ? ) ) )', |
32eab2da |
258 | bind => [qw(2 4 mr.happy yes YES yes YES)], |
259 | }, |
260 | #29 |
261 | { |
262 | func => 'select', |
263 | args => ['jeff', '*', { name => {'like', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']}, |
264 | -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ], |
96449e8e |
265 | yob => {'<', 1976} ] ] } ], |
266 | # LDNOTE : original test below was WRONG with respect to the doc. |
267 | # [-and, [cond1, cond2], cond3] should mean (cond1 OR cond2) AND cond3 |
268 | # instead of (cond1 AND cond2) OR cond3. |
269 | # Probably a misconception because of '=>' notation |
270 | # in [-and => [cond1, cond2], cond3]. |
271 | # Also some differences in parentheses, but without impact on semantics. |
272 | # stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )' |
273 | # . ' AND name NOT IN ( ?, ?, ?, ? ) AND name LIKE ? )', |
274 | # stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )' |
275 | # . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` LIKE ? )', |
276 | stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( age BETWEEN ? AND ? ) OR ( age != ? ) ) AND ( yob < ? ) ) )' |
277 | . ' AND ( name NOT IN ( ?, ?, ?, ? ) AND name LIKE ? ) )', |
278 | stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( `age` BETWEEN ? AND ? ) OR ( `age` != ? ) ) AND ( `yob` < ? ) ) )' |
279 | . ' AND ( `name` NOT IN ( ?, ?, ?, ? ) AND `name` LIKE ? ) )', |
32eab2da |
280 | bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)] |
281 | }, |
282 | #30 |
283 | { |
32eab2da |
284 | func => 'update', |
96449e8e |
285 | # LDNOTE : removed the "-maybe", because we no longer admit unknown ops |
286 | # args => ['fhole', {fpoles => 4}, [-maybe => {race => [-and => [qw(black white asian)]]}, |
287 | args => ['fhole', {fpoles => 4}, [ {race => [-and => [qw(black white asian)]]}, |
32eab2da |
288 | {-nest => {firsttime => [-or => {'=','yes'}, undef]}}, |
289 | [ -and => {firstname => {-not_like => 'candace'}}, {lastname => {-in => [qw(jugs canyon towers)]}} ] ] ], |
290 | stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )' |
291 | . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN ( ?, ?, ? ) ) ) )', |
292 | stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )' |
293 | . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN ( ?, ?, ? ) ) ) )', |
294 | bind => [qw(4 black white asian yes candace jugs canyon towers)] |
295 | }, |
96449e8e |
296 | #31 |
297 | { |
298 | func => 'insert', |
299 | args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}], |
300 | stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))', |
301 | stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))', |
302 | bind => [qw(1 02/02/02)], |
303 | }, |
304 | #32 |
305 | { |
306 | func => 'select', |
307 | # LDNOTE: modified test below because we agreed with MST that literal SQL |
308 | # should not automatically insert a '='; the user has to do it |
309 | # args => ['test', '*', { a => \["to_date(?, 'MM/DD/YY')", '02/02/02']}], |
310 | args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}], |
311 | stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )}, |
312 | stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )}, |
313 | bind => ['02/02/02'], |
314 | } |
32eab2da |
315 | ); |
316 | |
317 | use Data::Dumper; |
318 | |
319 | for (@tests) { |
96449e8e |
320 | local $"=', '; |
32eab2da |
321 | |
96449e8e |
322 | my $new = $_->{new} || {}; |
323 | $new->{debug} = $ENV{DEBUG} || 0; |
324 | my $sql = SQL::Abstract->new(%$new); |
32eab2da |
325 | |
96449e8e |
326 | #print "testing with args (@{$_->{args}}): "; |
327 | my $func = $_->{func}; |
328 | my($stmt, @bind) = $sql->$func(@{$_->{args}}); |
329 | is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind}); |
32eab2da |
330 | |
96449e8e |
331 | # test with quoted labels |
332 | my $sql_q = SQL::Abstract->new(%$new, quote_char => '`', name_sep => '.'); |
32eab2da |
333 | |
96449e8e |
334 | my $func_q = $_->{func}; |
335 | my($stmt_q, @bind_q) = $sql_q->$func_q(@{$_->{args}}); |
32eab2da |
336 | |
96449e8e |
337 | is_same_sql_bind($stmt_q, \@bind_q, $_->{stmt_q}, $_->{bind}); |
32eab2da |
338 | } |
339 | |
340 | |