| 1 | package DBIO::Relationship::HasOne; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Declare a one-to-one relationship | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 378 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 378 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 378 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 378 | ▶ | use DBIO::Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 378 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 378 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 378 | our %_pod_inherit_config = | |||||||||||||||||||||||||||
| 14 | ( | ||||||||||||||||||||||||||||
| 15 | class_map => { 'DBIO::Relationship::HasOne' => 'DBIO::Relationship' } | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1795 | ▶ | sub might_have { | ||||||||||||||||||||||||||
|
Subroutine
might_have: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 1795 | shift->_has_one('LEFT' => @_); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 513 | ▶ | sub has_one { | ||||||||||||||||||||||||||
|
Subroutine
has_one: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 513 | shift->_has_one(undef() => @_); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2308 | ▶ | sub _has_one { | ||||||||||||||||||||||||||
|
Subroutine
_has_one: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||
| 30 | 2308 | my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_; | |||||||||||||||||||||||||||
| 31 | 2308 | ▶ | unless (ref $cond) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1792 | my $pri = $class->result_source_instance->_single_pri_col_or_die; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1792 | my ($f_key,$guess,$f_rsrc); | |||||||||||||||||||||||||||
| 35 | 1792 | ▶ | if (defined $cond && length $cond) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cond and length $cond
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
| |||||||||||||||||||||||||||||
| 36 | 1536 | $f_key = $cond; | |||||||||||||||||||||||||||
| 37 | 1536 | $guess = "caller specified foreign key '$f_key'"; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | # at this point we need to load the foreigner, expensive or not | ||||||||||||||||||||||||||||
| 41 | 256 | $class->ensure_class_loaded($f_class); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 256 | ▶ | $f_rsrc = try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 44 | 256 | my $r = $f_class->result_source_instance; | |||||||||||||||||||||||||||
| 45 | 256 | ▶ | die "There got to be some columns by now... (exception caught and rewritten by catch below)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | unless $r->columns; | ||||||||||||||||||||||||||||
| 47 | 256 | $r; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | $class->throw_exception( | |||||||||||||||||||||||||||
| 51 | "Foreign class '$f_class' does not seem to be a Result class " | ||||||||||||||||||||||||||||
| 52 | . "(or it simply did not load entirely due to a circular relation chain)" | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | 256 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 256 | ▶ | if ($f_rsrc->has_column($rel)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $f_key = $rel; | |||||||||||||||||||||||||||
| 58 | 0 | $guess = "using given relationship name '$rel' as foreign key column name"; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 256 | $f_key = $f_rsrc->_single_pri_col_or_die; | |||||||||||||||||||||||||||
| 62 | 256 | $guess = "using primary key of foreign class for foreign key"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # FIXME - this check needs to be moved to schema-composition time... | ||||||||||||||||||||||||||||
| 67 | # # only perform checks if the far side was not preloaded above *AND* | ||||||||||||||||||||||||||||
| 68 | # # appears to have been loaded by something else (has a rsrc_instance) | ||||||||||||||||||||||||||||
| 69 | # if (! $f_rsrc and $f_rsrc = try { $f_class->result_source_instance }) { | ||||||||||||||||||||||||||||
| 70 | # $class->throw_exception( | ||||||||||||||||||||||||||||
| 71 | # "No such column '$f_key' on foreign class ${f_class} ($guess)" | ||||||||||||||||||||||||||||
| 72 | # ) if !$f_rsrc->has_column($f_key); | ||||||||||||||||||||||||||||
| 73 | # } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1792 | $cond = { "foreign.${f_key}" => "self.${pri}" }; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 2308 | $class->_validate_has_one_condition($cond); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2308 | ▶ | my $default_cascade = ref $cond eq 'CODE' ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | $class->add_relationship($rel, $f_class, | ||||||||||||||||||||||||||||
| 82 | $cond, | ||||||||||||||||||||||||||||
| 83 | { accessor => 'single', | ||||||||||||||||||||||||||||
| 84 | cascade_update => $default_cascade, | ||||||||||||||||||||||||||||
| 85 | cascade_delete => $default_cascade, | ||||||||||||||||||||||||||||
| 86 | is_depends_on => 0, | ||||||||||||||||||||||||||||
| 87 | ($join_type ? ('join_type' => $join_type) : ()), | ||||||||||||||||||||||||||||
| 88 | 2308 | ▶ | %{$attrs || {}} }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$attrs || {}
MC/DC100%1/1
$attrs || {}
$attrs
Truth table100%2/2
A
A= $attrs
| |||||||||||||||||||||||||||||
| 89 | 2306 | 1; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2308 | ▶ | sub _validate_has_one_condition { | ||||||||||||||||||||||||||
|
Subroutine
_validate_has_one_condition: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 94 | 2308 | my ($class, $cond ) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2308 | ▶ | return if $ENV{DBIO_DONT_VALIDATE_RELS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 2307 | ▶ | return unless 'HASH' eq ref $cond; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 2051 | foreach my $foreign_id ( keys %$cond ) { | |||||||||||||||||||||||||||
| 99 | 2051 | my $self_id = $cond->{$foreign_id}; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # we can ignore a bad $self_id because add_relationship handles this | ||||||||||||||||||||||||||||
| 102 | # exception | ||||||||||||||||||||||||||||
| 103 | 2051 | ▶ | return unless $self_id =~ /^self\.(.*)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2051 | my $key = $1; | |||||||||||||||||||||||||||
| 106 | 2051 | ▶ | $class->throw_exception("Defining rel on ${class} that includes '$key' but no such column defined here yet") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | unless $class->has_column($key); | ||||||||||||||||||||||||||||
| 108 | 2051 | my $column_info = $class->column_info($key); | |||||||||||||||||||||||||||
| 109 | 2051 | ▶ | if ( $column_info->{is_nullable} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 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.'); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 378 | 1; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||