DBIx/Class/Relationship/HasOne.pm

filter: Statementstmt 94.4% 51 / 54 Branchbran 83.3% 20 / 24 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 2 totaltotal 87.5% 84 / 96 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/FilterColumn.pm ↑ Summary DBIx/Class/CDBICompat/Iterator.pm »
1 package # hide from PAUSE
2 DBIx::Class::Relationship::HasOne;
3  
4 308 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 308 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 308 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 308 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 308 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 308 our %_pod_inherit_config =
11 (
12 class_map => { 'DBIx::Class::Relationship::HasOne' => 'DBIx::Class::Relationship' }
13 );
14  
15 sub might_have {
16 2158 ▶ shift->_has_one('LEFT' => @_);
Subroutine might_have: called CC=1 SCAR=0.0
Pod undocumented
17 }
18  
19 sub has_one {
20 616 ▶ shift->_has_one(undef() => @_);
Subroutine has_one: called CC=1 SCAR=0.0
Pod undocumented
21 }
22  
23 sub _has_one {
24 2774 ▶ my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
Subroutine _has_one: called CC=8 SCAR=21.0
25 2774 ▶ unless (ref $cond) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cond) 2156 618 2774
26 2156 my $pri = $class->result_source_instance->_single_pri_col_or_die;
27  
28 2156 my ($f_key,$guess,$f_rsrc);
29 2156 ▶ if (defined $cond && length $cond) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cond and length $cond) { } 1848 308 2156
Condition66%2/3
defined $cond and length $cond
!ll&&!rl&&r
30801848
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 1848 $f_key = $cond;
31 1848 $guess = "caller specified foreign key '$f_key'";
32 }
33 else {
34 # at this point we need to load the foreigner, expensive or not
35 308 $class->ensure_class_loaded($f_class);
36  
37 $f_rsrc = try {
38 308 ▶ my $r = $f_class->result_source_instance;
Subroutine __ANON__: called CC=2 SCAR=7.1
39 308 ▶ die "There got to be some columns by now... (exception caught and rewritten by catch below)"
Branch: 1 branch
BranchTrueFalseTotal
unless $r->columns 0 308 308
40 unless $r->columns;
41 308 $r;
42 }
43 catch {
44 0 ▶ $class->throw_exception(
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 308 };
49  
50 308 ▶ if ($f_rsrc->has_column($rel)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f_rsrc->has_column($rel)) { } 0 308 308
51 0 $f_key = $rel;
52 0 $guess = "using given relationship name '$rel' as foreign key column name";
53 }
54 else {
55 308 $f_key = $f_rsrc->_single_pri_col_or_die;
56 308 $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 2156 $cond = { "foreign.${f_key}" => "self.${pri}" };
70 }
71 2774 $class->_validate_has_one_condition($cond);
72  
73 2774 ▶ my $default_cascade = ref $cond eq 'CODE' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'CODE' ? : 308 2466 2774
74  
75 $class->add_relationship($rel, $f_class,
76 $cond,
77 { accessor => 'single',
78 cascade_update => $default_cascade,
79 cascade_delete => $default_cascade,
80 ($join_type ? ('join_type' => $join_type) : ()),
81 2774 ▶ %{$attrs || {}} });
Branch: 2 branches
BranchTrueFalseTotal
$join_type ? : 2158 616 2774
$attrs or {} 1850 924 2774
82 2774 1;
83 }
84  
85 sub _validate_has_one_condition {
86 2774 ▶ my ($class, $cond ) = @_;
Subroutine _validate_has_one_condition: called CC=7 SCAR=19.5
87  
88 2774 ▶ return if $ENV{DBIC_DONT_VALIDATE_RELS};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBIC_DONT_VALIDATE_RELS"} 1 2773 2774
89 2773 ▶ return unless 'HASH' eq ref $cond;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $cond 308 2465 2773
90 2465 foreach my $foreign_id ( keys %$cond ) {
91 2465 my $self_id = $cond->{$foreign_id};
92  
93 # we can ignore a bad $self_id because add_relationship handles this
94 # warning
95 2465 ▶ return unless $self_id =~ /^self\.(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self_id =~ /^self\.(.*)$/ 0 2465 2465
96 2465 my $key = $1;
97 2465 ▶ $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 2465 2465
98 unless $class->has_column($key);
99 2465 my $column_info = $class->column_info($key);
100 2465 ▶ if ( $column_info->{is_nullable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"is_nullable"}) 1 2464 2465
101 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.');
102 }
103 }
104 }
105  
106 308 1;
« DBIx/Class/FilterColumn.pm ↑ Summary DBIx/Class/CDBICompat/Iterator.pm »