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