eval => try where tests for $@ done
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasOne.pm
CommitLineData
c0e7b4e5 1package # hide from PAUSE
2 DBIx::Class::Relationship::HasOne;
22b15c96 3
4use strict;
5use warnings;
dc571b76 6use Carp::Clan qw/^DBIx::Class/;
22b15c96 7
044e70c7 8our %_pod_inherit_config =
9 (
10 class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
11 );
12
07037f89 13sub might_have {
14 shift->_has_one('LEFT' => @_);
15}
16
22b15c96 17sub has_one {
e5d98edd 18 shift->_has_one(undef() => @_);
07037f89 19}
20
21sub _has_one {
503536d5 22 my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
99be059e 23 unless (ref $cond) {
fd4df975 24 $class->ensure_class_loaded($f_class);
e36de82e 25
dc571b76 26 my $pri = $class->_get_primary_key;
e8fb771b 27
e36de82e 28 $class->throw_exception(
29 "might_have/has_one needs a primary key to infer a join; ".
30 "${class} has none"
31 ) if !defined $pri && (!defined $cond || !length $cond);
32
103647d5 33 my $f_class_loaded = eval { $f_class->columns };
dc571b76 34 my ($f_key,$too_many,$guess);
dcf8330b 35 if (defined $cond && length $cond) {
99be059e 36 $f_key = $cond;
dcf8330b 37 $guess = "caller specified foreign key '$f_key'";
103647d5 38 } elsif ($f_class_loaded && $f_class->has_column($rel)) {
07037f89 39 $f_key = $rel;
dcf8330b 40 $guess = "using given relationship '$rel' for foreign key";
07037f89 41 } else {
dc571b76 42 $f_key = $class->_get_primary_key($f_class);
dcf8330b 43 $guess = "using primary key of foreign class for foreign key";
22b15c96 44 }
fd4df975 45 $class->throw_exception(
46 "No such column ${f_key} on foreign class ${f_class} ($guess)"
47 ) if $f_class_loaded && !$f_class->has_column($f_key);
503536d5 48 $cond = { "foreign.${f_key}" => "self.${pri}" };
07037f89 49 }
2339d6c7 50 $class->_validate_has_one_condition($cond);
07037f89 51 $class->add_relationship($rel, $f_class,
52 $cond,
503536d5 53 { accessor => 'single',
07037f89 54 cascade_update => 1, cascade_delete => 1,
55 ($join_type ? ('join_type' => $join_type) : ()),
503536d5 56 %{$attrs || {}} });
07037f89 57 1;
22b15c96 58}
59
dc571b76 60sub _get_primary_key {
61 my ( $class, $target_class ) = @_;
62 $target_class ||= $class;
e8fb771b 63 my ($pri, $too_many) = eval { $target_class->_pri_cols };
64 $class->throw_exception(
a83cdbf2 65 "Can't infer join condition on ${target_class}: $@"
e8fb771b 66 ) if $@;
67
dc571b76 68 $class->throw_exception(
69 "might_have/has_one can only infer join for a single primary key; ".
70 "${class} has more"
71 ) if $too_many;
72 return $pri;
73}
74
2339d6c7 75sub _validate_has_one_condition {
dc571b76 76 my ($class, $cond ) = @_;
77
78 return if $ENV{DBIC_DONT_VALIDATE_RELS};
79 return unless 'HASH' eq ref $cond;
80 foreach my $foreign_id ( keys %$cond ) {
81 my $self_id = $cond->{$foreign_id};
82
83 # we can ignore a bad $self_id because add_relationship handles this
84 # warning
85 return unless $self_id =~ /^self\.(.*)$/;
86 my $key = $1;
2339d6c7 87 $class->throw_exception("Defining rel on ${class} that includes ${key} but no such column defined here yet")
88 unless $class->has_column($key);
dc571b76 89 my $column_info = $class->column_info($key);
90 if ( $column_info->{is_nullable} ) {
416e0bc0 91 carp(qq'"might_have/has_one" must not be on columns with is_nullable set to true ($class/$key). This might indicate an incorrect use of those relationship helpers instead of belongs_to.');
dc571b76 92 }
93 }
94}
95
22b15c96 961;