Move simple IN/BETWEEN tests to t/05in_between.t
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Warn;
5 use Test::Exception;
6
7 use SQL::Abstract::Test import => [qw( is_same_sql_bind diag_where dumper )];
8
9 use SQL::Abstract;
10
11 #### WARNING ####
12 #
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
16 #
17 #################
18
19
20 my @tests = (
21       {
22               func   => 'select',
23               args   => ['test', '*'],
24               stmt   => 'SELECT * FROM test',
25               stmt_q => 'SELECT * FROM `test`',
26               bind   => []
27       },
28       {
29               func   => 'select',
30               args   => ['test', [qw(one two three)]],
31               stmt   => 'SELECT one, two, three FROM test',
32               stmt_q => 'SELECT `one`, `two`, `three` FROM `test`',
33               bind   => []
34       },
35       {
36               func   => 'select',
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`',
40               bind   => [0]
41       },
42       {
43               func   => 'select',
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` = ? ) )',
47               bind   => [5,6]
48       },
49       {
50               func   => 'select',
51               args   => ['test', '*', undef, ['id']],
52               stmt   => 'SELECT * FROM test ORDER BY id',
53               stmt_q => 'SELECT * FROM `test` ORDER BY `id`',
54               bind   => []
55       },
56       {
57               func   => 'select',
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`',
61               bind   => ['boom']
62       },
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       {
71               func   => 'select',
72               args   => ['test', '*', { a => { '!=', 'boom' } }],
73               stmt   => 'SELECT * FROM test WHERE ( a != ? )',
74               stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
75               bind   => ['boom']
76       },
77       {
78               func   => 'update',
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 )',
82               bind   => ['boom']
83       },
84       {
85               func   => 'update',
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']
90       },
91       {
92               func   => 'update',
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']
97       },
98       {
99               func   => 'delete',
100               args   => ['test', {requestor => undef}],
101               stmt   => 'DELETE FROM test WHERE ( requestor IS NULL )',
102               stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )',
103               bind   => []
104       },
105       {
106               func   => 'delete',
107               args   => [[qw/test1 test2 test3/],
108                          { 'test1.field' => \'!= test2.field',
109                             user => {'!=','nwiger'} },
110                         ],
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.
113               bind   => ['nwiger']
114       },
115       {
116               func   => 'insert',
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/],
121       },
122       {
123               func   => 'insert',
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/],
128       },
129       {
130               func   => 'insert',
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],
135       },
136       {
137               func   => 'update',
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/],
142       },
143       {
144               func   => 'update',
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/],
149       },
150       {
151               func   => 'update',
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)],
156       },
157       {
158               func   => 'insert',
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), ?)',
162               bind   => ['4'],
163       },
164       {
165               func   => 'insert',
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), ?)',
169               bind   => ['4'],
170       },
171       {
172               func   => 'insert',
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...
178       },
179       {
180               func   => 'select',
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...
186       },
187       {
188               func   => 'update',
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%']],
198       },
199       {
200               func   => 'select',
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 ? ) ) )',
204               bind   => [qw(2 3%)],
205       },
206       {
207               func   => 'select',
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)],
213       },
214       {
215               func   => 'update',
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} ] ] },
220                         ],
221               warns  => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
222
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)],
228       },
229       {
230               func   => 'select',
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%)]
239       },
240       {
241               func   => 'update',
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)] } } ] },
246                         ] ],
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)]
252       },
253       {
254               func   => 'insert',
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)],
259       },
260       {
261               func   => 'select',
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'],
266       },
267       {
268               func   => 'insert',
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]],
274       },
275       {
276               func   => 'insert',
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]]],
282       },
283       {
284               func   => 'update',
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]],
290       },
291       {
292               func   => 'update',
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]]],
298       },
299       {
300               func   => 'select',
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` = ? )',
304               bind   => [8],
305       },
306       {
307               func   => 'select',
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],
312       },
313       { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
314               func   => 'insert',
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,
320       },
321       {
322               func   => 'update',
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 ? )',
326               bind   => [qw(1 1 2)],
327       },
328       {
329               func   => 'insert',
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)',
333               bind   => [qw(1)],
334       },
335       {
336               func   => 'select',
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` = ? )},
340               bind   => [qw(1)],
341       },
342       {
343               func   => 'select',
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` = ? )',
347               bind   => [qw(8)],
348       },
349       {
350               func   => 'insert',
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']],
356       },
357       {
358               func   => 'update',
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']],
364       },
365       {
366               func   => 'select',
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']],
372       },
373       {
374               func   => 'select',
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]],
380       },
381       {
382               func   => 'insert',
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\]/,
386       },
387       {
388               func   => 'update',
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\]/,
392       },
393       {
394               func   => 'select',
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\]/,
398       },
399       {
400               func   => 'select',
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\]/,
404       },
405       {
406               func   => 'select',
407               args   => ['test', '*', { foo => { '>=' => [] }} ],
408               throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
409       },
410       {
411               func   => 'select',
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]],
417       },
418       {
419               func   => 'select',
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\]/,
423       },
424       {
425               func   => 'insert',
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']],
431       },
432       {
433               func   => 'update',
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']],
439       },
440       {
441               func   => 'select',
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']],
447       },
448       {
449               func   => 'select',
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]],
455       },
456       {
457               func   => 'select',
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']],
463       },
464       {
465               func   => 'select',
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]],
471       },
472       {
473               func   => 'select',
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]],
479       },
480       {
481               func   => 'insert',
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/],
486       },
487       {
488               func   => 'insert',
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/],
493       },
494       {
495               func   => 'insert',
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/],
500       },
501       {
502               func   => 'insert',
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/],
507       },
508       {
509               func   => 'insert',
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/],
514       },
515       {
516               func   => 'select',
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']],
522       },
523       {
524               func => 'select',
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 )',
528               bind => [],
529               warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
530       },
531       {
532               func => 'select',
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 )',
536               bind => [],
537               warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
538       },
539       {
540               func => 'select',
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 )',
544               bind => [],
545               warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
546       },
547       {
548               func => 'select',
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 )',
552               bind => [],
553               warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
554       },
555 );
556
557 # check is( not) => undef
558 for my $op (qw( is is_not), 'is not' ) {
559   (my $sop = uc $op) =~ s/_/ /gi;
560
561   push @tests, {
562     func => 'where',
563     args => [{ a => { "$_$op" => undef } }],
564     stmt => "WHERE a $sop NULL",
565     stmt_q => "WHERE `a` $sop NULL",
566     bind => [],
567   } for ('', '-');  # with and without -
568 }
569
570 # check single-element inequality ops for no warnings
571 for my $op ( qw(!= <>) ) {
572   for my $val (undef, 42) {
573     push @tests, {
574       func => 'where',
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 -
580   }
581 }
582
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;
587
588   for my $val (undef, 42) {
589     push @tests, {
590       func => 'where',
591       args => [ { x => { "$_$op" => [ $val ] } } ],
592       $val ? (
593         stmt => "WHERE x $sop ?",
594         stmt_q => "WHERE `x` $sop ?",
595         bind => [ $val ],
596       ) : (
597         stmt => "WHERE x IS NOT NULL",
598         stmt_q => "WHERE `x` IS NOT NULL",
599         bind => [],
600         warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
601       ),
602     } for ('', '-');  # with and without -
603   }
604 }
605
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;
609
610   push @tests, {
611     func => 'where',
612     args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
613     stmt => "WHERE x $sop ? OR x $sop ?",
614     stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
615     bind => [ 42, 69 ],
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 -
618 }
619
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;
623
624   push @tests, {
625     func => 'where',
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" ),
629     bind => [],
630     warns  => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
631   } for ('', '-');  # with and without -
632 }
633
634 for my $t (@tests) {
635   my $new = $t->{new} || {};
636
637   for my $quoted (0, 1) {
638
639     my $maker = SQL::Abstract->new(%$new, $quoted
640       ? (quote_char => '`', name_sep => '.')
641       : ()
642     );
643
644     my($stmt, @bind);
645
646     my $cref = sub {
647       my $op = $t->{func};
648       ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
649     };
650
651     if (my $e = $t->{throws}) {
652       throws_ok(
653         sub { $cref->() },
654         $e,
655       ) || diag dumper ({ args => $t->{args}, result => $stmt });
656     }
657     else {
658       warnings_exist(
659         sub { $cref->() },
660         $t->{warns} || [],
661       );
662
663       is_same_sql_bind(
664         $stmt,
665         \@bind,
666         $quoted ? $t->{stmt_q}: $t->{stmt},
667         $t->{bind}
668       );
669     }
670   }
671 }
672
673 done_testing;