blib/lib/DBIO/Relationship/BelongsTo.pm

filter: Statementstmt 97.9% 47 / 48 Branchbran 90.9% 20 / 22 Conditioncond 82.3% 14 / 17 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 90.3% 94 / 104 CC 19 SCAR 27.0
CC
19
Cov
93%
belongs_to
19.2
__ANON__
2.0
BEGIN
1.0
CRAP 14.9
« blib/lib/DBIO/Extension/List.pm ↑ Summary blib/lib/DBIO/Util.pm »
1 package DBIO::Relationship::BelongsTo;
2 # ABSTRACT: Declare a foreign-key relationship to a parent row
3  
4 # Documentation for these methods can be found in
5 # DBIO::Relationship
6  
7 378 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 378 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 378 our $VERSION = '0.900007';
11  
12 378 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 378 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 378 our %_pod_inherit_config =
16 (
17 class_map => { 'DBIO::Relationship::BelongsTo' => 'DBIO::Relationship' }
18 );
19  
20  
21 10887 ▶sub belongs_to {
Subroutine belongs_to: called CC=19 SCAR=29.6
Pod documented
22 10887 my ($class, $rel, $f_class, $cond, $attrs) = @_;
23  
24 # assume a foreign key constraint unless defined otherwise; remember when
25 # it was asked for, so a driver can refuse an explicit one it cannot create
26 10887 ▶ if (exists $attrs->{is_foreign_key_constraint}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{'is_foreign_key_constraint'}) { } 258 10629 10887
27 258 $attrs->{_explicit_foreign_key_constraint} = 1;
28 }
29 else {
30 10629 $attrs->{is_foreign_key_constraint} = 1;
31 }
32 $attrs->{undef_on_null_fk} = 1
33 10887 ▶ if not exists $attrs->{undef_on_null_fk};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"undef_on_null_fk"} 10631 256 10887
34  
35 # no join condition or just a column name
36 10887 ▶ if (!ref $cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $cond) { } 6779 4108 10887
37  
38 6779 my ($f_key, $guess);
39 6779 ▶ if (defined $cond and length $cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cond and length $cond) { } 4726 2053 6779
Condition66%2/3
defined $cond and length $cond
!ll&&!rl&&r
205304726
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
40 4726 $f_key = $cond;
41 4726 $guess = "caller specified foreign key '$f_key'";
42 }
43 else {
44 2053 $f_key = $rel;
45 2053 $guess = "using given relationship name '$rel' as foreign key column name";
46 }
47  
48 6779 ▶ $class->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $class->has_column($f_key) 0 6779 6779
49 "No such column '$f_key' declared yet on ${class} ($guess)"
50 ) unless $class->has_column($f_key);
51  
52 6779 $class->ensure_class_loaded($f_class);
53 6779 ▶ my $f_rsrc = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 6779 $f_class->result_source_instance;
55 }
56 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 $class->throw_exception(
58 "Foreign class '$f_class' does not seem to be a Result class "
59 . "(or it simply did not load entirely due to a circular relation chain)"
60 );
61 6779 };
62  
63 6779 my $pri = $f_rsrc->_single_pri_col_or_die;
64  
65 6779 $cond = { "foreign.${pri}" => "self.${f_key}" };
66  
67 }
68 # explicit join condition
69 else {
70 4108 ▶ if (ref $cond eq 'HASH') { # ARRAY is also valid
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cond eq "HASH") 3084 1024 4108
71 3084 my $cond_rel;
72 # FIXME This loop is ridiculously incomplete and dangerous
73 # staving off changes until implmentation of the swindon consensus
74 3084 for (keys %$cond) {
75 3084 ▶ if (m/\./) { # Explicit join condition
Branch: 1 branch
BranchTrueFalseTotal
if (/\./) 3083 1 3084
76 3083 $cond_rel = $cond;
77 3083 last;
78 }
79 1 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
80 }
81 3084 $cond = $cond_rel;
82 }
83 }
84  
85 10887 ▶ my $acc_type = (
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'HASH' && keys %$cond == 1 && (keys %$cond)[0] =~ /^foreign\./ && $class->has_column($rel) ? : 4358 6529 10887
Condition88%8/9
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /^foreign\./ and $class->has_column($rel)
!ll&&!rl&&r
179247374358
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /^foreign\./
!ll&&!rl&&r
179209095
ref $cond eq "HASH" and keys %$cond == 1
!ll&&!rl&&r
10247689095
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
86 ref $cond eq 'HASH'
87 and
88 keys %$cond == 1
89 and
90 (keys %$cond)[0] =~ /^foreign\./
91 and
92 $class->has_column($rel)
93 ) ? 'filter' : 'single';
94  
95 my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
96 10887 ▶ ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
Branch: 2 branches
BranchTrueFalseTotal
$acc_type eq 'single' && ref $cond eq 'HASH' ? : 5505 5382 10887
$_ =~ /^self\.(.+)/ ? : 6785 0 6785
Condition100%3/3
$acc_type eq "single" and ref $cond eq "HASH"
!ll&&!rl&&r
435810245505
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
97 : undef
98 ;
99  
100 $class->add_relationship($rel, $f_class,
101 $cond,
102 {
103 is_depends_on => 1,
104 accessor => $acc_type,
105 $fk_columns ? ( fk_columns => $fk_columns ) : (),
106 10887 ▶ %{$attrs || {}}
Branch: 1 branch
BranchTrueFalseTotal
$fk_columns ? : 5505 5382 10887
Condition50%1/2
$attrs || {}
l!l
108870
MC/DC0%0/1
$attrs || {}
$attrs
Truth table50%1/2
A
A= $attrs
Aresult
11
00
107 }
108 );
109  
110 10887 return 1;
111 }
112  
113 378 1;
114  
115 __END__
« blib/lib/DBIO/Extension/List.pm ↑ Summary blib/lib/DBIO/Util.pm »