DBIx/Class/Relationship/HasOne.pm

filter: Statementstmt 94.4% 51 / 54 Branchbran 81.8% 18 / 22 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 2 totaltotal 87.6% 85 / 97 CC 15 SCAR 16.3
CC
15
Cov
90%
_has_one
8.2
_validate_has_one_condition
7.0
__ANON__
2.0
CRAP 5.1
« DBIx/Class/PK.pm ↑ Summary DBIx/Class/CDBICompat/Iterator.pm »
1 package # hide from PAUSE
2 DBIx::Class::Relationship::HasOne;
3  
4 379 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 379 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 379 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 379 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 379 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 379 our %_pod_inherit_config =
11 (
12 class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
13 );
14  
15 2315 ▶sub might_have {
Subroutine might_have: called CC=1 SCAR=0.0
Pod undocumented
16 2315 shift->_has_one('LEFT' => @_);
17 }
18  
19 674 ▶sub has_one {
Subroutine has_one: called CC=1 SCAR=0.0
Pod undocumented
20 674 shift->_has_one(undef() => @_);
21 }
22  
23 2989 ▶sub _has_one {
Subroutine _has_one: called CC=8 SCAR=21.0
24 2989 my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
25 2989 ▶ unless (ref $cond) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cond) 2324 665 2989
26 2324 my $pri = $class->result_source_instance->_single_pri_col_or_die;
27  
28 2324 my ($f_key,$guess,$f_rsrc);
29 2324 ▶ if (defined $cond && length $cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cond and length $cond) { } 1988 336 2324
Condition66%2/3
defined $cond and length $cond
!ll&&!rl&&r
33601988
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
30 1988 $f_key = $cond;
31 1988 $guess = "caller specified foreign key '$f_key'";
32 }
33 else {
34 # at this point we need to load the foreigner, expensive or not
35 336 $class->ensure_class_loaded($f_class);
36  
37 336 ▶ $f_rsrc = try {
Subroutine __ANON__: called CC=2 SCAR=7.1
38 336 my $r = $f_class->result_source_instance;
39 336 ▶ die "There got to be some columns by now... (exception caught and rewritten by catch below)"
Branch: 1 branch
BranchTrueFalseTotal
unless $r->columns 0 336 336
40 unless $r->columns;
41 336 $r;
42 }
43 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 $class->throw_exception(
45 "Foreign class '$f_class' does not seem to be a Result class "
46 . "(or it simply did not load entirely due to a circular relation chain)"
47 );
48 336 };
49  
50 336 ▶ if ($f_rsrc->has_column($rel)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_rsrc->has_column($rel)) { } 0 336 336
51 0 $f_key = $rel;
52 0 $guess = "using given relationship name '$rel' as foreign key column name";
53 }
54 else {
55 336 $f_key = $f_rsrc->_single_pri_col_or_die;
56 336 $guess = "using primary key of foreign class for foreign key";
57 }
58 }
59  
60 # FIXME - this check needs to be moved to schema-composition time...
61 # # only perform checks if the far side was not preloaded above *AND*
62 # # appears to have been loaded by something else (has a rsrc_instance)
63 # if (! $f_rsrc and $f_rsrc = try { $f_class->result_source_instance }) {
64 # $class->throw_exception(
65 # "No such column '$f_key' on foreign class ${f_class} ($guess)"
66 # ) if !$f_rsrc->has_column($f_key);
67 # }
68  
69 2324 $cond = { "foreign.${f_key}" => "self.${pri}" };
70 }
71 2989 $class->_validate_has_one_condition($cond);
72  
73 2989 ▶ my $default_cascade = ref $cond eq 'CODE' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'CODE' ? : 336 2653 2989
74  
75 $class->add_relationship($rel, $f_class,
76 $cond,
77 { accessor => 'single',
78 cascade_update => $default_cascade,
79 cascade_delete => $default_cascade,
80 is_depends_on => 0,
81 ($join_type ? ('join_type' => $join_type) : ()),
82 2989 ▶ %{$attrs || {}} });
Branch: 1 branch
BranchTrueFalseTotal
$join_type ? : 2315 674 2989
Condition100%2/2
$attrs || {}
l!l
9862003
MC/DC100%1/1
$attrs || {}
$attrs
Truth table100%2/2
A
A= $attrs
Aresult
11
00
83 2987 1;
84 }
85  
86 2989 ▶sub _validate_has_one_condition {
Subroutine _validate_has_one_condition: called CC=7 SCAR=19.5
87 2989 my ($class, $cond ) = @_;
88  
89 2989 ▶ return if $ENV{DBIC_DONT_VALIDATE_RELS};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBIC_DONT_VALIDATE_RELS"} 1 2988 2989
90 2988 ▶ return unless 'HASH' eq ref $cond;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $cond 336 2652 2988
91 2652 foreach my $foreign_id ( keys %$cond ) {
92 2652 my $self_id = $cond->{$foreign_id};
93  
94 # we can ignore a bad $self_id because add_relationship handles this
95 # exception
96 2652 ▶ return unless $self_id =~ /^self\.(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self_id =~ /^self\.(.*)$/ 0 2652 2652
97  
98 2652 my $key = $1;
99 2652 ▶ $class->throw_exception("Defining rel on ${class} that includes '$key' but no such column defined here yet")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->has_column($key) 0 2652 2652
100 unless $class->has_column($key);
101 2652 my $column_info = $class->column_info($key);
102 2652 ▶ if ( $column_info->{is_nullable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"is_nullable"}) 1 2651 2652
103 1 carp(qq'"might_have/has_one" must not be on columns with is_nullable set to true ($class/$key). This might indicate an incorrect use of those relationship helpers instead of belongs_to.');
104 }
105 }
106 }
107  
108 379 1;
« DBIx/Class/PK.pm ↑ Summary DBIx/Class/CDBICompat/Iterator.pm »