From: Justin Guenther Date: Thu, 22 Jun 2006 21:56:56 +0000 (+0000) Subject: changed relationship helpers to only call ensure_class_loaded when the join cond... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fd4df97578076cdb1a82385b21995ed28637e096;p=dbsrgits%2FDBIx-Class-Historic.git changed relationship helpers to only call ensure_class_loaded when the join cond is inferred --- diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index e32dd6d..b871266 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -9,22 +9,30 @@ use warnings; sub belongs_to { my ($class, $rel, $f_class, $cond, $attrs) = @_; - $class->ensure_class_loaded($f_class); # no join condition or just a column name if (!ref $cond) { + $class->ensure_class_loaded($f_class); my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns }; - $class->throw_exception("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}") - if $@; + $class->throw_exception( + "Can't infer join condition for ${rel} on ${class}; ". + "unable to load ${f_class}: $@" + ) if $@; my ($pri, $too_many) = keys %f_primaries; - $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys") - unless defined $pri; - $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary keys") - if $too_many; + $class->throw_exception( + "Can't infer join condition for ${rel} on ${class}; ". + "${f_class} has no primary keys" + ) unless defined $pri; + $class->throw_exception( + "Can't infer join condition for ${rel} on ${class}; ". + "${f_class} has multiple primary keys" + ) if $too_many; my $fk = defined $cond ? $cond : $rel; - $class->throw_exception("Can't infer join condition for ${rel} on ${class}; $fk is not a column") - unless $class->has_column($fk); + $class->throw_exception( + "Can't infer join condition for ${rel} on ${class}; ". + "$fk is not a column of $class" + ) unless $class->has_column($fk); my $acc_type = $class->has_column($rel) ? 'filter' : 'single'; $class->add_relationship($rel, $f_class, @@ -42,14 +50,19 @@ sub belongs_to { } $cond_rel->{"foreign.$_"} = "self.".$cond->{$_}; } - my $acc_type = (keys %$cond_rel == 1 and $class->has_column($rel)) ? 'filter' : 'single'; + my $acc_type = (keys %$cond_rel == 1 and $class->has_column($rel)) + ? 'filter' + : 'single'; $class->add_relationship($rel, $f_class, $cond_rel, { accessor => $acc_type, %{$attrs || {}} } ); } else { - $class->throw_exception('third argument for belongs_to must be undef, a column name, or a join condition'); + $class->throw_exception( + 'third argument for belongs_to must be undef, a column name, '. + 'or a join condition' + ); } return 1; } diff --git a/lib/DBIx/Class/Relationship/HasMany.pm b/lib/DBIx/Class/Relationship/HasMany.pm index eeead26..2c9a3bb 100644 --- a/lib/DBIx/Class/Relationship/HasMany.pm +++ b/lib/DBIx/Class/Relationship/HasMany.pm @@ -7,13 +7,14 @@ use warnings; sub has_many { my ($class, $rel, $f_class, $cond, $attrs) = @_; - $class->ensure_class_loaded($f_class); - unless (ref $cond) { + $class->ensure_class_loaded($f_class); my ($pri, $too_many) = $class->primary_columns; - $class->throw_exception( "has_many can only infer join for a single ". - "primary key; ${class} has more" ) - if $too_many; + + $class->throw_exception( + "has_many can only infer join for a single primary key; ". + "${class} has more" + ) if $too_many; my ($f_key,$guess); if (defined $cond && length $cond) { diff --git a/lib/DBIx/Class/Relationship/HasOne.pm b/lib/DBIx/Class/Relationship/HasOne.pm index aa94a08..568078c 100644 --- a/lib/DBIx/Class/Relationship/HasOne.pm +++ b/lib/DBIx/Class/Relationship/HasOne.pm @@ -14,11 +14,13 @@ sub has_one { sub _has_one { my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_; - $class->ensure_class_loaded($f_class); unless (ref $cond) { + $class->ensure_class_loaded($f_class); my ($pri, $too_many) = $class->primary_columns; - $class->throw_exception( "might_have/has_one can only infer join for a single primary key; ${class} has more" ) - if $too_many; + $class->throw_exception( + "might_have/has_one can only infer join for a single primary key; ". + "${class} has more" + ) if $too_many; my $f_class_loaded = eval { $f_class->columns }; my ($f_key,$guess); if (defined $cond && length $cond) { @@ -29,12 +31,15 @@ sub _has_one { $guess = "using given relationship '$rel' for foreign key"; } else { ($f_key, $too_many) = $f_class->primary_columns; - $class->throw_exception( "might_have/has_one can only infer join for a single primary key; ${f_class} has more" ) - if $too_many; + $class->throw_exception( + "might_have/has_one can only infer join for a single primary key; ". + "${f_class} has more" + ) if $too_many; $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); + $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->add_relationship($rel, $f_class,