Switch some tests to explicit parenthesis comparison
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
CommitLineData
41751122 1#!/usr/bin/perl
32eab2da 2
3use strict;
41751122 4use warnings;
5use Test::More;
7fb57243 6use Test::Warn;
fe3ae272 7use Test::Exception;
32eab2da 8
5aad8cf3 9use SQL::Abstract::Test import => ['is_same_sql_bind'];
32eab2da 10
11use SQL::Abstract;
12
13my @tests = (
14 #1
15 {
16 func => 'select',
17 args => ['test', '*'],
18 stmt => 'SELECT * FROM test',
19 stmt_q => 'SELECT * FROM `test`',
20 bind => []
21 },
22 #2
23 {
24 func => 'select',
25 args => ['test', [qw(one two three)]],
26 stmt => 'SELECT one, two, three FROM test',
27 stmt_q => 'SELECT `one`, `two`, `three` FROM `test`',
28 bind => []
29 },
30 #3
31 {
32 func => 'select',
33 args => ['test', '*', { a => 0 }, [qw/boom bada bing/]],
34 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY boom, bada, bing',
35 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `boom`, `bada`, `bing`',
36 bind => [0]
37 },
38 #4
39 {
40 func => 'select',
41 args => ['test', '*', [ { a => 5 }, { b => 6 } ]],
42 stmt => 'SELECT * FROM test WHERE ( ( a = ? ) OR ( b = ? ) )',
43 stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` = ? ) OR ( `b` = ? ) )',
44 bind => [5,6]
45 },
46 #5
47 {
48 func => 'select',
49 args => ['test', '*', undef, ['id']],
50 stmt => 'SELECT * FROM test ORDER BY id',
51 stmt_q => 'SELECT * FROM `test` ORDER BY `id`',
52 bind => []
53 },
54 #6
55 {
56 func => 'select',
57 args => ['test', '*', { a => 'boom' } , ['id']],
58 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY id',
59 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `id`',
60 bind => ['boom']
61 },
62 #7
63 {
64 func => 'select',
65 args => ['test', '*', { a => ['boom', 'bang'] }],
66 stmt => 'SELECT * FROM test WHERE ( ( ( a = ? ) OR ( a = ? ) ) )',
67 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `a` = ? ) OR ( `a` = ? ) ) )',
68 bind => ['boom', 'bang']
69 },
70 #8
71 {
72 func => 'select',
73 args => [[qw/test1 test2/], '*', { 'test1.a' => { 'In', ['boom', 'bang'] } }],
74 stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a IN ( ?, ? ) )',
75 stmt_q => 'SELECT * FROM `test1`, `test2` WHERE ( `test1`.`a` IN ( ?, ? ) )',
76 bind => ['boom', 'bang']
77 },
78 #9
79 {
80 func => 'select',
81 args => ['test', '*', { a => { 'between', ['boom', 'bang'] } }],
82 stmt => 'SELECT * FROM test WHERE ( a BETWEEN ? AND ? )',
83 stmt_q => 'SELECT * FROM `test` WHERE ( `a` BETWEEN ? AND ? )',
84 bind => ['boom', 'bang']
85 },
86 #10
87 {
88 func => 'select',
89 args => ['test', '*', { a => { '!=', 'boom' } }],
90 stmt => 'SELECT * FROM test WHERE ( a != ? )',
91 stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
92 bind => ['boom']
93 },
94 #11
95 {
96 func => 'update',
97 args => ['test', {a => 'boom'}, {a => undef}],
98 stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )',
99 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )',
100 bind => ['boom']
101 },
102 #12
103 {
104 func => 'update',
105 args => ['test', {a => 'boom'}, { a => {'!=', "bang" }} ],
106 stmt => 'UPDATE test SET a = ? WHERE ( a != ? )',
107 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` != ? )',
108 bind => ['boom', 'bang']
109 },
110 #13
111 {
112 func => 'update',
113 args => ['test', {'a-funny-flavored-candy' => 'yummy', b => 'oops'}, { a42 => "bang" }],
114 stmt => 'UPDATE test SET a-funny-flavored-candy = ?, b = ? WHERE ( a42 = ? )',
115 stmt_q => 'UPDATE `test` SET `a-funny-flavored-candy` = ?, `b` = ? WHERE ( `a42` = ? )',
116 bind => ['yummy', 'oops', 'bang']
117 },
118 #14
119 {
120 func => 'delete',
121 args => ['test', {requestor => undef}],
122 stmt => 'DELETE FROM test WHERE ( requestor IS NULL )',
123 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )',
124 bind => []
125 },
126 #15
127 {
128 func => 'delete',
129 args => [[qw/test1 test2 test3/],
130 { 'test1.field' => \'!= test2.field',
131 user => {'!=','nwiger'} },
132 ],
133 stmt => 'DELETE FROM test1, test2, test3 WHERE ( test1.field != test2.field AND user != ? )',
134 stmt_q => 'DELETE FROM `test1`, `test2`, `test3` WHERE ( `test1`.`field` != test2.field AND `user` != ? )', # test2.field is a literal value, cannnot be quoted.
135 bind => ['nwiger']
136 },
137 #16
138 {
139 func => 'insert',
140 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
141 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
142 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
143 bind => [qw/1 2 3 4 5/],
144 },
145 #17
146 {
147 func => 'insert',
148 args => ['test', [qw/1 2 3 4 5/]],
149 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?)',
150 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?)',
151 bind => [qw/1 2 3 4 5/],
152 },
153 #18
154 {
155 func => 'insert',
156 args => ['test', [qw/1 2 3 4 5/, undef]],
157 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)',
158 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)',
159 bind => [qw/1 2 3 4 5/, undef],
160 },
161 #19
162 {
163 func => 'update',
164 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
165 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?',
166 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?',
167 bind => [qw/1 2 3 4 5/],
168 },
169 #20
170 {
171 func => 'update',
172 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}],
173 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )',
174 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )',
175 bind => [qw/1 2 3 4 5 1 2 3 4 5/],
176 },
177 #21
178 {
179 func => 'update',
96449e8e 180 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
32eab2da 181 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
182 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
183 bind => [qw(1 02/02/02 1 2)],
184 },
185 #22
186 {
187 func => 'insert',
188 args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ],
189 stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)',
190 stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)',
191 bind => ['4'],
192 },
257d9a42 193 {
194 func => 'insert',
195 args => ['test.table', [ \'max(all_limits)', 4 ] ],
196 stmt => 'INSERT INTO test.table VALUES (max(all_limits), ?)',
197 stmt_q => 'INSERT INTO `test`.`table` VALUES (max(all_limits), ?)',
198 bind => ['4'],
199 },
32eab2da 200 #23
201 {
202 func => 'insert',
203 new => {bindtype => 'columns'},
204 args => ['test.table', {one => 2, three => 4, five => 6} ],
205 stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
206 stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
207 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
208 },
209 #24
210 {
211 func => 'select',
212 new => {bindtype => 'columns', case => 'lower'},
213 args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
214 stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
215 stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
216 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
217 },
218 #25
219 {
220 func => 'update',
221 new => {bindtype => 'columns', cmp => 'like'},
222 args => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
223 {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
224 stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
225 . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
226 stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
227 . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
228 bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
229 ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
230 },
231 #26
232 {
233 func => 'select',
234 args => ['test', '*', {priority => [ -and => {'!=', 2}, {'!=', 1} ]}],
235 stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority != ? ) ) )',
236 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` != ? ) ) )',
237 bind => [qw(2 1)],
238 },
239 #27
240 {
241 func => 'select',
242 args => ['Yo Momma', '*', { user => 'nwiger',
99ecc29e 243 -paren => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
244 stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? OR geo = ? ) AND user = ? ) )',
245 stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? OR `geo` = ? ) AND `user` = ? ) )',
32eab2da 246 bind => [qw(20 ASIA nwiger)],
99ecc29e 247 keep_paren => 1,
32eab2da 248 },
249 #28
250 {
251 func => 'update',
252 args => ['taco_punches', { one => 2, three => 4 },
253 { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
254 tasty => { '!=', [qw(yes YES)] },
99ecc29e 255 -paren => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
32eab2da 256 ],
257 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
e30faf88 258 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
32eab2da 259 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
e30faf88 260 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
32eab2da 261 bind => [qw(2 4 mr.happy yes YES yes YES)],
99ecc29e 262 keep_paren => 1,
32eab2da 263 },
264 #29
265 {
266 func => 'select',
267 args => ['jeff', '*', { name => {'like', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
99ecc29e 268 -paren => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
96449e8e 269 yob => {'<', 1976} ] ] } ],
99ecc29e 270 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( age BETWEEN ? AND ? ) AND age != ? ) OR yob < ? )'
271 . ' AND ( name NOT IN ( ?, ?, ?, ? ) AND name LIKE ? ) ) )',
272 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( `age` BETWEEN ? AND ? ) AND `age` != ?) OR `yob` < ? )'
273 . ' AND ( `name` NOT IN ( ?, ?, ?, ? ) AND `name` LIKE ? ) ) )',
274 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)],
275 keep_paren => 1,
32eab2da 276 },
277 #30
278 {
32eab2da 279 func => 'update',
96449e8e 280# LDNOTE : removed the "-maybe", because we no longer admit unknown ops
e30faf88 281#
282# acked by RIBASUSHI
96449e8e 283# args => ['fhole', {fpoles => 4}, [-maybe => {race => [-and => [qw(black white asian)]]},
284 args => ['fhole', {fpoles => 4}, [ {race => [-and => [qw(black white asian)]]},
99ecc29e 285 {-paren => {firsttime => [-or => {'=','yes'}, undef]}},
32eab2da 286 [ -and => {firstname => {-not_like => 'candace'}}, {lastname => {-in => [qw(jugs canyon towers)]}} ] ] ],
99ecc29e 287 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( race = ? OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
32eab2da 288 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN ( ?, ?, ? ) ) ) )',
289 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
290 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN ( ?, ?, ? ) ) ) )',
99ecc29e 291 bind => [qw(4 black white asian yes candace jugs canyon towers)],
292 keep_paren => 1,
293
32eab2da 294 },
96449e8e 295 #31
296 {
297 func => 'insert',
298 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
299 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
300 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
301 bind => [qw(1 02/02/02)],
302 },
303 #32
304 {
305 func => 'select',
306# LDNOTE: modified test below because we agreed with MST that literal SQL
307# should not automatically insert a '='; the user has to do it
e30faf88 308#
309# acked by MSTROUT
96449e8e 310# args => ['test', '*', { a => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
311 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
312 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
313 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
314 bind => ['02/02/02'],
d82b8afb 315 },
316 #33
317 {
318 func => 'insert',
319 new => {array_datatypes => 1},
320 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
321 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
322 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
323 bind => [1, [1, 1, 2, 3, 5, 8]],
324 },
325 #34
326 {
327 func => 'insert',
328 new => {bindtype => 'columns', array_datatypes => 1},
329 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
330 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
331 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
332 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
333 },
334 #35
335 {
336 func => 'update',
337 new => {array_datatypes => 1},
338 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
339 stmt => 'UPDATE test SET a = ?, b = ?',
340 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
341 bind => [1, [1, 1, 2, 3, 5, 8]],
342 },
343 #36
344 {
345 func => 'update',
346 new => {bindtype => 'columns', array_datatypes => 1},
347 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
348 stmt => 'UPDATE test SET a = ?, b = ?',
349 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
350 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
351 },
145fbfc8 352 #37
353 {
354 func => 'select',
355 args => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
356 stmt => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
357 stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
358 bind => [8],
359 },
b3be7bd0 360 #38
361 {
362 func => 'select',
363 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
364 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
365 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
366 bind => ['02/02/02', 8],
367 },
5db47f9f 368 #39
369 { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
370 func => 'insert',
371 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => { answer => 42 }}],
372 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
373 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
374 bind => [qw/1 2 3 4/, { answer => 42}],
7fb57243 375 warning_like => qr/HASH ref as bind value in insert is not supported/i,
5db47f9f 376 },
ef4d99a5 377 #40
378 {
379 func => 'update',
380 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
381 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
382 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
383 bind => [qw(1 1 2)],
384 },
385 #41
386 {
387 func => 'insert',
388 args => ['test', {a => 1, b => \["42"]}],
389 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
390 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
391 bind => [qw(1)],
392 },
393 #42
394 {
395 func => 'select',
396 args => ['test', '*', { a => \["= 42"], b => 1}],
397 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
398 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
399 bind => [qw(1)],
400 },
401 #43
402 {
403 func => 'select',
404 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
405 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
406 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
407 bind => [qw(8)],
408 },
cd87fd4c 409 #44
410 {
411 func => 'insert',
412 new => {bindtype => 'columns'},
fe3ae272 413 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 414 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
415 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
fe3ae272 416 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 417 },
418 #45
419 {
420 func => 'update',
421 new => {bindtype => 'columns'},
fe3ae272 422 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 423 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
424 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
fe3ae272 425 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
cd87fd4c 426 },
427 #46
428 {
429 func => 'select',
430 new => {bindtype => 'columns'},
fe3ae272 431 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 432 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
433 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
fe3ae272 434 bind => [[dummy => '02/02/02']],
cd87fd4c 435 },
436 #47
437 {
438 func => 'select',
439 new => {bindtype => 'columns'},
fe3ae272 440 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 441 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
442 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
fe3ae272 443 bind => [[dummy => '02/02/02'], [b => 8]],
444 },
445 #48
446 {
447 func => 'insert',
448 new => {bindtype => 'columns'},
449 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
450 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
451 },
452 #49
453 {
454 func => 'update',
455 new => {bindtype => 'columns'},
456 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
457 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
458 },
459 #49
460 {
461 func => 'select',
462 new => {bindtype => 'columns'},
463 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
464 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
465 },
466 #50
467 {
468 func => 'select',
469 new => {bindtype => 'columns'},
470 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
471 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
cd87fd4c 472 },
044e8ff4 473 #51
474 {
475 func => 'select',
476 new => {bindtype => 'columns'},
477 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
478 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
479 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
480 bind => [[dummy => '02/02/02'], [b => 8]],
481 },
482 #52
483 {
484 func => 'select',
485 new => {bindtype => 'columns'},
486 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
487 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
488 },
26f2dca5 489 #53
490 {
491 func => 'insert',
492 new => {bindtype => 'columns'},
493 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
494 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
495 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
496 bind => [[a => '1'], [{dummy => 1} => '02/02/02']],
497 },
498 #54
499 {
500 func => 'update',
501 new => {bindtype => 'columns'},
502 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, {a => {'between', [1,2]}}],
503 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
504 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
505 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [a => '1'], [a => '2']],
506 },
507 #55
508 {
509 func => 'select',
510 new => {bindtype => 'columns'},
511 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
512 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
513 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
514 bind => [[{dummy => 1} => '02/02/02']],
515 },
516 #56
517 {
518 func => 'select',
519 new => {bindtype => 'columns'},
520 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
521 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
522 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
523 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
05a05fd1 524 },
97b9b66e 525 #57
526 {
527 func => 'select',
528 new => {bindtype => 'columns'},
e30faf88 529 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
530 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
531 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
97b9b66e 532 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 533 },
534 #58
535 {
536 func => 'select',
537 new => {bindtype => 'columns'},
538 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
539 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
540 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
541 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
542 },
543 #59
544 {
545 func => 'select',
546 new => {bindtype => 'columns'},
547 args => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
548 stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
549 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
550 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
551 },
32eab2da 552);
553
6dcf723c 554
7fb57243 555plan tests => scalar(grep { !$_->{warning_like} } @tests) * 2
556 + scalar(grep { $_->{warning_like} } @tests) * 4;
32eab2da 557
558for (@tests) {
96449e8e 559 local $"=', ';
32eab2da 560
96449e8e 561 my $new = $_->{new} || {};
562 $new->{debug} = $ENV{DEBUG} || 0;
32eab2da 563
99ecc29e 564 local $SQL::Abstract::Test::parenthesis_significant = $_->{keep_paren};
565
7fb57243 566 # test without quoting labels
567 {
568 my $sql = SQL::Abstract->new(%$new);
569
570 my $func = $_->{func};
571 my($stmt, @bind);
572 my $test = sub {
573 ($stmt, @bind) = $sql->$func(@{$_->{args}})
574 };
fe3ae272 575 if ($_->{exception_like}) {
576 throws_ok { &$test } $_->{exception_like}, "throws the expected exception ($_->{exception_like})";
7fb57243 577 } else {
fe3ae272 578 if ($_->{warning_like}) {
579 warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
580 } else {
581 &$test;
582 }
583 is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
7fb57243 584 }
7fb57243 585 }
32eab2da 586
96449e8e 587 # test with quoted labels
7fb57243 588 {
589 my $sql_q = SQL::Abstract->new(%$new, quote_char => '`', name_sep => '.');
32eab2da 590
7fb57243 591 my $func_q = $_->{func};
592 my($stmt_q, @bind_q);
593 my $test = sub {
594 ($stmt_q, @bind_q) = $sql_q->$func_q(@{$_->{args}})
595 };
fe3ae272 596 if ($_->{exception_like}) {
597 throws_ok { &$test } $_->{exception_like}, "throws the expected exception ($_->{exception_like})";
7fb57243 598 } else {
fe3ae272 599 if ($_->{warning_like}) {
600 warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
601 } else {
602 &$test;
603 }
32eab2da 604
fe3ae272 605 is_same_sql_bind($stmt_q, \@bind_q, $_->{stmt_q}, $_->{bind});
606 }
7fb57243 607 }
32eab2da 608}