7 use SQL::Abstract::Test import => [qw( is_same_sql_bind diag_where dumper )];
13 # -nest has been undocumented on purpose, but is still supported for the
14 # foreseable future. Do not rip out the -nest tests before speaking to
15 # someone on the DBIC mailing list or in irc.perl.org#dbix-class
23 args => ['test', '*'],
24 stmt => 'SELECT * FROM test',
25 stmt_q => 'SELECT * FROM `test`',
30 args => ['test', [qw(one two three)]],
31 stmt => 'SELECT one, two, three FROM test',
32 stmt_q => 'SELECT `one`, `two`, `three` FROM `test`',
37 args => ['test', '*', { a => 0 }, [qw/boom bada bing/]],
38 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY boom, bada, bing',
39 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `boom`, `bada`, `bing`',
44 args => ['test', '*', [ { a => 5 }, { b => 6 } ]],
45 stmt => 'SELECT * FROM test WHERE ( ( a = ? ) OR ( b = ? ) )',
46 stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` = ? ) OR ( `b` = ? ) )',
51 args => ['test', '*', undef, ['id']],
52 stmt => 'SELECT * FROM test ORDER BY id',
53 stmt_q => 'SELECT * FROM `test` ORDER BY `id`',
58 args => ['test', '*', { a => 'boom' } , ['id']],
59 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY id',
60 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `id`',
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']
72 args => ['test', '*', { a => { '!=', 'boom' } }],
73 stmt => 'SELECT * FROM test WHERE ( a != ? )',
74 stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
79 args => ['test', {a => 'boom'}, {a => undef}],
80 stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )',
81 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )',
86 args => ['test', {a => 'boom'}, { a => {'!=', "bang" }} ],
87 stmt => 'UPDATE test SET a = ? WHERE ( a != ? )',
88 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` != ? )',
89 bind => ['boom', 'bang']
93 args => ['test', {'a-funny-flavored-candy' => 'yummy', b => 'oops'}, { a42 => "bang" }],
94 stmt => 'UPDATE test SET a-funny-flavored-candy = ?, b = ? WHERE ( a42 = ? )',
95 stmt_q => 'UPDATE `test` SET `a-funny-flavored-candy` = ?, `b` = ? WHERE ( `a42` = ? )',
96 bind => ['yummy', 'oops', 'bang']
100 args => ['test', {requestor => undef}],
101 stmt => 'DELETE FROM test WHERE ( requestor IS NULL )',
102 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )',
107 args => [[qw/test1 test2 test3/],
108 { 'test1.field' => \'!= test2.field',
109 user => {'!=','nwiger'} },
111 stmt => 'DELETE FROM test1, test2, test3 WHERE ( test1.field != test2.field AND user != ? )',
112 stmt_q => 'DELETE FROM `test1`, `test2`, `test3` WHERE ( `test1`.`field` != test2.field AND `user` != ? )', # test2.field is a literal value, cannnot be quoted.
117 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
118 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
119 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
120 bind => [qw/1 2 3 4 5/],
124 args => ['test', [qw/1 2 3 4 5/]],
125 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?)',
126 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?)',
127 bind => [qw/1 2 3 4 5/],
131 args => ['test', [qw/1 2 3 4 5/, undef]],
132 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)',
133 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)',
134 bind => [qw/1 2 3 4 5/, undef],
138 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
139 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?',
140 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?',
141 bind => [qw/1 2 3 4 5/],
145 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}],
146 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )',
147 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )',
148 bind => [qw/1 2 3 4 5 1 2 3 4 5/],
152 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
153 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
154 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
155 bind => [qw(1 02/02/02 1 2)],
159 args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ],
160 stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)',
161 stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)',
166 args => ['test.table', [ \'max(all_limits)', 4 ] ],
167 stmt => 'INSERT INTO test.table VALUES (max(all_limits), ?)',
168 stmt_q => 'INSERT INTO `test`.`table` VALUES (max(all_limits), ?)',
173 new => {bindtype => 'columns'},
174 args => ['test.table', {one => 2, three => 4, five => 6} ],
175 stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
176 stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
177 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
181 new => {bindtype => 'columns', case => 'lower'},
182 args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
183 stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
184 stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
185 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
189 new => {bindtype => 'columns', cmp => 'like'},
190 args => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
191 {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
192 stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
193 . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
194 stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
195 . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
196 bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
197 ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
201 args => ['test', '*', {priority => [ -and => {'!=', 2}, { -not_like => '3%'} ]}],
202 stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority NOT LIKE ? ) ) )',
203 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` NOT LIKE ? ) ) )',
208 args => ['Yo Momma', '*', { user => 'nwiger',
209 -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
210 stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )',
211 stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )',
212 bind => [qw(20 ASIA nwiger)],
216 args => ['taco_punches', { one => 2, three => 4 },
217 { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
218 tasty => { '!=', [qw(yes YES)] },
219 -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
221 warns => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
223 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
224 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
225 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
226 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
227 bind => [qw(2 4 mr.happy yes YES yes YES)],
231 args => ['jeff', '*', { name => {'ilike', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
232 -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
233 yob => {'<', 1976} ] ] } ],
234 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )'
235 . ' AND name NOT IN ( ?, ?, ?, ? ) AND name ILIKE ? )',
236 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )'
237 . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` ILIKE ? )',
238 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
242 args => ['fhole', {fpoles => 4}, [
243 { race => [qw/-or black white asian /] },
244 { -nest => { firsttime => [-or => {'=','yes'}, undef] } },
245 { -and => [ { firstname => {-not_like => 'candace'} }, { lastname => {-in => [qw(jugs canyon towers)] } } ] },
247 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
248 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
249 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
250 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
251 bind => [qw(4 black white asian yes candace jugs canyon towers)]
255 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
256 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
257 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
258 bind => [qw(1 02/02/02)],
262 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
263 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
264 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
265 bind => ['02/02/02'],
269 new => {array_datatypes => 1},
270 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
271 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
272 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
273 bind => [1, [1, 1, 2, 3, 5, 8]],
277 new => {bindtype => 'columns', array_datatypes => 1},
278 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
279 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
280 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
281 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
285 new => {array_datatypes => 1},
286 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
287 stmt => 'UPDATE test SET a = ?, b = ?',
288 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
289 bind => [1, [1, 1, 2, 3, 5, 8]],
293 new => {bindtype => 'columns', array_datatypes => 1},
294 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
295 stmt => 'UPDATE test SET a = ?, b = ?',
296 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
297 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
301 args => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
302 stmt => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
303 stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
308 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
309 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
310 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
311 bind => ['02/02/02', 8],
313 { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
315 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => { answer => 42 }}],
316 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
317 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
318 bind => [qw/1 2 3 4/, { answer => 42}],
319 warns => qr/HASH ref as bind value in insert is not supported/i,
323 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
324 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
325 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
330 args => ['test', {a => 1, b => \["42"]}],
331 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
332 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
337 args => ['test', '*', { a => \["= 42"], b => 1}],
338 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
339 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
344 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
345 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
346 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
351 new => {bindtype => 'columns'},
352 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
353 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
354 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
355 bind => [[a => '1'], [dummy => '02/02/02']],
359 new => {bindtype => 'columns'},
360 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
361 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
362 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
363 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
367 new => {bindtype => 'columns'},
368 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
369 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
370 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
371 bind => [[dummy => '02/02/02']],
375 new => {bindtype => 'columns'},
376 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
377 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
378 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
379 bind => [[dummy => '02/02/02'], [b => 8]],
383 new => {bindtype => 'columns'},
384 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
385 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
389 new => {bindtype => 'columns'},
390 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
391 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
395 new => {bindtype => 'columns'},
396 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
397 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
401 new => {bindtype => 'columns'},
402 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
403 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
407 args => ['test', '*', { foo => { '>=' => [] }} ],
408 throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
412 new => {bindtype => 'columns'},
413 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
414 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
415 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
416 bind => [[dummy => '02/02/02'], [b => 8]],
420 new => {bindtype => 'columns'},
421 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
422 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
426 new => {bindtype => 'columns'},
427 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
428 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
429 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
430 bind => [[a => '1'], [{dummy => 1} => '02/02/02']],
434 new => {bindtype => 'columns'},
435 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
436 stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER ? WHERE ( a BETWEEN ? AND ? )",
437 stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ? WHERE ( `a` BETWEEN ? AND ? )",
438 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
442 new => {bindtype => 'columns'},
443 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
444 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
445 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
446 bind => [[{dummy => 1} => '02/02/02']],
450 new => {bindtype => 'columns'},
451 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
452 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
453 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
454 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
458 new => {bindtype => 'columns'},
459 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
460 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
461 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
462 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
466 new => {bindtype => 'columns'},
467 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
468 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
469 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
470 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
474 new => {bindtype => 'columns'},
475 args => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
476 stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
477 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
478 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
482 args => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
483 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
484 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
485 bind => [qw/1 2 3 4 5/],
489 args => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
490 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
491 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
492 bind => [qw/1 2 3 4 5/],
496 args => ['test', [qw/1 2 3 4 5/], { returning => [qw(id foo bar) ] }],
497 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
498 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`, `foo`, `bar`',
499 bind => [qw/1 2 3 4 5/],
503 args => ['test', [qw/1 2 3 4 5/], { returning => \'id, foo, bar' }],
504 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
505 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
506 bind => [qw/1 2 3 4 5/],
510 args => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
511 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
512 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
513 bind => [qw/1 2 3 4 5/],
517 new => {bindtype => 'columns'},
518 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
519 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
520 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
521 bind => [[Y => 'x']],
525 args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }],
526 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )',
527 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )',
529 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
533 args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }],
534 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL )',
535 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL AND `c` IS NOT NULL )',
537 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
541 args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }],
542 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )',
543 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )',
545 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
549 args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }],
550 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL )',
551 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL )',
553 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
557 # check is( not) => undef
558 for my $op (qw( is is_not), 'is not' ) {
559 (my $sop = uc $op) =~ s/_/ /gi;
563 args => [{ a => { "$_$op" => undef } }],
564 stmt => "WHERE a $sop NULL",
565 stmt_q => "WHERE `a` $sop NULL",
567 } for ('', '-'); # with and without -
570 # check single-element inequality ops for no warnings
571 for my $op ( qw(!= <>) ) {
572 for my $val (undef, 42) {
575 args => [ { x => { "$_$op" => [ $val ] } } ],
576 stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
577 stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
578 bind => [ $val || () ],
579 } for ('', '-'); # with and without -
583 # check single-element not-like ops for no warnings, and NULL exception
584 # (the last two "is not X" are a weird syntax, but mebbe a dialect...)
585 for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
586 (my $sop = uc $op) =~ s/_/ /gi;
588 for my $val (undef, 42) {
591 args => [ { x => { "$_$op" => [ $val ] } } ],
593 stmt => "WHERE x $sop ?",
594 stmt_q => "WHERE `x` $sop ?",
597 stmt => "WHERE x IS NOT NULL",
598 stmt_q => "WHERE `x` IS NOT NULL",
600 warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
602 } for ('', '-'); # with and without -
606 # check all multi-element inequality/not-like ops for warnings
607 for my $op ( qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
608 (my $sop = uc $op) =~ s/_/ /gi;
612 args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
613 stmt => "WHERE x $sop ? OR x $sop ?",
614 stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
616 warns => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
617 } for ('', '-'); # with and without -
620 # check all like/not-like ops for empty-arrayref warnings
621 for my $op ( qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
622 (my $sop = uc $op) =~ s/_/ /gi;
626 args => [ { x => { "$_$op" => [] } } ],
627 stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
628 stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
630 warns => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
631 } for ('', '-'); # with and without -
635 my $new = $t->{new} || {};
637 for my $quoted (0, 1) {
639 my $maker = SQL::Abstract->new(%$new, $quoted
640 ? (quote_char => '`', name_sep => '.')
648 ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
651 if (my $e = $t->{throws}) {
655 ) || diag dumper ({ args => $t->{args}, result => $stmt });
666 $quoted ? $t->{stmt_q}: $t->{stmt},