1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
8 use DBICTest ':DiffSQL';
9 use SQL::Abstract qw(is_plain_value is_literal_value);
10 use List::Util 'shuffle';
12 $Data::Dumper::Terse = 1;
13 $Data::Dumper::Useqq = 1;
14 $Data::Dumper::Indent = 0;
16 my $schema = DBICTest->init_schema();
19 { cond => undef, sql => 'IS NULL' },
20 { cond => { -value => undef }, sql => 'IS NULL' },
21 { cond => \'foo', sql => '= foo' },
22 { cond => 'foo', sql => '= ?', bind => [
23 [ { dbic_colname => "title", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
24 [ { dbic_colname => "year", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
26 { cond => { -value => 'foo' }, sql => '= ?', bind => [
27 [ { dbic_colname => "title", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
28 [ { dbic_colname => "year", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
30 { cond => \[ '?', "foo" ], sql => '= ?', bind => [
35 my $rs = $schema->resultset('CD')->search({}, { columns => 'title' });
37 my $bare_cond = is_literal_value($c->{cond}) ? { '=', $c->{cond} } : $c->{cond};
40 # these are monkey-wrenches, always there
41 { title => { '!=', [ -and => \'bar' ] }, year => { '!=', [ -and => 'bar' ] } },
42 { -or => [ genreid => undef, genreid => { '!=' => \42 } ] },
43 { -or => [ genreid => undef, genreid => { '!=' => \42 } ] },
45 { title => $bare_cond, year => { '=', $c->{cond} } },
46 { -and => [ year => $bare_cond, { title => { '=', $c->{cond} } } ] },
47 [ year => $bare_cond ],
48 [ title => $bare_cond ],
49 { -and => [ { year => { '=', $c->{cond} } }, { title => { '=', $c->{cond} } } ] },
50 { -and => { -or => { year => { '=', $c->{cond} } } }, -or => { title => $bare_cond } },
53 if (my $v = is_plain_value($c->{cond})) {
57 { -and => [ year => $$v, title => $$v ] },
61 @query_steps = shuffle @query_steps;
63 $rs = $rs->search($_) for @query_steps;
65 my @bind = @{$c->{bind} || []};
68 splice @bind, 1, 0, [ { dbic_colname => "year", sqlt_datatype => "varchar", sqlt_size => 100 } => 'bar' ];
77 ( genreid != 42 OR genreid IS NULL )
79 ( genreid != 42 OR genreid IS NULL )
90 'Double condition correctly collapsed for steps' . Dumper \@query_steps,