Update to add myself to contributors and to hide Modules from the PAUSE Indexer.
[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
7 sub might_have {
8   shift->_has_one('LEFT' => @_);
9 }
10
11 sub has_one {
12   shift->_has_one(undef() => @_);
13 }
14
15 sub _has_one {
16   my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
17   eval "require $f_class";
18   if ($@) {
19     $class->throw_exception($@) unless $@ =~ /Can't locate/;
20   }
21
22   unless (ref $cond) {
23     my ($pri, $too_many) = $class->primary_columns;
24     $class->throw_exception( "might_have/has_one can only infer join for a single primary key; ${class} has more" )
25       if $too_many;
26     my $f_class_loaded = eval { $f_class->columns };
27     my ($f_key,$guess);
28     if (defined $cond && length $cond) {
29       $f_key = $cond;
30       $guess = "caller specified foreign key '$f_key'";
31     } elsif ($f_class_loaded && $f_class->has_column($rel)) {
32       $f_key = $rel;
33       $guess = "using given relationship '$rel' for foreign key";
34     } else {
35       ($f_key, $too_many) = $f_class->primary_columns;
36       $class->throw_exception( "might_have/has_one can only infer join for a single primary key; ${f_class} has more" )
37         if $too_many;
38       $guess = "using primary key of foreign class for foreign key";
39     }
40     $class->throw_exception("No such column ${f_key} on foreign class ${f_class} ($guess)")
41       if $f_class_loaded && !$f_class->has_column($f_key);
42     $cond = { "foreign.${f_key}" => "self.${pri}" };
43   }
44   $class->add_relationship($rel, $f_class,
45    $cond,
46    { accessor => 'single',
47      cascade_update => 1, cascade_delete => 1,
48      ($join_type ? ('join_type' => $join_type) : ()),
49      %{$attrs || {}} });
50   1;
51 }
52
53 1;