expand alias
[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
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]}}],
6e9a377b 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 ? )",
0ec3aec7 459 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
4049d0e3 460 },
26f2dca5 461 {
462 func => 'select',
463 new => {bindtype => 'columns'},
464 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
465 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
466 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
467 bind => [[{dummy => 1} => '02/02/02']],
468 },
26f2dca5 469 {
470 func => 'select',
471 new => {bindtype => 'columns'},
472 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
473 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
474 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
475 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
05a05fd1 476 },
97b9b66e 477 {
478 func => 'select',
479 new => {bindtype => 'columns'},
e30faf88 480 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
481 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
482 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
97b9b66e 483 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 484 },
05a05fd1 485 {
486 func => 'select',
487 new => {bindtype => 'columns'},
488 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
489 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
490 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
491 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
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 = ? OR b = ? ) OR ( a = ? AND b = ? )',
498 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
499 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
500 },
02288357 501 {
502 func => 'insert',
503 args => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
504 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
505 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
506 bind => [qw/1 2 3 4 5/],
507 },
02288357 508 {
509 func => 'insert',
510 args => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
511 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
512 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
513 bind => [qw/1 2 3 4 5/],
514 },
02288357 515 {
516 func => 'insert',
517 args => ['test', [qw/1 2 3 4 5/], { returning => [qw(id foo bar) ] }],
518 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
519 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`, `foo`, `bar`',
520 bind => [qw/1 2 3 4 5/],
521 },
02288357 522 {
523 func => 'insert',
524 args => ['test', [qw/1 2 3 4 5/], { returning => \'id, foo, bar' }],
525 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
526 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
527 bind => [qw/1 2 3 4 5/],
528 },
02288357 529 {
530 func => 'insert',
531 args => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
532 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
533 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
534 bind => [qw/1 2 3 4 5/],
535 },
07936978 536 {
537 func => 'select',
538 new => {bindtype => 'columns'},
953d164e 539 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
540 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
541 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
07936978 542 bind => [[Y => 'x']],
543 },
0dfd2442 544 {
545 func => 'select',
e3f38515 546 args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }],
547 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )',
548 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )',
549 bind => [],
550 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
551 },
552 {
553 func => 'select',
554 args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }],
555 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL )',
556 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL AND `c` IS NOT NULL )',
557 bind => [],
558 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
559 },
560 {
561 func => 'select',
562 args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }],
563 stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )',
564 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )',
565 bind => [],
566 warns => qr/\QSupplying an undefined argument to 'LIKE' is deprecated/,
567 },
568 {
569 func => 'select',
570 args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }],
571 stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT NULL )',
572 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT NULL AND `b` IS NOT NULL )',
573 bind => [],
574 warns => qr/\QSupplying an undefined argument to 'NOT LIKE' is deprecated/,
575 },
46be4313 576 {
577 func => 'select',
578 args => ['`test``table`', ['`test``column`']],
579 stmt => 'SELECT `test``column` FROM `test``table`',
580 stmt_q => 'SELECT ```test````column``` FROM ```test````table```',
581 bind => [],
582 },
583 {
584 func => 'select',
585 args => ['`test\\`table`', ['`test`\\column`']],
586 stmt => 'SELECT `test`\column` FROM `test\`table`',
587 stmt_q => 'SELECT `\`test\`\\\\column\`` FROM `\`test\\\\\`table\``',
588 esc => '\\',
589 bind => [],
590 },
95904db5 591 {
592 func => 'update',
593 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => 'id' }],
594 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id',
595 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`',
596 bind => [42, 32],
597 },
598 {
599 func => 'update',
600 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => \'*' }],
601 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING *',
602 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING *',
603 bind => [42, 32],
604 },
605 {
606 func => 'update',
607 args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => ['id','created_at'] }],
608 stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id, created_at',
609 stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`, `created_at`',
610 bind => [42, 32],
611 },
85327cd5 612 {
613 func => 'delete',
614 args => ['test', {requestor => undef}, {returning => 'id'}],
615 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING id',
616 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING `id`',
617 bind => []
618 },
619 {
620 func => 'delete',
621 args => ['test', {requestor => undef}, {returning => \'*'}],
622 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING *',
623 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING *',
624 bind => []
625 },
626 {
627 func => 'delete',
628 args => ['test', {requestor => undef}, {returning => ['id', 'created_at']}],
629 stmt => 'DELETE FROM test WHERE ( requestor IS NULL ) RETURNING id, created_at',
630 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING `id`, `created_at`',
631 bind => []
632 },
32eab2da 633);
634
b9b5a0b1 635# check is( not) => undef
ca4f826a 636for my $op (qw(not is is_not), 'is not') {
b9b5a0b1 637 (my $sop = uc $op) =~ s/_/ /gi;
638
40f2f231 639 $sop = 'IS NOT' if $sop eq 'NOT';
640
641 for my $uc (0, 1) {
642 for my $prefix ('', '-') {
643 push @tests, {
644 func => 'where',
645 args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
646 stmt => "WHERE a $sop NULL",
647 stmt_q => "WHERE `a` $sop NULL",
648 bind => [],
649 };
650 }
651 }
b9b5a0b1 652}
653
3cdadcbe 654# check single-element inequality ops for no warnings
ca4f826a 655for my $op (qw(!= <>)) {
3cdadcbe 656 for my $val (undef, 42) {
657 push @tests, {
658 func => 'where',
659 args => [ { x => { "$_$op" => [ $val ] } } ],
660 stmt => "WHERE x " . ($val ? "$op ?" : 'IS NOT NULL'),
661 stmt_q => "WHERE `x` " . ($val ? "$op ?" : 'IS NOT NULL'),
662 bind => [ $val || () ],
663 } for ('', '-'); # with and without -
664 }
665}
666
667# check single-element not-like ops for no warnings, and NULL exception
668# (the last two "is not X" are a weird syntax, but mebbe a dialect...)
669for my $op (qw(not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
670 (my $sop = uc $op) =~ s/_/ /gi;
671
672 for my $val (undef, 42) {
673 push @tests, {
674 func => 'where',
675 args => [ { x => { "$_$op" => [ $val ] } } ],
676 $val ? (
677 stmt => "WHERE x $sop ?",
678 stmt_q => "WHERE `x` $sop ?",
679 bind => [ $val ],
680 ) : (
681 stmt => "WHERE x IS NOT NULL",
682 stmt_q => "WHERE `x` IS NOT NULL",
683 bind => [],
684 warns => qr/\QSupplying an undefined argument to '$sop' is deprecated/,
685 ),
686 } for ('', '-'); # with and without -
687 }
688}
689
690# check all multi-element inequality/not-like ops for warnings
ca4f826a 691for my $op (qw(!= <> not_like not_rlike), 'not like', 'not rlike', 'is not like','is not rlike') {
3cdadcbe 692 (my $sop = uc $op) =~ s/_/ /gi;
693
694 push @tests, {
695 func => 'where',
696 args => [ { x => { "$_$op" => [ 42, 69 ] } } ],
697 stmt => "WHERE x $sop ? OR x $sop ?",
698 stmt_q => "WHERE `x` $sop ? OR `x` $sop ?",
699 bind => [ 42, 69 ],
700 warns => qr/\QA multi-element arrayref as an argument to the inequality op '$sop' is technically equivalent to an always-true 1=1/,
701 } for ('', '-'); # with and without -
702}
703
704# check all like/not-like ops for empty-arrayref warnings
ca4f826a 705for my $op (qw(like rlike not_like not_rlike), 'not like', 'not rlike', 'is like', 'is not like', 'is rlike', 'is not rlike') {
3cdadcbe 706 (my $sop = uc $op) =~ s/_/ /gi;
707
708 push @tests, {
709 func => 'where',
710 args => [ { x => { "$_$op" => [] } } ],
711 stmt => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
712 stmt_q => ( $sop =~ /NOT/ ? "WHERE 1=1" : "WHERE 0=1" ),
713 bind => [],
714 warns => qr/\QSupplying an empty arrayref to '$sop' is deprecated/,
715 } for ('', '-'); # with and without -
716}
717
b5a576d2 718# check emtpty-lhs in a hashpair and arraypair
719for my $lhs (undef, '') {
720 no warnings 'uninitialized';
721
722##
723## hard exceptions - never worked
724 for my $where_arg (
725 ( map { $_, { @$_ } }
726 [ $lhs => "foo" ],
727 [ $lhs => { "=" => "bozz" } ],
728 [ $lhs => { "=" => \"bozz" } ],
729 [ $lhs => { -max => \"bizz" } ],
730 ),
731 [ -and => { $lhs => "baz" }, bizz => "buzz" ],
732 [ foo => "bar", { $lhs => "baz" }, bizz => "buzz" ],
733 { foo => "bar", -or => { $lhs => "baz" } },
734
735 # the hashref forms of these work sadly - check for warnings below
736 { foo => "bar", -and => [ $lhs => \"baz" ], bizz => "buzz" },
737 { foo => "bar", -or => [ $lhs => \"baz" ], bizz => "buzz" },
738 [ foo => "bar", [ $lhs => \"baz" ], bizz => "buzz" ],
739 [ foo => "bar", $lhs => \"baz", bizz => "buzz" ],
740 [ foo => "bar", $lhs => \["baz"], bizz => "buzz" ],
741 [ $lhs => \"baz" ],
742 [ $lhs => \["baz"] ],
b5a576d2 743 ) {
744 push @tests, {
745 func => 'where',
746 args => [ $where_arg ],
747 throws => qr/\QSupplying an empty left hand side argument is not supported/,
748 };
749 }
750
751##
752## deprecations - sorta worked, likely abused by folks
753 for my $where_arg (
754 # the arrayref forms of this never worked and throw above
dd2d5bf7 755 { foo => "bar", -or => { $lhs => \"baz" }, bizz => "buzz" },
b5a576d2 756 { foo => "bar", -and => { $lhs => \"baz" }, bizz => "buzz" },
757 { foo => "bar", $lhs => \"baz", bizz => "buzz" },
758 { foo => "bar", $lhs => \["baz"], bizz => "buzz" },
759 ) {
760 push @tests, {
761 func => 'where',
762 args => [ $where_arg ],
763 stmt => 'WHERE baz AND bizz = ? AND foo = ?',
764 stmt_q => 'WHERE baz AND `bizz` = ? AND `foo` = ?',
765 bind => [qw( buzz bar )],
766 warns => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
767 };
768 }
769
770 for my $where_arg (
771 { $lhs => \"baz" },
772 { $lhs => \["baz"] },
773 ) {
774 push @tests, {
775 func => 'where',
776 args => [ $where_arg ],
777 stmt => 'WHERE baz',
778 stmt_q => 'WHERE baz',
779 bind => [],
780 warns => qr/\QHash-pairs consisting of an empty string with a literal are deprecated/,
781 }
782 }
783}
784
785# check false lhs, silly but possible
786{
787 for my $where_arg (
788 [ { 0 => "baz" }, bizz => "buzz", foo => "bar" ],
789 [ -or => { foo => "bar", -or => { 0 => "baz" }, bizz => "buzz" } ],
790 ) {
791 push @tests, {
792 func => 'where',
793 args => [ $where_arg ],
794 stmt => 'WHERE 0 = ? OR bizz = ? OR foo = ?',
795 stmt_q => 'WHERE `0` = ? OR `bizz` = ? OR `foo` = ?',
796 bind => [qw( baz buzz bar )],
797 };
798 }
799
800 for my $where_arg (
801 { foo => "bar", -and => [ 0 => \"= baz" ], bizz => "buzz" },
802 { foo => "bar", -or => [ 0 => \"= baz" ], bizz => "buzz" },
803
804 { foo => "bar", -and => { 0 => \"= baz" }, bizz => "buzz" },
805 { foo => "bar", -or => { 0 => \"= baz" }, bizz => "buzz" },
806
807 { foo => "bar", 0 => \"= baz", bizz => "buzz" },
808 { foo => "bar", 0 => \["= baz"], bizz => "buzz" },
809 ) {
810 push @tests, {
811 func => 'where',
812 args => [ $where_arg ],
813 stmt => 'WHERE 0 = baz AND bizz = ? AND foo = ?',
814 stmt_q => 'WHERE `0` = baz AND `bizz` = ? AND `foo` = ?',
815 bind => [qw( buzz bar )],
816 };
817 }
818
819 for my $where_arg (
820 [ -and => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
821 [ -or => [ 0 => \"= baz" ], bizz => "buzz", foo => "bar" ],
822 [ 0 => \"= baz", bizz => "buzz", foo => "bar" ],
823 [ 0 => \["= baz"], bizz => "buzz", foo => "bar" ],
824 ) {
825 push @tests, {
826 func => 'where',
827 args => [ $where_arg ],
828 stmt => 'WHERE 0 = baz OR bizz = ? OR foo = ?',
829 stmt_q => 'WHERE `0` = baz OR `bizz` = ? OR `foo` = ?',
830 bind => [qw( buzz bar )],
831 };
832 }
833}
834
3a06278c 835for my $t (@tests) {
3a06278c 836 my $new = $t->{new} || {};
32eab2da 837
3a06278c 838 for my $quoted (0, 1) {
7fb57243 839
46be4313 840 my $maker = SQL::Abstract->new(
841 %$new,
842 ($quoted ? (
843 quote_char => '`',
844 name_sep => '.',
845 ( $t->{esc} ? (
846 escape_char => $t->{esc},
847 ) : ())
848 ) : ())
3a06278c 849 );
32eab2da 850
3a06278c 851 my($stmt, @bind);
32eab2da 852
3a06278c 853 my $cref = sub {
854 my $op = $t->{func};
ca4f826a 855 ($stmt, @bind) = $maker->$op(@{ $t->{args} });
7fb57243 856 };
3a06278c 857
97084113 858 if (my $e = $t->{throws}) {
3a06278c 859 throws_ok(
860 sub { $cref->() },
97084113 861 $e,
ca4f826a 862 ) || diag dumper({ args => $t->{args}, result => $stmt });
97084113 863 }
864 else {
3bea90ab 865 lives_ok(sub {
aa03ef04 866 alarm(1); local $SIG{ALRM} = sub {
867 no warnings 'redefine';
868 my $orig = Carp->can('caller_info');
869 local *Carp::caller_info = sub { return if $_[0] > 20; &$orig };
870 print STDERR "ARGH ($SQL::Abstract::Default_Scalar_To): ".Carp::longmess();
871 die "timed out";
872 };
3bea90ab 873 warnings_like(
874 sub { $cref->() },
875 $t->{warns} || [],
876 ) || diag dumper({ args => $t->{args}, result => $stmt });
877 }) || diag dumper({ args => $t->{args}, result => $stmt, threw => $@ });
032dfe20 878
3a06278c 879 is_same_sql_bind(
880 $stmt,
881 \@bind,
882 $quoted ? $t->{stmt_q}: $t->{stmt},
883 $t->{bind}
89690da2 884 ) || diag dumper({ args => $t->{args}, result => $stmt });;
fe3ae272 885 }
7fb57243 886 }
32eab2da 887}
10e6c946 888
889done_testing;