use DBIx::Class::ResultSetColumn;
use Scalar::Util qw/blessed weaken/;
use Try::Tiny;
+use Test::Deep::NoTest 'eq_deeply';
# not importing first() as it will clash with our own method
use List::Util ();
sub _stack_cond {
my ($self, $left, $right) = @_;
+
+ # collapse single element top-level conditions
+ # (single pass only, unlikely to need recursion)
+ for ($left, $right) {
+ if (ref $_ eq 'ARRAY') {
+ if (@$_ == 0) {
+ $_ = undef;
+ }
+ elsif (@$_ == 1) {
+ $_ = $_->[0];
+ }
+ }
+ elsif (ref $_ eq 'HASH') {
+ my ($first, $more) = keys %$_;
+
+ # empty hash
+ if (! defined $first) {
+ $_ = undef;
+ }
+ # one element hash
+ elsif (! defined $more) {
+ if ($first eq '-and' and ref $_->{'-and'} eq 'HASH') {
+ $_ = $_->{'-and'};
+ }
+ elsif ($first eq '-or' and ref $_->{'-or'} eq 'ARRAY') {
+ $_ = $_->{'-or'};
+ }
+ }
+ }
+ }
+
+ # merge hashes with weeding out of duplicates (simple cases only)
+ if (ref $left eq 'HASH' and ref $right eq 'HASH') {
+
+ # shallow copy to destroy
+ $right = { %$right };
+ for (grep { exists $right->{$_} } keys %$left) {
+ # the use of eq_deeply here is justified - the rhs of an
+ # expression can contain a lot of twisted weird stuff
+ delete $right->{$_} if eq_deeply( $left->{$_}, $right->{$_} );
+ }
+
+ $right = undef unless keys %$right;
+ }
+
+
if (defined $left xor defined $right) {
return defined $left ? $left : $right;
}
- elsif (defined $left) {
- return { -and => [ map
- { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
- ($left, $right)
- ]};
+ elsif (! defined $left) {
+ return undef;
+ }
+ else {
+ return { -and => [ $left, $right ] };
}
-
- return undef;
}
=head2 search_literal
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+use lib qw(t/lib);
+use DBICTest;
+use DBIC::DebugObj;
+use DBIC::SqlMakerTest;
+
+my $schema = DBICTest->init_schema();
+
+$schema->resultset('Artist')->delete;
+$schema->resultset('CD')->delete;
+
+my $artist = $schema->resultset("Artist")->create({ artistid => 21, name => 'Michael Jackson', rank => 20 });
+my $cd = $artist->create_related('cds', { year => 1975, title => 'Compilation from 1975' });
+
+my ($sql, @bind);
+local $schema->storage->{debug} = 1;
+local $schema->storage->{debugobj} = DBIC::DebugObj->new(\$sql, \@bind);
+
+my $find_cd = $artist->find_related('cds',{title => 'Compilation from 1975'});
+
+s/^'//, s/'\z// for @bind; # why does DBIC::DebugObj not do this?
+
+is_same_sql_bind (
+ $sql,
+ \@bind,
+ 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( ( me.artist = ? AND me.title = ? ) ) ORDER BY year ASC',
+ [21, 'Compilation from 1975'],
+ 'find_related only uses foreign key condition once',
+);
+
+done_testing;