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