| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # no join condition or just a column name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 10887 | ▶ | if (!ref $cond) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 6779 | my ($f_key, $guess); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 6779 | ▶ | if (defined $cond and 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition88%8/9
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /^foreign\./ and $class->has_column($rel)
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /^foreign\./
ref $cond eq "HASH" and keys %$cond == 1
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$acc_type eq "single" and ref $cond eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$attrs || {}
MC/DC0%0/1
$attrs || {}
$attrs
Truth table50%1/2
A
A= $attrs
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 10887 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 378 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||