Fix ->insert($table, \@values) with >26 values (RT#112684)
[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   => 'select',
117               args   => [[\'test1', 'test2'], '*', { 'test1.a' => 'boom' } ],
118               stmt   => 'SELECT * FROM test1, test2 WHERE ( test1.a = ? )',
119               stmt_q => 'SELECT * FROM test1, `test2` WHERE ( `test1`.`a` = ? )',
120               bind   => ['boom']
121       },
122       {
123               func   => 'insert',
124               args   => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
125               stmt   => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
126               stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
127               bind   => [qw/1 2 3 4 5/],
128       },
129       {
130               func   => 'insert',
131               args   => ['test', [1..30]],
132               stmt   => 'INSERT INTO test VALUES ('.join(', ', ('?')x30).')',
133               stmt_q => 'INSERT INTO `test` VALUES ('.join(', ', ('?')x30).')',
134               bind   => [1..30],
135       },
136       {
137               func   => 'insert',
138               args   => ['test', [qw/1 2 3 4 5/, undef]],
139               stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)',
140               stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)',
141               bind   => [qw/1 2 3 4 5/, undef],
142       },
143       {
144               func   => 'update',
145               args   => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
146               stmt   => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?',
147               stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?',
148               bind   => [qw/1 2 3 4 5/],
149       },
150       {
151               func   => 'update',
152               args   => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}],
153               stmt   => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )',
154               stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )',
155               bind   => [qw/1 2 3 4 5 1 2 3 4 5/],
156       },
157       {
158               func   => 'update',
159               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
160               stmt   => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
161               stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
162               bind   => [qw(1 02/02/02 1 2)],
163       },
164       {
165               func   => 'insert',
166               args   => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ],
167               stmt   => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)',
168               stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)',
169               bind   => ['4'],
170       },
171       {
172               func   => 'insert',
173               args   => ['test.table', [ \'max(all_limits)', 4 ] ],
174               stmt   => 'INSERT INTO test.table VALUES (max(all_limits), ?)',
175               stmt_q => 'INSERT INTO `test`.`table` VALUES (max(all_limits), ?)',
176               bind   => ['4'],
177       },
178       {
179               func   => 'insert',
180               new    => {bindtype => 'columns'},
181               args   => ['test.table', {one => 2, three => 4, five => 6} ],
182               stmt   => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
183               stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
184               bind   => [['five', 6], ['one', 2], ['three', 4]],  # alpha order, man...
185       },
186       {
187               func   => 'select',
188               new    => {bindtype => 'columns', case => 'lower'},
189               args   => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
190               stmt   => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
191               stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
192               bind   => [['five', 6], ['one', 2], ['three', 4]],  # alpha order, man...
193       },
194       {
195               func   => 'update',
196               new    => {bindtype => 'columns', cmp => 'like'},
197               args   => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
198                                           {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
199               stmt   => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
200                        . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
201               stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
202                        . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
203               bind   => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
204                          ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
205       },
206       {
207               func   => 'select',
208               args   => ['test', '*', {priority => [ -and => {'!=', 2}, { -not_like => '3%'} ]}],
209               stmt   => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority NOT LIKE ? ) ) )',
210               stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` NOT LIKE ? ) ) )',
211               bind   => [qw(2 3%)],
212       },
213       {
214               func   => 'select',
215               args   => ['Yo Momma', '*', { user => 'nwiger',
216                                        -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
217               stmt   => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )',
218               stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )',
219               bind   => [qw(20 ASIA nwiger)],
220       },
221       {
222               func   => 'update',
223               args   => ['taco_punches', { one => 2, three => 4 },
224                                          { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
225                                            tasty => { '!=', [qw(yes YES)] },
226                                            -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
227                         ],
228               warns  => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
229
230               stmt   => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
231                       . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
232               stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
233                       . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
234               bind   => [qw(2 4 mr.happy yes YES yes YES)],
235       },
236       {
237               func   => 'select',
238               args   => ['jeff', '*', { name => {'ilike', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
239                                        -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
240                                                                    yob => {'<', 1976} ] ] } ],
241               stmt   => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )'
242                       . ' AND name NOT IN ( ?, ?, ?, ? ) AND name ILIKE ? )',
243               stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )'
244                       . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` ILIKE ? )',
245               bind   => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
246       },
247       {
248               func   => 'update',
249               args   => ['fhole', {fpoles => 4}, [
250                           { race => [qw/-or black white asian /] },
251                           { -nest => { firsttime => [-or => {'=','yes'}, undef] } },
252                           { -and => [ { firstname => {-not_like => 'candace'} }, { lastname => {-in => [qw(jugs canyon towers)] } } ] },
253                         ] ],
254               stmt   => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
255                       . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
256               stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
257                       . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
258               bind   => [qw(4 black white asian yes candace jugs canyon towers)]
259       },
260       {
261               func   => 'insert',
262               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
263               stmt   => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
264               stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
265               bind   => [qw(1 02/02/02)],
266       },
267       {
268               func   => 'select',
269               args   => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
270               stmt   => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
271               stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
272               bind   => ['02/02/02'],
273       },
274       {
275               func   => 'insert',
276               new    => {array_datatypes => 1},
277               args   => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
278               stmt   => 'INSERT INTO test (a, b) VALUES (?, ?)',
279               stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
280               bind   => [1, [1, 1, 2, 3, 5, 8]],
281       },
282       {
283               func   => 'insert',
284               new    => {bindtype => 'columns', array_datatypes => 1},
285               args   => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
286               stmt   => 'INSERT INTO test (a, b) VALUES (?, ?)',
287               stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
288               bind   => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
289       },
290       {
291               func   => 'update',
292               new    => {array_datatypes => 1},
293               args   => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
294               stmt   => 'UPDATE test SET a = ?, b = ?',
295               stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
296               bind   => [1, [1, 1, 2, 3, 5, 8]],
297       },
298       {
299               func   => 'update',
300               new    => {bindtype => 'columns', array_datatypes => 1},
301               args   => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
302               stmt   => 'UPDATE test SET a = ?, b = ?',
303               stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
304               bind   => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
305       },
306       {
307               func   => 'select',
308               args   => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
309               stmt   => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
310               stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
311               bind   => [8],
312       },
313       {
314               func   => 'select',
315               args   => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
316               stmt   => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
317               stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
318               bind   => ['02/02/02', 8],
319       },
320       { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
321               func   => 'insert',
322               args   => ['test', {a => 1, b => 2, c => 3, d => 4, e => { answer => 42 }}],
323               stmt   => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
324               stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
325               bind   => [qw/1 2 3 4/, { answer => 42}],
326               warns  => qr/HASH ref as bind value in insert is not supported/i,
327       },
328       {
329               func   => 'update',
330               args   => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
331               stmt   => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
332               stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
333               bind   => [qw(1 1 2)],
334       },
335       {
336               func   => 'insert',
337               args   => ['test', {a => 1, b => \["42"]}],
338               stmt   => 'INSERT INTO test (a, b) VALUES (?, 42)',
339               stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
340               bind   => [qw(1)],
341       },
342       {
343               func   => 'select',
344               args   => ['test', '*', { a => \["= 42"], b => 1}],
345               stmt   => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
346               stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
347               bind   => [qw(1)],
348       },
349       {
350               func   => 'select',
351               args   => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
352               stmt   => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
353               stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
354               bind   => [qw(8)],
355       },
356       {
357               func   => 'insert',
358               new    => {bindtype => 'columns'},
359               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
360               stmt   => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
361               stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
362               bind   => [[a => '1'], [dummy => '02/02/02']],
363       },
364       {
365               func   => 'update',
366               new    => {bindtype => 'columns'},
367               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
368               stmt   => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
369               stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
370               bind   => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
371       },
372       {
373               func   => 'select',
374               new    => {bindtype => 'columns'},
375               args   => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
376               stmt   => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
377               stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
378               bind   => [[dummy => '02/02/02']],
379       },
380       {
381               func   => 'select',
382               new    => {bindtype => 'columns'},
383               args   => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
384               stmt   => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
385               stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
386               bind   => [[dummy => '02/02/02'], [b => 8]],
387       },
388       {
389               func   => 'insert',
390               new    => {bindtype => 'columns'},
391               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
392               throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
393       },
394       {
395               func   => 'update',
396               new    => {bindtype => 'columns'},
397               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
398               throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
399       },
400       {
401               func   => 'select',
402               new    => {bindtype => 'columns'},
403               args   => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
404               throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
405       },
406       {
407               func   => 'select',
408               new    => {bindtype => 'columns'},
409               args   => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
410               throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
411       },
412       {
413               func   => 'select',
414               args   => ['test', '*', { foo => { '>=' => [] }} ],
415               throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
416       },
417       {
418               func   => 'select',
419               new    => {bindtype => 'columns'},
420               args   => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
421               stmt   => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
422               stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
423               bind   => [[dummy => '02/02/02'], [b => 8]],
424       },
425       {
426               func   => 'select',
427               new    => {bindtype => 'columns'},
428               args   => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
429               throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
430       },
431       {
432               func   => 'insert',
433               new    => {bindtype => 'columns'},
434               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
435               stmt   => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
436               stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
437               bind   => [[a => '1'], [{dummy => 1} => '02/02/02']],
438       },
439       {
440               func   => 'update',
441               new    => {bindtype => 'columns'},
442               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
443               stmt   => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER ? WHERE ( a BETWEEN ? AND ? )",
444               stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ? WHERE ( `a` BETWEEN ? AND ? )",
445               bind   => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
446       },
447       {
448               func   => 'select',
449               new    => {bindtype => 'columns'},
450               args   => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
451               stmt   => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
452               stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
453               bind   => [[{dummy => 1} => '02/02/02']],
454       },
455       {
456               func   => 'select',
457               new    => {bindtype => 'columns'},
458               args   => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
459               stmt   => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
460               stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
461               bind   => [[{dummy => 1} => '02/02/02'], [b => 8]],
462       },
463       {
464               func   => 'select',
465               new    => {bindtype => 'columns'},
466               args   => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ]  ]  }],
467               stmt   => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ?  )',
468               stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ?  ) OR ( `c` = ? AND `d` = ? )',
469               bind   => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
470       },
471       {
472               func   => 'select',
473               new    => {bindtype => 'columns'},
474               args   => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
475               stmt   => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
476               stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
477               bind   => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
478       },
479       {
480               func   => 'select',
481               new    => {bindtype => 'columns'},
482               args   => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
483               stmt   => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
484               stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
485               bind   => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
486       },
487       {
488               func   => 'insert',
489               args   => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
490               stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
491               stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
492               bind   => [qw/1 2 3 4 5/],
493       },
494       {
495               func   => 'insert',
496               args   => ['test', [qw/1 2 3 4 5/], { returning => '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 => [qw(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, 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/],
514       },
515       {
516               func   => 'insert',
517               args   => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
518               stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
519               stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
520               bind   => [qw/1 2 3 4 5/],
521       },
522       {
523               func   => 'select',
524               new    => {bindtype => 'columns'},
525               args   => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
526               stmt   => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
527               stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
528               bind   => [[Y => 'x']],
529       },
530       {
531               func => 'select',
532               args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }],
533               stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )',
534               stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )',
535               bind => [],
536               warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
537       },
538       {
539               func => 'select',
540               args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }],
541               stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT  NULL AND c IS NOT  NULL )',
542               stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT  NULL AND `b` IS NOT  NULL AND `c` IS NOT  NULL )',
543               bind => [],
544               warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
545       },
546       {
547               func => 'select',
548               args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }],
549               stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )',
550               stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )',
551               bind => [],
552               warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
553       },
554       {
555               func => 'select',
556               args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }],
557               stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT  NULL )',
558               stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT  NULL AND `b` IS NOT  NULL )',
559               bind => [],
560               warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
561       },
562       {
563               func => 'select',
564               args => ['`test``table`', ['`test``column`']],
565               stmt => 'SELECT `test``column` FROM `test``table`',
566               stmt_q => 'SELECT ```test````column``` FROM ```test````table```',
567               bind => [],
568       },
569       {
570               func => 'select',
571               args => ['`test\\`table`', ['`test`\\column`']],
572               stmt => 'SELECT `test`\column` FROM `test\`table`',
573               stmt_q => 'SELECT `\`test\`\\\\column\`` FROM `\`test\\\\\`table\``',
574               esc  => '\\',
575               bind => [],
576       },
577       {
578               func => 'update',
579               args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => 'id' }],
580               stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id',
581               stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`',
582               bind => [42, 32],
583       },
584       {
585               func => 'update',
586               args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => \'*' }],
587               stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING *',
588               stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING *',
589               bind => [42, 32],
590       },
591       {
592               func => 'update',
593               args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => ['id','created_at'] }],
594               stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id, created_at',
595               stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`, `created_at`',
596               bind => [42, 32],
597       },
598 );
599
600 # check is( not) => undef
601 for my $op ( qw(not is is_not), 'is not' ) {
602   (my $sop = uc $op) =~ s/_/ /gi;
603
604   $sop = 'IS NOT' if $sop eq 'NOT';
605
606   for my $uc (0, 1) {
607     for my $prefix ('', '-') {
608       push @tests, {
609         func => 'where',
610         args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
611         stmt => "WHERE a $sop NULL",
612         stmt_q => "WHERE `a` $sop NULL",
613         bind => [],
614       };
615     }
616   }
617 }
618
619 # check single-element inequality ops for no warnings
620 for my $op ( qw(!= <>) ) {
621   for my $val (undef, 42) {
622     push @tests, {
623       func => 'where',
624       args => [ { x => { "$_$op" => [ $val ] } } ],
625       stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
626       stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
627       bind => [ $val || () ],
628     } for ('', '-');  # with and without -
629   }
630 }
631
632 # check single-element not-like ops for no warnings, and NULL exception
633 # (the last two "is not X" are a weird syntax, but mebbe a dialect...)
634 for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
635   (my $sop = uc $op) =~ s/_/ /gi;
636
637   for my $val (undef, 42) {
638     push @tests, {
639       func => 'where',
640       args => [ { x => { "$_$op" => [ $val ] } } ],
641       $val ? (
642         stmt => "WHERE x $sop ?",
643         stmt_q => "WHERE `x` $sop ?",
644         bind => [ $val ],
645       ) : (
646         stmt => "WHERE x IS NOT NULL",
647         stmt_q => "WHERE `x` IS NOT NULL",
648         bind => [],
649         warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
650       ),
651     } for ('', '-');  # with and without -
652   }
653 }
654
655 # check all multi-element inequality/not-like ops for warnings
656 for my $op ( qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
657   (my $sop = uc $op) =~ s/_/ /gi;
658
659   push @tests, {
660     func => 'where',
661     args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
662     stmt => "WHERE x $sop ? OR x $sop ?",
663     stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
664     bind => [ 42, 69 ],
665     warns  => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
666   } for ('', '-');  # with and without -
667 }
668
669 # check all like/not-like ops for empty-arrayref warnings
670 for my $op ( qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
671   (my $sop = uc $op) =~ s/_/ /gi;
672
673   push @tests, {
674     func => 'where',
675     args => [ { x => { "$_$op" => [] } } ],
676     stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
677     stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
678     bind => [],
679     warns  => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
680   } for ('', '-');  # with and without -
681 }
682
683 # check emtpty-lhs in a hashpair and arraypair
684 for my $lhs (undef, '') {
685   no warnings 'uninitialized';
686
687 ##
688 ## hard exceptions - never worked
689   for my $where_arg (
690     ( map { $_, { @$_ } }
691       [ $lhs => "foo" ],
692       [ $lhs => { "=" => "bozz" } ],
693       [ $lhs => { "=" => \"bozz" } ],
694       [ $lhs => { -max => \"bizz" } ],
695     ),
696     [ -and => { $lhs => "baz" }, bizz => "buzz" ],
697     [ foo => "bar", { $lhs => "baz" }, bizz => "buzz" ],
698     { foo => "bar", -or => { $lhs => "baz" } },
699
700     # the hashref forms of these work sadly - check for warnings below
701     { foo => "bar", -and => [ $lhs => \"baz" ], bizz => "buzz" },
702     { foo => "bar", -or => [ $lhs => \"baz" ], bizz => "buzz" },
703     [ foo => "bar", [ $lhs => \"baz" ], bizz => "buzz" ],
704     [ foo => "bar", $lhs => \"baz", bizz => "buzz" ],
705     [ foo => "bar", $lhs => \["baz"], bizz => "buzz" ],
706     [ $lhs => \"baz" ],
707     [ $lhs => \["baz"] ],
708
709     # except for this one, that is automagically arrayified
710     { foo => "bar", -or => { $lhs => \"baz" }, bizz => "buzz" },
711   ) {
712     push @tests, {
713       func => 'where',
714       args => [ $where_arg ],
715       throws  => qr/\QSupplying an empty left hand side argument is not supported/,
716     };
717   }
718
719 ##
720 ## deprecations - sorta worked, likely abused by folks
721   for my $where_arg (
722     # the arrayref forms of this never worked and throw above
723     { foo => "bar", -and => { $lhs => \"baz" }, bizz => "buzz" },
724     { foo => "bar", $lhs => \"baz", bizz => "buzz" },
725     { foo => "bar", $lhs => \["baz"], bizz => "buzz" },
726   ) {
727     push @tests, {
728       func    => 'where',
729       args    => [ $where_arg ],
730       stmt    => 'WHERE baz AND bizz = ? AND foo = ?',
731       stmt_q  => 'WHERE baz AND `bizz` = ? AND `foo` = ?',
732       bind    => [qw( buzz bar )],
733       warns   => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
734     };
735   }
736
737   for my $where_arg (
738     { $lhs => \"baz" },
739     { $lhs => \["baz"] },
740   ) {
741     push @tests, {
742       func    => 'where',
743       args    => [ $where_arg ],
744       stmt    => 'WHERE baz',
745       stmt_q  => 'WHERE baz',
746       bind    => [],
747       warns   => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
748     }
749   }
750 }
751
752 # check false lhs, silly but possible
753 {
754   for my $where_arg (
755     [ { 0 => "baz" }, bizz => "buzz", foo => "bar" ],
756     [ -or => { foo => "bar", -or => { 0 => "baz" }, bizz => "buzz" } ],
757   ) {
758     push @tests, {
759       func    => 'where',
760       args    => [ $where_arg ],
761       stmt    => 'WHERE 0 = ? OR bizz = ? OR foo = ?',
762       stmt_q  => 'WHERE `0` = ? OR `bizz` = ? OR `foo` = ?',
763       bind    => [qw( baz buzz bar )],
764     };
765   }
766
767   for my $where_arg (
768     { foo => "bar", -and => [ 0 => \"= baz" ], bizz => "buzz" },
769     { foo => "bar", -or => [ 0 => \"= baz" ], bizz => "buzz" },
770
771     { foo => "bar", -and => { 0 => \"= baz" }, bizz => "buzz" },
772     { foo => "bar", -or => { 0 => \"= baz" }, bizz => "buzz" },
773
774     { foo => "bar", 0 => \"= baz", bizz => "buzz" },
775     { foo => "bar", 0 => \["= baz"], bizz => "buzz" },
776   ) {
777     push @tests, {
778       func    => 'where',
779       args    => [ $where_arg ],
780       stmt    => 'WHERE 0 = baz AND bizz = ? AND foo = ?',
781       stmt_q  => 'WHERE `0` = baz AND `bizz` = ? AND `foo` = ?',
782       bind    => [qw( buzz bar )],
783     };
784   }
785
786   for my $where_arg (
787     [ -and => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
788     [ -or => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
789     [ 0 => \"= baz", bizz => "buzz", foo => "bar" ],
790     [ 0 => \["= baz"], bizz => "buzz", foo => "bar" ],
791   ) {
792     push @tests, {
793       func    => 'where',
794       args    => [ $where_arg ],
795       stmt    => 'WHERE 0 = baz OR bizz = ? OR foo = ?',
796       stmt_q  => 'WHERE `0` = baz OR `bizz` = ? OR `foo` = ?',
797       bind    => [qw( buzz bar )],
798     };
799   }
800 }
801
802 for my $t (@tests) {
803   my $new = $t->{new} || {};
804
805   for my $quoted (0, 1) {
806
807     my $maker = SQL::Abstract->new(
808       %$new,
809       ($quoted ? (
810         quote_char => '`',
811         name_sep => '.',
812         ( $t->{esc} ? (
813           escape_char => $t->{esc},
814         ) : ())
815       ) : ())
816     );
817
818     my($stmt, @bind);
819
820     my $cref = sub {
821       my $op = $t->{func};
822       ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
823     };
824
825     if (my $e = $t->{throws}) {
826       throws_ok(
827         sub { $cref->() },
828         $e,
829       ) || diag dumper ({ args => $t->{args}, result => $stmt });
830     }
831     else {
832       warnings_like(
833         sub { $cref->() },
834         $t->{warns} || [],
835       ) || diag dumper ({ args => $t->{args}, result => $stmt });
836
837       is_same_sql_bind(
838         $stmt,
839         \@bind,
840         $quoted ? $t->{stmt_q}: $t->{stmt},
841         $t->{bind}
842       );
843     }
844   }
845 }
846
847 done_testing;