Commit | Line | Data |
8d005ad9 |
1 | use strict; |
2 | use warnings; |
3 | use Test::More; |
4 | use Test::Warn; |
5 | |
6 | use lib qw(t/lib); |
a5a7bb73 |
7 | use DBICTest ':DiffSQL'; |
b5ce6748 |
8 | use DBIx::Class::_Util 'UNRESOLVABLE_CONDITION'; |
8d005ad9 |
9 | |
8d005ad9 |
10 | use Data::Dumper; |
11 | |
12 | my $schema = DBICTest->init_schema( no_deploy => 1); |
13 | my $sm = $schema->storage->sql_maker; |
14 | |
7638636b |
15 | { |
16 | package # hideee |
17 | DBICTest::SillyInt; |
18 | |
19 | use overload |
20 | fallback => 1, |
21 | '0+' => sub { ${$_[0]} }, |
22 | ; |
23 | } |
24 | my $num = bless( \do { my $foo = 69 }, 'DBICTest::SillyInt' ); |
25 | |
26 | is($num, 69, 'test overloaded object is "sane"'); |
27 | is("$num", 69, 'test overloaded object is "sane"'); |
28 | |
8d005ad9 |
29 | for my $t ( |
30 | { |
31 | where => { artistid => 1, charfield => undef }, |
32 | cc_result => { artistid => 1, charfield => undef }, |
33 | sql => 'WHERE artistid = ? AND charfield IS NULL', |
8e40a627 |
34 | efcc_result => { artistid => 1 }, |
35 | efcc_n_result => { artistid => 1, charfield => undef }, |
8d005ad9 |
36 | }, |
37 | { |
38 | where => { -and => [ artistid => 1, charfield => undef, { rank => 13 } ] }, |
39 | cc_result => { artistid => 1, charfield => undef, rank => 13 }, |
40 | sql => 'WHERE artistid = ? AND charfield IS NULL AND rank = ?', |
8e40a627 |
41 | efcc_result => { artistid => 1, rank => 13 }, |
42 | efcc_n_result => { artistid => 1, charfield => undef, rank => 13 }, |
8d005ad9 |
43 | }, |
44 | { |
45 | where => { -and => [ { artistid => 1, charfield => undef}, { rank => 13 } ] }, |
46 | cc_result => { artistid => 1, charfield => undef, rank => 13 }, |
47 | sql => 'WHERE artistid = ? AND charfield IS NULL AND rank = ?', |
8e40a627 |
48 | efcc_result => { artistid => 1, rank => 13 }, |
49 | efcc_n_result => { artistid => 1, charfield => undef, rank => 13 }, |
8d005ad9 |
50 | }, |
51 | { |
52 | where => { -and => [ -or => { name => 'Caterwauler McCrae' }, 'rank' ] }, |
53 | cc_result => { name => 'Caterwauler McCrae', rank => undef }, |
54 | sql => 'WHERE name = ? AND rank IS NULL', |
8e40a627 |
55 | efcc_result => { name => 'Caterwauler McCrae' }, |
56 | efcc_n_result => { name => 'Caterwauler McCrae', rank => undef }, |
8d005ad9 |
57 | }, |
58 | { |
59 | where => { -and => [ [ [ artist => {'=' => \'foo' } ] ], { name => \[ '= ?', 'bar' ] } ] }, |
60 | cc_result => { artist => {'=' => \'foo' }, name => \[ '= ?', 'bar' ] }, |
61 | sql => 'WHERE artist = foo AND name = ?', |
8e40a627 |
62 | efcc_result => { artist => \'foo' }, |
8d005ad9 |
63 | }, |
64 | { |
65 | where => { -and => [ -or => { name => 'Caterwauler McCrae', artistid => 2 } ] }, |
66 | cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] }, |
67 | sql => 'WHERE artistid = ? OR name = ?', |
8e40a627 |
68 | efcc_result => {}, |
69 | }, |
70 | { |
71 | where => { -or => { name => 'Caterwauler McCrae', artistid => 2 } }, |
72 | cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] }, |
73 | sql => 'WHERE artistid = ? OR name = ?', |
74 | efcc_result => {}, |
8d005ad9 |
75 | }, |
76 | { |
7638636b |
77 | where => { -and => [ \'foo=bar', [ { artistid => { '=', $num } } ], { name => 'Caterwauler McCrae'} ] }, |
78 | cc_result => { '' => \'foo=bar', name => 'Caterwauler McCrae', artistid => $num }, |
8d005ad9 |
79 | sql => 'WHERE foo=bar AND artistid = ? AND name = ?', |
8e40a627 |
80 | efcc_result => { name => 'Caterwauler McCrae', artistid => $num }, |
8d005ad9 |
81 | }, |
82 | { |
7638636b |
83 | where => { artistid => [ $num ], rank => [ 13, 2, 3 ], charfield => [ undef ] }, |
84 | cc_result => { artistid => $num, charfield => undef, rank => [13, 2, 3] }, |
8d005ad9 |
85 | sql => 'WHERE artistid = ? AND charfield IS NULL AND ( rank = ? OR rank = ? OR rank = ? )', |
8e40a627 |
86 | efcc_result => { artistid => $num }, |
87 | efcc_n_result => { artistid => $num, charfield => undef }, |
8d005ad9 |
88 | }, |
89 | { |
90 | where => { artistid => { '=' => 1 }, rank => { '>' => 12 }, charfield => { '=' => undef } }, |
91 | cc_result => { artistid => 1, charfield => undef, rank => { '>' => 12 } }, |
92 | sql => 'WHERE artistid = ? AND charfield IS NULL AND rank > ?', |
8e40a627 |
93 | efcc_result => { artistid => 1 }, |
94 | efcc_n_result => { artistid => 1, charfield => undef }, |
8d005ad9 |
95 | }, |
96 | { |
7638636b |
97 | where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [-and => \'1', \['?',2] ] }, rank => { '=' => [ $num, $num ] } }, |
98 | cc_result => { artistid => 1, charfield => [-and => { '=' => \'1' }, { '=' => \['?',2] } ], rank => { '=' => [$num, $num] } }, |
8d005ad9 |
99 | sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )', |
8e40a627 |
100 | efcc_result => { artistid => 1, charfield => UNRESOLVABLE_CONDITION }, |
8d005ad9 |
101 | }, |
102 | { |
103 | where => { -and => [ artistid => 1, artistid => 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => [ -or => { '=', 2 } ], rank => [-and => undef, { '=', undef }, { '!=', 2 } ] }, |
104 | cc_result => { artistid => [ -and => 1, 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => 2, rank => [ -and => undef, undef, { '!=', 2 } ] }, |
105 | sql => 'WHERE artistid = ? AND artistid = ? AND charfield = ? AND name != ? AND name = ? AND rank IS NULL AND rank IS NULL AND rank != ?', |
8e40a627 |
106 | efcc_result => { |
107 | artistid => UNRESOLVABLE_CONDITION, |
108 | name => 2, |
109 | charfield => 2, |
110 | }, |
111 | efcc_n_result => { |
112 | artistid => UNRESOLVABLE_CONDITION, |
113 | name => 2, |
114 | charfield => 2, |
115 | rank => undef, |
116 | }, |
8d005ad9 |
117 | }, |
118 | { |
119 | where => { -and => [ |
120 | [ '_macro.to' => { -like => '%correct%' }, '_wc_macros.to' => { -like => '%correct%' } ], |
121 | { -and => [ { 'group.is_active' => 1 }, { 'me.is_active' => 1 } ] } |
122 | ] }, |
123 | cc_result => { |
124 | 'group.is_active' => 1, |
125 | 'me.is_active' => 1, |
126 | -or => [ |
127 | '_macro.to' => { -like => '%correct%' }, |
128 | '_wc_macros.to' => { -like => '%correct%' }, |
129 | ], |
130 | }, |
131 | sql => 'WHERE ( _macro.to LIKE ? OR _wc_macros.to LIKE ? ) AND group.is_active = ? AND me.is_active = ?', |
8e40a627 |
132 | efcc_result => { 'group.is_active' => 1, 'me.is_active' => 1 }, |
8d005ad9 |
133 | }, |
5f35ba0f |
134 | |
b5ce6748 |
135 | { |
5f35ba0f |
136 | where => { -and => [ |
137 | artistid => { -value => [1] }, |
138 | charfield => { -ident => 'foo' }, |
139 | name => { '=' => { -value => undef } }, |
140 | rank => { '=' => { -ident => 'bar' } }, |
141 | ] }, |
142 | sql => 'WHERE artistid = ? AND charfield = foo AND name IS NULL AND rank = bar', |
143 | cc_result => { |
144 | artistid => { -value => [1] }, |
145 | name => undef, |
146 | charfield => { '=', { -ident => 'foo' } }, |
147 | rank => { '=' => { -ident => 'bar' } }, |
148 | }, |
149 | efcc_result => { |
150 | artistid => [1], |
151 | charfield => { -ident => 'foo' }, |
152 | rank => { -ident => 'bar' }, |
153 | }, |
154 | efcc_n_result => { |
155 | artistid => [1], |
156 | name => undef, |
157 | charfield => { -ident => 'foo' }, |
158 | rank => { -ident => 'bar' }, |
159 | }, |
b5ce6748 |
160 | }, |
5f35ba0f |
161 | |
8d005ad9 |
162 | { |
163 | where => { artistid => [] }, |
164 | cc_result => { artistid => [] }, |
8e40a627 |
165 | efcc_result => {}, |
8d005ad9 |
166 | }, |
167 | (map { |
168 | { |
169 | where => { -and => $_ }, |
170 | cc_result => undef, |
8e40a627 |
171 | efcc_result => {}, |
8d005ad9 |
172 | sql => '', |
173 | }, |
174 | { |
175 | where => { -or => $_ }, |
176 | cc_result => undef, |
8e40a627 |
177 | efcc_result => {}, |
8d005ad9 |
178 | sql => '', |
179 | }, |
180 | } ( |
181 | # bare |
182 | [], {}, |
183 | # singles |
184 | [ {} ], [ [] ], |
185 | # doubles |
186 | [ [], [] ], [ {}, {} ], [ [], {} ], [ {}, [] ], |
187 | # tripples |
188 | [ {}, [], {} ], [ [], {}, [] ] |
189 | )), |
190 | |
191 | # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker |
8e40a627 |
192 | { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => {}, efcc_n_result => {} }, |
8d005ad9 |
193 | |
194 | # batshit insanity, just to be thorough |
195 | { |
196 | where => { -and => [ [ 'artistid' ], [ -and => [ artistid => { '!=', 69 }, artistid => undef, artistid => { '=' => 200 } ]], artistid => [], { -or => [] }, { -and => [] }, [ 'charfield' ], { name => [] }, 'rank' ] }, |
197 | cc_result => { artistid => [ -and => undef, { '!=', 69 }, undef, 200, [] ], charfield => undef, name => [], rank => undef }, |
198 | sql => 'WHERE artistid IS NULL AND artistid != ? AND artistid IS NULL AND artistid = ? AND 0=1 AND charfield IS NULL AND 0=1 AND rank IS NULL', |
8e40a627 |
199 | efcc_result => { artistid => UNRESOLVABLE_CONDITION }, |
200 | efcc_n_result => { artistid => UNRESOLVABLE_CONDITION, charfield => undef, rank => undef }, |
8d005ad9 |
201 | }, |
8acda57c |
202 | |
203 | # original test from RT#93244 |
204 | { |
205 | where => { |
206 | -and => [ |
207 | \[ |
208 | "LOWER(me.title) LIKE ?", |
209 | '%spoon%', |
210 | ], |
211 | [ { 'me.title' => 'Spoonful of bees' } ], |
212 | ]}, |
213 | cc_result => { |
214 | '' => \[ |
215 | "LOWER(me.title) LIKE ?", |
216 | '%spoon%', |
217 | ], |
218 | 'me.title' => 'Spoonful of bees', |
219 | }, |
220 | sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?', |
8e40a627 |
221 | efcc_result => { 'me.title' => 'Spoonful of bees' }, |
8acda57c |
222 | } |
8d005ad9 |
223 | ) { |
224 | |
225 | for my $w ( |
226 | $t->{where}, |
227 | [ -and => $t->{where} ], |
8e40a627 |
228 | ( keys %{$t->{where}} <= 1 ? [ %{$t->{where}} ] : () ), |
229 | ( (keys %{$t->{where}} == 1 and $t->{where}{-or}) |
230 | ? ( ref $t->{where}{-or} eq 'HASH' |
231 | ? [ map { $_ => $t->{where}{-or}{$_} } sort keys %{$t->{where}{-or}} ] |
232 | : $t->{where}{-or} |
233 | ) |
234 | : () |
235 | ), |
8d005ad9 |
236 | ) { |
237 | my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w }; |
238 | |
239 | my @orig_sql_bind = $sm->where($w); |
240 | |
241 | is_same_sql ( $orig_sql_bind[0], $t->{sql}, "Expected SQL from $name" ) |
242 | if exists $t->{sql}; |
243 | |
244 | my $collapsed_cond = $schema->storage->_collapse_cond($w); |
245 | |
246 | is_same_sql_bind( |
247 | \[ $sm->where($collapsed_cond) ], |
248 | \\@orig_sql_bind, |
249 | "Collapse did not alter final SQL based on $name", |
250 | ); |
251 | |
252 | is_deeply( |
253 | $collapsed_cond, |
254 | $t->{cc_result}, |
255 | "Expected collapsed condition produced on $name", |
256 | ); |
257 | |
258 | is_deeply( |
259 | $schema->storage->_extract_fixed_condition_columns($w), |
260 | $t->{efcc_result}, |
261 | "Expected fixed_condition produced on $name", |
262 | ); |
8e40a627 |
263 | |
264 | is_deeply( |
265 | $schema->storage->_extract_fixed_condition_columns($w, 'consider_nulls'), |
266 | $t->{efcc_n_result}, |
267 | "Expected fixed_condition including NULLs produced on $name", |
268 | ) if $t->{efcc_n_result}; |
8d005ad9 |
269 | } |
270 | } |
271 | |
272 | done_testing; |