From: Peter Rabbitson Date: Tue, 26 Oct 2010 11:59:56 +0000 (+0200) Subject: Extra adjustments of find() documentation and code (as suggested by freetime) X-Git-Tag: v0.08124~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cf48da0c9ecff776e12d1f194c3be24b8079f1f4;p=dbsrgits%2FDBIx-Class.git Extra adjustments of find() documentation and code (as suggested by freetime) --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index aabae51..6dcbbf9 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -439,14 +439,14 @@ which are fully defined by the available condition. If no such constraint is found, C currently defaults to a simple C<< search->(\%column_values) >> which may or may not do what you expect. Note that this fallback behavior may be deprecated in further versions. If -you need to search with arbitrary conditions - use L. +you need to search with arbitrary conditions - use L. If the query +resulting from this fallback produces more than one row, a warning to the +effect is issued, though only the first row is constructed and returned as +C<$row_object>. In addition to C, L recognizes and applies standard L in the same way as L does. -If the resulting query produces more than one row, a warning to the effect is -issued, though only the first row is constructed and returned as C<$row_object> - Note that if you have extra concerns about the correctness of the resulting query you need to specify the C attribute and supply the entire condition as an argument to find (since it is not always possible to perform the @@ -559,7 +559,7 @@ sub find { $final_cond = @unique_queries ? [ map { $self->_qualify_cond_columns($_, $alias) } @unique_queries ] - : $self->_qualify_cond_columns($call_cond, $alias) + : $self->_non_unique_find_fallback ($call_cond, $attrs) ; } @@ -575,6 +575,30 @@ sub find { } } +# This is a stop-gap method as agreed during the discussion on find() cleanup: +# http://lists.scsys.co.uk/pipermail/dbix-class/2010-October/009535.html +# +# It is invoked when find() is called in legacy-mode with insufficiently-unique +# condition. It is provided for overrides until a saner way forward is devised +# +# *NOTE* This is not a public method, and it's *GUARANTEED* to disappear down +# the road. Please adjust your tests accordingly to catch this situation early +# DBIx::Class::ResultSet->can('_non_unique_find_fallback') is reasonable +# +# The method will not be removed without an adequately complete replacement +# for strict-mode enforcement +sub _non_unique_find_fallback { + my ($self, $cond, $attrs) = @_; + + return $self->_qualify_cond_columns( + $cond, + exists $attrs->{alias} + ? $attrs->{alias} + : $self->{attrs}{alias} + ); +} + + sub _qualify_cond_columns { my ($self, $cond, $alias) = @_; @@ -603,7 +627,7 @@ sub _build_unique_cond { $final_cond = { map { $_ => $final_cond->{$_} } @c_cols }; if (my @missing = grep { ! defined $final_cond->{$_} } (@c_cols) ) { - $self->throw_exception( sprintf ( "Unable to satisfy constraint '%s', no values for column(s): %s", + $self->throw_exception( sprintf ( "Unable to satisfy requested constraint '%s', no values for column(s): %s", $constraint_name, join (', ', map { "'$_'" } @missing), ) ); diff --git a/t/61findnot.t b/t/61findnot.t index e7f4249..6d23dc9 100644 --- a/t/61findnot.t +++ b/t/61findnot.t @@ -59,7 +59,7 @@ warnings_exist { => "Non-unique find generated a cursor inexhaustion warning"; throws_ok { $artist_rs->find({}, { key => 'primary' }) -} qr/Unable to satisfy constraint 'primary'/; +} qr/Unable to satisfy requested constraint 'primary'/; $artist_rs = $schema->resultset("Artist")->search({}, { prefetch => 'cds' }); warnings_exist { @@ -67,6 +67,6 @@ warnings_exist { } qr/\QDBIx::Class::ResultSet::find(): Query returned more than one row/, "Non-unique find generated a cursor inexhaustion warning"; throws_ok { $artist_rs->find({}, { key => 'primary' }) -} qr/Unable to satisfy constraint 'primary'/; +} qr/Unable to satisfy requested constraint 'primary'/; done_testing;