Fix stupid not exists omission
[scpubgit/Q-Branch.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 },
5db47f9f 336 { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
337 func => 'insert',
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 (?, ?, ?, ?, ?)',
340 stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
341 bind => [qw/1 2 3 4/, { answer => 42}],
7fb57243 342 warning_like => qr/HASH ref as bind value in insert is not supported/i,
4049d0e3 343 },
344 {
ef4d99a5 345 func => 'update',
346 args => ['test', {a => 1, b => \["42"]}, {a => {'between', [1,2]}}],
347 stmt => 'UPDATE test SET a = ?, b = 42 WHERE ( a BETWEEN ? AND ? )',
348 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = 42 WHERE ( `a` BETWEEN ? AND ? )',
349 bind => [qw(1 1 2)],
4049d0e3 350 },
ef4d99a5 351 {
352 func => 'insert',
353 args => ['test', {a => 1, b => \["42"]}],
354 stmt => 'INSERT INTO test (a, b) VALUES (?, 42)',
355 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, 42)',
356 bind => [qw(1)],
357 },
ef4d99a5 358 {
359 func => 'select',
360 args => ['test', '*', { a => \["= 42"], b => 1}],
361 stmt => q{SELECT * FROM test WHERE ( a = 42 ) AND (b = ? )},
362 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = 42 ) AND ( `b` = ? )},
363 bind => [qw(1)],
364 },
ef4d99a5 365 {
366 func => 'select',
367 args => ['test', '*', { a => {'<' => \["42"]}, b => 8 }],
368 stmt => 'SELECT * FROM test WHERE ( a < 42 AND b = ? )',
369 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < 42 AND `b` = ? )',
370 bind => [qw(8)],
4049d0e3 371 },
cd87fd4c 372 {
373 func => 'insert',
374 new => {bindtype => 'columns'},
fe3ae272 375 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 376 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
377 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
fe3ae272 378 bind => [[a => '1'], [dummy => '02/02/02']],
cd87fd4c 379 },
4049d0e3 380 {
cd87fd4c 381 func => 'update',
382 new => {bindtype => 'columns'},
fe3ae272 383 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
cd87fd4c 384 stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
385 stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
fe3ae272 386 bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
4049d0e3 387 },
cd87fd4c 388 {
389 func => 'select',
390 new => {bindtype => 'columns'},
fe3ae272 391 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
cd87fd4c 392 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
393 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
fe3ae272 394 bind => [[dummy => '02/02/02']],
cd87fd4c 395 },
cd87fd4c 396 {
397 func => 'select',
398 new => {bindtype => 'columns'},
fe3ae272 399 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
cd87fd4c 400 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
401 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
fe3ae272 402 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 403 },
fe3ae272 404 {
405 func => 'insert',
406 new => {bindtype => 'columns'},
407 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
408 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
409 },
4049d0e3 410 {
fe3ae272 411 func => 'update',
412 new => {bindtype => 'columns'},
413 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
414 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 415 },
fe3ae272 416 {
417 func => 'select',
418 new => {bindtype => 'columns'},
419 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
420 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
421 },
fe3ae272 422 {
423 func => 'select',
424 new => {bindtype => 'columns'},
425 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
426 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 427 },
044e8ff4 428 {
429 func => 'select',
430 new => {bindtype => 'columns'},
431 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", [dummy => '02/02/02']]}, b => 8 }],
432 stmt => 'SELECT * FROM test WHERE ( a IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND b = ? )',
433 stmt_q => 'SELECT * FROM `test` WHERE ( `a` IN (SELECT d FROM to_date(?, \'MM/DD/YY\') AS d) AND `b` = ? )',
434 bind => [[dummy => '02/02/02'], [b => 8]],
4049d0e3 435 },
044e8ff4 436 {
437 func => 'select',
438 new => {bindtype => 'columns'},
439 args => ['test', '*', { a => {-in => \["(SELECT d FROM to_date(?, 'MM/DD/YY') AS d)", '02/02/02']}, b => 8 }],
440 exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
4049d0e3 441 },
26f2dca5 442 {
443 func => 'insert',
444 new => {bindtype => 'columns'},
445 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
446 stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
447 stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
448 bind => [[a => '1'], [{dummy => 1} => '02/02/02']],
449 },
4049d0e3 450 {
26f2dca5 451 func => 'update',
452 new => {bindtype => 'columns'},
0ec3aec7 453 args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
6e9a377b 454 stmt => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER ? WHERE ( a BETWEEN ? AND ? )",
455 stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ? WHERE ( `a` BETWEEN ? AND ? )",
0ec3aec7 456 bind => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
4049d0e3 457 },
26f2dca5 458 {
459 func => 'select',
460 new => {bindtype => 'columns'},
461 args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}],
462 stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
463 stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
464 bind => [[{dummy => 1} => '02/02/02']],
465 },
26f2dca5 466 {
467 func => 'select',
468 new => {bindtype => 'columns'},
469 args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']]}, b => 8 }],
470 stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
471 stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
472 bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
05a05fd1 473 },
97b9b66e 474 {
475 func => 'select',
476 new => {bindtype => 'columns'},
e30faf88 477 args => ['test', '*', { -or => [ -and => [ a => 'a', b => 'b' ], -and => [ c => 'c', d => 'd' ] ] }],
478 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
479 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
97b9b66e 480 bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
05a05fd1 481 },
05a05fd1 482 {
483 func => 'select',
484 new => {bindtype => 'columns'},
485 args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
486 stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
487 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
488 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
489 },
05a05fd1 490 {
491 func => 'select',
492 new => {bindtype => 'columns'},
493 args => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
494 stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
495 stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
496 bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
497 },
02288357 498 {
499 func => 'insert',
500 args => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
501 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
502 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
503 bind => [qw/1 2 3 4 5/],
504 },
02288357 505 {
506 func => 'insert',
507 args => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
508 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
509 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
510 bind => [qw/1 2 3 4 5/],
511 },
02288357 512 {
513 func => 'insert',
514 args => ['test', [qw/1 2 3 4 5/], { returning => [qw(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 => \'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' }],
529 stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
530 stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
531 bind => [qw/1 2 3 4 5/],
532 },
07936978 533 {
534 func => 'select',
535 new => {bindtype => 'columns'},
953d164e 536 args => ['test', '*', [ Y => { '=' => { -max => { -LENGTH => { -min => 'x' } } } } ] ],
537 stmt => 'SELECT * FROM test WHERE ( Y = ( MAX( LENGTH( MIN ? ) ) ) )',
538 stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
07936978 539 bind => [[Y => 'x']],
540 },
32eab2da 541);
542
6dcf723c 543
7fb57243 544plan tests => scalar(grep { !$_->{warning_like} } @tests) * 2
545 + scalar(grep { $_->{warning_like} } @tests) * 4;
32eab2da 546
3a06278c 547for my $t (@tests) {
96449e8e 548 local $"=', ';
32eab2da 549
3a06278c 550 my $new = $t->{new} || {};
96449e8e 551 $new->{debug} = $ENV{DEBUG} || 0;
32eab2da 552
3a06278c 553 for my $quoted (0, 1) {
7fb57243 554
3a06278c 555 my $maker = SQL::Abstract->new(%$new, $quoted
556 ? (quote_char => '`', name_sep => '.')
557 : ()
558 );
32eab2da 559
3a06278c 560 my($stmt, @bind);
32eab2da 561
3a06278c 562 my $cref = sub {
563 my $op = $t->{func};
564 ($stmt, @bind) = $maker->$op (@ { $t->{args} } );
7fb57243 565 };
3a06278c 566
567 if ($t->{exception_like}) {
568 throws_ok(
569 sub { $cref->() },
570 $t->{exception_like},
571 "throws the expected exception ($t->{exception_like})"
572 );
7fb57243 573 } else {
3a06278c 574 if ($t->{warning_like}) {
575 warning_like(
576 sub { $cref->() },
577 $t->{warning_like},
578 "issues the expected warning ($t->{warning_like})"
579 );
fe3ae272 580 }
3a06278c 581 else {
582 $cref->();
583 }
584 is_same_sql_bind(
585 $stmt,
586 \@bind,
587 $quoted ? $t->{stmt_q}: $t->{stmt},
588 $t->{bind}
589 );
fe3ae272 590 }
7fb57243 591 }
32eab2da 592}