Bumped version number to 1.49_03.
[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 },
193 #23
194 {
195 func => 'insert',
196 new => {bindtype => 'columns'},
197 args => ['test.table', {one => 2, three => 4, five => 6} ],
198 stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
199 stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
200 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
201 },
202 #24
203 {
204 func => 'select',
205 new => {bindtype => 'columns', case => 'lower'},
206 args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
207 stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
208 stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
209 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
210 },
211 #25
212 {
213 func => 'update',
214 new => {bindtype => 'columns', cmp => 'like'},
215 args => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
216 {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
217 stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
218 . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
219 stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
220 . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
221 bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
222 ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
223 },
224 #26
225 {
226 func => 'select',
227 args => ['test', '*', {priority => [ -and => {'!=', 2}, {'!=', 1} ]}],
228 stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority != ? ) ) )',
229 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` != ? ) ) )',
230 bind => [qw(2 1)],
231 },
232 #27
233 {
234 func => 'select',
235 args => ['Yo Momma', '*', { user => 'nwiger',
236 -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
237 stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )',
238 stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )',
239 bind => [qw(20 ASIA nwiger)],
240 },
241 #28
242 {
243 func => 'update',
244 args => ['taco_punches', { one => 2, three => 4 },
245 { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
246 tasty => { '!=', [qw(yes YES)] },
247 -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
248 ],
96449e8e 249# LDNOTE : modified the test below, same reasons as #14 in 00where.t
32eab2da 250 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
96449e8e 251# . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
252 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) AND ( tasty != ? ) ) )',
32eab2da 253 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
96449e8e 254# . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
255 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) AND ( `tasty` != ? ) ) )',
32eab2da 256 bind => [qw(2 4 mr.happy yes YES yes YES)],
257 },
258 #29
259 {
260 func => 'select',
261 args => ['jeff', '*', { name => {'like', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
262 -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
96449e8e 263 yob => {'<', 1976} ] ] } ],
264# LDNOTE : original test below was WRONG with respect to the doc.
265# [-and, [cond1, cond2], cond3] should mean (cond1 OR cond2) AND cond3
266# instead of (cond1 AND cond2) OR cond3.
267# Probably a misconception because of '=>' notation
268# in [-and => [cond1, cond2], cond3].
269# Also some differences in parentheses, but without impact on semantics.
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 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( age BETWEEN ? AND ? ) OR ( age != ? ) ) AND ( yob < ? ) ) )'
275 . ' AND ( name NOT IN ( ?, ?, ?, ? ) AND name LIKE ? ) )',
276 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( `age` BETWEEN ? AND ? ) OR ( `age` != ? ) ) AND ( `yob` < ? ) ) )'
277 . ' AND ( `name` NOT IN ( ?, ?, ?, ? ) AND `name` LIKE ? ) )',
32eab2da 278 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
279 },
280 #30
281 {
32eab2da 282 func => 'update',
96449e8e 283# LDNOTE : removed the "-maybe", because we no longer admit unknown ops
284# args => ['fhole', {fpoles => 4}, [-maybe => {race => [-and => [qw(black white asian)]]},
285 args => ['fhole', {fpoles => 4}, [ {race => [-and => [qw(black white asian)]]},
32eab2da 286 {-nest => {firsttime => [-or => {'=','yes'}, undef]}},
287 [ -and => {firstname => {-not_like => 'candace'}}, {lastname => {-in => [qw(jugs canyon towers)]}} ] ] ],
288 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
289 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN ( ?, ?, ? ) ) ) )',
290 stmt_q => '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 bind => [qw(4 black white asian yes candace jugs canyon towers)]
293 },
96449e8e 294 #31
295 {
296 func => 'insert',
297 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
298 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
299 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
300 bind => [qw(1 02/02/02)],
301 },
302 #32
303 {
304 func => 'select',
305# LDNOTE: modified test below because we agreed with MST that literal SQL
306# should not automatically insert a '='; the user has to do it
307# args => ['test', '*', { a => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
308 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
309 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
310 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
311 bind => ['02/02/02'],
d82b8afb 312 },
313 #33
314 {
315 func => 'insert',
316 new => {array_datatypes => 1},
317 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
318 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
319 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
320 bind => [1, [1, 1, 2, 3, 5, 8]],
321 },
322 #34
323 {
324 func => 'insert',
325 new => {bindtype => 'columns', array_datatypes => 1},
326 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
327 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
328 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
329 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
330 },
331 #35
332 {
333 func => 'update',
334 new => {array_datatypes => 1},
335 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
336 stmt => 'UPDATE test SET a = ?, b = ?',
337 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
338 bind => [1, [1, 1, 2, 3, 5, 8]],
339 },
340 #36
341 {
342 func => 'update',
343 new => {bindtype => 'columns', array_datatypes => 1},
344 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
345 stmt => 'UPDATE test SET a = ?, b = ?',
346 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
347 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
348 },
145fbfc8 349 #37
350 {
351 func => 'select',
352 args => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
353 stmt => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
354 stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
355 bind => [8],
356 },
b3be7bd0 357 #38
358 {
359 func => 'select',
360 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
361 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
362 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
363 bind => ['02/02/02', 8],
364 },
5db47f9f 365 #39
366 { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
367 func => 'insert',
368 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => { answer => 42 }}],
369 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
370 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
371 bind => [qw/1 2 3 4/, { answer => 42}],
7fb57243 372 warning_like => qr/HASH ref as bind value in insert is not supported/i,
5db47f9f 373 },
ef4d99a5 374 #40
375 {
376 func => 'update',
377 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
378 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
379 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
380 bind => [qw(1 1 2)],
381 },
382 #41
383 {
384 func => 'insert',
385 args => ['test', {a => 1, b => \["42"]}],
386 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
387 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
388 bind => [qw(1)],
389 },
390 #42
391 {
392 func => 'select',
393 args => ['test', '*', { a => \["= 42"], b => 1}],
394 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
395 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
396 bind => [qw(1)],
397 },
398 #43
399 {
400 func => 'select',
401 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
402 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
403 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
404 bind => [qw(8)],
405 },
cd87fd4c 406 #44
407 {
408 func => 'insert',
409 new => {bindtype => 'columns'},
fe3ae272 410 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 411 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
412 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
fe3ae272 413 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 414 },
415 #45
416 {
417 func => 'update',
418 new => {bindtype => 'columns'},
fe3ae272 419 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 420 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
421 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
fe3ae272 422 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
cd87fd4c 423 },
424 #46
425 {
426 func => 'select',
427 new => {bindtype => 'columns'},
fe3ae272 428 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 429 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
430 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
fe3ae272 431 bind => [[dummy => '02/02/02']],
cd87fd4c 432 },
433 #47
434 {
435 func => 'select',
436 new => {bindtype => 'columns'},
fe3ae272 437 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 438 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
439 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
fe3ae272 440 bind => [[dummy => '02/02/02'], [b => 8]],
441 },
442 #48
443 {
444 func => 'insert',
445 new => {bindtype => 'columns'},
446 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
447 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
448 },
449 #49
450 {
451 func => 'update',
452 new => {bindtype => 'columns'},
453 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
454 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
455 },
456 #49
457 {
458 func => 'select',
459 new => {bindtype => 'columns'},
460 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
461 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
462 },
463 #50
464 {
465 func => 'select',
466 new => {bindtype => 'columns'},
467 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
468 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
cd87fd4c 469 },
044e8ff4 470 #51
471 {
472 func => 'select',
473 new => {bindtype => 'columns'},
474 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
475 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
476 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
477 bind => [[dummy => '02/02/02'], [b => 8]],
478 },
479 #52
480 {
481 func => 'select',
482 new => {bindtype => 'columns'},
483 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
484 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
485 },
32eab2da 486);
487
6dcf723c 488
7fb57243 489plan tests => scalar(grep { !$_->{warning_like} } @tests) * 2
490 + scalar(grep { $_->{warning_like} } @tests) * 4;
32eab2da 491
492for (@tests) {
96449e8e 493 local $"=', ';
32eab2da 494
96449e8e 495 my $new = $_->{new} || {};
496 $new->{debug} = $ENV{DEBUG} || 0;
32eab2da 497
7fb57243 498 # test without quoting labels
499 {
500 my $sql = SQL::Abstract->new(%$new);
501
502 my $func = $_->{func};
503 my($stmt, @bind);
504 my $test = sub {
505 ($stmt, @bind) = $sql->$func(@{$_->{args}})
506 };
fe3ae272 507 if ($_->{exception_like}) {
508 throws_ok { &$test } $_->{exception_like}, "throws the expected exception ($_->{exception_like})";
7fb57243 509 } else {
fe3ae272 510 if ($_->{warning_like}) {
511 warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
512 } else {
513 &$test;
514 }
515 is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
7fb57243 516 }
7fb57243 517 }
32eab2da 518
96449e8e 519 # test with quoted labels
7fb57243 520 {
521 my $sql_q = SQL::Abstract->new(%$new, quote_char => '`', name_sep => '.');
32eab2da 522
7fb57243 523 my $func_q = $_->{func};
524 my($stmt_q, @bind_q);
525 my $test = sub {
526 ($stmt_q, @bind_q) = $sql_q->$func_q(@{$_->{args}})
527 };
fe3ae272 528 if ($_->{exception_like}) {
529 throws_ok { &$test } $_->{exception_like}, "throws the expected exception ($_->{exception_like})";
7fb57243 530 } else {
fe3ae272 531 if ($_->{warning_like}) {
532 warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
533 } else {
534 &$test;
535 }
32eab2da 536
fe3ae272 537 is_same_sql_bind($stmt_q, \@bind_q, $_->{stmt_q}, $_->{bind});
538 }
7fb57243 539 }
32eab2da 540}