Reintroduce handling of IS( NOT) ops
[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',
72 args => [[qw/test1 test2/], '*', { 'test1.a' => { 'In', ['boom', 'bang'] } }],
73 stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a IN ( ?, ? ) )',
74 stmt_q => 'SELECT * FROM `test1`, `test2` WHERE ( `test1`.`a` IN ( ?, ? ) )',
75 bind => ['boom', 'bang']
4049d0e3 76 },
77 {
32eab2da 78 func => 'select',
79 args => ['test', '*', { a => { 'between', ['boom', 'bang'] } }],
80 stmt => 'SELECT * FROM test WHERE ( a BETWEEN ? AND ? )',
81 stmt_q => 'SELECT * FROM `test` WHERE ( `a` BETWEEN ? AND ? )',
82 bind => ['boom', 'bang']
4049d0e3 83 },
84 {
32eab2da 85 func => 'select',
86 args => ['test', '*', { a => { '!=', 'boom' } }],
87 stmt => 'SELECT * FROM test WHERE ( a != ? )',
88 stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
89 bind => ['boom']
4049d0e3 90 },
91 {
32eab2da 92 func => 'update',
93 args => ['test', {a => 'boom'}, {a => undef}],
94 stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )',
95 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )',
96 bind => ['boom']
4049d0e3 97 },
98 {
32eab2da 99 func => 'update',
100 args => ['test', {a => 'boom'}, { a => {'!=', "bang" }} ],
101 stmt => 'UPDATE test SET a = ? WHERE ( a != ? )',
102 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` != ? )',
103 bind => ['boom', 'bang']
4049d0e3 104 },
105 {
32eab2da 106 func => 'update',
107 args => ['test', {'a-funny-flavored-candy' => 'yummy', b => 'oops'}, { a42 => "bang" }],
108 stmt => 'UPDATE test SET a-funny-flavored-candy = ?, b = ? WHERE ( a42 = ? )',
109 stmt_q => 'UPDATE `test` SET `a-funny-flavored-candy` = ?, `b` = ? WHERE ( `a42` = ? )',
110 bind => ['yummy', 'oops', 'bang']
4049d0e3 111 },
112 {
32eab2da 113 func => 'delete',
114 args => ['test', {requestor => undef}],
115 stmt => 'DELETE FROM test WHERE ( requestor IS NULL )',
116 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )',
117 bind => []
4049d0e3 118 },
119 {
32eab2da 120 func => 'delete',
121 args => [[qw/test1 test2 test3/],
122 { 'test1.field' => \'!= test2.field',
123 user => {'!=','nwiger'} },
124 ],
125 stmt => 'DELETE FROM test1, test2, test3 WHERE ( test1.field != test2.field AND user != ? )',
126 stmt_q => 'DELETE FROM `test1`, `test2`, `test3` WHERE ( `test1`.`field` != test2.field AND `user` != ? )', # test2.field is a literal value, cannnot be quoted.
127 bind => ['nwiger']
4049d0e3 128 },
129 {
32eab2da 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/],
4049d0e3 135 },
136 {
32eab2da 137 func => 'insert',
138 args => ['test', [qw/1 2 3 4 5/]],
139 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?)',
140 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?)',
141 bind => [qw/1 2 3 4 5/],
4049d0e3 142 },
143 {
32eab2da 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],
4049d0e3 149 },
150 {
32eab2da 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/],
4049d0e3 156 },
157 {
32eab2da 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/],
4049d0e3 163 },
164 {
32eab2da 165 func => 'update',
96449e8e 166 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
32eab2da 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)],
4049d0e3 170 },
171 {
32eab2da 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'],
4049d0e3 177 },
178 {
257d9a42 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'],
4049d0e3 184 },
185 {
32eab2da 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...
4049d0e3 192 },
193 {
32eab2da 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...
4049d0e3 200 },
201 {
32eab2da 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%']],
4049d0e3 212 },
213 {
32eab2da 214 func => 'select',
07936978 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%)],
4049d0e3 219 },
220 {
32eab2da 221 func => 'select',
4049d0e3 222 args => ['Yo Momma', '*', { user => 'nwiger',
32eab2da 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)],
4049d0e3 227 },
228 {
32eab2da 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 ],
3cdadcbe 235 warns => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
236
32eab2da 237 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
e30faf88 238 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
32eab2da 239 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
e30faf88 240 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
32eab2da 241 bind => [qw(2 4 mr.happy yes YES yes YES)],
4049d0e3 242 },
243 {
32eab2da 244 func => 'select',
2d2df6ba 245 args => ['jeff', '*', { name => {'ilike', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
32eab2da 246 -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
96449e8e 247 yob => {'<', 1976} ] ] } ],
e30faf88 248 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )'
2d2df6ba 249 . ' AND name NOT IN ( ?, ?, ?, ? ) AND name ILIKE ? )',
e30faf88 250 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )'
2d2df6ba 251 . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` ILIKE ? )',
32eab2da 252 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
4049d0e3 253 },
254 {
32eab2da 255 func => 'update',
24c898da 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 ] ],
32eab2da 261 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
b43d4cef 262 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
32eab2da 263 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
b43d4cef 264 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
32eab2da 265 bind => [qw(4 black white asian yes candace jugs canyon towers)]
266 },
96449e8e 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 },
96449e8e 274 {
275 func => 'select',
96449e8e 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'],
d82b8afb 280 },
d82b8afb 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 },
d82b8afb 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 },
d82b8afb 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 },
d82b8afb 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 },
145fbfc8 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],
4049d0e3 319 },
b3be7bd0 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],
4049d0e3 326 },
5db47f9f 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}],
97084113 333 warns => qr/HASH ref as bind value in insert is not supported/i,
4049d0e3 334 },
335 {
ef4d99a5 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)],
4049d0e3 341 },
ef4d99a5 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 },
ef4d99a5 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 },
ef4d99a5 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)],
4049d0e3 362 },
cd87fd4c 363 {
364 func => 'insert',
365 new => {bindtype => 'columns'},
fe3ae272 366 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 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\'))',
fe3ae272 369 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 370 },
4049d0e3 371 {
cd87fd4c 372 func => 'update',
373 new => {bindtype => 'columns'},
fe3ae272 374 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 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 ? )',
fe3ae272 377 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
4049d0e3 378 },
cd87fd4c 379 {
380 func => 'select',
381 new => {bindtype => 'columns'},
fe3ae272 382 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 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') )},
fe3ae272 385 bind => [[dummy => '02/02/02']],
cd87fd4c 386 },
cd87fd4c 387 {
388 func => 'select',
389 new => {bindtype => 'columns'},
fe3ae272 390 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 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` = ? )',
fe3ae272 393 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 394 },
fe3ae272 395 {
396 func => 'insert',
397 new => {bindtype => 'columns'},
398 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 399 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 400 },
4049d0e3 401 {
fe3ae272 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]}}],
97084113 405 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 406 },
fe3ae272 407 {
408 func => 'select',
409 new => {bindtype => 'columns'},
410 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 411 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 412 },
fe3ae272 413 {
414 func => 'select',
415 new => {bindtype => 'columns'},
416 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
97084113 417 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 418 },
044e8ff4 419 {
420 func => 'select',
a5e0ac94 421 args => ['test', '*', { foo => { '>=' => [] }} ],
422 throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
423 },
424 {
425 func => 'select',
044e8ff4 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]],
4049d0e3 431 },
044e8ff4 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 }],
97084113 436 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 437 },
26f2dca5 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 },
4049d0e3 446 {
26f2dca5 447 func => 'update',
448 new => {bindtype => 'columns'},
0ec3aec7 449 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
6e9a377b 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 ? )",
0ec3aec7 452 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
4049d0e3 453 },
26f2dca5 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 },
26f2dca5 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]],
05a05fd1 469 },
97b9b66e 470 {
471 func => 'select',
472 new => {bindtype => 'columns'},
e30faf88 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` = ? )',
97b9b66e 476 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 477 },
05a05fd1 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 },
05a05fd1 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 },
02288357 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 },
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 => [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 },
02288357 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 },
02288357 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 },
07936978 529 {
530 func => 'select',
531 new => {bindtype => 'columns'},
953d164e 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 ? ) ) ) )',
07936978 535 bind => [[Y => 'x']],
536 },
0dfd2442 537 {
538 func => 'select',
539 args => ['test', '*', { a => { -in => [] }, b => { -not_in => [] }, c => { -in => 42 } }],
540 stmt => 'SELECT * FROM test WHERE ( 0=1 AND 1=1 AND c IN ( ? ))',
541 stmt_q => 'SELECT * FROM `test` WHERE ( 0=1 AND 1=1 AND `c` IN ( ? ))',
542 bind => [ 42 ],
543 },
ed56a54c 544 {
545 func => 'select',
546 args => ['test', '*', { a => { -in => [] }, b => { -not_in => [] } }],
547 stmt => 'SELECT * FROM test WHERE ( 0=1 AND 1=1 )',
548 stmt_q => 'SELECT * FROM `test` WHERE ( 0=1 AND 1=1 )',
549 bind => [],
550 },
032dfe20 551 {
97084113 552 throws => qr/
032dfe20 553 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
554 \Qwhen the -IN operator was given an undef-containing list: \E
555 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
556 \Qversion of SQL::Abstract will emit the logically correct SQL \E
557 \Qinstead of raising this exception)\E
558 /x,
559 func => 'select',
560 args => ['test', '*', { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } } ],
561 stmt => 'SELECT * FROM test WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
562 stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` IN ( ? ) OR `a` IS NULL ) AND `b` NOT IN ( ? ) AND `b` IS NOT NULL )',
563 bind => [ 42, 42 ],
564 },
565 {
97084113 566 throws => qr/
032dfe20 567 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
568 \Qwhen the -IN operator was given an undef-containing list: \E
569 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
570 \Qversion of SQL::Abstract will emit the logically correct SQL \E
571 \Qinstead of raising this exception)\E
572 /x,
573 func => 'select',
574 args => ['test', '*', { a => { -in => [undef] }, b => { -not_in => [undef] } } ],
575 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NOT NULL )',
576 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NOT NULL )',
577 bind => [],
578 },
ff8ca6b4 579 {
580 func => 'select',
581 args => ['test', '*', { a => { -in => undef } }],
97084113 582 throws => qr/Argument passed to the 'IN' operator can not be undefined/,
ff8ca6b4 583 },
32eab2da 584);
585
b9b5a0b1 586# check is( not) => undef
587for my $op (qw( is is_not), 'is not' ) {
588 (my $sop = uc $op) =~ s/_/ /gi;
589
590 push @tests, {
591 func => 'where',
592 args => [{ a => { "$_$op" => undef } }],
593 stmt => "WHERE a $sop NULL",
594 stmt_q => "WHERE `a` $sop NULL",
595 bind => [],
596 } for ('', '-'); # with and without -
597}
598
3cdadcbe 599# check single-element inequality ops for no warnings
600for my $op ( qw(!= <>) ) {
601 for my $val (undef, 42) {
602 push @tests, {
603 func => 'where',
604 args => [ { x => { "$_$op" => [ $val ] } } ],
605 stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
606 stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
607 bind => [ $val || () ],
608 } for ('', '-'); # with and without -
609 }
610}
611
612# check single-element not-like ops for no warnings, and NULL exception
613# (the last two "is not X" are a weird syntax, but mebbe a dialect...)
614for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
615 (my $sop = uc $op) =~ s/_/ /gi;
616
617 for my $val (undef, 42) {
618 push @tests, {
619 func => 'where',
620 args => [ { x => { "$_$op" => [ $val ] } } ],
621 $val ? (
622 stmt => "WHERE x $sop ?",
623 stmt_q => "WHERE `x` $sop ?",
624 bind => [ $val ],
625 ) : (
626 stmt => "WHERE x IS NOT NULL",
627 stmt_q => "WHERE `x` IS NOT NULL",
628 bind => [],
629 warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
630 ),
631 } for ('', '-'); # with and without -
632 }
633}
634
635# check all multi-element inequality/not-like ops for warnings
636for my $op ( qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
637 (my $sop = uc $op) =~ s/_/ /gi;
638
639 push @tests, {
640 func => 'where',
641 args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
642 stmt => "WHERE x $sop ? OR x $sop ?",
643 stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
644 bind => [ 42, 69 ],
645 warns => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
646 } for ('', '-'); # with and without -
647}
648
649# check all like/not-like ops for empty-arrayref warnings
650for my $op ( qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
651 (my $sop = uc $op) =~ s/_/ /gi;
652
653 push @tests, {
654 func => 'where',
655 args => [ { x => { "$_$op" => [] } } ],
656 stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
657 stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
658 bind => [],
659 warns => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
660 } for ('', '-'); # with and without -
661}
662
3a06278c 663for my $t (@tests) {
3a06278c 664 my $new = $t->{new} || {};
32eab2da 665
3a06278c 666 for my $quoted (0, 1) {
7fb57243 667
3a06278c 668 my $maker = SQL::Abstract->new(%$new, $quoted
669 ? (quote_char => '`', name_sep => '.')
670 : ()
671 );
32eab2da 672
3a06278c 673 my($stmt, @bind);
32eab2da 674
3a06278c 675 my $cref = sub {
676 my $op = $t->{func};
677 ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
7fb57243 678 };
3a06278c 679
97084113 680 if (my $e = $t->{throws}) {
3a06278c 681 throws_ok(
682 sub { $cref->() },
97084113 683 $e,
3cdadcbe 684 ) || diag dumper ({ args => $t->{args}, result => $stmt });
97084113 685 }
686 else {
687 warnings_exist(
688 sub { $cref->() },
689 $t->{warns} || [],
3a06278c 690 );
032dfe20 691
3a06278c 692 is_same_sql_bind(
693 $stmt,
694 \@bind,
695 $quoted ? $t->{stmt_q}: $t->{stmt},
696 $t->{bind}
697 );
fe3ae272 698 }
7fb57243 699 }
32eab2da 700}
10e6c946 701
702done_testing;