Commit | Line | Data |
41751122 |
1 | #!/usr/bin/perl |
32eab2da |
2 | |
3 | use strict; |
41751122 |
4 | use warnings; |
5 | use Test::More; |
32eab2da |
6 | |
5aad8cf3 |
7 | use SQL::Abstract::Test import => ['is_same_sql_bind']; |
32eab2da |
8 | |
96449e8e |
9 | #LDNOTE: renamed all "bind" into "where" because that's what they are |
10 | |
32eab2da |
11 | my @handle_tests = ( |
12 | #1 |
13 | { |
14 | args => {logic => 'OR'}, |
96449e8e |
15 | # stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? )' |
16 | # LDNOTE: modified the line above (changing the test suite!!!) because |
17 | # the test was not consistent with the doc: hashrefs should not be |
18 | # influenced by the current logic, they always mean 'AND'. So |
19 | # { a => 4, b => 0} should ALWAYS mean ( a = ? AND b = ? ). |
20 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
32eab2da |
21 | }, |
22 | #2 |
23 | { |
24 | args => {}, |
25 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
26 | }, |
27 | #3 |
28 | { |
29 | args => {case => "upper"}, |
30 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
31 | }, |
32 | #4 |
33 | { |
34 | args => {case => "upper", cmp => "="}, |
35 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
36 | }, |
37 | #5 |
38 | { |
39 | args => {cmp => "=", logic => 'or'}, |
96449e8e |
40 | # LDNOTE idem |
41 | # stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? )' |
42 | stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' |
32eab2da |
43 | }, |
44 | #6 |
45 | { |
46 | args => {cmp => "like"}, |
47 | stmt => 'SELECT * FROM test WHERE ( a LIKE ? AND b LIKE ? )' |
48 | }, |
49 | #7 |
50 | { |
51 | args => {logic => "or", cmp => "like"}, |
96449e8e |
52 | # LDNOTE idem |
53 | # stmt => 'SELECT * FROM test WHERE ( a LIKE ? OR b LIKE ? )' |
54 | stmt => 'SELECT * FROM test WHERE ( a LIKE ? AND b LIKE ? )' |
32eab2da |
55 | }, |
56 | #8 |
57 | { |
58 | args => {case => "lower"}, |
59 | stmt => 'select * from test where ( a = ? and b = ? )' |
60 | }, |
61 | #9 |
62 | { |
63 | args => {case => "lower", cmp => "="}, |
64 | stmt => 'select * from test where ( a = ? and b = ? )' |
65 | }, |
66 | #10 |
67 | { |
68 | args => {case => "lower", cmp => "like"}, |
69 | stmt => 'select * from test where ( a like ? and b like ? )' |
70 | }, |
71 | #11 |
72 | { |
73 | args => {case => "lower", convert => "lower", cmp => "like"}, |
74 | stmt => 'select * from test where ( lower(a) like lower(?) and lower(b) like lower(?) )' |
75 | }, |
76 | #12 |
77 | { |
78 | args => {convert => "Round"}, |
79 | stmt => 'SELECT * FROM test WHERE ( ROUND(a) = ROUND(?) AND ROUND(b) = ROUND(?) )', |
80 | }, |
81 | #13 |
82 | { |
83 | args => {convert => "lower"}, |
84 | stmt => 'SELECT * FROM test WHERE ( ( LOWER(ticket) = LOWER(?) ) OR ( LOWER(hostname) = LOWER(?) ) OR ( LOWER(taco) = LOWER(?) ) OR ( LOWER(salami) = LOWER(?) ) )', |
96449e8e |
85 | where => [ { ticket => 11 }, { hostname => 11 }, { taco => 'salad' }, { salami => 'punch' } ], |
32eab2da |
86 | }, |
87 | #14 |
88 | { |
89 | args => {convert => "upper"}, |
96449e8e |
90 | # LDNOTE : modified the test below, because modified the semantics |
91 | # of "e => { '!=', [qw(f g)] }" : generating "e != 'f' OR e != 'g'" |
92 | # is nonsense (will always be true whatever the value of e). Since |
93 | # this is a 'negative' operator, we must apply the Morgan laws and |
94 | # interpret it as "e != 'f' AND e != 'g'" (and actually the user |
95 | # should rather write "e => {-not_in => [qw/f g/]}". |
96 | |
97 | # 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(?) ) ) )', |
98 | 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(?) ) AND ( UPPER(e) != UPPER(?) ) ) AND UPPER(q) NOT IN ( UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?), UPPER(?) ) ) )', |
99 | where => [ { ticket => [11, 12, 13], |
100 | hostname => { in => ['ntf', 'avd', 'bvd', '123'] } }, |
32eab2da |
101 | { tack => { between => [qw/tick tock/] } }, |
96449e8e |
102 | { a => [qw/b c d/], |
103 | e => { '!=', [qw(f g)] }, |
104 | q => { 'not in', [14..20] } } ], |
32eab2da |
105 | }, |
106 | ); |
107 | |
6dcf723c |
108 | |
109 | plan tests => (1 + scalar(@handle_tests)); |
110 | |
111 | use_ok('SQL::Abstract'); |
112 | |
32eab2da |
113 | for (@handle_tests) { |
96449e8e |
114 | local $" = ', '; |
115 | #print "creating a handle with args ($_->{args}): "; |
116 | my $sql = SQL::Abstract->new($_->{args}); |
117 | my $where = $_->{where} || { a => 4, b => 0}; |
118 | my($stmt, @bind) = $sql->select('test', '*', $where); |
119 | |
120 | # LDNOTE: this original test suite from NWIGER did no comparisons |
121 | # on @bind values, just checking if @bind is nonempty. |
122 | # So here we just fake a [1] bind value for the comparison. |
123 | is_same_sql_bind($stmt, [@bind ? 1 : 0], $_->{stmt}, [1]); |
32eab2da |
124 | } |
125 | |
126 | |