From: Peter Rabbitson Date: Mon, 7 Sep 2009 14:46:14 +0000 (+0000) Subject: Prune duplicate constraints from the find() condition X-Git-Tag: v0.08112~40 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=246fa39d71feecdc126a39d916f34a28c1a554d5;p=dbsrgits%2FDBIx-Class.git Prune duplicate constraints from the find() condition --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 9bbeb73..d528023 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -570,12 +570,16 @@ sub _unique_queries { my $where = $self->_collapse_cond($self->{attrs}{where} || {}); my $num_where = scalar keys %$where; - my @unique_queries; + my (@unique_queries, %seen_column_combinations); foreach my $name (@constraint_names) { - my @unique_cols = $self->result_source->unique_constraint_columns($name); - my $unique_query = $self->_build_unique_query($query, \@unique_cols); + my @constraint_cols = $self->result_source->unique_constraint_columns($name); - my $num_cols = scalar @unique_cols; + my $constraint_sig = join "\x00", sort @constraint_cols; + next if $seen_column_combinations{$constraint_sig}++; + + my $unique_query = $self->_build_unique_query($query, \@constraint_cols); + + my $num_cols = scalar @constraint_cols; my $num_query = scalar keys %$unique_query; my $total = $num_query + $num_where; diff --git a/t/80unique.t b/t/80unique.t index 8bd7e2c..2245511 100644 --- a/t/80unique.t +++ b/t/80unique.t @@ -1,14 +1,14 @@ use strict; -use warnings; +use warnings; use Test::More; use lib qw(t/lib); use DBICTest; +use DBIC::SqlMakerTest; +use DBIC::DebugObj; my $schema = DBICTest->init_schema(); -plan tests => 49; - # Check the defined unique constraints is_deeply( [ sort $schema->source('CD')->unique_constraint_names ], @@ -209,4 +209,27 @@ is($row->baz, 3, 'baz is correct'); ); ok($cd2->in_storage, 'Updating year using update_or_new was successful'); is($cd2->id, $cd1->id, 'Got the same CD using update_or_new'); -} \ No newline at end of file +} + +# make sure the ident condition is assembled sanely +{ + my $artist = $schema->resultset('Artist')->next; + + my ($sql, @bind); + $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)), + $schema->storage->debug(1); + + $artist->discard_changes; + + is_same_sql_bind ( + $sql, + \@bind, + 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?', + [qw/'1'/], + ); + + $schema->storage->debug(0); + $schema->storage->debugobj(undef); +} + +done_testing; diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index 2745c03..56c1191 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -30,6 +30,7 @@ __PACKAGE__->add_columns( }, ); __PACKAGE__->set_primary_key('artistid'); +__PACKAGE__->add_unique_constraint(['artistid']); # do not remove, part of a test __PACKAGE__->mk_classdata('field_name_for', { artistid => 'primary key',