Commit | Line | Data |
41751122 |
1 | #!/usr/bin/perl |
32eab2da |
2 | |
3 | use strict; |
41751122 |
4 | use warnings; |
5 | use Test::More; |
8a68b5be |
6 | use Test::Exception; |
5aad8cf3 |
7 | use SQL::Abstract::Test import => ['is_same_sql_bind']; |
fffe6900 |
8 | |
ba566dc3 |
9 | plan tests => 18; |
32eab2da |
10 | |
11 | use SQL::Abstract; |
12 | |
13 | # Make sure to test the examples, since having them break is somewhat |
14 | # embarrassing. :-( |
15 | |
ba566dc3 |
16 | my $not_stringifiable = SQLA::NotStringifiable->new(); |
17 | |
32eab2da |
18 | my @handle_tests = ( |
19 | { |
20 | where => { |
21 | requestor => 'inna', |
22 | worker => ['nwiger', 'rcwe', 'sfz'], |
23 | status => { '!=', 'completed' } |
24 | }, |
25 | order => [], |
26 | stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR" |
27 | . " ( worker = ? ) OR ( worker = ? ) ) )", |
28 | bind => [qw/inna completed nwiger rcwe sfz/], |
29 | }, |
30 | |
31 | { |
32 | where => { |
33 | user => 'nwiger', |
34 | status => 'completed' |
35 | }, |
36 | order => [qw/ticket/], |
37 | stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket", |
38 | bind => [qw/completed nwiger/], |
39 | }, |
40 | |
41 | { |
42 | where => { |
43 | user => 'nwiger', |
44 | status => { '!=', 'completed' } |
45 | }, |
46 | order => [qw/ticket/], |
47 | stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket", |
48 | bind => [qw/completed nwiger/], |
49 | }, |
50 | |
51 | { |
52 | where => { |
53 | status => 'completed', |
54 | reportid => { 'in', [567, 2335, 2] } |
55 | }, |
56 | order => [], |
57 | stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )", |
58 | bind => [qw/567 2335 2 completed/], |
59 | }, |
60 | |
61 | { |
62 | where => { |
63 | status => 'completed', |
64 | reportid => { 'not in', [567, 2335, 2] } |
65 | }, |
66 | order => [], |
67 | stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )", |
68 | bind => [qw/567 2335 2 completed/], |
69 | }, |
70 | |
71 | { |
72 | where => { |
73 | status => 'completed', |
74 | completion_date => { 'between', ['2002-10-01', '2003-02-06'] }, |
75 | }, |
76 | order => \'ticket, requestor', |
96449e8e |
77 | #LDNOTE: modified parentheses |
78 | # stmt => " WHERE ( completion_date BETWEEN ? AND ? AND status = ? ) ORDER BY ticket, requestor", |
79 | stmt => " WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor", |
32eab2da |
80 | bind => [qw/2002-10-01 2003-02-06 completed/], |
81 | }, |
82 | |
83 | { |
84 | where => [ |
85 | { |
86 | user => 'nwiger', |
87 | status => { 'in', ['pending', 'dispatched'] }, |
88 | }, |
89 | { |
90 | user => 'robot', |
91 | status => 'unassigned', |
92 | }, |
93 | ], |
94 | order => [], |
95 | stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )", |
96 | bind => [qw/pending dispatched nwiger unassigned robot/], |
97 | }, |
98 | |
99 | { |
100 | where => { |
101 | priority => [ {'>', 3}, {'<', 1} ], |
102 | requestor => \'is not null', |
103 | }, |
104 | order => 'priority', |
105 | stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority", |
106 | bind => [qw/3 1/], |
107 | }, |
108 | |
109 | { |
110 | where => { |
111 | priority => [ {'>', 3}, {'<', 1} ], |
112 | requestor => { '!=', undef }, |
113 | }, |
114 | order => [qw/a b c d e f g/], |
115 | stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )" |
116 | . " ORDER BY a, b, c, d, e, f, g", |
117 | bind => [qw/3 1/], |
118 | }, |
119 | |
120 | { |
121 | where => { |
122 | priority => { 'between', [1, 3] }, |
123 | requestor => { 'like', undef }, |
124 | }, |
125 | order => \'requestor, ticket', |
96449e8e |
126 | #LDNOTE: modified parentheses |
127 | # stmt => " WHERE ( priority BETWEEN ? AND ? AND requestor IS NULL ) ORDER BY requestor, ticket", |
128 | stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket", |
32eab2da |
129 | bind => [qw/1 3/], |
130 | }, |
131 | |
132 | |
133 | { |
134 | where => { |
135 | id => 1, |
136 | num => { |
137 | '<=' => 20, |
138 | '>' => 10, |
139 | }, |
140 | }, |
96449e8e |
141 | # LDNOTE : modified test below, just parentheses differ |
142 | # stmt => " WHERE ( id = ? AND num <= ? AND num > ? )", |
143 | stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )", |
32eab2da |
144 | bind => [qw/1 20 10/], |
145 | }, |
146 | |
147 | { |
148 | where => { foo => {-not_like => [7,8,9]}, |
149 | fum => {'like' => [qw/a b/]}, |
150 | nix => {'between' => [100,200] }, |
151 | nox => {'not between' => [150,160] }, |
152 | wix => {'in' => [qw/zz yy/]}, |
153 | wux => {'not_in' => [qw/30 40/]} |
154 | }, |
96449e8e |
155 | # LDNOTE: modified parentheses for BETWEEN (trivial). |
156 | # Also modified the logic of "not_like" (severe, same reasons as #14 in 00where.t) |
157 | # stmt => " WHERE ( ( ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) ) AND ( ( fum LIKE ? ) OR ( fum LIKE ? ) ) AND nix BETWEEN ? AND ? AND nox NOT BETWEEN ? AND ? AND wix IN ( ?, ? ) AND wux NOT IN ( ?, ? ) )", |
158 | stmt => " WHERE ( ( foo NOT LIKE ? AND foo NOT LIKE ? AND foo NOT LIKE ? ) AND ( ( fum LIKE ? ) OR ( fum LIKE ? ) ) AND ( nix BETWEEN ? AND ? ) AND ( nox NOT BETWEEN ? AND ? ) AND wix IN ( ?, ? ) AND wux NOT IN ( ?, ? ) )", |
32eab2da |
159 | bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'], |
160 | }, |
161 | |
8a68b5be |
162 | { |
163 | where => { |
164 | id => [], |
165 | bar => {'!=' => []}, |
166 | }, |
167 | stmt => " WHERE ( 1=1 AND 0=1 )", |
168 | bind => [], |
169 | }, |
170 | |
96449e8e |
171 | |
172 | { |
173 | where => { |
174 | foo => \["IN (?, ?)", 22, 33], |
175 | bar => [-and => \["> ?", 44], \["< ?", 55] ], |
176 | }, |
177 | stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )", |
178 | bind => [44, 55, 22, 33], |
179 | }, |
180 | |
4b7b6026 |
181 | { |
182 | where => { -and => [{}, { 'me.id' => '1'}] }, |
183 | stmt => " WHERE ( ( me.id = ? ) )", |
184 | bind => [ 1 ], |
185 | }, |
186 | |
fffe6900 |
187 | { |
188 | where => { foo => SQLA::FourtyTwo->new(), }, |
189 | stmt => " WHERE ( foo = ? )", |
190 | bind => [ 'The Life, the Universe and Everything.' ], |
191 | }, |
192 | |
ba566dc3 |
193 | { |
194 | where => { foo => $not_stringifiable, }, |
195 | stmt => " WHERE ( foo = ? )", |
196 | bind => [ $not_stringifiable ], |
197 | }, |
198 | |
4b7b6026 |
199 | |
32eab2da |
200 | ); |
201 | |
8a68b5be |
202 | for my $case (@handle_tests) { |
32eab2da |
203 | my $sql = SQL::Abstract->new; |
8a68b5be |
204 | my($stmt, @bind) = $sql->where($case->{where}, $case->{order}); |
96449e8e |
205 | is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind}) |
32eab2da |
206 | } |
207 | |
8a68b5be |
208 | dies_ok { |
209 | my $sql = SQL::Abstract->new; |
210 | $sql->where({ foo => { '>=' => [] }},); |
fffe6900 |
211 | }; |
212 | |
213 | |
214 | |
215 | #====================================================================== |
216 | package SQLA::FourtyTwo; # testing stringification of arguments |
217 | #====================================================================== |
218 | |
219 | use strict; |
220 | use warnings; |
221 | |
222 | use overload |
223 | '""' => \&to_str; |
224 | |
225 | sub new |
226 | { |
227 | bless {}, shift; |
228 | } |
229 | |
230 | sub to_str |
231 | { |
232 | return "The Life, the Universe and Everything."; |
8a68b5be |
233 | } |
fffe6900 |
234 | |
235 | 1; |
ba566dc3 |
236 | |
237 | |
238 | #====================================================================== |
239 | package SQLA::NotStringifiable; # testing stringification of arguments |
240 | #====================================================================== |
241 | |
242 | use strict; |
243 | use warnings; |
244 | |
245 | sub new |
246 | { |
247 | bless {}, shift; |
248 | } |
249 | |
250 | 1; |