Fix regression in column level { not => undef } op
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
CommitLineData
32eab2da 1use strict;
41751122 2use warnings;
3use Test::More;
7fb57243 4use Test::Warn;
fe3ae272 5use Test::Exception;
32eab2da 6
3cdadcbe 7use SQL::Abstract::Test import => [qw( is_same_sql_bind diag_where dumper )];
32eab2da 8
9use SQL::Abstract;
10
48d9f5f8 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
32eab2da 20my @tests = (
32eab2da 21 {
22 func => 'select',
23 args => ['test', '*'],
24 stmt => 'SELECT * FROM test',
25 stmt_q => 'SELECT * FROM `test`',
26 bind => []
27 },
32eab2da 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 },
32eab2da 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]
4049d0e3 41 },
42 {
32eab2da 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]
4049d0e3 48 },
49 {
32eab2da 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 => []
4049d0e3 55 },
56 {
32eab2da 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']
4049d0e3 62 },
63 {
32eab2da 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']
4049d0e3 69 },
70 {
32eab2da 71 func => 'select',
32eab2da 72 args => ['test', '*', { a => { '!=', 'boom' } }],
73 stmt => 'SELECT * FROM test WHERE ( a != ? )',
74 stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
75 bind => ['boom']
4049d0e3 76 },
77 {
32eab2da 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']
4049d0e3 83 },
84 {
32eab2da 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']
4049d0e3 90 },
91 {
32eab2da 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']
4049d0e3 97 },
98 {
32eab2da 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 => []
4049d0e3 104 },
105 {
32eab2da 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']
4049d0e3 114 },
115 {
32eab2da 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/],
4049d0e3 121 },
122 {
32eab2da 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/],
4049d0e3 128 },
129 {
32eab2da 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],
4049d0e3 135 },
136 {
32eab2da 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/],
4049d0e3 142 },
143 {
32eab2da 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/],
4049d0e3 149 },
150 {
32eab2da 151 func => 'update',
96449e8e 152 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
32eab2da 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)],
4049d0e3 156 },
157 {
32eab2da 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'],
4049d0e3 163 },
164 {
257d9a42 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'],
4049d0e3 170 },
171 {
32eab2da 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...
4049d0e3 178 },
179 {
32eab2da 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...
4049d0e3 186 },
187 {
32eab2da 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%']],
4049d0e3 198 },
199 {
32eab2da 200 func => 'select',
07936978 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%)],
4049d0e3 205 },
206 {
32eab2da 207 func => 'select',
4049d0e3 208 args => ['Yo Momma', '*', { user => 'nwiger',
32eab2da 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)],
4049d0e3 213 },
214 {
32eab2da 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 ],
3cdadcbe 221 warns => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
222
32eab2da 223 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
e30faf88 224 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
32eab2da 225 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
e30faf88 226 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
32eab2da 227 bind => [qw(2 4 mr.happy yes YES yes YES)],
4049d0e3 228 },
229 {
32eab2da 230 func => 'select',
2d2df6ba 231 args => ['jeff', '*', { name => {'ilike', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
32eab2da 232 -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
96449e8e 233 yob => {'<', 1976} ] ] } ],
e30faf88 234 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )'
2d2df6ba 235 . ' AND name NOT IN ( ?, ?, ?, ? ) AND name ILIKE ? )',
e30faf88 236 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )'
2d2df6ba 237 . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` ILIKE ? )',
32eab2da 238 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
4049d0e3 239 },
240 {
32eab2da 241 func => 'update',
24c898da 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 ] ],
32eab2da 247 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
b43d4cef 248 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
32eab2da 249 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
b43d4cef 250 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
32eab2da 251 bind => [qw(4 black white asian yes candace jugs canyon towers)]
252 },
96449e8e 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 },
96449e8e 260 {
261 func => 'select',
96449e8e 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'],
d82b8afb 266 },
d82b8afb 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 },
d82b8afb 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 },
d82b8afb 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 },
d82b8afb 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 },
145fbfc8 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],
4049d0e3 305 },
b3be7bd0 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],
4049d0e3 312 },
5db47f9f 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}],
97084113 319 warns => qr/HASH ref as bind value in insert is not supported/i,
4049d0e3 320 },
321 {
ef4d99a5 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)],
4049d0e3 327 },
ef4d99a5 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 },
ef4d99a5 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 },
ef4d99a5 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)],
4049d0e3 348 },
cd87fd4c 349 {
350 func => 'insert',
351 new => {bindtype => 'columns'},
fe3ae272 352 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 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\'))',
fe3ae272 355 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 356 },
4049d0e3 357 {
cd87fd4c 358 func => 'update',
359 new => {bindtype => 'columns'},
fe3ae272 360 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 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 ? )',
fe3ae272 363 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
4049d0e3 364 },
cd87fd4c 365 {
366 func => 'select',
367 new => {bindtype => 'columns'},
fe3ae272 368 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 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') )},
fe3ae272 371 bind => [[dummy => '02/02/02']],
cd87fd4c 372 },
cd87fd4c 373 {
374 func => 'select',
375 new => {bindtype => 'columns'},
fe3ae272 376 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 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` = ? )',
fe3ae272 379 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 380 },
fe3ae272 381 {
382 func => 'insert',
383 new => {bindtype => 'columns'},
384 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 385 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 386 },
4049d0e3 387 {
fe3ae272 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]}}],
97084113 391 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 392 },
fe3ae272 393 {
394 func => 'select',
395 new => {bindtype => 'columns'},
396 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 397 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 398 },
fe3ae272 399 {
400 func => 'select',
401 new => {bindtype => 'columns'},
402 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
97084113 403 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 404 },
044e8ff4 405 {
406 func => 'select',
a5e0ac94 407 args => ['test', '*', { foo => { '>=' => [] }} ],
408 throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
409 },
410 {
411 func => 'select',
044e8ff4 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]],
4049d0e3 417 },
044e8ff4 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 }],
97084113 422 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 423 },
26f2dca5 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 },
4049d0e3 432 {
26f2dca5 433 func => 'update',
434 new => {bindtype => 'columns'},
0ec3aec7 435 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
6e9a377b 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 ? )",
0ec3aec7 438 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
4049d0e3 439 },
26f2dca5 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 },
26f2dca5 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]],
05a05fd1 455 },
97b9b66e 456 {
457 func => 'select',
458 new => {bindtype => 'columns'},
e30faf88 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` = ? )',
97b9b66e 462 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 463 },
05a05fd1 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 },
05a05fd1 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 },
02288357 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 },
02288357 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 },
02288357 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 },
02288357 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 },
02288357 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 },
07936978 515 {
516 func => 'select',
517 new => {bindtype => 'columns'},
953d164e 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 ? ) ) ) )',
07936978 521 bind => [[Y => 'x']],
522 },
0dfd2442 523 {
524 func => 'select',
e3f38515 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 },
32eab2da 555);
556
b9b5a0b1 557# check is( not) => undef
40f2f231 558for my $op ( qw(not is is_not), 'is not' ) {
b9b5a0b1 559 (my $sop = uc $op) =~ s/_/ /gi;
560
40f2f231 561 $sop = 'IS NOT' if $sop eq 'NOT';
562
563 for my $uc (0, 1) {
564 for my $prefix ('', '-') {
565 push @tests, {
566 func => 'where',
567 args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
568 stmt => "WHERE a $sop NULL",
569 stmt_q => "WHERE `a` $sop NULL",
570 bind => [],
571 };
572 }
573 }
b9b5a0b1 574}
575
3cdadcbe 576# check single-element inequality ops for no warnings
577for my $op ( qw(!= <>) ) {
578 for my $val (undef, 42) {
579 push @tests, {
580 func => 'where',
581 args => [ { x => { "$_$op" => [ $val ] } } ],
582 stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
583 stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
584 bind => [ $val || () ],
585 } for ('', '-'); # with and without -
586 }
587}
588
589# check single-element not-like ops for no warnings, and NULL exception
590# (the last two "is not X" are a weird syntax, but mebbe a dialect...)
591for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
592 (my $sop = uc $op) =~ s/_/ /gi;
593
594 for my $val (undef, 42) {
595 push @tests, {
596 func => 'where',
597 args => [ { x => { "$_$op" => [ $val ] } } ],
598 $val ? (
599 stmt => "WHERE x $sop ?",
600 stmt_q => "WHERE `x` $sop ?",
601 bind => [ $val ],
602 ) : (
603 stmt => "WHERE x IS NOT NULL",
604 stmt_q => "WHERE `x` IS NOT NULL",
605 bind => [],
606 warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
607 ),
608 } for ('', '-'); # with and without -
609 }
610}
611
612# check all multi-element inequality/not-like ops for warnings
613for my $op ( qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
614 (my $sop = uc $op) =~ s/_/ /gi;
615
616 push @tests, {
617 func => 'where',
618 args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
619 stmt => "WHERE x $sop ? OR x $sop ?",
620 stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
621 bind => [ 42, 69 ],
622 warns => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
623 } for ('', '-'); # with and without -
624}
625
626# check all like/not-like ops for empty-arrayref warnings
627for my $op ( qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
628 (my $sop = uc $op) =~ s/_/ /gi;
629
630 push @tests, {
631 func => 'where',
632 args => [ { x => { "$_$op" => [] } } ],
633 stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
634 stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
635 bind => [],
636 warns => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
637 } for ('', '-'); # with and without -
638}
639
3a06278c 640for my $t (@tests) {
3a06278c 641 my $new = $t->{new} || {};
32eab2da 642
3a06278c 643 for my $quoted (0, 1) {
7fb57243 644
3a06278c 645 my $maker = SQL::Abstract->new(%$new, $quoted
646 ? (quote_char => '`', name_sep => '.')
647 : ()
648 );
32eab2da 649
3a06278c 650 my($stmt, @bind);
32eab2da 651
3a06278c 652 my $cref = sub {
653 my $op = $t->{func};
654 ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
7fb57243 655 };
3a06278c 656
97084113 657 if (my $e = $t->{throws}) {
3a06278c 658 throws_ok(
659 sub { $cref->() },
97084113 660 $e,
3cdadcbe 661 ) || diag dumper ({ args => $t->{args}, result => $stmt });
97084113 662 }
663 else {
5eccd8f0 664 warnings_like(
97084113 665 sub { $cref->() },
666 $t->{warns} || [],
5eccd8f0 667 ) || diag dumper ({ args => $t->{args}, result => $stmt });
032dfe20 668
3a06278c 669 is_same_sql_bind(
670 $stmt,
671 \@bind,
672 $quoted ? $t->{stmt_q}: $t->{stmt},
673 $t->{bind}
674 );
fe3ae272 675 }
7fb57243 676 }
32eab2da 677}
10e6c946 678
679done_testing;