7 use DBICTest ':DiffSQL';
11 my $schema = DBICTest->init_schema( no_deploy => 1);
12 my $sm = $schema->storage->sql_maker;
20 '0+' => sub { ${$_[0]} },
23 my $num = bless( \do { my $foo = 69 }, 'DBICTest::SillyInt' );
25 is($num, 69, 'test overloaded object is "sane"');
26 is("$num", 69, 'test overloaded object is "sane"');
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 )],
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 )],
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 )],
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 )],
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 )],
60 where => { -and => [ -or => { name => 'Caterwauler McCrae', artistid => 2 } ] },
61 cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] },
62 sql => 'WHERE artistid = ? OR name = ?',
66 where => { -and => [ \'foo=bar', [ { artistid => { '=', $num } } ], { name => 'Caterwauler McCrae'} ] },
67 cc_result => { '' => \'foo=bar', name => 'Caterwauler McCrae', artistid => $num },
68 sql => 'WHERE foo=bar AND artistid = ? AND name = ?',
69 efcc_result => [qw( artistid name )],
72 where => { artistid => [ $num ], rank => [ 13, 2, 3 ], charfield => [ undef ] },
73 cc_result => { artistid => $num, charfield => undef, rank => [13, 2, 3] },
74 sql => 'WHERE artistid = ? AND charfield IS NULL AND ( rank = ? OR rank = ? OR rank = ? )',
75 efcc_result => [qw( artistid )],
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 )],
84 where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [-and => \'1', \['?',2] ] }, rank => { '=' => [ $num, $num ] } },
85 cc_result => { artistid => 1, charfield => [-and => { '=' => \'1' }, { '=' => \['?',2] } ], rank => { '=' => [$num, $num] } },
86 sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )',
87 efcc_result => [qw( artistid charfield )],
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 )],
97 [ '_macro.to' => { -like => '%correct%' }, '_wc_macros.to' => { -like => '%correct%' } ],
98 { -and => [ { 'group.is_active' => 1 }, { 'me.is_active' => 1 } ] }
101 'group.is_active' => 1,
104 '_macro.to' => { -like => '%correct%' },
105 '_wc_macros.to' => { -like => '%correct%' },
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 )],
112 where => { artistid => [] },
113 cc_result => { artistid => [] },
118 where => { -and => $_ },
124 where => { -or => $_ },
135 [ [], [] ], [ {}, {} ], [ [], {} ], [ {}, [] ],
137 [ {}, [], {} ], [ [], {}, [] ]
140 # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker
141 { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => [] },
143 # batshit insanity, just to be thorough
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 )],
151 # original test from RT#93244
156 "LOWER(me.title) LIKE ?",
159 [ { 'me.title' => 'Spoonful of bees' } ],
163 "LOWER(me.title) LIKE ?",
166 'me.title' => 'Spoonful of bees',
168 sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?',
169 efcc_result => [qw( me.title )],
175 [ -and => $t->{where} ],
176 ( keys %{$t->{where}} <= 1 ) ? [ %{$t->{where}} ] : ()
178 my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w };
180 my @orig_sql_bind = $sm->where($w);
182 is_same_sql ( $orig_sql_bind[0], $t->{sql}, "Expected SQL from $name" )
185 my $collapsed_cond = $schema->storage->_collapse_cond($w);
188 \[ $sm->where($collapsed_cond) ],
190 "Collapse did not alter final SQL based on $name",
196 "Expected collapsed condition produced on $name",
200 $schema->storage->_extract_fixed_condition_columns($w),
202 "Expected fixed_condition produced on $name",