Handle { foo => { -in => [undef, …] } }
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
CommitLineData
41751122 1#!/usr/bin/perl
32eab2da 2
3use strict;
41751122 4use warnings;
5use Test::More;
7fb57243 6use Test::Warn;
fe3ae272 7use Test::Exception;
31a41fd9 8use Data::Dumper;
32eab2da 9
5aad8cf3 10use SQL::Abstract::Test import => ['is_same_sql_bind'];
32eab2da 11
12use SQL::Abstract;
13
48d9f5f8 14#### WARNING ####
15#
16# -nest has been undocumented on purpose, but is still supported for the
17# foreseable future. Do not rip out the -nest tests before speaking to
18# someone on the DBIC mailing list or in irc.perl.org#dbix-class
19#
20#################
21
22
32eab2da 23my @tests = (
32eab2da 24 {
25 func => 'select',
26 args => ['test', '*'],
27 stmt => 'SELECT * FROM test',
28 stmt_q => 'SELECT * FROM `test`',
29 bind => []
30 },
32eab2da 31 {
32 func => 'select',
33 args => ['test', [qw(one two three)]],
34 stmt => 'SELECT one, two, three FROM test',
35 stmt_q => 'SELECT `one`, `two`, `three` FROM `test`',
36 bind => []
37 },
32eab2da 38 {
39 func => 'select',
40 args => ['test', '*', { a => 0 }, [qw/boom bada bing/]],
41 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY boom, bada, bing',
42 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `boom`, `bada`, `bing`',
43 bind => [0]
4049d0e3 44 },
45 {
32eab2da 46 func => 'select',
47 args => ['test', '*', [ { a => 5 }, { b => 6 } ]],
48 stmt => 'SELECT * FROM test WHERE ( ( a = ? ) OR ( b = ? ) )',
49 stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` = ? ) OR ( `b` = ? ) )',
50 bind => [5,6]
4049d0e3 51 },
52 {
32eab2da 53 func => 'select',
54 args => ['test', '*', undef, ['id']],
55 stmt => 'SELECT * FROM test ORDER BY id',
56 stmt_q => 'SELECT * FROM `test` ORDER BY `id`',
57 bind => []
4049d0e3 58 },
59 {
32eab2da 60 func => 'select',
61 args => ['test', '*', { a => 'boom' } , ['id']],
62 stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY id',
63 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `id`',
64 bind => ['boom']
4049d0e3 65 },
66 {
32eab2da 67 func => 'select',
68 args => ['test', '*', { a => ['boom', 'bang'] }],
69 stmt => 'SELECT * FROM test WHERE ( ( ( a = ? ) OR ( a = ? ) ) )',
70 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `a` = ? ) OR ( `a` = ? ) ) )',
71 bind => ['boom', 'bang']
4049d0e3 72 },
73 {
32eab2da 74 func => 'select',
75 args => [[qw/test1 test2/], '*', { 'test1.a' => { 'In', ['boom', 'bang'] } }],
76 stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a IN ( ?, ? ) )',
77 stmt_q => 'SELECT * FROM `test1`, `test2` WHERE ( `test1`.`a` IN ( ?, ? ) )',
78 bind => ['boom', 'bang']
7732c37a 79 },
80 {
81 func => 'select',
82 args => [[\'test1', 'test2'], '*', { 'test1.a' => { 'In', ['boom', 'bang'] } }],
83 stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a IN ( ?, ? ) )',
84 stmt_q => 'SELECT * FROM test1, `test2` WHERE ( `test1`.`a` IN ( ?, ? ) )',
85 bind => ['boom', 'bang']
4049d0e3 86 },
87 {
32eab2da 88 func => 'select',
89 args => ['test', '*', { a => { 'between', ['boom', 'bang'] } }],
90 stmt => 'SELECT * FROM test WHERE ( a BETWEEN ? AND ? )',
91 stmt_q => 'SELECT * FROM `test` WHERE ( `a` BETWEEN ? AND ? )',
92 bind => ['boom', 'bang']
4049d0e3 93 },
94 {
32eab2da 95 func => 'select',
96 args => ['test', '*', { a => { '!=', 'boom' } }],
97 stmt => 'SELECT * FROM test WHERE ( a != ? )',
98 stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )',
99 bind => ['boom']
4049d0e3 100 },
101 {
32eab2da 102 func => 'update',
103 args => ['test', {a => 'boom'}, {a => undef}],
104 stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )',
105 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )',
106 bind => ['boom']
4049d0e3 107 },
108 {
32eab2da 109 func => 'update',
110 args => ['test', {a => 'boom'}, { a => {'!=', "bang" }} ],
111 stmt => 'UPDATE test SET a = ? WHERE ( a != ? )',
112 stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` != ? )',
113 bind => ['boom', 'bang']
4049d0e3 114 },
115 {
32eab2da 116 func => 'update',
117 args => ['test', {'a-funny-flavored-candy' => 'yummy', b => 'oops'}, { a42 => "bang" }],
118 stmt => 'UPDATE test SET a-funny-flavored-candy = ?, b = ? WHERE ( a42 = ? )',
119 stmt_q => 'UPDATE `test` SET `a-funny-flavored-candy` = ?, `b` = ? WHERE ( `a42` = ? )',
120 bind => ['yummy', 'oops', 'bang']
4049d0e3 121 },
122 {
32eab2da 123 func => 'delete',
124 args => ['test', {requestor => undef}],
125 stmt => 'DELETE FROM test WHERE ( requestor IS NULL )',
126 stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )',
127 bind => []
4049d0e3 128 },
129 {
32eab2da 130 func => 'delete',
131 args => [[qw/test1 test2 test3/],
132 { 'test1.field' => \'!= test2.field',
133 user => {'!=','nwiger'} },
134 ],
135 stmt => 'DELETE FROM test1, test2, test3 WHERE ( test1.field != test2.field AND user != ? )',
136 stmt_q => 'DELETE FROM `test1`, `test2`, `test3` WHERE ( `test1`.`field` != test2.field AND `user` != ? )', # test2.field is a literal value, cannnot be quoted.
137 bind => ['nwiger']
4049d0e3 138 },
139 {
32eab2da 140 func => 'insert',
141 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
142 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
143 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
144 bind => [qw/1 2 3 4 5/],
4049d0e3 145 },
146 {
32eab2da 147 func => 'insert',
148 args => ['test', [qw/1 2 3 4 5/]],
149 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?)',
150 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?)',
151 bind => [qw/1 2 3 4 5/],
4049d0e3 152 },
153 {
32eab2da 154 func => 'insert',
155 args => ['test', [qw/1 2 3 4 5/, undef]],
156 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)',
157 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)',
158 bind => [qw/1 2 3 4 5/, undef],
4049d0e3 159 },
160 {
32eab2da 161 func => 'update',
162 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}],
163 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?',
164 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?',
165 bind => [qw/1 2 3 4 5/],
4049d0e3 166 },
167 {
32eab2da 168 func => 'update',
169 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}],
170 stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )',
171 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )',
172 bind => [qw/1 2 3 4 5 1 2 3 4 5/],
4049d0e3 173 },
174 {
32eab2da 175 func => 'update',
96449e8e 176 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
32eab2da 177 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
178 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
179 bind => [qw(1 02/02/02 1 2)],
4049d0e3 180 },
181 {
32eab2da 182 func => 'insert',
183 args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ],
184 stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)',
185 stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)',
186 bind => ['4'],
4049d0e3 187 },
188 {
257d9a42 189 func => 'insert',
190 args => ['test.table', [ \'max(all_limits)', 4 ] ],
191 stmt => 'INSERT INTO test.table VALUES (max(all_limits), ?)',
192 stmt_q => 'INSERT INTO `test`.`table` VALUES (max(all_limits), ?)',
193 bind => ['4'],
4049d0e3 194 },
195 {
32eab2da 196 func => 'insert',
197 new => {bindtype => 'columns'},
198 args => ['test.table', {one => 2, three => 4, five => 6} ],
199 stmt => 'INSERT INTO test.table (five, one, three) VALUES (?, ?, ?)',
200 stmt_q => 'INSERT INTO `test`.`table` (`five`, `one`, `three`) VALUES (?, ?, ?)',
201 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
4049d0e3 202 },
203 {
32eab2da 204 func => 'select',
205 new => {bindtype => 'columns', case => 'lower'},
206 args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ],
207 stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )',
208 stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )',
209 bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man...
4049d0e3 210 },
211 {
32eab2da 212 func => 'update',
213 new => {bindtype => 'columns', cmp => 'like'},
214 args => ['testin.table2', {One => 22, Three => 44, FIVE => 66},
215 {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}],
216 stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE '
217 . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )',
218 stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE '
219 . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )',
220 bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'],
221 ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']],
4049d0e3 222 },
223 {
32eab2da 224 func => 'select',
07936978 225 args => ['test', '*', {priority => [ -and => {'!=', 2}, { -not_like => '3%'} ]}],
226 stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority NOT LIKE ? ) ) )',
227 stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` NOT LIKE ? ) ) )',
228 bind => [qw(2 3%)],
4049d0e3 229 },
230 {
32eab2da 231 func => 'select',
4049d0e3 232 args => ['Yo Momma', '*', { user => 'nwiger',
32eab2da 233 -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }],
234 stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )',
235 stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )',
236 bind => [qw(20 ASIA nwiger)],
4049d0e3 237 },
238 {
32eab2da 239 func => 'update',
240 args => ['taco_punches', { one => 2, three => 4 },
241 { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ],
242 tasty => { '!=', [qw(yes YES)] },
243 -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] },
244 ],
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',
96449e8e 264# LDNOTE : removed the "-maybe", because we no longer admit unknown ops
e30faf88 265#
266# acked by RIBASUSHI
96449e8e 267# args => ['fhole', {fpoles => 4}, [-maybe => {race => [-and => [qw(black white asian)]]},
24c898da 268 args => ['fhole', {fpoles => 4}, [
269 { race => [qw/-or black white asian /] },
270 { -nest => { firsttime => [-or => {'=','yes'}, undef] } },
271 { -and => [ { firstname => {-not_like => 'candace'} }, { lastname => {-in => [qw(jugs canyon towers)] } } ] },
272 ] ],
32eab2da 273 stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )'
b43d4cef 274 . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN (?, ?, ?) ) ) )',
32eab2da 275 stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )'
b43d4cef 276 . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN( ?, ?, ? )) ) )',
32eab2da 277 bind => [qw(4 black white asian yes candace jugs canyon towers)]
278 },
96449e8e 279 {
280 func => 'insert',
281 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
282 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
283 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
284 bind => [qw(1 02/02/02)],
285 },
96449e8e 286 {
287 func => 'select',
288# LDNOTE: modified test below because we agreed with MST that literal SQL
289# should not automatically insert a '='; the user has to do it
e30faf88 290#
291# acked by MSTROUT
96449e8e 292# args => ['test', '*', { a => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
293 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
294 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
295 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
296 bind => ['02/02/02'],
d82b8afb 297 },
d82b8afb 298 {
299 func => 'insert',
300 new => {array_datatypes => 1},
301 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
302 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
303 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
304 bind => [1, [1, 1, 2, 3, 5, 8]],
305 },
d82b8afb 306 {
307 func => 'insert',
308 new => {bindtype => 'columns', array_datatypes => 1},
309 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
310 stmt => 'INSERT INTO test (a, b) VALUES (?, ?)',
311 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, ?)',
312 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
313 },
d82b8afb 314 {
315 func => 'update',
316 new => {array_datatypes => 1},
317 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
318 stmt => 'UPDATE test SET a = ?, b = ?',
319 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
320 bind => [1, [1, 1, 2, 3, 5, 8]],
321 },
d82b8afb 322 {
323 func => 'update',
324 new => {bindtype => 'columns', array_datatypes => 1},
325 args => ['test', {a => 1, b => [1, 1, 2, 3, 5, 8]}],
326 stmt => 'UPDATE test SET a = ?, b = ?',
327 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?',
328 bind => [[a => 1], [b => [1, 1, 2, 3, 5, 8]]],
329 },
145fbfc8 330 {
331 func => 'select',
332 args => ['test', '*', { a => {'>', \'1 + 1'}, b => 8 }],
333 stmt => 'SELECT * FROM test WHERE ( a > 1 + 1 AND b = ? )',
334 stmt_q => 'SELECT * FROM `test` WHERE ( `a` > 1 + 1 AND `b` = ? )',
335 bind => [8],
4049d0e3 336 },
b3be7bd0 337 {
338 func => 'select',
339 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
340 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
341 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
342 bind => ['02/02/02', 8],
4049d0e3 343 },
ed821a87 344 {
5db47f9f 345 func => 'insert',
ed821a87 346 args => ['test', {a => 1, b => 2, c => 3, d => 4, e => { -answer => 42 }}],
347 stmt => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ANSWER(?))',
348 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ANSWER(?))',
349 bind => [qw/1 2 3 4 42/],
4049d0e3 350 },
351 {
ef4d99a5 352 func => 'update',
353 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
354 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
355 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
356 bind => [qw(1 1 2)],
4049d0e3 357 },
ef4d99a5 358 {
359 func => 'insert',
360 args => ['test', {a => 1, b => \["42"]}],
361 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
362 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
363 bind => [qw(1)],
364 },
ef4d99a5 365 {
366 func => 'select',
367 args => ['test', '*', { a => \["= 42"], b => 1}],
368 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
369 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
370 bind => [qw(1)],
371 },
ef4d99a5 372 {
373 func => 'select',
374 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
375 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
376 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
377 bind => [qw(8)],
4049d0e3 378 },
cd87fd4c 379 {
380 func => 'insert',
381 new => {bindtype => 'columns'},
fe3ae272 382 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 383 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
384 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
fe3ae272 385 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 386 },
4049d0e3 387 {
cd87fd4c 388 func => 'update',
389 new => {bindtype => 'columns'},
fe3ae272 390 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 391 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
392 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
fe3ae272 393 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
4049d0e3 394 },
cd87fd4c 395 {
396 func => 'select',
397 new => {bindtype => 'columns'},
fe3ae272 398 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 399 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
400 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
fe3ae272 401 bind => [[dummy => '02/02/02']],
cd87fd4c 402 },
cd87fd4c 403 {
404 func => 'select',
405 new => {bindtype => 'columns'},
fe3ae272 406 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 407 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
408 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
fe3ae272 409 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 410 },
fe3ae272 411 {
412 func => 'insert',
413 new => {bindtype => 'columns'},
414 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
415 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
416 },
4049d0e3 417 {
fe3ae272 418 func => 'update',
419 new => {bindtype => 'columns'},
420 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
421 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 422 },
fe3ae272 423 {
424 func => 'select',
425 new => {bindtype => 'columns'},
426 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
427 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
428 },
fe3ae272 429 {
430 func => 'select',
431 new => {bindtype => 'columns'},
432 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
433 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 434 },
044e8ff4 435 {
436 func => 'select',
437 new => {bindtype => 'columns'},
438 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
439 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
440 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
441 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 442 },
044e8ff4 443 {
444 func => 'select',
445 new => {bindtype => 'columns'},
446 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
447 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 448 },
26f2dca5 449 {
450 func => 'insert',
451 new => {bindtype => 'columns'},
452 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
453 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
454 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
455 bind => [[a => '1'], [{dummy => 1} => '02/02/02']],
456 },
4049d0e3 457 {
26f2dca5 458 func => 'update',
459 new => {bindtype => 'columns'},
0ec3aec7 460 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
427ef969 461 stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER(?) WHERE ( a BETWEEN ? AND ? )",
462 stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER(?) WHERE ( `a` BETWEEN ? AND ? )",
0ec3aec7 463 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
4049d0e3 464 },
26f2dca5 465 {
466 func => 'select',
467 new => {bindtype => 'columns'},
468 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
469 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
470 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
471 bind => [[{dummy => 1} => '02/02/02']],
472 },
26f2dca5 473 {
474 func => 'select',
475 new => {bindtype => 'columns'},
476 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
477 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
478 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
479 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
05a05fd1 480 },
97b9b66e 481 {
482 func => 'select',
483 new => {bindtype => 'columns'},
e30faf88 484 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
485 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
486 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
97b9b66e 487 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 488 },
05a05fd1 489 {
490 func => 'select',
491 new => {bindtype => 'columns'},
492 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
493 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
494 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
495 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
496 },
05a05fd1 497 {
498 func => 'select',
499 new => {bindtype => 'columns'},
500 args => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
501 stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
502 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
503 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
504 },
02288357 505 {
506 func => 'insert',
507 args => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
508 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
509 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
510 bind => [qw/1 2 3 4 5/],
511 },
02288357 512 {
513 func => 'insert',
514 args => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
515 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
516 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
517 bind => [qw/1 2 3 4 5/],
518 },
02288357 519 {
520 func => 'insert',
521 args => ['test', [qw/1 2 3 4 5/], { returning => [qw(id foo bar) ] }],
522 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
523 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`, `foo`, `bar`',
524 bind => [qw/1 2 3 4 5/],
525 },
02288357 526 {
527 func => 'insert',
528 args => ['test', [qw/1 2 3 4 5/], { returning => \'id, foo, bar' }],
529 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
530 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
531 bind => [qw/1 2 3 4 5/],
532 },
02288357 533 {
534 func => 'insert',
535 args => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
536 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
537 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
538 bind => [qw/1 2 3 4 5/],
539 },
07936978 540 {
541 func => 'select',
542 new => {bindtype => 'columns'},
953d164e 543 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
544 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
545 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
07936978 546 bind => [[Y => 'x']],
547 },
bdf53200 548 {
549 func => 'select',
023fd01d 550 args => ['test', '*', { a => { -in => [] }, b => { -not_in => [] }, c => { -in => 42 } }],
551 stmt => 'SELECT * FROM test WHERE ( 0=1 AND 1=1 AND c IN ( ? ))',
552 stmt_q => 'SELECT * FROM `test` WHERE ( 0=1 AND 1=1 AND `c` IN ( ? ))',
553 bind => [ 42 ],
bdf53200 554 },
5b670507 555 {
556 func => 'select',
557 args => ['test', '*', { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } } ],
558 stmt => 'SELECT * FROM test WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
559 stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` IN ( ? ) OR `a` IS NULL ) AND `b` NOT IN ( ? ) AND `b` IS NOT NULL )',
560 bind => [ 42, 42 ],
561 },
32eab2da 562);
563
3a06278c 564for my $t (@tests) {
96449e8e 565 local $"=', ';
32eab2da 566
3a06278c 567 my $new = $t->{new} || {};
96449e8e 568 $new->{debug} = $ENV{DEBUG} || 0;
32eab2da 569
3a06278c 570 for my $quoted (0, 1) {
7fb57243 571
3a06278c 572 my $maker = SQL::Abstract->new(%$new, $quoted
573 ? (quote_char => '`', name_sep => '.')
574 : ()
575 );
32eab2da 576
3a06278c 577 my($stmt, @bind);
32eab2da 578
3a06278c 579 my $cref = sub {
580 my $op = $t->{func};
581 ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
7fb57243 582 };
3a06278c 583
584 if ($t->{exception_like}) {
585 throws_ok(
586 sub { $cref->() },
587 $t->{exception_like},
588 "throws the expected exception ($t->{exception_like})"
589 );
7fb57243 590 } else {
3a06278c 591 if ($t->{warning_like}) {
592 warning_like(
593 sub { $cref->() },
594 $t->{warning_like},
595 "issues the expected warning ($t->{warning_like})"
596 );
fe3ae272 597 }
3a06278c 598 else {
31a41fd9 599 unless (eval { $cref->(); 1 }) {
600 die "Unexpected exception thrown for structure:\n"
601 .Dumper($t)."Exception was: $@";
602 }
3a06278c 603 }
604 is_same_sql_bind(
605 $stmt,
606 \@bind,
607 $quoted ? $t->{stmt_q}: $t->{stmt},
608 $t->{bind}
609 );
fe3ae272 610 }
7fb57243 611 }
32eab2da 612}
10e6c946 613
614done_testing;