9 use DBIC::SqlMakerTest;
12 my $schema = DBICTest->init_schema( no_deploy => 1);
13 my $sm = $schema->storage->sql_maker;
17 where => { artistid => 1, charfield => undef },
18 cc_result => { artistid => 1, charfield => undef },
19 sql => 'WHERE artistid = ? AND charfield IS NULL',
20 efcc_result => [qw( artistid )],
23 where => { -and => [ artistid => 1, charfield => undef, { rank => 13 } ] },
24 cc_result => { artistid => 1, charfield => undef, rank => 13 },
25 sql => 'WHERE artistid = ? AND charfield IS NULL AND rank = ?',
26 efcc_result => [qw( artistid rank )],
29 where => { -and => [ { artistid => 1, charfield => undef}, { rank => 13 } ] },
30 cc_result => { artistid => 1, charfield => undef, rank => 13 },
31 sql => 'WHERE artistid = ? AND charfield IS NULL AND rank = ?',
32 efcc_result => [qw( artistid rank )],
35 where => { -and => [ -or => { name => 'Caterwauler McCrae' }, 'rank' ] },
36 cc_result => { name => 'Caterwauler McCrae', rank => undef },
37 sql => 'WHERE name = ? AND rank IS NULL',
38 efcc_result => [qw( name )],
41 where => { -and => [ [ [ artist => {'=' => \'foo' } ] ], { name => \[ '= ?', 'bar' ] } ] },
42 cc_result => { artist => {'=' => \'foo' }, name => \[ '= ?', 'bar' ] },
43 sql => 'WHERE artist = foo AND name = ?',
44 efcc_result => [qw( artist )],
47 where => { -and => [ -or => { name => 'Caterwauler McCrae', artistid => 2 } ] },
48 cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] },
49 sql => 'WHERE artistid = ? OR name = ?',
53 where => { -and => [ \'foo=bar', [ { artistid => { '=', 3 } } ], { name => 'Caterwauler McCrae'} ] },
54 cc_result => { '' => \'foo=bar', name => 'Caterwauler McCrae', artistid => 3 },
55 sql => 'WHERE foo=bar AND artistid = ? AND name = ?',
56 efcc_result => [qw( artistid name )],
59 where => { artistid => [ 1 ], rank => [ 13, 2, 3 ], charfield => [ undef ] },
60 cc_result => { artistid => 1, charfield => undef, rank => [13, 2, 3] },
61 sql => 'WHERE artistid = ? AND charfield IS NULL AND ( rank = ? OR rank = ? OR rank = ? )',
62 efcc_result => [qw( artistid )],
65 where => { artistid => { '=' => 1 }, rank => { '>' => 12 }, charfield => { '=' => undef } },
66 cc_result => { artistid => 1, charfield => undef, rank => { '>' => 12 } },
67 sql => 'WHERE artistid = ? AND charfield IS NULL AND rank > ?',
68 efcc_result => [qw( artistid )],
71 where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [-and => \'1', \['?',2] ] }, rank => { '=' => [ 1, 2 ] } },
72 cc_result => { artistid => 1, charfield => [-and => { '=' => \'1' }, { '=' => \['?',2] } ], rank => { '=' => [1, 2] } },
73 sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )',
74 efcc_result => [qw( artistid charfield )],
77 where => { -and => [ artistid => 1, artistid => 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => [ -or => { '=', 2 } ], rank => [-and => undef, { '=', undef }, { '!=', 2 } ] },
78 cc_result => { artistid => [ -and => 1, 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => 2, rank => [ -and => undef, undef, { '!=', 2 } ] },
79 sql => 'WHERE artistid = ? AND artistid = ? AND charfield = ? AND name != ? AND name = ? AND rank IS NULL AND rank IS NULL AND rank != ?',
80 efcc_result => [qw( artistid charfield name )],
84 [ '_macro.to' => { -like => '%correct%' }, '_wc_macros.to' => { -like => '%correct%' } ],
85 { -and => [ { 'group.is_active' => 1 }, { 'me.is_active' => 1 } ] }
88 'group.is_active' => 1,
91 '_macro.to' => { -like => '%correct%' },
92 '_wc_macros.to' => { -like => '%correct%' },
95 sql => 'WHERE ( _macro.to LIKE ? OR _wc_macros.to LIKE ? ) AND group.is_active = ? AND me.is_active = ?',
96 efcc_result => [qw( group.is_active me.is_active )],
99 where => { artistid => [] },
100 cc_result => { artistid => [] },
105 where => { -and => $_ },
111 where => { -or => $_ },
122 [ [], [] ], [ {}, {} ], [ [], {} ], [ {}, [] ],
124 [ {}, [], {} ], [ [], {}, [] ]
127 # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker
128 { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => [] },
130 # batshit insanity, just to be thorough
132 where => { -and => [ [ 'artistid' ], [ -and => [ artistid => { '!=', 69 }, artistid => undef, artistid => { '=' => 200 } ]], artistid => [], { -or => [] }, { -and => [] }, [ 'charfield' ], { name => [] }, 'rank' ] },
133 cc_result => { artistid => [ -and => undef, { '!=', 69 }, undef, 200, [] ], charfield => undef, name => [], rank => undef },
134 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',
135 efcc_result => [qw( artistid )],
138 # original test from RT#93244
143 "LOWER(me.title) LIKE ?",
146 [ { 'me.title' => 'Spoonful of bees' } ],
150 "LOWER(me.title) LIKE ?",
153 'me.title' => 'Spoonful of bees',
155 sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?',
156 efcc_result => [qw( me.title )],
162 [ -and => $t->{where} ],
163 ( keys %{$t->{where}} <= 1 ) ? [ %{$t->{where}} ] : ()
165 my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w };
167 my @orig_sql_bind = $sm->where($w);
169 is_same_sql ( $orig_sql_bind[0], $t->{sql}, "Expected SQL from $name" )
172 my $collapsed_cond = $schema->storage->_collapse_cond($w);
175 \[ $sm->where($collapsed_cond) ],
177 "Collapse did not alter final SQL based on $name",
183 "Expected collapsed condition produced on $name",
187 $schema->storage->_extract_fixed_condition_columns($w),
189 "Expected fixed_condition produced on $name",