7 use DBICTest ':DiffSQL';
8 use DBIx::Class::_Util qw(UNRESOLVABLE_CONDITION modver_gt_or_eq);
12 my $schema = DBICTest->init_schema( no_deploy => 1);
13 my $sm = $schema->storage->sql_maker;
21 '0+' => sub { ${$_[0]} },
24 my $num = bless( \do { my $foo = 69 }, 'DBICTest::SillyInt' );
26 is($num, 69, 'test overloaded object is "sane"');
27 is("$num", 69, 'test overloaded object is "sane"');
31 where => { artistid => 1, charfield => undef },
32 cc_result => { artistid => 1, charfield => undef },
33 sql => 'WHERE artistid = ? AND charfield IS NULL',
34 efcc_result => { artistid => 1 },
35 efcc_n_result => { artistid => 1, charfield => undef },
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 = ?',
41 efcc_result => { artistid => 1, rank => 13 },
42 efcc_n_result => { artistid => 1, charfield => undef, rank => 13 },
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 = ?',
48 efcc_result => { artistid => 1, rank => 13 },
49 efcc_n_result => { artistid => 1, charfield => undef, rank => 13 },
52 where => { -and => [ -or => { name => 'Caterwauler McCrae' }, 'rank' ] },
53 cc_result => { name => 'Caterwauler McCrae', rank => undef },
54 sql => 'WHERE name = ? AND rank IS NULL',
55 efcc_result => { name => 'Caterwauler McCrae' },
56 efcc_n_result => { name => 'Caterwauler McCrae', rank => undef },
59 where => { -and => [ [ [ artist => {'=' => \'foo' } ] ], { name => \[ '= ?', 'bar' ] } ] },
60 cc_result => { artist => {'=' => \'foo' }, name => \[ '= ?', 'bar' ] },
61 sql => 'WHERE artist = foo AND name = ?',
62 efcc_result => { artist => \'foo' },
65 where => { -and => [ -or => { name => 'Caterwauler McCrae', artistid => 2 } ] },
66 cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] },
67 sql => 'WHERE artistid = ? OR name = ?',
71 where => { -or => { name => 'Caterwauler McCrae', artistid => 2 } },
72 cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] },
73 sql => 'WHERE artistid = ? OR name = ?',
77 where => { -and => [ \'foo=bar', [ { artistid => { '=', $num } } ], { name => 'Caterwauler McCrae'} ] },
78 cc_result => { '' => \'foo=bar', name => 'Caterwauler McCrae', artistid => $num },
79 sql => 'WHERE foo=bar AND artistid = ? AND name = ?',
80 efcc_result => { name => 'Caterwauler McCrae', artistid => $num },
83 where => { artistid => [ $num ], rank => [ 13, 2, 3 ], charfield => [ undef ] },
84 cc_result => { artistid => $num, charfield => undef, rank => [13, 2, 3] },
85 sql => 'WHERE artistid = ? AND charfield IS NULL AND ( rank = ? OR rank = ? OR rank = ? )',
86 efcc_result => { artistid => $num },
87 efcc_n_result => { artistid => $num, charfield => undef },
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 > ?',
93 efcc_result => { artistid => 1 },
94 efcc_n_result => { artistid => 1, charfield => undef },
97 where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [-and => \'1', \['?',2] ] }, rank => { '=' => [ $num, $num ] } },
98 cc_result => { artistid => 1, charfield => [-and => { '=' => \'1' }, { '=' => \['?',2] } ], rank => { '=' => [$num, $num] } },
99 sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )',
100 efcc_result => { artistid => 1, charfield => UNRESOLVABLE_CONDITION },
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 != ?',
107 artistid => UNRESOLVABLE_CONDITION,
112 artistid => UNRESOLVABLE_CONDITION,
120 [ '_macro.to' => { -like => '%correct%' }, '_wc_macros.to' => { -like => '%correct%' } ],
121 { -and => [ { 'group.is_active' => 1 }, { 'me.is_active' => 1 } ] }
124 'group.is_active' => 1,
127 '_macro.to' => { -like => '%correct%' },
128 '_wc_macros.to' => { -like => '%correct%' },
131 sql => 'WHERE ( _macro.to LIKE ? OR _wc_macros.to LIKE ? ) AND group.is_active = ? AND me.is_active = ?',
132 efcc_result => { 'group.is_active' => 1, 'me.is_active' => 1 },
135 # need fixed SQLA to correctly work with this
137 ( modver_gt_or_eq('SQL::Abstract', '1.78_01') ? {
139 artistid => { -value => [1] },
140 charfield => { -ident => 'foo' },
141 name => { '=' => { -value => undef } },
142 rank => { '=' => { -ident => 'bar' } },
144 sql => 'WHERE artistid = ? AND charfield = foo AND name IS NULL AND rank = bar',
146 artistid => { -value => [1] },
148 charfield => { '=', { -ident => 'foo' } },
149 rank => { '=' => { -ident => 'bar' } },
153 charfield => { -ident => 'foo' },
154 rank => { -ident => 'bar' },
159 charfield => { -ident => 'foo' },
160 rank => { -ident => 'bar' },
165 where => { artistid => [] },
166 cc_result => { artistid => [] },
171 where => { -and => $_ },
177 where => { -or => $_ },
188 [ [], [] ], [ {}, {} ], [ [], {} ], [ {}, [] ],
190 [ {}, [], {} ], [ [], {}, [] ]
193 # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker
194 { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => {}, efcc_n_result => {} },
196 # batshit insanity, just to be thorough
198 where => { -and => [ [ 'artistid' ], [ -and => [ artistid => { '!=', 69 }, artistid => undef, artistid => { '=' => 200 } ]], artistid => [], { -or => [] }, { -and => [] }, [ 'charfield' ], { name => [] }, 'rank' ] },
199 cc_result => { artistid => [ -and => undef, { '!=', 69 }, undef, 200, [] ], charfield => undef, name => [], rank => undef },
200 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',
201 efcc_result => { artistid => UNRESOLVABLE_CONDITION },
202 efcc_n_result => { artistid => UNRESOLVABLE_CONDITION, charfield => undef, rank => undef },
205 # original test from RT#93244
210 "LOWER(me.title) LIKE ?",
213 [ { 'me.title' => 'Spoonful of bees' } ],
217 "LOWER(me.title) LIKE ?",
220 'me.title' => 'Spoonful of bees',
222 sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?',
223 efcc_result => { 'me.title' => 'Spoonful of bees' },
229 [ -and => $t->{where} ],
230 ( keys %{$t->{where}} <= 1 ? [ %{$t->{where}} ] : () ),
231 ( (keys %{$t->{where}} == 1 and $t->{where}{-or})
232 ? ( ref $t->{where}{-or} eq 'HASH'
233 ? [ map { $_ => $t->{where}{-or}{$_} } sort keys %{$t->{where}{-or}} ]
239 my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w };
241 my @orig_sql_bind = $sm->where($w);
243 is_same_sql ( $orig_sql_bind[0], $t->{sql}, "Expected SQL from $name" )
246 my $collapsed_cond = $schema->storage->_collapse_cond($w);
249 \[ $sm->where($collapsed_cond) ],
251 "Collapse did not alter final SQL based on $name",
257 "Expected collapsed condition produced on $name",
261 $schema->storage->_extract_fixed_condition_columns($w),
263 "Expected fixed_condition produced on $name",
267 $schema->storage->_extract_fixed_condition_columns($w, 'consider_nulls'),
269 "Expected fixed_condition including NULLs produced on $name",
270 ) if $t->{efcc_n_result};