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'; |
8d005ad9 |
8 | |
8d005ad9 |
9 | use Data::Dumper; |
10 | |
11 | my $schema = DBICTest->init_schema( no_deploy => 1); |
12 | my $sm = $schema->storage->sql_maker; |
13 | |
7638636b |
14 | { |
15 | package # hideee |
16 | DBICTest::SillyInt; |
17 | |
18 | use overload |
19 | fallback => 1, |
20 | '0+' => sub { ${$_[0]} }, |
21 | ; |
22 | } |
23 | my $num = bless( \do { my $foo = 69 }, 'DBICTest::SillyInt' ); |
24 | |
25 | is($num, 69, 'test overloaded object is "sane"'); |
26 | is("$num", 69, 'test overloaded object is "sane"'); |
27 | |
8d005ad9 |
28 | for my $t ( |
29 | { |
30 | where => { artistid => 1, charfield => undef }, |
31 | cc_result => { artistid => 1, charfield => undef }, |
32 | sql => 'WHERE artistid = ? AND charfield IS NULL', |
33 | efcc_result => [qw( artistid )], |
34 | }, |
35 | { |
36 | where => { -and => [ artistid => 1, charfield => undef, { rank => 13 } ] }, |
37 | cc_result => { artistid => 1, charfield => undef, rank => 13 }, |
38 | sql => 'WHERE artistid = ? AND charfield IS NULL AND rank = ?', |
39 | efcc_result => [qw( artistid rank )], |
40 | }, |
41 | { |
42 | where => { -and => [ { artistid => 1, charfield => undef}, { rank => 13 } ] }, |
43 | cc_result => { artistid => 1, charfield => undef, rank => 13 }, |
44 | sql => 'WHERE artistid = ? AND charfield IS NULL AND rank = ?', |
45 | efcc_result => [qw( artistid rank )], |
46 | }, |
47 | { |
48 | where => { -and => [ -or => { name => 'Caterwauler McCrae' }, 'rank' ] }, |
49 | cc_result => { name => 'Caterwauler McCrae', rank => undef }, |
50 | sql => 'WHERE name = ? AND rank IS NULL', |
51 | efcc_result => [qw( name )], |
52 | }, |
53 | { |
54 | where => { -and => [ [ [ artist => {'=' => \'foo' } ] ], { name => \[ '= ?', 'bar' ] } ] }, |
55 | cc_result => { artist => {'=' => \'foo' }, name => \[ '= ?', 'bar' ] }, |
56 | sql => 'WHERE artist = foo AND name = ?', |
57 | efcc_result => [qw( artist )], |
58 | }, |
59 | { |
60 | where => { -and => [ -or => { name => 'Caterwauler McCrae', artistid => 2 } ] }, |
61 | cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] }, |
62 | sql => 'WHERE artistid = ? OR name = ?', |
63 | efcc_result => [], |
64 | }, |
65 | { |
7638636b |
66 | where => { -and => [ \'foo=bar', [ { artistid => { '=', $num } } ], { name => 'Caterwauler McCrae'} ] }, |
67 | cc_result => { '' => \'foo=bar', name => 'Caterwauler McCrae', artistid => $num }, |
8d005ad9 |
68 | sql => 'WHERE foo=bar AND artistid = ? AND name = ?', |
69 | efcc_result => [qw( artistid name )], |
70 | }, |
71 | { |
7638636b |
72 | where => { artistid => [ $num ], rank => [ 13, 2, 3 ], charfield => [ undef ] }, |
73 | cc_result => { artistid => $num, charfield => undef, rank => [13, 2, 3] }, |
8d005ad9 |
74 | sql => 'WHERE artistid = ? AND charfield IS NULL AND ( rank = ? OR rank = ? OR rank = ? )', |
75 | efcc_result => [qw( artistid )], |
76 | }, |
77 | { |
78 | where => { artistid => { '=' => 1 }, rank => { '>' => 12 }, charfield => { '=' => undef } }, |
79 | cc_result => { artistid => 1, charfield => undef, rank => { '>' => 12 } }, |
80 | sql => 'WHERE artistid = ? AND charfield IS NULL AND rank > ?', |
81 | efcc_result => [qw( artistid )], |
82 | }, |
83 | { |
7638636b |
84 | where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [-and => \'1', \['?',2] ] }, rank => { '=' => [ $num, $num ] } }, |
85 | cc_result => { artistid => 1, charfield => [-and => { '=' => \'1' }, { '=' => \['?',2] } ], rank => { '=' => [$num, $num] } }, |
8d005ad9 |
86 | sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )', |
87 | efcc_result => [qw( artistid charfield )], |
88 | }, |
89 | { |
90 | where => { -and => [ artistid => 1, artistid => 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => [ -or => { '=', 2 } ], rank => [-and => undef, { '=', undef }, { '!=', 2 } ] }, |
91 | cc_result => { artistid => [ -and => 1, 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => 2, rank => [ -and => undef, undef, { '!=', 2 } ] }, |
92 | sql => 'WHERE artistid = ? AND artistid = ? AND charfield = ? AND name != ? AND name = ? AND rank IS NULL AND rank IS NULL AND rank != ?', |
93 | efcc_result => [qw( artistid charfield name )], |
94 | }, |
95 | { |
96 | where => { -and => [ |
97 | [ '_macro.to' => { -like => '%correct%' }, '_wc_macros.to' => { -like => '%correct%' } ], |
98 | { -and => [ { 'group.is_active' => 1 }, { 'me.is_active' => 1 } ] } |
99 | ] }, |
100 | cc_result => { |
101 | 'group.is_active' => 1, |
102 | 'me.is_active' => 1, |
103 | -or => [ |
104 | '_macro.to' => { -like => '%correct%' }, |
105 | '_wc_macros.to' => { -like => '%correct%' }, |
106 | ], |
107 | }, |
108 | sql => 'WHERE ( _macro.to LIKE ? OR _wc_macros.to LIKE ? ) AND group.is_active = ? AND me.is_active = ?', |
109 | efcc_result => [qw( group.is_active me.is_active )], |
110 | }, |
111 | { |
112 | where => { artistid => [] }, |
113 | cc_result => { artistid => [] }, |
114 | efcc_result => [], |
115 | }, |
116 | (map { |
117 | { |
118 | where => { -and => $_ }, |
119 | cc_result => undef, |
120 | efcc_result => [], |
121 | sql => '', |
122 | }, |
123 | { |
124 | where => { -or => $_ }, |
125 | cc_result => undef, |
126 | efcc_result => [], |
127 | sql => '', |
128 | }, |
129 | } ( |
130 | # bare |
131 | [], {}, |
132 | # singles |
133 | [ {} ], [ [] ], |
134 | # doubles |
135 | [ [], [] ], [ {}, {} ], [ [], {} ], [ {}, [] ], |
136 | # tripples |
137 | [ {}, [], {} ], [ [], {}, [] ] |
138 | )), |
139 | |
140 | # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker |
141 | { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => [] }, |
142 | |
143 | # batshit insanity, just to be thorough |
144 | { |
145 | where => { -and => [ [ 'artistid' ], [ -and => [ artistid => { '!=', 69 }, artistid => undef, artistid => { '=' => 200 } ]], artistid => [], { -or => [] }, { -and => [] }, [ 'charfield' ], { name => [] }, 'rank' ] }, |
146 | cc_result => { artistid => [ -and => undef, { '!=', 69 }, undef, 200, [] ], charfield => undef, name => [], rank => undef }, |
147 | 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', |
148 | efcc_result => [qw( artistid )], |
149 | }, |
8acda57c |
150 | |
151 | # original test from RT#93244 |
152 | { |
153 | where => { |
154 | -and => [ |
155 | \[ |
156 | "LOWER(me.title) LIKE ?", |
157 | '%spoon%', |
158 | ], |
159 | [ { 'me.title' => 'Spoonful of bees' } ], |
160 | ]}, |
161 | cc_result => { |
162 | '' => \[ |
163 | "LOWER(me.title) LIKE ?", |
164 | '%spoon%', |
165 | ], |
166 | 'me.title' => 'Spoonful of bees', |
167 | }, |
168 | sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?', |
169 | efcc_result => [qw( me.title )], |
170 | } |
8d005ad9 |
171 | ) { |
172 | |
173 | for my $w ( |
174 | $t->{where}, |
175 | [ -and => $t->{where} ], |
176 | ( keys %{$t->{where}} <= 1 ) ? [ %{$t->{where}} ] : () |
177 | ) { |
178 | my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w }; |
179 | |
180 | my @orig_sql_bind = $sm->where($w); |
181 | |
182 | is_same_sql ( $orig_sql_bind[0], $t->{sql}, "Expected SQL from $name" ) |
183 | if exists $t->{sql}; |
184 | |
185 | my $collapsed_cond = $schema->storage->_collapse_cond($w); |
186 | |
187 | is_same_sql_bind( |
188 | \[ $sm->where($collapsed_cond) ], |
189 | \\@orig_sql_bind, |
190 | "Collapse did not alter final SQL based on $name", |
191 | ); |
192 | |
193 | is_deeply( |
194 | $collapsed_cond, |
195 | $t->{cc_result}, |
196 | "Expected collapsed condition produced on $name", |
197 | ); |
198 | |
199 | is_deeply( |
200 | $schema->storage->_extract_fixed_condition_columns($w), |
201 | $t->{efcc_result}, |
202 | "Expected fixed_condition produced on $name", |
203 | ); |
204 | } |
205 | } |
206 | |
207 | done_testing; |