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 => 14 } |
10 | |
11 | use SQL::Abstract; |
12 | |
13 | my @handle_tests = ( |
14 | #1 |
15 | { |
16 | args => {logic => 'OR'}, |
17 | stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? )' |
18 | }, |
19 | #2 |
20 | { |
21 | args => {}, |
22 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
23 | }, |
24 | #3 |
25 | { |
26 | args => {case => "upper"}, |
27 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
28 | }, |
29 | #4 |
30 | { |
31 | args => {case => "upper", cmp => "="}, |
32 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
33 | }, |
34 | #5 |
35 | { |
36 | args => {cmp => "=", logic => 'or'}, |
37 | stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? )' |
38 | }, |
39 | #6 |
40 | { |
41 | args => {cmp => "like"}, |
42 | stmt => 'SELECT * FROM test WHERE ( a LIKE ? AND b LIKE ? )' |
43 | }, |
44 | #7 |
45 | { |
46 | args => {logic => "or", cmp => "like"}, |
47 | stmt => 'SELECT * FROM test WHERE ( a LIKE ? OR b LIKE ? )' |
48 | }, |
49 | #8 |
50 | { |
51 | args => {case => "lower"}, |
52 | stmt => 'select * from test where ( a = ? and b = ? )' |
53 | }, |
54 | #9 |
55 | { |
56 | args => {case => "lower", cmp => "="}, |
57 | stmt => 'select * from test where ( a = ? and b = ? )' |
58 | }, |
59 | #10 |
60 | { |
61 | args => {case => "lower", cmp => "like"}, |
62 | stmt => 'select * from test where ( a like ? and b like ? )' |
63 | }, |
64 | #11 |
65 | { |
66 | args => {case => "lower", convert => "lower", cmp => "like"}, |
67 | stmt => 'select * from test where ( lower(a) like lower(?) and lower(b) like lower(?) )' |
68 | }, |
69 | #12 |
70 | { |
71 | args => {convert => "Round"}, |
72 | stmt => 'SELECT * FROM test WHERE ( ROUND(a) = ROUND(?) AND ROUND(b) = ROUND(?) )', |
73 | }, |
74 | #13 |
75 | { |
76 | args => {convert => "lower"}, |
77 | stmt => 'SELECT * FROM test WHERE ( ( LOWER(ticket) = LOWER(?) ) OR ( LOWER(hostname) = LOWER(?) ) OR ( LOWER(taco) = LOWER(?) ) OR ( LOWER(salami) = LOWER(?) ) )', |
78 | bind => [ { ticket => 11 }, { hostname => 11 }, { taco => 'salad' }, { salami => 'punch' } ], |
79 | }, |
80 | #14 |
81 | { |
82 | args => {convert => "upper"}, |
83 | stmt => 'SELECT * FROM test WHERE ( ( UPPER(hostname) IN ( UPPER(?), UPPER(?), UPPER(?), UPPER(?) ) AND ( ( UPPER(ticket) = UPPER(?) ) OR ( UPPER(ticket) = UPPER(?) ) OR ( UPPER(ticket) = UPPER(?) ) ) ) OR ( UPPER(tack) BETWEEN UPPER(?) AND UPPER(?) ) OR ( ( ( UPPER(a) = UPPER(?) ) OR ( UPPER(a) = UPPER(?) ) OR ( UPPER(a) = UPPER(?) ) ) AND ( ( UPPER(e) != UPPER(?) ) OR ( UPPER(e) != UPPER(?) ) ) AND UPPER(q) NOT IN ( UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?) ) ) )', |
84 | bind => [ { ticket => [11, 12, 13], hostname => { in => ['ntf', 'avd', 'bvd', '123'] } }, |
85 | { tack => { between => [qw/tick tock/] } }, |
86 | { a => [qw/b c d/], e => { '!=', [qw(f g)] }, q => { 'not in', [14..20] } } ], |
87 | }, |
88 | ); |
89 | |
90 | for (@handle_tests) { |
91 | local $" = ', '; |
92 | #print "creating a handle with args ($_->{args}): "; |
93 | my $sql = SQL::Abstract->new($_->{args}); |
94 | my $bind = $_->{bind} || { a => 4, b => 0}; |
95 | my($stmt, @bind) = $sql->select('test', '*', $bind); |
96 | ok($stmt eq $_->{stmt} && @bind) or |
97 | warn "got\n", |
98 | "[$stmt], [@bind]\n", |
99 | "instead of\n", |
100 | "[$_->{stmt}] [4, 0]\n\n"; |
101 | } |
102 | |
103 | |