File Coverage

blib/lib/DBIx/Class/Relationship/BelongsTo.pm
Criterion Covered Total %
statement 42 43 97.6
branch 21 24 87.5
condition 8 9 88.8
subroutine 6 7 85.7
pod 0 1 0.0
total 77 84 91.6


line stmt bran cond sub pod time code
1             package # hide from PAUSE
2             DBIx::Class::Relationship::BelongsTo;
3              
4             # Documentation for these methods can be found in
5             # DBIx::Class::Relationship
6              
7 312     312   102432 use strict;
  312         795  
  312         8774  
8 312     312   1787 use warnings;
  312         645  
  312         9965  
9 312     312   1763 use DBIx::Class::_Util qw( dbic_internal_try dbic_internal_catch );
  312         654  
  312         14439  
10 312     312   1902 use namespace::clean;
  312         685  
  312         1876  
11              
12             our %_pod_inherit_config =
13             (
14             class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
15             );
16              
17             sub belongs_to {
18 10952     10952 0 292135 my ($class, $rel, $f_class, $cond, $attrs) = @_;
19              
20             # assume a foreign key constraint unless defined otherwise
21             $attrs->{is_foreign_key_constraint} = 1
22 10952 100       47516 if not exists $attrs->{is_foreign_key_constraint};
23             $attrs->{undef_on_null_fk} = 1
24 10952 100       37495 if not exists $attrs->{undef_on_null_fk};
25              
26             # no join condition or just a column name
27 10952 100       33348 if (!ref $cond) {
28              
29 6777         13944 my ($f_key, $guess);
30 6777 100 66     32287 if (defined $cond and length $cond) {
31 4690         9745 $f_key = $cond;
32 4690         14170 $guess = "caller specified foreign key '$f_key'";
33             }
34             else {
35 2087         4711 $f_key = $rel;
36 2087         7324 $guess = "using given relationship name '$rel' as foreign key column name";
37             }
38              
39 6777 50       40497 $class->throw_exception(
40             "No such column '$f_key' declared yet on ${class} ($guess)"
41             ) unless $class->result_source->has_column($f_key);
42              
43 6777         66418 $class->ensure_class_loaded($f_class);
44             my $f_rsrc = dbic_internal_try {
45 6777     6777   76460 $f_class->result_source;
46             }
47             dbic_internal_catch {
48 0     0   0 $class->throw_exception(
49             "Foreign class '$f_class' does not seem to be a Result class "
50             . "(or it simply did not load entirely due to a circular relation chain): $_"
51             );
52 6777         152686 };
53              
54 6777         64138 my $pri = $f_rsrc->_single_pri_col_or_die;
55              
56 6777         36077 $cond = { "foreign.${pri}" => "self.${f_key}" };
57              
58             }
59             # explicit join condition
60             else {
61 4175 100       16289 if (ref $cond eq 'HASH') { # ARRAY is also valid
62 3130         6144 my $cond_rel;
63             # FIXME This loop is ridiculously incomplete and dangerous
64             # staving off changes until implmentation of the swindon consensus
65 3130         12111 for (keys %$cond) {
66 3130 100       14852 if (m/\./) { # Explicit join condition
67 3129         6813 $cond_rel = $cond;
68 3129         7034 last;
69             }
70 1         5 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
71             }
72 3130         8334 $cond = $cond_rel;
73             }
74             }
75              
76 10952 100 100     146166 my $acc_type = (
77             ref $cond eq 'HASH'
78             and
79             keys %$cond == 1
80             and
81             (keys %$cond)[0] =~ /^foreign\./
82             and
83             $class->result_source->has_column($rel)
84             ) ? 'filter' : 'single';
85              
86             my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
87 10952 50 100     63542 ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
  6777 100       47820  
88             : undef
89             ;
90              
91             $class->add_relationship($rel, $f_class,
92             $cond,
93             {
94             is_depends_on => 1,
95             accessor => $acc_type,
96             $fk_columns ? ( fk_columns => $fk_columns ) : (),
97 10952 100       39192 %{$attrs || {}}
  10952 50       180031  
98             }
99             );
100              
101 10952         45593 return 1;
102             }
103              
104             1;