Make sure empty cond collapser works on all positions
[dbsrgits/DBIx-Class.git] / t / sqlmaker / dbihacks_internals.t
CommitLineData
8d005ad9 1use strict;
2use warnings;
3use Test::More;
4use Test::Warn;
5
6use lib qw(t/lib);
a5a7bb73 7use DBICTest ':DiffSQL';
b5ce6748 8use DBIx::Class::_Util 'UNRESOLVABLE_CONDITION';
8d005ad9 9
8d005ad9 10use Data::Dumper;
11
12my $schema = DBICTest->init_schema( no_deploy => 1);
13my $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}
24my $num = bless( \do { my $foo = 69 }, 'DBICTest::SillyInt' );
25
26is($num, 69, 'test overloaded object is "sane"');
27is("$num", 69, 'test overloaded object is "sane"');
28
8d005ad9 29for 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 {
953d5b7d 97 where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [ -AND => \'1', \['?',2] ] }, rank => { '=' => [ -OR => $num, $num ] } },
98 cc_result => { artistid => 1, charfield => [-and => { '=' => \['?',2] }, { '=' => \'1' } ], rank => { '=' => [$num, $num] } },
8d005ad9 99 sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )',
5268b1da 100 collapsed_sql => 'WHERE artistid = ? AND charfield = ? AND charfield = 1 AND ( rank = ? OR rank = ? )',
8e40a627 101 efcc_result => { artistid => 1, charfield => UNRESOLVABLE_CONDITION },
8d005ad9 102 },
103 {
104 where => { -and => [ artistid => 1, artistid => 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => [ -or => { '=', 2 } ], rank => [-and => undef, { '=', undef }, { '!=', 2 } ] },
5268b1da 105 cc_result => { artistid => [ -and => 1, 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => 2, rank => [ -and => { '!=', 2 }, undef ] },
8d005ad9 106 sql => 'WHERE artistid = ? AND artistid = ? AND charfield = ? AND name != ? AND name = ? AND rank IS NULL AND rank IS NULL AND rank != ?',
5268b1da 107 collapsed_sql => 'WHERE artistid = ? AND artistid = ? AND charfield = ? AND name != ? AND name = ? AND rank != ? AND rank IS NULL',
8e40a627 108 efcc_result => {
109 artistid => UNRESOLVABLE_CONDITION,
110 name => 2,
111 charfield => 2,
112 },
113 efcc_n_result => {
114 artistid => UNRESOLVABLE_CONDITION,
115 name => 2,
116 charfield => 2,
117 rank => undef,
118 },
8d005ad9 119 },
120 {
121 where => { -and => [
122 [ '_macro.to' => { -like => '%correct%' }, '_wc_macros.to' => { -like => '%correct%' } ],
123 { -and => [ { 'group.is_active' => 1 }, { 'me.is_active' => 1 } ] }
124 ] },
125 cc_result => {
126 'group.is_active' => 1,
127 'me.is_active' => 1,
128 -or => [
129 '_macro.to' => { -like => '%correct%' },
130 '_wc_macros.to' => { -like => '%correct%' },
131 ],
132 },
133 sql => 'WHERE ( _macro.to LIKE ? OR _wc_macros.to LIKE ? ) AND group.is_active = ? AND me.is_active = ?',
8e40a627 134 efcc_result => { 'group.is_active' => 1, 'me.is_active' => 1 },
8d005ad9 135 },
5f35ba0f 136
b5ce6748 137 {
5f35ba0f 138 where => { -and => [
139 artistid => { -value => [1] },
140 charfield => { -ident => 'foo' },
141 name => { '=' => { -value => undef } },
142 rank => { '=' => { -ident => 'bar' } },
143 ] },
144 sql => 'WHERE artistid = ? AND charfield = foo AND name IS NULL AND rank = bar',
145 cc_result => {
146 artistid => { -value => [1] },
147 name => undef,
148 charfield => { '=', { -ident => 'foo' } },
149 rank => { '=' => { -ident => 'bar' } },
150 },
151 efcc_result => {
152 artistid => [1],
153 charfield => { -ident => 'foo' },
154 rank => { -ident => 'bar' },
155 },
156 efcc_n_result => {
157 artistid => [1],
158 name => undef,
159 charfield => { -ident => 'foo' },
160 rank => { -ident => 'bar' },
161 },
b5ce6748 162 },
5f35ba0f 163
8d005ad9 164 {
165 where => { artistid => [] },
166 cc_result => { artistid => [] },
8e40a627 167 efcc_result => {},
8d005ad9 168 },
169 (map {
170 {
171 where => { -and => $_ },
172 cc_result => undef,
8e40a627 173 efcc_result => {},
8d005ad9 174 sql => '',
175 },
176 {
177 where => { -or => $_ },
178 cc_result => undef,
8e40a627 179 efcc_result => {},
8d005ad9 180 sql => '',
181 },
22485a7e 182 {
183 where => { -or => [ foo => 1, $_ ] },
184 cc_result => { foo => 1 },
185 efcc_result => { foo => 1 },
186 sql => 'WHERE foo = ?',
187 },
188 {
189 where => { -or => [ $_, foo => 1 ] },
190 cc_result => { foo => 1 },
191 efcc_result => { foo => 1 },
192 sql => 'WHERE foo = ?',
193 },
194 {
195 where => { -and => [ fuu => 2, $_, foo => 1 ] },
196 sql => 'WHERE fuu = ? AND foo = ?',
197 collapsed_sql => 'WHERE foo = ? AND fuu = ?',
198 cc_result => { foo => 1, fuu => 2 },
199 efcc_result => { foo => 1, fuu => 2 },
200 },
8d005ad9 201 } (
202 # bare
203 [], {},
204 # singles
205 [ {} ], [ [] ],
206 # doubles
207 [ [], [] ], [ {}, {} ], [ [], {} ], [ {}, [] ],
208 # tripples
209 [ {}, [], {} ], [ [], {}, [] ]
210 )),
211
212 # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker
8e40a627 213 { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => {}, efcc_n_result => {} },
8d005ad9 214
215 # batshit insanity, just to be thorough
216 {
217 where => { -and => [ [ 'artistid' ], [ -and => [ artistid => { '!=', 69 }, artistid => undef, artistid => { '=' => 200 } ]], artistid => [], { -or => [] }, { -and => [] }, [ 'charfield' ], { name => [] }, 'rank' ] },
5268b1da 218 cc_result => { artistid => [ -and => [], { '!=', 69 }, undef, 200 ], charfield => undef, name => [], rank => undef },
8d005ad9 219 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',
5268b1da 220 collapsed_sql => 'WHERE 0=1 AND artistid != ? AND artistid IS NULL AND artistid = ? AND charfield IS NULL AND 0=1 AND rank IS NULL',
8e40a627 221 efcc_result => { artistid => UNRESOLVABLE_CONDITION },
222 efcc_n_result => { artistid => UNRESOLVABLE_CONDITION, charfield => undef, rank => undef },
8d005ad9 223 },
8acda57c 224
225 # original test from RT#93244
226 {
227 where => {
228 -and => [
229 \[
230 "LOWER(me.title) LIKE ?",
231 '%spoon%',
232 ],
233 [ { 'me.title' => 'Spoonful of bees' } ],
234 ]},
235 cc_result => {
236 '' => \[
237 "LOWER(me.title) LIKE ?",
238 '%spoon%',
239 ],
240 'me.title' => 'Spoonful of bees',
241 },
242 sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?',
8e40a627 243 efcc_result => { 'me.title' => 'Spoonful of bees' },
8acda57c 244 }
8d005ad9 245) {
246
247 for my $w (
248 $t->{where},
953d5b7d 249 $t->{where}, # do it twice, make sure we didn't destory the condition
8d005ad9 250 [ -and => $t->{where} ],
953d5b7d 251 [ -AND => $t->{where} ],
252 { -OR => [ -AND => $t->{where} ] },
8e40a627 253 ( keys %{$t->{where}} <= 1 ? [ %{$t->{where}} ] : () ),
254 ( (keys %{$t->{where}} == 1 and $t->{where}{-or})
255 ? ( ref $t->{where}{-or} eq 'HASH'
256 ? [ map { $_ => $t->{where}{-or}{$_} } sort keys %{$t->{where}{-or}} ]
257 : $t->{where}{-or}
258 )
259 : ()
260 ),
8d005ad9 261 ) {
262 my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w };
263
5268b1da 264 my ($generated_sql) = $sm->where($w);
8d005ad9 265
5268b1da 266 is_same_sql ( $generated_sql, $t->{sql}, "Expected SQL from $name" )
8d005ad9 267 if exists $t->{sql};
268
269 my $collapsed_cond = $schema->storage->_collapse_cond($w);
270
5268b1da 271 is_same_sql(
272 ($sm->where($collapsed_cond))[0],
273 ( $t->{collapsed_sql} || $t->{sql} || $generated_sql ),
274 "Collapse did not alter *the semantics* of the final SQL based on $name",
8d005ad9 275 );
276
277 is_deeply(
278 $collapsed_cond,
279 $t->{cc_result},
280 "Expected collapsed condition produced on $name",
281 );
282
283 is_deeply(
284 $schema->storage->_extract_fixed_condition_columns($w),
285 $t->{efcc_result},
286 "Expected fixed_condition produced on $name",
287 );
8e40a627 288
289 is_deeply(
290 $schema->storage->_extract_fixed_condition_columns($w, 'consider_nulls'),
291 $t->{efcc_n_result},
292 "Expected fixed_condition including NULLs produced on $name",
293 ) if $t->{efcc_n_result};
8d005ad9 294 }
295}
296
297done_testing;