Commit | Line | Data |
e5360be4 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use Test::More; |
6 | use Test::Exception; |
7 | use SQL::Abstract::Test import => ['is_same_sql_bind']; |
8 | |
9 | use Data::Dumper; |
10 | use SQL::Abstract; |
11 | |
12 | =begin |
a8b90cb7 |
13 | Test -and -or and -nest modifiers, assuming the following: |
e5360be4 |
14 | |
15 | * Modifiers are respected in both hashrefs and arrayrefs (with the obvious limitation of one modifier type per hahsref) |
16 | * Each modifier affects only the immediate element following it |
17 | * In the case of -nestX simply wrap whatever the next element is in a pair of (), regardless of type |
18 | * In the case of -or/-and explicitly setting the logic within a following hashref or arrayref, |
19 | without imposing the logic on any sub-elements of the affected structure |
20 | * Ignore (maybe throw exception?) of the -or/-and modifier if the following element is missing, |
21 | or is of a type other than hash/arrayref |
22 | |
23 | =cut |
24 | |
05a05fd1 |
25 | # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage) |
26 | my $and_or_args = { |
27 | and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] }, |
28 | or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] }, |
29 | or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] }, |
30 | or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] }, |
31 | and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] }, |
32 | }; |
33 | |
a8b90cb7 |
34 | my @and_or_tests = ( |
05a05fd1 |
35 | # basic tests |
a8b90cb7 |
36 | { |
37 | where => { -and => [a => 1, b => 2] }, |
05a05fd1 |
38 | %{$and_or_args->{and}}, |
a8b90cb7 |
39 | }, |
40 | { |
41 | where => [ -and => [a => 1, b => 2] ], |
05a05fd1 |
42 | %{$and_or_args->{and}}, |
a8b90cb7 |
43 | }, |
44 | { |
45 | where => { -or => [a => 1, b => 2] }, |
05a05fd1 |
46 | %{$and_or_args->{or}}, |
a8b90cb7 |
47 | }, |
48 | { |
49 | where => [ -or => [a => 1, b => 2] ], |
05a05fd1 |
50 | %{$and_or_args->{or}}, |
51 | }, |
52 | |
53 | # test modifiers within hashrefs |
54 | { |
55 | where => { -or => [ |
56 | [ foo => 1, bar => 2 ], |
57 | baz => 3, |
58 | ]}, |
59 | %{$and_or_args->{or_or}}, |
60 | }, |
61 | { |
62 | where => { -and => [ |
63 | [ foo => 1, bar => 2 ], |
64 | baz => 3, |
65 | ]}, |
66 | %{$and_or_args->{or_and}}, |
67 | }, |
68 | |
69 | # test modifiers within arrayrefs |
70 | { |
71 | where => [ -or => [ |
72 | [ foo => 1, bar => 2 ], |
73 | baz => 3, |
74 | ]], |
75 | %{$and_or_args->{or_or}}, |
76 | }, |
77 | { |
78 | where => [ -and => [ |
79 | [ foo => 1, bar => 2 ], |
80 | baz => 3, |
81 | ]], |
82 | %{$and_or_args->{or_and}}, |
83 | }, |
84 | |
85 | # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only) |
86 | { |
87 | where => { -and => [ -or => |
88 | [ foo => 1, bar => 2 ], |
89 | baz => 3, |
90 | ]}, |
91 | %{$and_or_args->{or_and}}, |
92 | }, |
93 | { |
94 | where => { -or => [ -and => |
95 | [ foo => 1, bar => 2 ], |
96 | baz => 3, |
97 | ]}, |
98 | %{$and_or_args->{and_or}}, |
99 | }, |
100 | |
101 | # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only) |
102 | { |
103 | where => [ -and => [ -or => |
104 | [ foo => 1, bar => 2 ], |
105 | baz => 3, |
106 | ]], |
107 | %{$and_or_args->{or_and}}, |
108 | }, |
109 | { |
110 | where => [ -or => [ -and => |
111 | [ foo => 1, bar => 2 ], |
112 | baz => 3 |
113 | ]], |
114 | %{$and_or_args->{and_or}}, |
115 | }, |
116 | |
117 | # the -or should affect only the next element |
118 | { |
119 | where => { x => { |
120 | -or => { '!=', 1, '>=', 2 }, -like => 'x%' |
121 | }}, |
122 | stmt => 'WHERE (x != ? OR x >= ?) AND x LIKE ?', |
123 | bind => [qw/1 2 x%/], |
124 | }, |
125 | # the -and should affect only the next element |
126 | { |
127 | where => { x => [ |
128 | -and => [ 1, 2 ], { -like => 'x%' } |
129 | ]}, |
130 | stmt => 'WHERE (x = ? AND x = ?) OR x LIKE ?', |
131 | bind => [qw/1 2 x%/], |
a8b90cb7 |
132 | }, |
133 | { |
134 | where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } }, |
135 | stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?', |
136 | bind => [qw/1 2 3 4 9/], |
137 | }, |
138 | { |
139 | where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } }, |
140 | stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( l = ? OR l = ? OR c = ? OR d = ? ) AND x = ?', |
141 | bind => [qw/1 2 11 12 21 22 3 4 9/], |
142 | }, |
143 | { |
144 | where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } }, |
145 | stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?', |
146 | bind => [qw/3 4 21 22 1 2 11 12 9/], |
147 | }, |
e5360be4 |
148 | |
a8b90cb7 |
149 | { |
05a05fd1 |
150 | # flip logic except where excplicitly requested otherwise |
a8b90cb7 |
151 | args => { logic => 'or' }, |
152 | where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } }, |
153 | stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?', |
154 | bind => [qw/3 4 21 22 1 2 11 12 9/], |
155 | }, |
e5360be4 |
156 | |
a8b90cb7 |
157 | { |
158 | where => [ -or => [a => 1, b => 2], -or => { c => 3, d => 4}, e => 5, -and => [ f => 6, g => 7], [ h => 8, i => 9, -and => [ k => 10, l => 11] ], { m => 12, n => 13 }], |
159 | stmt => 'WHERE a = ? OR b = ? OR c = ? OR d = ? OR e = ? OR ( f = ? AND g = ?) OR h = ? OR i = ? OR ( k = ? AND l = ? ) OR (m = ? AND n = ?)', |
160 | bind => [1 .. 13], |
161 | }, |
162 | { |
05a05fd1 |
163 | # flip logic except where excplicitly requested otherwise |
a8b90cb7 |
164 | args => { logic => 'and' }, |
165 | where => [ -or => [a => 1, b => 2], -or => { c => 3, d => 4}, e => 5, -and => [ f => 6, g => 7], [ h => 8, i => 9, -and => [ k => 10, l => 11] ], { m => 12, n => 13 }], |
166 | stmt => 'WHERE (a = ? OR b = ?) AND (c = ? OR d = ?) AND e = ? AND f = ? AND g = ? AND h = ? AND i = ? AND k = ? AND l = ? AND m = ? AND n = ?', |
167 | bind => [1 .. 13], |
168 | }, |
05a05fd1 |
169 | |
170 | ########## |
171 | # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction) |
172 | # |
173 | |
174 | { |
175 | where => { foo => [ |
176 | -and => [ { -like => 'foo%'}, {'>' => 'moo'} ], |
177 | { -like => '%bar', '<' => 'baz'}, |
178 | [ {-like => '%alpha'}, {-like => '%beta'} ], |
179 | -or => { '!=' => 'toto', '=' => 'koko' } |
180 | ] }, |
181 | stmt => 'WHERE (foo LIKE ? AND foo > ?) OR (foo LIKE ? AND foo < ?) OR (foo LIKE ? OR foo LIKE ?) OR (foo != ? OR foo = ?)', |
182 | bind => [qw/foo% moo %bar baz %alpha %beta toto koko/], |
183 | }, |
184 | { |
185 | where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3 }] ], |
186 | stmt => 'WHERE foo = ? AND bar = ? AND baz = ?', |
187 | bind => [qw/1 2 3/], |
188 | }, |
189 | { |
190 | where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, boz => 4} ] ], |
191 | stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR boz = ?)', |
192 | bind => [1 .. 4], |
193 | }, |
194 | |
195 | # -and affects only the first {} thus a noop |
196 | { |
197 | where => { col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ] }, |
198 | stmt => 'WHERE col < ? OR col > ? OR col != ?', |
199 | bind => [qw/123 456 789/], |
200 | }, |
201 | |
202 | # -and affects the entire inner [], thus 3 ANDs |
203 | { |
204 | where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] }, |
205 | stmt => 'WHERE col < ? AND col > ? AND col != ?', |
206 | bind => [qw/123 456 789/], |
207 | }, |
a8b90cb7 |
208 | ); |
e5360be4 |
209 | |
a8b90cb7 |
210 | my @nest_tests = (); #can not be verified via is_same_sql_bind - need exact matching (parenthesis and all) |
211 | |
212 | my @numbered_tests = (); #need tests making sure warnings are emitted for modifierN (will go away in SQLA2) |
213 | |
214 | plan tests => @and_or_tests * 3; |
215 | |
216 | for my $case (@and_or_tests) { |
e5360be4 |
217 | local $Data::Dumper::Terse = 1; |
a8b90cb7 |
218 | |
219 | my @w; |
220 | local $SIG{__WARN__} = sub { push @w, @_ }; |
221 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
e5360be4 |
222 | lives_ok (sub { |
a8b90cb7 |
223 | my ($stmt, @bind) = $sql->where($case->{where}); |
e5360be4 |
224 | is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind}) |
225 | || diag "Search term:\n" . Dumper $case->{where}; |
226 | }); |
a8b90cb7 |
227 | is (@w, 0, 'No warnings within and-or tests') |
228 | || diag join "\n", 'Emitted warnings:', @w; |
e5360be4 |
229 | } |