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
22 args => ['test', '*'],
23 stmt => 'SELECT * FROM test',
24 stmt_q => 'SELECT * FROM `test`',
29 args => ['test', [qw(one two three)]],
30 stmt => 'SELECT one, two, three FROM test',
31 stmt_q => 'SELECT `one`, `two`, `three` FROM `test`',
36 args => ['test', '*', { a => 0 }, [qw/boom bada bing/]],
37 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY boom, bada, bing',
38 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `boom`, `bada`, `bing`',
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` = ? ) )',
50 args => ['test', '*', undef, ['id']],
51 stmt => 'SELECT * FROM test ORDER BY id',
52 stmt_q => 'SELECT * FROM `test` ORDER BY `id`',
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`',
64 args => ['test', '*', { a => ['boom', 'bang'] }],
65 stmt => 'SELECT * FROM test WHERE ( ( ( a = ? ) OR ( a = ? ) ) )',
66 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `a` = ? ) OR ( `a` = ? ) ) )',
67 bind => ['boom', 'bang']
71 args => ['test', '*', { a => { '!=', 'boom' } }],
72 stmt => 'SELECT * FROM test WHERE ( a != ? )',
73 stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
77 # this is maybe wrong but a single arg doesn't get quoted
79 args => ['test', 'id', { a => { '!=', 'boom' } }],
80 stmt => 'SELECT id FROM test WHERE ( a != ? )',
81 stmt_q => 'SELECT id FROM `test` WHERE ( `a` != ? )',
86 args => ['test', {a => 'boom'}, {a => undef}],
87 stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )',
88 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )',
93 args => ['test', {a => undef }, {a => 'boom'}],
94 stmt => 'UPDATE test SET a = ? WHERE ( a = ? )',
95 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` = ? )',
96 bind => [undef,'boom']
100 args => ['test', {a => 'boom'}, { a => {'!=', "bang" }} ],
101 stmt => 'UPDATE test SET a = ? WHERE ( a != ? )',
102 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` != ? )',
103 bind => ['boom', 'bang']
107 args => ['test', {'a-funny-flavored-candy' => 'yummy', b => 'oops'}, { a42 => "bang" }],
108 stmt => 'UPDATE test SET a-funny-flavored-candy = ?, b = ? WHERE ( a42 = ? )',
109 stmt_q => 'UPDATE `test` SET `a-funny-flavored-candy` = ?, `b` = ? WHERE ( `a42` = ? )',
110 bind => ['yummy', 'oops', 'bang']
114 args => ['test', {requestor => undef}],
115 stmt => 'DELETE FROM test WHERE ( requestor IS NULL )',
116 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )',
121 args => [[qw/test1 test2 test3/],
122 { 'test1.field' => \'!= test2.field',
123 user => {'!=','nwiger'} },
125 stmt => 'DELETE FROM test1, test2, test3 WHERE ( test1.field != test2.field AND user != ? )',
126 stmt_q => 'DELETE FROM `test1`, `test2`, `test3` WHERE ( `test1`.`field` != test2.field AND `user` != ? )', # test2.field is a literal value, cannnot be quoted.
131 args => [[\'test1', 'test2'], '*', { 'test1.a' => 'boom' } ],
132 stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a = ? )',
133 stmt_q => 'SELECT * FROM test1, `test2` WHERE ( `test1`.`a` = ? )',
138 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
139 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
140 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
141 bind => [qw/1 2 3 4 5/],
145 args => ['test', [1..30]],
146 stmt => 'INSERT INTO test VALUES ('.join(', ', ('?')x30).')',
147 stmt_q => 'INSERT INTO `test` VALUES ('.join(', ', ('?')x30).')',
152 args => ['test', [qw/1 2 3 4 5/, undef]],
153 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)',
154 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)',
155 bind => [qw/1 2 3 4 5/, undef],
159 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
160 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?',
161 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?',
162 bind => [qw/1 2 3 4 5/],
166 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}],
167 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )',
168 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )',
169 bind => [qw/1 2 3 4 5 1 2 3 4 5/],
173 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
174 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
175 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
176 bind => [qw(1 02/02/02 1 2)],
180 args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ],
181 stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)',
182 stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)',
187 args => ['test.table', [ \'max(all_limits)', 4 ] ],
188 stmt => 'INSERT INTO test.table VALUES (max(all_limits), ?)',
189 stmt_q => 'INSERT INTO `test`.`table` VALUES (max(all_limits), ?)',
194 new => {bindtype => 'columns'},
195 args => ['test.table', {one => 2, three => 4, five => 6} ],
196 stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
197 stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
198 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
202 new => {bindtype => 'columns', case => 'lower'},
203 args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
204 stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
205 stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
206 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
210 new => {bindtype => 'columns', cmp => 'like'},
211 args => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
212 {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
213 stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
214 . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
215 stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
216 . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
217 bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
218 ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
222 args => ['test', '*', {priority => [ -and => {'!=', 2}, { -not_like => '3%'} ]}],
223 stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority NOT LIKE ? ) ) )',
224 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` NOT LIKE ? ) ) )',
229 args => ['Yo Momma', '*', { user => 'nwiger',
230 -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
231 stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )',
232 stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )',
233 bind => [qw(20 ASIA nwiger)],
237 args => ['taco_punches', { one => 2, three => 4 },
238 { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
239 tasty => { '!=', [qw(yes YES)] },
240 -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
242 warns => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
244 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
245 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
246 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
247 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
248 bind => [qw(2 4 mr.happy yes YES yes YES)],
252 args => ['jeff', '*', { name => {'ilike', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
253 -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
254 yob => {'<', 1976} ] ] } ],
255 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )'
256 . ' AND name NOT IN ( ?, ?, ?, ? ) AND name ILIKE ? )',
257 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )'
258 . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` ILIKE ? )',
259 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
263 args => ['fhole', {fpoles => 4}, [
264 { race => [qw/-or black white asian /] },
265 { -nest => { firsttime => [-or => {'=','yes'}, undef] } },
266 { -and => [ { firstname => {-not_like => 'candace'} }, { lastname => {-in => [qw(jugs canyon towers)] } } ] },
268 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
269 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
270 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
271 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
272 bind => [qw(4 black white asian yes candace jugs canyon towers)]
276 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
277 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
278 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
279 bind => [qw(1 02/02/02)],
283 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
284 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
285 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
286 bind => ['02/02/02'],
290 new => {array_datatypes => 1},
291 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
292 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
293 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
294 bind => [1, [1, 1, 2, 3, 5, 8]],
298 new => {bindtype => 'columns', array_datatypes => 1},
299 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
300 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
301 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
302 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
306 new => {array_datatypes => 1},
307 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
308 stmt => 'UPDATE test SET a = ?, b = ?',
309 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
310 bind => [1, [1, 1, 2, 3, 5, 8]],
314 new => {bindtype => 'columns', array_datatypes => 1},
315 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
316 stmt => 'UPDATE test SET a = ?, b = ?',
317 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
318 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
322 args => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
323 stmt => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
324 stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
329 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
330 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
331 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
332 bind => ['02/02/02', 8],
334 { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
336 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => { answer => 42 }}],
337 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
338 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
339 bind => [qw/1 2 3 4/, { answer => 42}],
340 warns => qr/HASH ref as bind value in insert is not supported/i,
344 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
345 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
346 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
351 args => ['test', {a => 1, b => \["42"]}],
352 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
353 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
358 args => ['test', '*', { a => \["= 42"], b => 1}],
359 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
360 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
365 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
366 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
367 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
372 new => {bindtype => 'columns'},
373 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
374 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
375 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
376 bind => [[a => '1'], [dummy => '02/02/02']],
380 new => {bindtype => 'columns'},
381 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
382 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
383 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
384 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
388 new => {bindtype => 'columns'},
389 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
390 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
391 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
392 bind => [[dummy => '02/02/02']],
396 new => {bindtype => 'columns'},
397 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
398 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
399 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
400 bind => [[dummy => '02/02/02'], [b => 8]],
404 new => {bindtype => 'columns'},
405 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
406 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
410 new => {bindtype => 'columns'},
411 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
412 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
416 new => {bindtype => 'columns'},
417 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
418 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
422 new => {bindtype => 'columns'},
423 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
424 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
428 args => ['test', '*', { foo => { '>=' => [] }} ],
429 throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
433 new => {bindtype => 'columns'},
434 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
435 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
436 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
437 bind => [[dummy => '02/02/02'], [b => 8]],
441 new => {bindtype => 'columns'},
442 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
443 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
447 new => {bindtype => 'columns'},
448 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
449 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
450 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
451 bind => [[a => '1'], [{dummy => 1} => '02/02/02']],
455 new => {bindtype => 'columns'},
456 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
457 stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER ? WHERE ( a BETWEEN ? AND ? )",
458 stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ? WHERE ( `a` BETWEEN ? AND ? )",
459 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
463 new => {bindtype => 'columns'},
464 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
465 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
466 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
467 bind => [[{dummy => 1} => '02/02/02']],
471 new => {bindtype => 'columns'},
472 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
473 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
474 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
475 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
479 new => {bindtype => 'columns'},
480 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
481 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
482 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
483 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
487 new => {bindtype => 'columns'},
488 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
489 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
490 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
491 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
495 new => {bindtype => 'columns'},
496 args => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
497 stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
498 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
499 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
503 args => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
504 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
505 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
506 bind => [qw/1 2 3 4 5/],
510 args => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
511 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
512 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
513 bind => [qw/1 2 3 4 5/],
517 args => ['test', [qw/1 2 3 4 5/], { returning => [qw(id foo bar) ] }],
518 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
519 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`, `foo`, `bar`',
520 bind => [qw/1 2 3 4 5/],
524 args => ['test', [qw/1 2 3 4 5/], { returning => \'id, foo, bar' }],
525 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
526 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
527 bind => [qw/1 2 3 4 5/],
531 args => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
532 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
533 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
534 bind => [qw/1 2 3 4 5/],
538 new => {bindtype => 'columns'},
539 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
540 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
541 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
542 bind => [[Y => 'x']],
546 args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }],
547 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )',
548 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )',
550 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
554 args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }],
555 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL )',
556 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL AND `c` IS NOT NULL )',
558 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
562 args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }],
563 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )',
564 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )',
566 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
570 args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }],
571 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL )',
572 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL )',
574 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
578 args => ['`test``table`', ['`test``column`']],
579 stmt => 'SELECT `test``column` FROM `test``table`',
580 stmt_q => 'SELECT ```test````column``` FROM ```test````table```',
585 args => ['`test\\`table`', ['`test`\\column`']],
586 stmt => 'SELECT `test`\column` FROM `test\`table`',
587 stmt_q => 'SELECT `\`test\`\\\\column\`` FROM `\`test\\\\\`table\``',
593 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => 'id' }],
594 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id',
595 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`',
600 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => \'*' }],
601 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING *',
602 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING *',
607 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => ['id','created_at'] }],
608 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id, created_at',
609 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`, `created_at`',
614 args => ['test', {requestor => undef}, {returning => 'id'}],
615 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING id',
616 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING `id`',
621 args => ['test', {requestor => undef}, {returning => \'*'}],
622 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING *',
623 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING *',
628 args => ['test', {requestor => undef}, {returning => ['id', 'created_at']}],
629 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING id, created_at',
630 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING `id`, `created_at`',
635 # check is( not) => undef
636 for my $op (qw(not is is_not), 'is not') {
637 (my $sop = uc $op) =~ s/_/ /gi;
639 $sop = 'IS NOT' if $sop eq 'NOT';
642 for my $prefix ('', '-') {
645 args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
646 stmt => "WHERE a $sop NULL",
647 stmt_q => "WHERE `a` $sop NULL",
654 # check single-element inequality ops for no warnings
655 for my $op (qw(!= <>)) {
656 for my $val (undef, 42) {
659 args => [ { x => { "$_$op" => [ $val ] } } ],
660 stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
661 stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
662 bind => [ $val || () ],
663 } for ('', '-'); # with and without -
667 # check single-element not-like ops for no warnings, and NULL exception
668 # (the last two "is not X" are a weird syntax, but mebbe a dialect...)
669 for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
670 (my $sop = uc $op) =~ s/_/ /gi;
672 for my $val (undef, 42) {
675 args => [ { x => { "$_$op" => [ $val ] } } ],
677 stmt => "WHERE x $sop ?",
678 stmt_q => "WHERE `x` $sop ?",
681 stmt => "WHERE x IS NOT NULL",
682 stmt_q => "WHERE `x` IS NOT NULL",
684 warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
686 } for ('', '-'); # with and without -
690 # check all multi-element inequality/not-like ops for warnings
691 for my $op (qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
692 (my $sop = uc $op) =~ s/_/ /gi;
696 args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
697 stmt => "WHERE x $sop ? OR x $sop ?",
698 stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
700 warns => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
701 } for ('', '-'); # with and without -
704 # check all like/not-like ops for empty-arrayref warnings
705 for my $op (qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
706 (my $sop = uc $op) =~ s/_/ /gi;
710 args => [ { x => { "$_$op" => [] } } ],
711 stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
712 stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
714 warns => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
715 } for ('', '-'); # with and without -
718 # check emtpty-lhs in a hashpair and arraypair
719 for my $lhs (undef, '') {
720 no warnings 'uninitialized';
723 ## hard exceptions - never worked
725 ( map { $_, { @$_ } }
727 [ $lhs => { "=" => "bozz" } ],
728 [ $lhs => { "=" => \"bozz" } ],
729 [ $lhs => { -max => \"bizz" } ],
731 [ -and => { $lhs => "baz" }, bizz => "buzz" ],
732 [ foo => "bar", { $lhs => "baz" }, bizz => "buzz" ],
733 { foo => "bar", -or => { $lhs => "baz" } },
735 # the hashref forms of these work sadly - check for warnings below
736 { foo => "bar", -and => [ $lhs => \"baz" ], bizz => "buzz" },
737 { foo => "bar", -or => [ $lhs => \"baz" ], bizz => "buzz" },
738 [ foo => "bar", [ $lhs => \"baz" ], bizz => "buzz" ],
739 [ foo => "bar", $lhs => \"baz", bizz => "buzz" ],
740 [ foo => "bar", $lhs => \["baz"], bizz => "buzz" ],
742 [ $lhs => \["baz"] ],
746 args => [ $where_arg ],
747 throws => qr/\QSupplying an empty left hand side argument is not supported/,
752 ## deprecations - sorta worked, likely abused by folks
754 # the arrayref forms of this never worked and throw above
755 { foo => "bar", -or => { $lhs => \"baz" }, bizz => "buzz" },
756 { foo => "bar", -and => { $lhs => \"baz" }, bizz => "buzz" },
757 { foo => "bar", $lhs => \"baz", bizz => "buzz" },
758 { foo => "bar", $lhs => \["baz"], bizz => "buzz" },
762 args => [ $where_arg ],
763 stmt => 'WHERE baz AND bizz = ? AND foo = ?',
764 stmt_q => 'WHERE baz AND `bizz` = ? AND `foo` = ?',
765 bind => [qw( buzz bar )],
766 warns => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
772 { $lhs => \["baz"] },
776 args => [ $where_arg ],
778 stmt_q => 'WHERE baz',
780 warns => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
785 # check false lhs, silly but possible
788 [ { 0 => "baz" }, bizz => "buzz", foo => "bar" ],
789 [ -or => { foo => "bar", -or => { 0 => "baz" }, bizz => "buzz" } ],
793 args => [ $where_arg ],
794 stmt => 'WHERE 0 = ? OR bizz = ? OR foo = ?',
795 stmt_q => 'WHERE `0` = ? OR `bizz` = ? OR `foo` = ?',
796 bind => [qw( baz buzz bar )],
801 { foo => "bar", -and => [ 0 => \"= baz" ], bizz => "buzz" },
802 { foo => "bar", -or => [ 0 => \"= baz" ], bizz => "buzz" },
804 { foo => "bar", -and => { 0 => \"= baz" }, bizz => "buzz" },
805 { foo => "bar", -or => { 0 => \"= baz" }, bizz => "buzz" },
807 { foo => "bar", 0 => \"= baz", bizz => "buzz" },
808 { foo => "bar", 0 => \["= baz"], bizz => "buzz" },
812 args => [ $where_arg ],
813 stmt => 'WHERE 0 = baz AND bizz = ? AND foo = ?',
814 stmt_q => 'WHERE `0` = baz AND `bizz` = ? AND `foo` = ?',
815 bind => [qw( buzz bar )],
820 [ -and => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
821 [ -or => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
822 [ 0 => \"= baz", bizz => "buzz", foo => "bar" ],
823 [ 0 => \["= baz"], bizz => "buzz", foo => "bar" ],
827 args => [ $where_arg ],
828 stmt => 'WHERE 0 = baz OR bizz = ? OR foo = ?',
829 stmt_q => 'WHERE `0` = baz OR `bizz` = ? OR `foo` = ?',
830 bind => [qw( buzz bar )],
836 my $new = $t->{new} || {};
838 for my $quoted (0, 1) {
840 my $maker = SQL::Abstract->new(
846 escape_char => $t->{esc},
855 ($stmt, @bind) = $maker->$op(@{ $t->{args} });
858 if (my $e = $t->{throws}) {
862 ) || diag dumper({ args => $t->{args}, result => $stmt });
866 alarm(1); local $SIG{ALRM} = sub {
867 no warnings 'redefine';
868 my $orig = Carp->can('caller_info');
869 local *Carp::caller_info = sub { return if $_[0] > 20; &$orig };
870 print STDERR "ARGH ($SQL::Abstract::Default_Scalar_To): ".Carp::longmess();
876 ) || diag dumper({ args => $t->{args}, result => $stmt });
877 }) || diag dumper({ args => $t->{args}, result => $stmt, threw => $@ });
882 $quoted ? $t->{stmt_q}: $t->{stmt},
884 ) || diag dumper({ args => $t->{args}, result => $stmt });;