From: Peter Rabbitson Date: Fri, 13 Sep 2013 12:52:41 +0000 (+0200) Subject: Consolidate single-pk checks from relationship inferrence codepaths X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0b0743afcb6904727aa51cc39fabeea190f5dac6;p=dbsrgits%2FDBIx-Class-Historic.git Consolidate single-pk checks from relationship inferrence codepaths --- diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index 9db06fe..cbc4484 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -26,24 +26,24 @@ sub belongs_to { # no join condition or just a column name if (!ref $cond) { $class->ensure_class_loaded($f_class); - my %f_primaries = map { $_ => 1 } try { $f_class->result_source_instance->_pri_cols_or_die } - catch { - $class->throw_exception( "Can't infer join condition for '$rel' on ${class}: $_"); - }; - my ($pri, $too_many) = keys %f_primaries; - $class->throw_exception( - "Can't infer join condition for '$rel' on ${class}: " - . "${f_class} has multiple primary keys" - ) if $too_many; + my $pri = $f_class->result_source_instance->_single_pri_col_or_die; + + my ($f_key, $guess); + if (defined $cond and length $cond) { + $f_key = $cond; + $guess = "caller specified foreign key '$f_key'"; + } + else { + $f_key = $rel; + $guess = "using given relationship name '$rel' as foreign key column name"; + } - my $fk = defined $cond ? $cond : $rel; $class->throw_exception( - "Can't infer join condition for '$rel' on ${class}: " - . "'$fk' is not a column of $class" - ) unless $class->has_column($fk); + "No such column '$f_key' declared yet on ${class} ($guess)" + ) unless $class->has_column($f_key); - $cond = { "foreign.${pri}" => "self.${fk}" }; + $cond = { "foreign.${pri}" => "self.${f_key}" }; } # explicit join condition diff --git a/lib/DBIx/Class/Relationship/HasMany.pm b/lib/DBIx/Class/Relationship/HasMany.pm index f346997..5707c93 100644 --- a/lib/DBIx/Class/Relationship/HasMany.pm +++ b/lib/DBIx/Class/Relationship/HasMany.pm @@ -16,15 +16,8 @@ sub has_many { unless (ref $cond) { $class->ensure_class_loaded($f_class); - my ($pri, $too_many) = try { $class->result_source_instance->_pri_cols_or_die } - catch { - $class->throw_exception("Can't infer join condition for '$rel' on ${class}: $_"); - }; - $class->throw_exception( - "has_many can only infer join for a single primary key; ". - "${class} has more" - ) if $too_many; + my $pri = $class->result_source_instance->_single_pri_col_or_die; my ($f_key,$guess); if (defined $cond && length $cond) { @@ -33,7 +26,7 @@ sub has_many { } else { $class =~ /([^\:]+)$/; # match is safe - $class can't be '' $f_key = lc $1; # go ahead and guess; best we can do - $guess = "using our class name '$class' as foreign key"; + $guess = "using our class name '$class' as foreign key source"; } my $f_class_loaded = try { $f_class->columns }; diff --git a/lib/DBIx/Class/Relationship/HasOne.pm b/lib/DBIx/Class/Relationship/HasOne.pm index 1281905..57885b3 100644 --- a/lib/DBIx/Class/Relationship/HasOne.pm +++ b/lib/DBIx/Class/Relationship/HasOne.pm @@ -25,28 +25,27 @@ sub _has_one { unless (ref $cond) { $class->ensure_class_loaded($f_class); - my $pri = $class->_get_primary_key; - - $class->throw_exception( - "might_have/has_one needs a primary key to infer a join; ". - "${class} has none" - ) if !defined $pri && (!defined $cond || !length $cond); + my $pri = $class->result_source_instance->_single_pri_col_or_die; my $f_class_loaded = try { $f_class->columns }; - my ($f_key,$too_many,$guess); + my ($f_key,$guess); if (defined $cond && length $cond) { $f_key = $cond; $guess = "caller specified foreign key '$f_key'"; } elsif ($f_class_loaded && $f_class->has_column($rel)) { $f_key = $rel; - $guess = "using given relationship '$rel' for foreign key"; - } else { - $f_key = $class->_get_primary_key($f_class); + $guess = "using given relationship name '$rel' as foreign key column name"; + } elsif ($f_class_loaded and my $f_pri = try { + $f_class->result_source_instance->_single_pri_col_or_die + }) { + $f_key = $f_pri; $guess = "using primary key of foreign class for foreign key"; } + $class->throw_exception( "No such column '$f_key' on foreign class ${f_class} ($guess)" ) if $f_class_loaded && !$f_class->has_column($f_key); + $cond = { "foreign.${f_key}" => "self.${pri}" }; } $class->_validate_has_one_condition($cond); @@ -63,21 +62,6 @@ sub _has_one { 1; } -sub _get_primary_key { - my ( $class, $target_class ) = @_; - $target_class ||= $class; - my ($pri, $too_many) = try { $target_class->result_source_instance->_pri_cols_or_die } - catch { - $class->throw_exception("Can't infer join condition on ${target_class}: $_"); - }; - - $class->throw_exception( - "might_have/has_one can only infer join for a single primary key; ". - "${class} has more" - ) if $too_many; - return $pri; -} - sub _validate_has_one_condition { my ($class, $cond ) = @_; diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 6e8adcc..3233e3a 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -638,6 +638,20 @@ sub _pri_cols_or_die { return @pcols; } +# same as above but mandating single-column PK (used by relationship condition +# inferrence) +sub _single_pri_col_or_die { + my $self = shift; + my ($pri, @too_many) = $self->_pri_cols_or_die; + + $self->throw_exception( sprintf( + "Operation requires a single-column primary key declared on '%s'", + $self->source_name || $self->result_class || $self->name || 'Unknown source...?', + )) if @too_many; + return $pri; +} + + =head2 sequence Manually define the correct sequence for your table, to avoid the overhead