Commit | Line | Data |
32eab2da |
1 | #!/usr/bin/perl -I. -w |
2 | |
3 | use strict; |
4 | use vars qw($TESTING); |
5 | $TESTING = 1; |
6 | use Test; |
7 | |
8 | # use a BEGIN block so we print our plan before SQL::Abstract is loaded |
9 | BEGIN { plan tests => 60 } |
10 | |
11 | use SQL::Abstract; |
12 | |
13 | my @tests = ( |
14 | #1 |
15 | { |
16 | func => 'select', |
17 | args => ['test', '*'], |
18 | stmt => 'SELECT * FROM test', |
19 | stmt_q => 'SELECT * FROM `test`', |
20 | bind => [] |
21 | }, |
22 | #2 |
23 | { |
24 | func => 'select', |
25 | args => ['test', [qw(one two three)]], |
26 | stmt => 'SELECT one, two, three FROM test', |
27 | stmt_q => 'SELECT `one`, `two`, `three` FROM `test`', |
28 | bind => [] |
29 | }, |
30 | #3 |
31 | { |
32 | func => 'select', |
33 | args => ['test', '*', { a => 0 }, [qw/boom bada bing/]], |
34 | stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY boom, bada, bing', |
35 | stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `boom`, `bada`, `bing`', |
36 | bind => [0] |
37 | }, |
38 | #4 |
39 | { |
40 | func => 'select', |
41 | args => ['test', '*', [ { a => 5 }, { b => 6 } ]], |
42 | stmt => 'SELECT * FROM test WHERE ( ( a = ? ) OR ( b = ? ) )', |
43 | stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` = ? ) OR ( `b` = ? ) )', |
44 | bind => [5,6] |
45 | }, |
46 | #5 |
47 | { |
48 | func => 'select', |
49 | args => ['test', '*', undef, ['id']], |
50 | stmt => 'SELECT * FROM test ORDER BY id', |
51 | stmt_q => 'SELECT * FROM `test` ORDER BY `id`', |
52 | bind => [] |
53 | }, |
54 | #6 |
55 | { |
56 | func => 'select', |
57 | args => ['test', '*', { a => 'boom' } , ['id']], |
58 | stmt => 'SELECT * FROM test WHERE ( a = ? ) ORDER BY id', |
59 | stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? ) ORDER BY `id`', |
60 | bind => ['boom'] |
61 | }, |
62 | #7 |
63 | { |
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'] |
69 | }, |
70 | #8 |
71 | { |
72 | func => 'select', |
73 | args => [[qw/test1 test2/], '*', { 'test1.a' => { 'In', ['boom', 'bang'] } }], |
74 | stmt => 'SELECT * FROM test1, test2 WHERE ( test1.a IN ( ?, ? ) )', |
75 | stmt_q => 'SELECT * FROM `test1`, `test2` WHERE ( `test1`.`a` IN ( ?, ? ) )', |
76 | bind => ['boom', 'bang'] |
77 | }, |
78 | #9 |
79 | { |
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'] |
85 | }, |
86 | #10 |
87 | { |
88 | func => 'select', |
89 | args => ['test', '*', { a => { '!=', 'boom' } }], |
90 | stmt => 'SELECT * FROM test WHERE ( a != ? )', |
91 | stmt_q => 'SELECT * FROM `test` WHERE ( `a` != ? )', |
92 | bind => ['boom'] |
93 | }, |
94 | #11 |
95 | { |
96 | func => 'update', |
97 | args => ['test', {a => 'boom'}, {a => undef}], |
98 | stmt => 'UPDATE test SET a = ? WHERE ( a IS NULL )', |
99 | stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` IS NULL )', |
100 | bind => ['boom'] |
101 | }, |
102 | #12 |
103 | { |
104 | func => 'update', |
105 | args => ['test', {a => 'boom'}, { a => {'!=', "bang" }} ], |
106 | stmt => 'UPDATE test SET a = ? WHERE ( a != ? )', |
107 | stmt_q => 'UPDATE `test` SET `a` = ? WHERE ( `a` != ? )', |
108 | bind => ['boom', 'bang'] |
109 | }, |
110 | #13 |
111 | { |
112 | func => 'update', |
113 | args => ['test', {'a-funny-flavored-candy' => 'yummy', b => 'oops'}, { a42 => "bang" }], |
114 | stmt => 'UPDATE test SET a-funny-flavored-candy = ?, b = ? WHERE ( a42 = ? )', |
115 | stmt_q => 'UPDATE `test` SET `a-funny-flavored-candy` = ?, `b` = ? WHERE ( `a42` = ? )', |
116 | bind => ['yummy', 'oops', 'bang'] |
117 | }, |
118 | #14 |
119 | { |
120 | func => 'delete', |
121 | args => ['test', {requestor => undef}], |
122 | stmt => 'DELETE FROM test WHERE ( requestor IS NULL )', |
123 | stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL )', |
124 | bind => [] |
125 | }, |
126 | #15 |
127 | { |
128 | func => 'delete', |
129 | args => [[qw/test1 test2 test3/], |
130 | { 'test1.field' => \'!= test2.field', |
131 | user => {'!=','nwiger'} }, |
132 | ], |
133 | stmt => 'DELETE FROM test1, test2, test3 WHERE ( test1.field != test2.field AND user != ? )', |
134 | stmt_q => 'DELETE FROM `test1`, `test2`, `test3` WHERE ( `test1`.`field` != test2.field AND `user` != ? )', # test2.field is a literal value, cannnot be quoted. |
135 | bind => ['nwiger'] |
136 | }, |
137 | #16 |
138 | { |
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/], |
144 | }, |
145 | #17 |
146 | { |
147 | func => 'insert', |
148 | args => ['test', [qw/1 2 3 4 5/]], |
149 | stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?)', |
150 | stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?)', |
151 | bind => [qw/1 2 3 4 5/], |
152 | }, |
153 | #18 |
154 | { |
155 | func => 'insert', |
156 | args => ['test', [qw/1 2 3 4 5/, undef]], |
157 | stmt => 'INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)', |
158 | stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?, ?)', |
159 | bind => [qw/1 2 3 4 5/, undef], |
160 | }, |
161 | #19 |
162 | { |
163 | func => 'update', |
164 | args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}], |
165 | stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ?', |
166 | stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ?', |
167 | bind => [qw/1 2 3 4 5/], |
168 | }, |
169 | #20 |
170 | { |
171 | func => 'update', |
172 | args => ['test', {a => 1, b => 2, c => 3, d => 4, e => 5}, {a => {'in', [1..5]}}], |
173 | stmt => 'UPDATE test SET a = ?, b = ?, c = ?, d = ?, e = ? WHERE ( a IN ( ?, ?, ?, ?, ? ) )', |
174 | stmt_q => 'UPDATE `test` SET `a` = ?, `b` = ?, `c` = ?, `d` = ?, `e` = ? WHERE ( `a` IN ( ?, ?, ?, ?, ? ) )', |
175 | bind => [qw/1 2 3 4 5 1 2 3 4 5/], |
176 | }, |
177 | #21 |
178 | { |
179 | func => 'update', |
180 | args => ['test', {a => 1, b => ["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}], |
181 | stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )', |
182 | stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )', |
183 | bind => [qw(1 02/02/02 1 2)], |
184 | }, |
185 | #22 |
186 | { |
187 | func => 'insert', |
188 | args => ['test.table', {high_limit => \'max(all_limits)', low_limit => 4} ], |
189 | stmt => 'INSERT INTO test.table (high_limit, low_limit) VALUES (max(all_limits), ?)', |
190 | stmt_q => 'INSERT INTO `test`.`table` (`high_limit`, `low_limit`) VALUES (max(all_limits), ?)', |
191 | bind => ['4'], |
192 | }, |
193 | #23 |
194 | { |
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... |
201 | }, |
202 | #24 |
203 | { |
204 | func => 'select', |
205 | new => {bindtype => 'columns', case => 'lower'}, |
206 | args => ['test.table', [qw/one two three/], {one => 2, three => 4, five => 6} ], |
207 | stmt => 'select one, two, three from test.table where ( five = ? and one = ? and three = ? )', |
208 | stmt_q => 'select `one`, `two`, `three` from `test`.`table` where ( `five` = ? and `one` = ? and `three` = ? )', |
209 | bind => [['five', 6], ['one', 2], ['three', 4]], # alpha order, man... |
210 | }, |
211 | #25 |
212 | { |
213 | func => 'update', |
214 | new => {bindtype => 'columns', cmp => 'like'}, |
215 | args => ['testin.table2', {One => 22, Three => 44, FIVE => 66}, |
216 | {Beer => 'is', Yummy => '%YES%', IT => ['IS','REALLY','GOOD']}], |
217 | stmt => 'UPDATE testin.table2 SET FIVE = ?, One = ?, Three = ? WHERE ' |
218 | . '( Beer LIKE ? AND ( ( IT LIKE ? ) OR ( IT LIKE ? ) OR ( IT LIKE ? ) ) AND Yummy LIKE ? )', |
219 | stmt_q => 'UPDATE `testin`.`table2` SET `FIVE` = ?, `One` = ?, `Three` = ? WHERE ' |
220 | . '( `Beer` LIKE ? AND ( ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) OR ( `IT` LIKE ? ) ) AND `Yummy` LIKE ? )', |
221 | bind => [['FIVE', 66], ['One', 22], ['Three', 44], ['Beer','is'], |
222 | ['IT','IS'], ['IT','REALLY'], ['IT','GOOD'], ['Yummy','%YES%']], |
223 | }, |
224 | #26 |
225 | { |
226 | func => 'select', |
227 | args => ['test', '*', {priority => [ -and => {'!=', 2}, {'!=', 1} ]}], |
228 | stmt => 'SELECT * FROM test WHERE ( ( ( priority != ? ) AND ( priority != ? ) ) )', |
229 | stmt_q => 'SELECT * FROM `test` WHERE ( ( ( `priority` != ? ) AND ( `priority` != ? ) ) )', |
230 | bind => [qw(2 1)], |
231 | }, |
232 | #27 |
233 | { |
234 | func => 'select', |
235 | args => ['Yo Momma', '*', { user => 'nwiger', |
236 | -nest => [ workhrs => {'>', 20}, geo => 'ASIA' ] }], |
237 | stmt => 'SELECT * FROM Yo Momma WHERE ( ( ( workhrs > ? ) OR ( geo = ? ) ) AND user = ? )', |
238 | stmt_q => 'SELECT * FROM `Yo Momma` WHERE ( ( ( `workhrs` > ? ) OR ( `geo` = ? ) ) AND `user` = ? )', |
239 | bind => [qw(20 ASIA nwiger)], |
240 | }, |
241 | #28 |
242 | { |
243 | func => 'update', |
244 | args => ['taco_punches', { one => 2, three => 4 }, |
245 | { bland => [ -and => {'!=', 'yes'}, {'!=', 'YES'} ], |
246 | tasty => { '!=', [qw(yes YES)] }, |
247 | -nest => [ face => [ -or => {'=', 'mr.happy'}, {'=', undef} ] ] }, |
248 | ], |
249 | stmt => 'UPDATE taco_punches SET one = ?, three = ? WHERE ( ( ( ( ( face = ? ) OR ( face IS NULL ) ) ) )' |
250 | . ' AND ( ( bland != ? ) AND ( bland != ? ) ) AND ( ( tasty != ? ) OR ( tasty != ? ) ) )', |
251 | stmt_q => 'UPDATE `taco_punches` SET `one` = ?, `three` = ? WHERE ( ( ( ( ( `face` = ? ) OR ( `face` IS NULL ) ) ) )' |
252 | . ' AND ( ( `bland` != ? ) AND ( `bland` != ? ) ) AND ( ( `tasty` != ? ) OR ( `tasty` != ? ) ) )', |
253 | bind => [qw(2 4 mr.happy yes YES yes YES)], |
254 | }, |
255 | #29 |
256 | { |
257 | func => 'select', |
258 | args => ['jeff', '*', { name => {'like', '%smith%', -not_in => ['Nate','Jim','Bob','Sally']}, |
259 | -nest => [ -or => [ -and => [age => { -between => [20,30] }, age => {'!=', 25} ], |
260 | yob => {'<', 1976} ] ] } ], |
261 | stmt => 'SELECT * FROM jeff WHERE ( ( ( ( ( ( ( age BETWEEN ? AND ? ) AND ( age != ? ) ) ) OR ( yob < ? ) ) ) )' |
262 | . ' AND name NOT IN ( ?, ?, ?, ? ) AND name LIKE ? )', |
263 | stmt_q => 'SELECT * FROM `jeff` WHERE ( ( ( ( ( ( ( `age` BETWEEN ? AND ? ) AND ( `age` != ? ) ) ) OR ( `yob` < ? ) ) ) )' |
264 | . ' AND `name` NOT IN ( ?, ?, ?, ? ) AND `name` LIKE ? )', |
265 | bind => [qw(20 30 25 1976 Nate Jim Bob Sally %smith%)] |
266 | }, |
267 | #30 |
268 | { |
269 | # The "-maybe" should be ignored, as it sits at the top level (bug?) |
270 | func => 'update', |
271 | args => ['fhole', {fpoles => 4}, [-maybe => {race => [-and => [qw(black white asian)]]}, |
272 | {-nest => {firsttime => [-or => {'=','yes'}, undef]}}, |
273 | [ -and => {firstname => {-not_like => 'candace'}}, {lastname => {-in => [qw(jugs canyon towers)]}} ] ] ], |
274 | stmt => 'UPDATE fhole SET fpoles = ? WHERE ( ( ( ( ( ( ( race = ? ) OR ( race = ? ) OR ( race = ? ) ) ) ) ) )' |
275 | . ' OR ( ( ( ( firsttime = ? ) OR ( firsttime IS NULL ) ) ) ) OR ( ( ( firstname NOT LIKE ? ) ) AND ( lastname IN ( ?, ?, ? ) ) ) )', |
276 | stmt_q => 'UPDATE `fhole` SET `fpoles` = ? WHERE ( ( ( ( ( ( ( `race` = ? ) OR ( `race` = ? ) OR ( `race` = ? ) ) ) ) ) )' |
277 | . ' OR ( ( ( ( `firsttime` = ? ) OR ( `firsttime` IS NULL ) ) ) ) OR ( ( ( `firstname` NOT LIKE ? ) ) AND ( `lastname` IN ( ?, ?, ? ) ) ) )', |
278 | bind => [qw(4 black white asian yes candace jugs canyon towers)] |
279 | }, |
280 | ); |
281 | |
282 | use Data::Dumper; |
283 | |
284 | for (@tests) { |
285 | local $"=', '; |
286 | |
287 | my $new = $_->{new} || {}; |
288 | $new->{debug} = $ENV{DEBUG} || 0; |
289 | my $sql = SQL::Abstract->new(%$new); |
290 | |
291 | #print "testing with args (@{$_->{args}}): "; |
292 | my $func = $_->{func}; |
293 | my($stmt, @bind) = $sql->$func(@{$_->{args}}); |
294 | ok($stmt eq $_->{stmt} && equal(\@bind, $_->{bind})) or |
295 | print "got\n", |
296 | "[$stmt] [",Dumper(\@bind),"]\n", |
297 | "instead of\n", |
298 | "[$_->{stmt}] [",Dumper($_->{bind}),"]\n\n"; |
299 | |
300 | # test with quoted labels |
301 | my $sql_q = SQL::Abstract->new(%$new, quote_char => '`', name_sep => '.'); |
302 | |
303 | my $func_q = $_->{func}; |
304 | my($stmt_q, @bind_q) = $sql_q->$func_q(@{$_->{args}}); |
305 | ok($stmt_q eq $_->{stmt_q} && equal(\@bind_q, $_->{bind})) or |
306 | print "got\n", |
307 | "[$stmt_q] [",Dumper(\@bind_q),"]\n", |
308 | "instead of\n", |
309 | "[$_->{stmt_q}] [",Dumper($_->{bind}),"]\n\n"; |
310 | } |
311 | |
312 | sub equal { |
313 | my ($a, $b) = @_; |
314 | return 0 if @$a != @$b; |
315 | for (my $i = 0; $i < $#{$a}; $i++) { |
316 | next if (! defined($a->[$i])) && (! defined($b->[$i])); |
317 | if (ref $a->[$i] && ref $b->[$i]) { |
318 | return 0 if $a->[$i][0] ne $b->[$i][0] |
319 | || $a->[$i][1] ne $b->[$i][1]; |
320 | } else { |
321 | return 0 if $a->[$i] ne $b->[$i]; |
322 | } |
323 | } |
324 | return 1; |
325 | } |
326 | |
327 | |