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