remove obsolete thing that never worked
[scpubgit/Q-Branch.git] / t / 01generate.t
CommitLineData
32eab2da 1use strict;
41751122 2use warnings;
3use Test::More;
7fb57243 4use Test::Warn;
fe3ae272 5use Test::Exception;
32eab2da 6
ca4f826a 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
32eab2da 19my @tests = (
32eab2da 20 {
21 func => 'select',
22 args => ['test', '*'],
23 stmt => 'SELECT * FROM test',
24 stmt_q => 'SELECT * FROM `test`',
25 bind => []
26 },
32eab2da 27 {
28 func => 'select',
29 args => ['test', [qw(one two three)]],
30 stmt => 'SELECT one, two, three FROM test',
31 stmt_q => 'SELECT `one`, `two`, `three` FROM `test`',
32 bind => []
33 },
32eab2da 34 {
35 func => 'select',
36 args => ['test', '*', { a => 0 }, [qw/boom bada bing/]],
37 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY boom, bada, bing',
38 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `boom`, `bada`, `bing`',
39 bind => [0]
4049d0e3 40 },
41 {
32eab2da 42 func => 'select',
43 args => ['test', '*', [ { a => 5 }, { b => 6 } ]],
44 stmt => 'SELECT * FROM test WHERE ( ( a = ? ) OR ( b = ? ) )',
45 stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` = ? ) OR ( `b` = ? ) )',
46 bind => [5,6]
4049d0e3 47 },
48 {
32eab2da 49 func => 'select',
50 args => ['test', '*', undef, ['id']],
51 stmt => 'SELECT * FROM test ORDER BY id',
52 stmt_q => 'SELECT * FROM `test` ORDER BY `id`',
53 bind => []
4049d0e3 54 },
55 {
32eab2da 56 func => 'select',
57 args => ['test', '*', { a => 'boom' } , ['id']],
58 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY id',
59 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `id`',
60 bind => ['boom']
4049d0e3 61 },
62 {
32eab2da 63 func => 'select',
64 args => ['test', '*', { a => ['boom', 'bang'] }],
65 stmt => 'SELECT * FROM test WHERE ( ( ( a = ? ) OR ( a = ? ) ) )',
66 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `a` = ? ) OR ( `a` = ? ) ) )',
67 bind => ['boom', 'bang']
4049d0e3 68 },
69 {
32eab2da 70 func => 'select',
32eab2da 71 args => ['test', '*', { a => { '!=', 'boom' } }],
72 stmt => 'SELECT * FROM test WHERE ( a != ? )',
73 stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
74 bind => ['boom']
4049d0e3 75 },
76 {
de63ce57 77 # this is maybe wrong but a single arg doesn't get quoted
78 func => 'select',
79 args => ['test', 'id', { a => { '!=', 'boom' } }],
80 stmt => 'SELECT id FROM test WHERE ( a != ? )',
81 stmt_q => 'SELECT id FROM `test` WHERE ( `a` != ? )',
82 bind => ['boom']
83 },
84 {
32eab2da 85 func => 'update',
86 args => ['test', {a => 'boom'}, {a => undef}],
87 stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )',
88 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )',
89 bind => ['boom']
4049d0e3 90 },
91 {
32eab2da 92 func => 'update',
c4ed66f4 93 args => ['test', {a => undef }, {a => 'boom'}],
94 stmt => 'UPDATE test SET a = ? WHERE ( a = ? )',
95 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` = ? )',
96 bind => [undef,'boom']
97 },
98 {
99 func => 'update',
32eab2da 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 {
e4327a54 130 func => 'select',
131 args => [[\'test1', 'test2'], '*', { 'test1.a' => 'boom' } ],
132 stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a = ? )',
133 stmt_q => 'SELECT * FROM test1, `test2` WHERE ( `test1`.`a` = ? )',
134 bind => ['boom']
135 },
136 {
32eab2da 137 func => 'insert',
138 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
139 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
140 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
141 bind => [qw/1 2 3 4 5/],
4049d0e3 142 },
143 {
32eab2da 144 func => 'insert',
19b6ccce 145 args => ['test', [1..30]],
146 stmt => 'INSERT INTO test VALUES ('.join(', ', ('?')x30).')',
147 stmt_q => 'INSERT INTO `test` VALUES ('.join(', ', ('?')x30).')',
148 bind => [1..30],
4049d0e3 149 },
150 {
32eab2da 151 func => 'insert',
152 args => ['test', [qw/1 2 3 4 5/, undef]],
153 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)',
154 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)',
155 bind => [qw/1 2 3 4 5/, undef],
4049d0e3 156 },
157 {
32eab2da 158 func => 'update',
159 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
160 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?',
161 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?',
162 bind => [qw/1 2 3 4 5/],
4049d0e3 163 },
164 {
32eab2da 165 func => 'update',
166 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}],
167 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )',
168 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )',
169 bind => [qw/1 2 3 4 5 1 2 3 4 5/],
4049d0e3 170 },
171 {
32eab2da 172 func => 'update',
96449e8e 173 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
32eab2da 174 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
175 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
176 bind => [qw(1 02/02/02 1 2)],
4049d0e3 177 },
178 {
32eab2da 179 func => 'insert',
180 args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ],
181 stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)',
182 stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)',
183 bind => ['4'],
4049d0e3 184 },
185 {
257d9a42 186 func => 'insert',
187 args => ['test.table', [ \'max(all_limits)', 4 ] ],
188 stmt => 'INSERT INTO test.table VALUES (max(all_limits), ?)',
189 stmt_q => 'INSERT INTO `test`.`table` VALUES (max(all_limits), ?)',
190 bind => ['4'],
4049d0e3 191 },
192 {
32eab2da 193 func => 'insert',
194 new => {bindtype => 'columns'},
195 args => ['test.table', {one => 2, three => 4, five => 6} ],
196 stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
197 stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
198 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
4049d0e3 199 },
200 {
32eab2da 201 func => 'select',
202 new => {bindtype => 'columns', case => 'lower'},
203 args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
204 stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
205 stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
206 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
4049d0e3 207 },
208 {
32eab2da 209 func => 'update',
210 new => {bindtype => 'columns', cmp => 'like'},
211 args => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
212 {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
213 stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
214 . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
215 stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
216 . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
217 bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
218 ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
4049d0e3 219 },
220 {
32eab2da 221 func => 'select',
07936978 222 args => ['test', '*', {priority => [ -and => {'!=', 2}, { -not_like => '3%'} ]}],
223 stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority NOT LIKE ? ) ) )',
224 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` NOT LIKE ? ) ) )',
225 bind => [qw(2 3%)],
4049d0e3 226 },
227 {
32eab2da 228 func => 'select',
4049d0e3 229 args => ['Yo Momma', '*', { user => 'nwiger',
32eab2da 230 -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
231 stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )',
232 stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )',
233 bind => [qw(20 ASIA nwiger)],
4049d0e3 234 },
235 {
32eab2da 236 func => 'update',
237 args => ['taco_punches', { one => 2, three => 4 },
238 { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
239 tasty => { '!=', [qw(yes YES)] },
240 -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
241 ],
3cdadcbe 242 warns => qr/\QA multi-element arrayref as an argument to the inequality op '!=' is technically equivalent to an always-true 1=1/,
243
32eab2da 244 stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )'
e30faf88 245 . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )',
32eab2da 246 stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )'
e30faf88 247 . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )',
32eab2da 248 bind => [qw(2 4 mr.happy yes YES yes YES)],
4049d0e3 249 },
250 {
32eab2da 251 func => 'select',
2d2df6ba 252 args => ['jeff', '*', { name => {'ilike', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']},
32eab2da 253 -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ],
96449e8e 254 yob => {'<', 1976} ] ] } ],
e30faf88 255 stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )'
2d2df6ba 256 . ' AND name NOT IN ( ?, ?, ?, ? ) AND name ILIKE ? )',
e30faf88 257 stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )'
2d2df6ba 258 . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` ILIKE ? )',
32eab2da 259 bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)]
4049d0e3 260 },
261 {
32eab2da 262 func => 'update',
24c898da 263 args => ['fhole', {fpoles => 4}, [
264 { race => [qw/-or black white asian /] },
265 { -nest => { firsttime => [-or => {'=','yes'}, undef] } },
266 { -and => [ { firstname => {-not_like => 'candace'} }, { lastname => {-in => [qw(jugs canyon towers)] } } ] },
267 ] ],
32eab2da 268 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
b43d4cef 269 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
32eab2da 270 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
b43d4cef 271 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
32eab2da 272 bind => [qw(4 black white asian yes candace jugs canyon towers)]
273 },
96449e8e 274 {
275 func => 'insert',
276 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
277 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
278 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
279 bind => [qw(1 02/02/02)],
280 },
96449e8e 281 {
282 func => 'select',
96449e8e 283 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
284 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
285 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
286 bind => ['02/02/02'],
d82b8afb 287 },
d82b8afb 288 {
289 func => 'insert',
290 new => {array_datatypes => 1},
291 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
292 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
293 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
294 bind => [1, [1, 1, 2, 3, 5, 8]],
295 },
d82b8afb 296 {
297 func => 'insert',
298 new => {bindtype => 'columns', array_datatypes => 1},
299 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
300 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
301 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
302 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
303 },
d82b8afb 304 {
305 func => 'update',
306 new => {array_datatypes => 1},
307 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
308 stmt => 'UPDATE test SET a = ?, b = ?',
309 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
310 bind => [1, [1, 1, 2, 3, 5, 8]],
311 },
d82b8afb 312 {
313 func => 'update',
314 new => {bindtype => 'columns', array_datatypes => 1},
315 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
316 stmt => 'UPDATE test SET a = ?, b = ?',
317 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
318 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
319 },
145fbfc8 320 {
321 func => 'select',
322 args => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
323 stmt => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
324 stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
325 bind => [8],
4049d0e3 326 },
b3be7bd0 327 {
328 func => 'select',
329 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
330 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
331 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
332 bind => ['02/02/02', 8],
4049d0e3 333 },
5db47f9f 334 { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
335 func => 'insert',
336 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => { answer => 42 }}],
337 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
338 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
339 bind => [qw/1 2 3 4/, { answer => 42}],
97084113 340 warns => qr/HASH ref as bind value in insert is not supported/i,
4049d0e3 341 },
342 {
ef4d99a5 343 func => 'update',
344 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
345 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
346 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
347 bind => [qw(1 1 2)],
4049d0e3 348 },
ef4d99a5 349 {
350 func => 'insert',
351 args => ['test', {a => 1, b => \["42"]}],
352 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
353 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
354 bind => [qw(1)],
355 },
ef4d99a5 356 {
357 func => 'select',
358 args => ['test', '*', { a => \["= 42"], b => 1}],
359 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
360 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
361 bind => [qw(1)],
362 },
ef4d99a5 363 {
364 func => 'select',
365 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
366 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
367 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
368 bind => [qw(8)],
4049d0e3 369 },
cd87fd4c 370 {
371 func => 'insert',
372 new => {bindtype => 'columns'},
fe3ae272 373 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 374 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
375 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
fe3ae272 376 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 377 },
4049d0e3 378 {
cd87fd4c 379 func => 'update',
380 new => {bindtype => 'columns'},
fe3ae272 381 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 382 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
383 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
fe3ae272 384 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
4049d0e3 385 },
cd87fd4c 386 {
387 func => 'select',
388 new => {bindtype => 'columns'},
fe3ae272 389 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 390 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
391 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
fe3ae272 392 bind => [[dummy => '02/02/02']],
cd87fd4c 393 },
cd87fd4c 394 {
395 func => 'select',
396 new => {bindtype => 'columns'},
fe3ae272 397 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 398 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
399 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
fe3ae272 400 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 401 },
fe3ae272 402 {
403 func => 'insert',
404 new => {bindtype => 'columns'},
405 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 406 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 407 },
4049d0e3 408 {
fe3ae272 409 func => 'update',
410 new => {bindtype => 'columns'},
411 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
97084113 412 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 413 },
fe3ae272 414 {
415 func => 'select',
416 new => {bindtype => 'columns'},
417 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
97084113 418 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
fe3ae272 419 },
fe3ae272 420 {
421 func => 'select',
422 new => {bindtype => 'columns'},
423 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
97084113 424 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 425 },
044e8ff4 426 {
427 func => 'select',
a5e0ac94 428 args => ['test', '*', { foo => { '>=' => [] }} ],
429 throws => qr/\Qoperator '>=' applied on an empty array (field 'foo')/,
430 },
431 {
432 func => 'select',
044e8ff4 433 new => {bindtype => 'columns'},
434 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
435 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
436 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
437 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 438 },
044e8ff4 439 {
440 func => 'select',
441 new => {bindtype => 'columns'},
442 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
97084113 443 throws => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 444 },
26f2dca5 445 {
446 func => 'insert',
447 new => {bindtype => 'columns'},
448 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
449 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
450 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
451 bind => [[a => '1'], [{dummy => 1} => '02/02/02']],
452 },
4049d0e3 453 {
26f2dca5 454 func => 'update',
455 new => {bindtype => 'columns'},
0ec3aec7 456 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
03e6883c 457 stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER(?) WHERE ( a BETWEEN ? AND ? )",
458 stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER(?) WHERE ( `a` BETWEEN ? AND ? )",
459 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
460 },
461 {
462 func => 'update',
463 new => {bindtype => 'columns',restore_old_unop_handling => 1},
464 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
6e9a377b 465 stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER ? WHERE ( a BETWEEN ? AND ? )",
466 stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ? WHERE ( `a` BETWEEN ? AND ? )",
0ec3aec7 467 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
4049d0e3 468 },
26f2dca5 469 {
470 func => 'select',
471 new => {bindtype => 'columns'},
472 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
473 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
474 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
475 bind => [[{dummy => 1} => '02/02/02']],
476 },
26f2dca5 477 {
478 func => 'select',
479 new => {bindtype => 'columns'},
480 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
481 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
482 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
483 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
05a05fd1 484 },
97b9b66e 485 {
486 func => 'select',
487 new => {bindtype => 'columns'},
e30faf88 488 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
489 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
490 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
97b9b66e 491 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 492 },
05a05fd1 493 {
494 func => 'select',
495 new => {bindtype => 'columns'},
496 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
497 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
498 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
499 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
500 },
05a05fd1 501 {
502 func => 'select',
503 new => {bindtype => 'columns'},
504 args => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
505 stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
506 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
507 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
508 },
02288357 509 {
510 func => 'insert',
511 args => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
512 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
513 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
514 bind => [qw/1 2 3 4 5/],
515 },
02288357 516 {
517 func => 'insert',
518 args => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
519 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
520 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
521 bind => [qw/1 2 3 4 5/],
522 },
02288357 523 {
524 func => 'insert',
525 args => ['test', [qw/1 2 3 4 5/], { returning => [qw(id foo bar) ] }],
526 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
527 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`, `foo`, `bar`',
528 bind => [qw/1 2 3 4 5/],
529 },
02288357 530 {
531 func => 'insert',
532 args => ['test', [qw/1 2 3 4 5/], { returning => \'id, foo, bar' }],
533 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
534 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
535 bind => [qw/1 2 3 4 5/],
536 },
02288357 537 {
538 func => 'insert',
539 args => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
540 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
541 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
542 bind => [qw/1 2 3 4 5/],
543 },
07936978 544 {
545 func => 'select',
546 new => {bindtype => 'columns'},
953d164e 547 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
03e6883c 548 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN(?) ) ) ) )',
549 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN(?) ) ) ) )',
550 bind => [[Y => 'x']],
551 },
552 {
553 func => 'select',
554 new => {bindtype => 'columns',restore_old_unop_handling => 1},
555 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
953d164e 556 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
557 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
07936978 558 bind => [[Y => 'x']],
559 },
0dfd2442 560 {
561 func => 'select',
e3f38515 562 args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }],
563 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )',
564 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )',
565 bind => [],
566 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
567 },
568 {
569 func => 'select',
570 args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }],
571 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL )',
572 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL AND `c` IS NOT NULL )',
573 bind => [],
574 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
575 },
576 {
577 func => 'select',
578 args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }],
579 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )',
580 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )',
581 bind => [],
582 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
583 },
584 {
585 func => 'select',
586 args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }],
587 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL )',
588 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL )',
589 bind => [],
590 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
591 },
46be4313 592 {
593 func => 'select',
594 args => ['`test``table`', ['`test``column`']],
595 stmt => 'SELECT `test``column` FROM `test``table`',
596 stmt_q => 'SELECT ```test````column``` FROM ```test````table```',
597 bind => [],
598 },
599 {
600 func => 'select',
601 args => ['`test\\`table`', ['`test`\\column`']],
602 stmt => 'SELECT `test`\column` FROM `test\`table`',
603 stmt_q => 'SELECT `\`test\`\\\\column\`` FROM `\`test\\\\\`table\``',
604 esc => '\\',
605 bind => [],
606 },
95904db5 607 {
608 func => 'update',
609 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => 'id' }],
610 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id',
611 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`',
612 bind => [42, 32],
613 },
614 {
615 func => 'update',
616 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => \'*' }],
617 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING *',
618 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING *',
619 bind => [42, 32],
620 },
621 {
622 func => 'update',
623 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => ['id','created_at'] }],
624 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id, created_at',
625 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`, `created_at`',
626 bind => [42, 32],
627 },
85327cd5 628 {
629 func => 'delete',
630 args => ['test', {requestor => undef}, {returning => 'id'}],
631 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING id',
632 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING `id`',
633 bind => []
634 },
635 {
636 func => 'delete',
637 args => ['test', {requestor => undef}, {returning => \'*'}],
638 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING *',
639 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING *',
640 bind => []
641 },
642 {
643 func => 'delete',
644 args => ['test', {requestor => undef}, {returning => ['id', 'created_at']}],
645 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING id, created_at',
646 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING `id`, `created_at`',
647 bind => []
648 },
f958ea62 649 {
650 func => 'delete',
651 args => ['test', \[ undef ] ],
652 stmt => 'DELETE FROM test',
653 stmt_q => 'DELETE FROM `test`',
654 bind => []
655 },
32eab2da 656);
657
b9b5a0b1 658# check is( not) => undef
ca4f826a 659for my $op (qw(not is is_not), 'is not') {
b9b5a0b1 660 (my $sop = uc $op) =~ s/_/ /gi;
661
40f2f231 662 $sop = 'IS NOT' if $sop eq 'NOT';
663
664 for my $uc (0, 1) {
665 for my $prefix ('', '-') {
666 push @tests, {
667 func => 'where',
668 args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
669 stmt => "WHERE a $sop NULL",
670 stmt_q => "WHERE `a` $sop NULL",
671 bind => [],
672 };
673 }
674 }
b9b5a0b1 675}
676
3cdadcbe 677# check single-element inequality ops for no warnings
ca4f826a 678for my $op (qw(!= <>)) {
3cdadcbe 679 for my $val (undef, 42) {
680 push @tests, {
681 func => 'where',
682 args => [ { x => { "$_$op" => [ $val ] } } ],
683 stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
684 stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
685 bind => [ $val || () ],
686 } for ('', '-'); # with and without -
687 }
688}
689
690# check single-element not-like ops for no warnings, and NULL exception
691# (the last two "is not X" are a weird syntax, but mebbe a dialect...)
692for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
693 (my $sop = uc $op) =~ s/_/ /gi;
694
695 for my $val (undef, 42) {
696 push @tests, {
697 func => 'where',
698 args => [ { x => { "$_$op" => [ $val ] } } ],
699 $val ? (
700 stmt => "WHERE x $sop ?",
701 stmt_q => "WHERE `x` $sop ?",
702 bind => [ $val ],
703 ) : (
704 stmt => "WHERE x IS NOT NULL",
705 stmt_q => "WHERE `x` IS NOT NULL",
706 bind => [],
707 warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
708 ),
709 } for ('', '-'); # with and without -
710 }
711}
712
713# check all multi-element inequality/not-like ops for warnings
ca4f826a 714for my $op (qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
3cdadcbe 715 (my $sop = uc $op) =~ s/_/ /gi;
716
717 push @tests, {
718 func => 'where',
719 args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
720 stmt => "WHERE x $sop ? OR x $sop ?",
721 stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
722 bind => [ 42, 69 ],
723 warns => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
724 } for ('', '-'); # with and without -
725}
726
727# check all like/not-like ops for empty-arrayref warnings
ca4f826a 728for my $op (qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
3cdadcbe 729 (my $sop = uc $op) =~ s/_/ /gi;
730
731 push @tests, {
732 func => 'where',
733 args => [ { x => { "$_$op" => [] } } ],
734 stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
735 stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
736 bind => [],
737 warns => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
738 } for ('', '-'); # with and without -
739}
740
b5a576d2 741# check emtpty-lhs in a hashpair and arraypair
742for my $lhs (undef, '') {
743 no warnings 'uninitialized';
744
745##
746## hard exceptions - never worked
747 for my $where_arg (
748 ( map { $_, { @$_ } }
749 [ $lhs => "foo" ],
750 [ $lhs => { "=" => "bozz" } ],
751 [ $lhs => { "=" => \"bozz" } ],
752 [ $lhs => { -max => \"bizz" } ],
753 ),
754 [ -and => { $lhs => "baz" }, bizz => "buzz" ],
755 [ foo => "bar", { $lhs => "baz" }, bizz => "buzz" ],
756 { foo => "bar", -or => { $lhs => "baz" } },
757
758 # the hashref forms of these work sadly - check for warnings below
759 { foo => "bar", -and => [ $lhs => \"baz" ], bizz => "buzz" },
760 { foo => "bar", -or => [ $lhs => \"baz" ], bizz => "buzz" },
761 [ foo => "bar", [ $lhs => \"baz" ], bizz => "buzz" ],
762 [ foo => "bar", $lhs => \"baz", bizz => "buzz" ],
763 [ foo => "bar", $lhs => \["baz"], bizz => "buzz" ],
764 [ $lhs => \"baz" ],
765 [ $lhs => \["baz"] ],
b5a576d2 766 ) {
767 push @tests, {
768 func => 'where',
769 args => [ $where_arg ],
770 throws => qr/\QSupplying an empty left hand side argument is not supported/,
771 };
772 }
773
774##
775## deprecations - sorta worked, likely abused by folks
776 for my $where_arg (
777 # the arrayref forms of this never worked and throw above
dd2d5bf7 778 { foo => "bar", -or => { $lhs => \"baz" }, bizz => "buzz" },
b5a576d2 779 { foo => "bar", -and => { $lhs => \"baz" }, bizz => "buzz" },
780 { foo => "bar", $lhs => \"baz", bizz => "buzz" },
781 { foo => "bar", $lhs => \["baz"], bizz => "buzz" },
782 ) {
783 push @tests, {
784 func => 'where',
785 args => [ $where_arg ],
786 stmt => 'WHERE baz AND bizz = ? AND foo = ?',
787 stmt_q => 'WHERE baz AND `bizz` = ? AND `foo` = ?',
788 bind => [qw( buzz bar )],
789 warns => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
790 };
791 }
792
793 for my $where_arg (
794 { $lhs => \"baz" },
795 { $lhs => \["baz"] },
796 ) {
797 push @tests, {
798 func => 'where',
799 args => [ $where_arg ],
800 stmt => 'WHERE baz',
801 stmt_q => 'WHERE baz',
802 bind => [],
803 warns => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
804 }
805 }
806}
807
808# check false lhs, silly but possible
809{
810 for my $where_arg (
811 [ { 0 => "baz" }, bizz => "buzz", foo => "bar" ],
812 [ -or => { foo => "bar", -or => { 0 => "baz" }, bizz => "buzz" } ],
813 ) {
814 push @tests, {
815 func => 'where',
816 args => [ $where_arg ],
817 stmt => 'WHERE 0 = ? OR bizz = ? OR foo = ?',
818 stmt_q => 'WHERE `0` = ? OR `bizz` = ? OR `foo` = ?',
819 bind => [qw( baz buzz bar )],
820 };
821 }
822
823 for my $where_arg (
824 { foo => "bar", -and => [ 0 => \"= baz" ], bizz => "buzz" },
825 { foo => "bar", -or => [ 0 => \"= baz" ], bizz => "buzz" },
826
827 { foo => "bar", -and => { 0 => \"= baz" }, bizz => "buzz" },
828 { foo => "bar", -or => { 0 => \"= baz" }, bizz => "buzz" },
829
830 { foo => "bar", 0 => \"= baz", bizz => "buzz" },
831 { foo => "bar", 0 => \["= baz"], bizz => "buzz" },
832 ) {
833 push @tests, {
834 func => 'where',
835 args => [ $where_arg ],
836 stmt => 'WHERE 0 = baz AND bizz = ? AND foo = ?',
837 stmt_q => 'WHERE `0` = baz AND `bizz` = ? AND `foo` = ?',
838 bind => [qw( buzz bar )],
839 };
840 }
841
842 for my $where_arg (
843 [ -and => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
844 [ -or => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
845 [ 0 => \"= baz", bizz => "buzz", foo => "bar" ],
846 [ 0 => \["= baz"], bizz => "buzz", foo => "bar" ],
847 ) {
848 push @tests, {
849 func => 'where',
850 args => [ $where_arg ],
851 stmt => 'WHERE 0 = baz OR bizz = ? OR foo = ?',
852 stmt_q => 'WHERE `0` = baz OR `bizz` = ? OR `foo` = ?',
853 bind => [qw( buzz bar )],
854 };
855 }
856}
857
3a06278c 858for my $t (@tests) {
3a06278c 859 my $new = $t->{new} || {};
32eab2da 860
3a06278c 861 for my $quoted (0, 1) {
7fb57243 862
46be4313 863 my $maker = SQL::Abstract->new(
864 %$new,
865 ($quoted ? (
866 quote_char => '`',
867 name_sep => '.',
868 ( $t->{esc} ? (
869 escape_char => $t->{esc},
870 ) : ())
871 ) : ())
3a06278c 872 );
32eab2da 873
3a06278c 874 my($stmt, @bind);
32eab2da 875
3a06278c 876 my $cref = sub {
877 my $op = $t->{func};
ca4f826a 878 ($stmt, @bind) = $maker->$op(@{ $t->{args} });
7fb57243 879 };
3a06278c 880
97084113 881 if (my $e = $t->{throws}) {
3a06278c 882 throws_ok(
883 sub { $cref->() },
97084113 884 $e,
ca4f826a 885 ) || diag dumper({ args => $t->{args}, result => $stmt });
97084113 886 }
887 else {
3bea90ab 888 lives_ok(sub {
aa03ef04 889 alarm(1); local $SIG{ALRM} = sub {
890 no warnings 'redefine';
891 my $orig = Carp->can('caller_info');
892 local *Carp::caller_info = sub { return if $_[0] > 20; &$orig };
893 print STDERR "ARGH ($SQL::Abstract::Default_Scalar_To): ".Carp::longmess();
894 die "timed out";
895 };
3bea90ab 896 warnings_like(
897 sub { $cref->() },
898 $t->{warns} || [],
899 ) || diag dumper({ args => $t->{args}, result => $stmt });
900 }) || diag dumper({ args => $t->{args}, result => $stmt, threw => $@ });
032dfe20 901
3a06278c 902 is_same_sql_bind(
903 $stmt,
904 \@bind,
905 $quoted ? $t->{stmt_q}: $t->{stmt},
906 $t->{bind}
89690da2 907 ) || diag dumper({ args => $t->{args}, result => $stmt });;
fe3ae272 908 }
7fb57243 909 }
32eab2da 910}
10e6c946 911
912done_testing;