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