DBIx/Class/Relationship/BelongsTo.pm

filter: Statementstmt 97.7% 44 / 45 Branchbran 90.9% 20 / 22 Conditioncond 82.3% 14 / 17 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 89.1% 90 / 101 CC 19 SCAR 27.0
CC
19
Cov
93%
belongs_to
19.2
__ANON__
2.0
BEGIN
1.0
CRAP 14.9
« DBIx/Class/InflateColumn/DateTime.pm ↑ Summary DBIx/Class/Row.pm »
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 379 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 379 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 379 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 379 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 379 our %_pod_inherit_config =
13 (
14 class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
15 );
16  
17 13768 ▶sub belongs_to {
Subroutine belongs_to: called CC=19 SCAR=29.6
Pod undocumented
18 13768 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 13768 ▶ if not exists $attrs->{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"is_foreign_key_constraint"} 13443 325 13768
23 $attrs->{undef_on_null_fk} = 1
24 13768 ▶ if not exists $attrs->{undef_on_null_fk};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"undef_on_null_fk"} 13432 336 13768
25  
26 # no join condition or just a column name
27 13768 ▶ if (!ref $cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $cond) { } 8519 5249 13768
28  
29 8519 my ($f_key, $guess);
30 8519 ▶ if (defined $cond and length $cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cond and length $cond) { } 5896 2623 8519
Condition66%2/3
defined $cond and length $cond
!ll&&!rl&&r
262305896
MC/DC50%1/2
defined $cond and length $cond
defined $cond length $cond
Truth table66%2/3
A and B
A= defined $condB= length $cond
ABresult
0X0
100
111
31 5896 $f_key = $cond;
32 5896 $guess = "caller specified foreign key '$f_key'";
33 }
34 else {
35 2623 $f_key = $rel;
36 2623 $guess = "using given relationship name '$rel' as foreign key column name";
37 }
38  
39 8519 ▶ $class->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $class->has_column($f_key) 0 8519 8519
40 "No such column '$f_key' declared yet on ${class} ($guess)"
41 ) unless $class->has_column($f_key);
42  
43 8519 $class->ensure_class_loaded($f_class);
44 8519 ▶ my $f_rsrc = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 8519 $f_class->result_source_instance;
46 }
47 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 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 8519 };
53  
54 8519 my $pri = $f_rsrc->_single_pri_col_or_die;
55  
56 8519 $cond = { "foreign.${pri}" => "self.${f_key}" };
57  
58 }
59 # explicit join condition
60 else {
61 5249 ▶ if (ref $cond eq 'HASH') { # ARRAY is also valid
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cond eq "HASH") 3936 1313 5249
62 3936 my $cond_rel;
63 # FIXME This loop is ridiculously incomplete and dangerous
64 # staving off changes until implmentation of the swindon consensus
65 3936 for (keys %$cond) {
66 3936 ▶ if (m/\./) { # Explicit join condition
Branch: 1 branch
BranchTrueFalseTotal
if (/\./) 3935 1 3936
67 3935 $cond_rel = $cond;
68 3935 last;
69 }
70 1 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
71 }
72 3936 $cond = $cond_rel;
73 }
74 }
75  
76 13768 ▶ my $acc_type = (
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'HASH' && keys %$cond == 1 && (keys %$cond)[0] =~ /^foreign\./ && $class->has_column($rel) ? : 5559 8209 13768
Condition88%8/9
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /^foreign\./ and $class->has_column($rel)
!ll&&!rl&&r
228859215559
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /^foreign\./
!ll&&!rl&&r
2288011480
ref $cond eq "HASH" and keys %$cond == 1
!ll&&!rl&&r
131397511480
MC/DC75%3/4
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /^foreign\./ and $class->has_column($rel)
ref $cond eq "HASH" keys %$cond == 1 (keys %$cond)[0] =~ /^foreign\./ $class->has_column($rel)
Truth table80%4/5
A and B and C and D
A= ref $cond eq "HASH"B= keys %$cond == 1C= (keys %$cond)[0] =~ /^foreign\./D= $class->has_column($rel)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
77 ref $cond eq 'HASH'
78 and
79 keys %$cond == 1
80 and
81 (keys %$cond)[0] =~ /^foreign\./
82 and
83 $class->has_column($rel)
84 ) ? 'filter' : 'single';
85  
86 my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
87 13768 ▶ ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
Branch: 2 branches
BranchTrueFalseTotal
$acc_type eq 'single' && ref $cond eq 'HASH' ? : 6896 6872 13768
$_ =~ /^self\.(.+)/ ? : 8521 0 8521
Condition100%3/3
$acc_type eq "single" and ref $cond eq "HASH"
!ll&&!rl&&r
555913136896
MC/DC100%2/2
$acc_type eq "single" and ref $cond eq "HASH"
$acc_type eq "single" ref $cond eq "HASH"
Truth table100%3/3
A and B
A= $acc_type eq "single"B= ref $cond eq "HASH"
ABresult
0X0
100
111
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 13768 ▶ %{$attrs || {}}
Branch: 1 branch
BranchTrueFalseTotal
$fk_columns ? : 6896 6872 13768
Condition50%1/2
$attrs || {}
l!l
137680
MC/DC0%0/1
$attrs || {}
$attrs
Truth table50%1/2
A
A= $attrs
Aresult
11
00
98 }
99 );
100  
101 13768 return 1;
102 }
103  
104 379 1;
« DBIx/Class/InflateColumn/DateTime.pm ↑ Summary DBIx/Class/Row.pm »