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