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