From: Peter Rabbitson Date: Fri, 15 Oct 2010 20:53:14 +0000 (+0200) Subject: Remove support for find( col1 => $val1, col2 => $val2, ... ) X-Git-Tag: v0.08124~37^2~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=49ca473e;p=dbsrgits%2FDBIx-Class.git Remove support for find( col1 => $val1, col2 => $val2, ... ) --- diff --git a/Changes b/Changes index 6ddd10a..d724473 100644 --- a/Changes +++ b/Changes @@ -22,6 +22,8 @@ Revision history for DBIx::Class at instantiation time - New documentation map organized by features (DBIx::Class::Manual::Features) + - find( col1 => $val1, col2 => $val2, ... ) is no longer supported + (it has been in deprecated state for more than 4 years) * Fixes - Fix memory leak during populate() on 5.8.x perls diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 6c4c976..9988227 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -471,47 +471,49 @@ sub find { my $self = shift; my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}); - # Default to the primary key, but allow a specific key - my @cols = exists $attrs->{key} - ? $self->result_source->unique_constraint_columns($attrs->{key}) - : $self->result_source->primary_columns; - $self->throw_exception( - "Can't find unless a primary key is defined or unique constraint is specified" - ) unless @cols; - - # Parse out a hashref from input + # Parse out a query from input my $input_query; if (ref $_[0] eq 'HASH') { $input_query = { %{$_[0]} }; } - elsif (@_ == @cols) { - $input_query = {}; - @{$input_query}{@cols} = @_; - } else { - # Compatibility: Allow e.g. find(id => $value) - carp "Find by key => value deprecated; please use a hashref instead"; - $input_query = {@_}; - } + my $constraint = exists $attrs->{key} ? $attrs->{key} : 'primary'; + my @c_cols = $self->result_source->unique_constraint_columns($constraint); - my (%related, $info); + $self->throw_exception( + "No constraint columns, maybe a malformed '$constraint' constraint?" + ) unless @c_cols; + + $self->throw_exception ( + 'find() expects either a column/value hashref, or a list of values ' + . "corresponding to the columns of the specified unique constraint '$constraint'" + ) unless @c_cols == @_; - KEY: foreach my $key (keys %$input_query) { - if (ref($input_query->{$key}) - && ($info = $self->result_source->relationship_info($key))) { + $input_query = {}; + @{$input_query}{@c_cols} = @_; + } + + my %related; + for my $key (keys %$input_query) { + if ( + my $keyref = ref($input_query->{$key}) + and + my $relinfo = $self->result_source->relationship_info($key) + ) { my $val = delete $input_query->{$key}; - next KEY if (ref($val) eq 'ARRAY'); # has_many for multi_create + + next if $keyref eq 'ARRAY'; # has_many for multi_create + my $rel_q = $self->result_source->_resolve_condition( - $info->{cond}, $val, $key - ); - die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY'; + $relinfo->{cond}, $val, $key + ); + die "Can't handle complex relationship conditions in find" if ref($rel_q) ne 'HASH'; @related{keys %$rel_q} = values %$rel_q; } } - if (my @keys = keys %related) { - @{$input_query}{@keys} = values %related; - } + # relationship conditions take precedence (?) + @{$input_query}{keys %related} = values %related; # Build the final query: Default to the disjunction of the unique queries, # but allow the input query in case the ResultSet defines the query or the diff --git a/t/60core.t b/t/60core.t index 215c033..f063cb3 100644 --- a/t/60core.t +++ b/t/60core.t @@ -122,16 +122,10 @@ is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id gener $artist->delete; } -# Test backwards compatibility -{ - my $warnings = ''; - local $SIG{__WARN__} = sub { $warnings .= $_[0] }; - - my $artist_by_hash = $schema->resultset('Artist')->find(artistid => 4); - is($artist_by_hash->name, 'Man With A Spoon', 'Retrieved correctly'); - is($artist_by_hash->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly'); - like($warnings, qr/deprecated/, 'warned about deprecated find usage'); -} +# this has been warning for 4 years, killing +throws_ok { + $schema->resultset('Artist')->find(artistid => 4); +} qr|expects either a column/value hashref, or a list of values corresponding to the columns of the specified unique constraint|; is($schema->resultset("Artist")->count, 4, 'count ok');