Fold column_info() into columns_info()
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / HasOne.pm
1 package # hide from PAUSE
2     DBIx::Class::Relationship::HasOne;
3
4 use strict;
5 use warnings;
6 use DBIx::Class::Carp;
7 use Try::Tiny;
8 use DBIx::Class::_Util 'dbic_internal_try';
9 use namespace::clean;
10
11 our %_pod_inherit_config =
12   (
13    class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
14   );
15
16 sub might_have {
17   shift->_has_one('LEFT' => @_);
18 }
19
20 sub has_one {
21   shift->_has_one(undef() => @_);
22 }
23
24 sub _has_one {
25   my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
26   unless (ref $cond) {
27     my $pri = $class->result_source_instance->_single_pri_col_or_die;
28
29     my ($f_key,$guess,$f_rsrc);
30     if (defined $cond && length $cond) {
31       $f_key = $cond;
32       $guess = "caller specified foreign key '$f_key'";
33     }
34     else {
35       # at this point we need to load the foreigner, expensive or not
36       $class->ensure_class_loaded($f_class);
37
38       $f_rsrc = dbic_internal_try {
39         my $r = $f_class->result_source_instance;
40         die "There got to be some columns by now... (exception caught and rewritten by catch below)"
41           unless $r->columns;
42         $r;
43       }
44       catch {
45         $class->throw_exception(
46           "Foreign class '$f_class' does not seem to be a Result class "
47         . "(or it simply did not load entirely due to a circular relation chain)"
48         );
49       };
50
51       if ($f_rsrc->has_column($rel)) {
52         $f_key = $rel;
53         $guess = "using given relationship name '$rel' as foreign key column name";
54       }
55       else {
56         $f_key = $f_rsrc->_single_pri_col_or_die;
57         $guess = "using primary key of foreign class for foreign key";
58       }
59     }
60
61 # FIXME - this check needs to be moved to schema-composition time...
62 #    # only perform checks if the far side was not preloaded above *AND*
63 #    # appears to have been loaded by something else (has a rsrc_instance)
64 #    if (! $f_rsrc and $f_rsrc = dbic_internal_try { $f_class->result_source_instance }) {
65 #      $class->throw_exception(
66 #        "No such column '$f_key' on foreign class ${f_class} ($guess)"
67 #      ) if !$f_rsrc->has_column($f_key);
68 #    }
69
70     $cond = { "foreign.${f_key}" => "self.${pri}" };
71   }
72   $class->_validate_has_one_condition($cond);
73
74   my $default_cascade = ref $cond eq 'CODE' ? 0 : 1;
75
76   $class->add_relationship($rel, $f_class,
77    $cond,
78    { accessor => 'single',
79      cascade_update => $default_cascade,
80      cascade_delete => $default_cascade,
81      is_depends_on => 0,
82      ($join_type ? ('join_type' => $join_type) : ()),
83      %{$attrs || {}} });
84   1;
85 }
86
87 sub _validate_has_one_condition {
88   my ($class, $cond )  = @_;
89
90   return if $ENV{DBIC_DONT_VALIDATE_RELS};
91   return unless 'HASH' eq ref $cond;
92   foreach my $foreign_id ( keys %$cond ) {
93     my $self_id = $cond->{$foreign_id};
94
95     # we can ignore a bad $self_id because add_relationship handles this
96     # exception
97     return unless $self_id =~ /^self\.(.*)$/;
98
99     my $key = $1;
100
101     my $column_info = $class->result_source->columns_info->{$key}
102       or $class->throw_exception(
103         "Defining rel on ${class} that includes '$key' "
104       . 'but no such column defined there yet'
105       );
106
107     carp(
108       "'might_have'/'has_one' must not be used on columns with is_nullable "
109     . "set to true ($class/$key). This almost certainly indicates an "
110     . "incorrect use of these relationship helpers instead of 'belongs_to'"
111     ) if $column_info->{is_nullable};
112   }
113 }
114
115 1;