First cut of a DQ-SQLA trial
[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 {
e4327a54 116 func => 'select',
117 args => [[\'test1', 'test2'], '*', { 'test1.a' => 'boom' } ],
118 stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a = ? )',
119 stmt_q => 'SELECT * FROM test1, `test2` WHERE ( `test1`.`a` = ? )',
120 bind => ['boom']
121 },
122 {
32eab2da 123 func => 'insert',
124 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
125 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
126 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
127 bind => [qw/1 2 3 4 5/],
4049d0e3 128 },
129 {
32eab2da 130 func => 'insert',
131 args => ['test', [qw/1 2 3 4 5/]],
132 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?)',
133 stmt_q => 'INSERT INTO `test` 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/, undef]],
139 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)',
140 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)',
141 bind => [qw/1 2 3 4 5/, undef],
4049d0e3 142 },
143 {
32eab2da 144 func => 'update',
145 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
146 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?',
147 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?',
148 bind => [qw/1 2 3 4 5/],
4049d0e3 149 },
150 {
32eab2da 151 func => 'update',
152 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}],
153 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )',
154 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )',
155 bind => [qw/1 2 3 4 5 1 2 3 4 5/],
4049d0e3 156 },
157 {
32eab2da 158 func => 'update',
96449e8e 159 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
32eab2da 160 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
161 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
162 bind => [qw(1 02/02/02 1 2)],
4049d0e3 163 },
164 {
32eab2da 165 func => 'insert',
166 args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ],
167 stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)',
168 stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)',
169 bind => ['4'],
4049d0e3 170 },
171 {
257d9a42 172 func => 'insert',
173 args => ['test.table', [ \'max(all_limits)', 4 ] ],
174 stmt => 'INSERT INTO test.table VALUES (max(all_limits), ?)',
175 stmt_q => 'INSERT INTO `test`.`table` VALUES (max(all_limits), ?)',
176 bind => ['4'],
4049d0e3 177 },
178 {
32eab2da 179 func => 'insert',
180 new => {bindtype => 'columns'},
181 args => ['test.table', {one => 2, three => 4, five => 6} ],
182 stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
183 stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
184 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
4049d0e3 185 },
186 {
32eab2da 187 func => 'select',
188 new => {bindtype => 'columns', case => 'lower'},
189 args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
190 stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
191 stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
192 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
4049d0e3 193 },
194 {
32eab2da 195 func => 'update',
196 new => {bindtype => 'columns', cmp => 'like'},
197 args => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
198 {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
199 stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
200 . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
201 stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
202 . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
203 bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
204 ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
4049d0e3 205 },
206 {
32eab2da 207 func => 'select',
07936978 208 args => ['test', '*', {priority => [ -and => {'!=', 2}, { -not_like => '3%'} ]}],
209 stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority NOT LIKE ? ) ) )',
210 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` NOT LIKE ? ) ) )',
211 bind => [qw(2 3%)],
4049d0e3 212 },
213 {
32eab2da 214 func => 'select',
4049d0e3 215 args => ['Yo Momma', '*', { user => 'nwiger',
32eab2da 216 -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
217 stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )',
218 stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )',
219 bind => [qw(20 ASIA nwiger)],
4049d0e3 220 },
221 {
32eab2da 222 func => 'update',
223 args => ['taco_punches', { one => 2, three => 4 },
224 { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
225 tasty => { '!=', [qw(yes YES)] },
226 -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
227 ],
3cdadcbe 228 warns => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
229
32eab2da 230 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
e30faf88 231 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
32eab2da 232 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
e30faf88 233 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
32eab2da 234 bind => [qw(2 4 mr.happy yes YES yes YES)],
4049d0e3 235 },
236 {
32eab2da 237 func => 'select',
2d2df6ba 238 args => ['jeff', '*', { name => {'ilike', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
32eab2da 239 -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
96449e8e 240 yob => {'<', 1976} ] ] } ],
e30faf88 241 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )'
2d2df6ba 242 . ' AND name NOT IN ( ?, ?, ?, ? ) AND name ILIKE ? )',
e30faf88 243 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )'
2d2df6ba 244 . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` ILIKE ? )',
32eab2da 245 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
4049d0e3 246 },
247 {
32eab2da 248 func => 'update',
24c898da 249 args => ['fhole', {fpoles => 4}, [
250 { race => [qw/-or black white asian /] },
251 { -nest => { firsttime => [-or => {'=','yes'}, undef] } },
252 { -and => [ { firstname => {-not_like => 'candace'} }, { lastname => {-in => [qw(jugs canyon towers)] } } ] },
253 ] ],
32eab2da 254 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
b43d4cef 255 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
32eab2da 256 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
b43d4cef 257 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
32eab2da 258 bind => [qw(4 black white asian yes candace jugs canyon towers)]
259 },
96449e8e 260 {
261 func => 'insert',
262 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
263 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
264 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
265 bind => [qw(1 02/02/02)],
266 },
96449e8e 267 {
268 func => 'select',
96449e8e 269 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
270 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
271 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
272 bind => ['02/02/02'],
d82b8afb 273 },
d82b8afb 274 {
275 func => 'insert',
276 new => {array_datatypes => 1},
277 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
278 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
279 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
280 bind => [1, [1, 1, 2, 3, 5, 8]],
281 },
d82b8afb 282 {
283 func => 'insert',
284 new => {bindtype => 'columns', array_datatypes => 1},
285 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
286 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
287 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
288 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
289 },
d82b8afb 290 {
291 func => 'update',
292 new => {array_datatypes => 1},
293 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
294 stmt => 'UPDATE test SET a = ?, b = ?',
295 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
296 bind => [1, [1, 1, 2, 3, 5, 8]],
297 },
d82b8afb 298 {
299 func => 'update',
300 new => {bindtype => 'columns', array_datatypes => 1},
301 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
302 stmt => 'UPDATE test SET a = ?, b = ?',
303 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
304 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
305 },
145fbfc8 306 {
307 func => 'select',
308 args => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
309 stmt => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
310 stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
311 bind => [8],
4049d0e3 312 },
b3be7bd0 313 {
314 func => 'select',
315 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
316 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
317 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
318 bind => ['02/02/02', 8],
4049d0e3 319 },
ed821a87 320 {
5db47f9f 321 func => 'insert',
ed821a87 322 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => { -answer => 42 }}],
323 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ANSWER(?))',
324 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ANSWER(?))',
325 bind => [qw/1 2 3 4 42/],
4049d0e3 326 },
327 {
ef4d99a5 328 func => 'update',
329 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
330 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
331 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
332 bind => [qw(1 1 2)],
4049d0e3 333 },
ef4d99a5 334 {
335 func => 'insert',
336 args => ['test', {a => 1, b => \["42"]}],
337 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
338 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
339 bind => [qw(1)],
340 },
ef4d99a5 341 {
342 func => 'select',
343 args => ['test', '*', { a => \["= 42"], b => 1}],
344 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
345 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
346 bind => [qw(1)],
347 },
ef4d99a5 348 {
349 func => 'select',
350 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
351 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
352 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
353 bind => [qw(8)],
4049d0e3 354 },
cd87fd4c 355 {
356 func => 'insert',
357 new => {bindtype => 'columns'},
fe3ae272 358 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 359 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
360 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
fe3ae272 361 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 362 },
4049d0e3 363 {
cd87fd4c 364 func => 'update',
365 new => {bindtype => 'columns'},
fe3ae272 366 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 367 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
368 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
fe3ae272 369 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
4049d0e3 370 },
cd87fd4c 371 {
372 func => 'select',
373 new => {bindtype => 'columns'},
fe3ae272 374 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 375 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
376 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
fe3ae272 377 bind => [[dummy => '02/02/02']],
cd87fd4c 378 },
cd87fd4c 379 {
380 func => 'select',
381 new => {bindtype => 'columns'},
fe3ae272 382 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 383 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
384 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
fe3ae272 385 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 386 },
fe3ae272 387 {
388 func => 'insert',
389 new => {bindtype => 'columns'},
390 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 391 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 392 },
4049d0e3 393 {
fe3ae272 394 func => 'update',
395 new => {bindtype => 'columns'},
396 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
97084113 397 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 398 },
fe3ae272 399 {
400 func => 'select',
401 new => {bindtype => 'columns'},
402 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 403 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 404 },
fe3ae272 405 {
406 func => 'select',
407 new => {bindtype => 'columns'},
408 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
97084113 409 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 410 },
044e8ff4 411 {
412 func => 'select',
a5e0ac94 413 args => ['test', '*', { foo => { '>=' => [] }} ],
414 throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
4049d0e3 415 },
044e8ff4 416 {
417 func => 'select',
418 new => {bindtype => 'columns'},
419 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
420 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
421 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
422 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 423 },
044e8ff4 424 {
425 func => 'select',
426 new => {bindtype => 'columns'},
427 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
97084113 428 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 429 },
26f2dca5 430 {
431 func => 'insert',
432 new => {bindtype => 'columns'},
433 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
434 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
435 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
436 bind => [[a => '1'], [{dummy => 1} => '02/02/02']],
437 },
4049d0e3 438 {
26f2dca5 439 func => 'update',
440 new => {bindtype => 'columns'},
0ec3aec7 441 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
427ef969 442 stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER(?) WHERE ( a BETWEEN ? AND ? )",
443 stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER(?) WHERE ( `a` BETWEEN ? AND ? )",
0ec3aec7 444 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
4049d0e3 445 },
26f2dca5 446 {
447 func => 'select',
448 new => {bindtype => 'columns'},
449 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
450 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
451 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
452 bind => [[{dummy => 1} => '02/02/02']],
453 },
26f2dca5 454 {
455 func => 'select',
456 new => {bindtype => 'columns'},
457 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
458 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
459 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
460 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
05a05fd1 461 },
97b9b66e 462 {
463 func => 'select',
464 new => {bindtype => 'columns'},
e30faf88 465 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
466 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
467 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
97b9b66e 468 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 469 },
05a05fd1 470 {
471 func => 'select',
472 new => {bindtype => 'columns'},
473 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
474 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
475 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
476 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
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 = ? OR b = ? ) OR ( a = ? AND b = ? )',
483 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
484 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
485 },
02288357 486 {
487 func => 'insert',
488 args => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
489 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
490 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
491 bind => [qw/1 2 3 4 5/],
492 },
02288357 493 {
494 func => 'insert',
495 args => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
496 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
497 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
498 bind => [qw/1 2 3 4 5/],
499 },
02288357 500 {
501 func => 'insert',
502 args => ['test', [qw/1 2 3 4 5/], { returning => [qw(id foo bar) ] }],
503 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
504 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`, `foo`, `bar`',
505 bind => [qw/1 2 3 4 5/],
506 },
02288357 507 {
508 func => 'insert',
509 args => ['test', [qw/1 2 3 4 5/], { returning => \'id, foo, bar' }],
510 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
511 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
512 bind => [qw/1 2 3 4 5/],
513 },
02288357 514 {
515 func => 'insert',
516 args => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
517 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
518 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
519 bind => [qw/1 2 3 4 5/],
520 },
07936978 521 {
522 func => 'select',
523 new => {bindtype => 'columns'},
953d164e 524 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
525 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
526 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
07936978 527 bind => [[Y => 'x']],
528 },
bdf53200 529 {
530 func => 'select',
fce4b7a9 531 args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }],
532 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )',
533 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )',
534 bind => [],
e3f38515 535 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
fce4b7a9 536 },
537 {
538 func => 'select',
539 args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }],
540 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL )',
541 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL AND `c` IS NOT NULL )',
542 bind => [],
e3f38515 543 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
fce4b7a9 544 },
545 {
546 func => 'select',
547 args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }],
548 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )',
549 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )',
550 bind => [],
e3f38515 551 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
fce4b7a9 552 },
553 {
554 func => 'select',
555 args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }],
556 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL )',
557 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL )',
558 bind => [],
e3f38515 559 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
90c87778 560 },
32eab2da 561);
562
c8d97b3d 563# check is( not) => undef
564for my $op ( qw(not is is_not), 'is not' ) {
565 (my $sop = uc $op) =~ s/_/ /gi;
566
567 $sop = 'IS NOT' if $sop eq 'NOT';
568
569 for my $uc (0, 1) {
570 for my $prefix ('', '-') {
571 push @tests, {
572 func => 'where',
573 args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
574 stmt => "WHERE a $sop NULL",
575 stmt_q => "WHERE `a` $sop NULL",
576 bind => [],
577 };
578 }
579 }
580}
581
3cdadcbe 582# check single-element inequality ops for no warnings
583for my $op ( qw(!= <>) ) {
584 for my $val (undef, 42) {
585 push @tests, {
586 func => 'where',
587 args => [ { x => { "$_$op" => [ $val ] } } ],
588 stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
589 stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
590 bind => [ $val || () ],
591 } for ('', '-'); # with and without -
592 }
593}
594
595# check single-element not-like ops for no warnings, and NULL exception
596# (the last two "is not X" are a weird syntax, but mebbe a dialect...)
597for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
598 (my $sop = uc $op) =~ s/_/ /gi;
599
600 for my $val (undef, 42) {
601 push @tests, {
602 func => 'where',
603 args => [ { x => { "$_$op" => [ $val ] } } ],
604 $val ? (
605 stmt => "WHERE x $sop ?",
606 stmt_q => "WHERE `x` $sop ?",
607 bind => [ $val ],
608 ) : (
609 stmt => "WHERE x IS NOT NULL",
610 stmt_q => "WHERE `x` IS NOT NULL",
611 bind => [],
612 warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
613 ),
614 } for ('', '-'); # with and without -
615 }
616}
617
618# check all multi-element inequality/not-like ops for warnings
619for my $op ( qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
620 (my $sop = uc $op) =~ s/_/ /gi;
621
622 push @tests, {
623 func => 'where',
624 args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
625 stmt => "WHERE x $sop ? OR x $sop ?",
626 stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
627 bind => [ 42, 69 ],
628 warns => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
629 } for ('', '-'); # with and without -
630}
32eab2da 631
3cdadcbe 632# check all like/not-like ops for empty-arrayref warnings
633for my $op ( qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
634 (my $sop = uc $op) =~ s/_/ /gi;
635
636 push @tests, {
637 func => 'where',
638 args => [ { x => { "$_$op" => [] } } ],
639 stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
640 stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
641 bind => [],
642 warns => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
643 } for ('', '-'); # with and without -
644}
645
3a06278c 646for my $t (@tests) {
3a06278c 647 my $new = $t->{new} || {};
32eab2da 648
3a06278c 649 for my $quoted (0, 1) {
7fb57243 650
3a06278c 651 my $maker = SQL::Abstract->new(%$new, $quoted
652 ? (quote_char => '`', name_sep => '.')
653 : ()
654 );
32eab2da 655
3a06278c 656 my($stmt, @bind);
32eab2da 657
3a06278c 658 my $cref = sub {
659 my $op = $t->{func};
660 ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
7fb57243 661 };
3a06278c 662
97084113 663 if (my $e = $t->{throws}) {
3a06278c 664 throws_ok(
665 sub { $cref->() },
97084113 666 $e,
3cdadcbe 667 ) || diag dumper ({ args => $t->{args}, result => $stmt });
97084113 668 }
669 else {
5eccd8f0 670 warnings_like(
97084113 671 sub { $cref->() },
672 $t->{warns} || [],
5eccd8f0 673 ) || diag dumper ({ args => $t->{args}, result => $stmt });
032dfe20 674
3a06278c 675 is_same_sql_bind(
676 $stmt,
677 \@bind,
678 $quoted ? $t->{stmt_q}: $t->{stmt},
679 $t->{bind}
680 );
fe3ae272 681 }
7fb57243 682 }
32eab2da 683}
10e6c946 684
685done_testing;