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