blib/lib/DBIO/Relationship/Codegen.pm

filter: Statementstmt 96.9% 64 / 66 Branchbran 80.0% 24 / 30 Conditioncond 54.5% 12 / 22 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 81.3% 118 / 145 CC 26 SCAR 15.3
CC
26
Cov
85%
proxy_to_related
6.4
__ANON__
6.3
_relationship_null_fk_miss
5.9
CRAP 4.6
« blib/lib/DBIO/Storage/Debug/PrettyTrace.pm ↑ Summary blib/lib/DBIO/Test/StorageLayer.pm »
1 package DBIO::Relationship::Codegen;
2 # ABSTRACT: Schema-time relationship method synthesis (accessors and proxies)
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 Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 378 ▶use DBIO::Util qw(quote_sub perlstring assert_no_list_context);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 378 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 378 our %_pod_inherit_config =
15 (
16 class_map => { 'DBIO::Relationship::Codegen' => 'DBIO::Relationship' }
17 );
18  
19  
20 22702 ▶sub register_relationship {
Subroutine register_relationship: called CC=3 SCAR=11.0
Pod documented
21 22702 my ($class, $rel, $info) = @_;
22  
23 22702 ▶ if (my $acc_type = $info->{attrs}{accessor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $acc_type = $info->{"attrs"}{"accessor"}) 22702 0 22702
24 22702 $class->add_relationship_accessor($rel => $acc_type);
25 }
26  
27 22702 ▶ if (my $proxy_args = $info->{attrs}{proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy_args = $info->{"attrs"}{"proxy"}) 1536 21166 22702
28 1536 $class->proxy_to_related($rel, $proxy_args);
29 }
30  
31 22702 $class->next::method($rel => $info);
32 }
33  
34  
35 22702 ▶sub add_relationship_accessor {
Subroutine add_relationship_accessor: called CC=5 SCAR=16.3
Pod documented
36 22702 my ($class, $rel, $acc_type) = @_;
37  
38 22702 ▶ if ($acc_type eq 'single') {
Branch: 3 branches
BranchTrueFalseTotal
if ($acc_type eq 'single') { } 8835 13867 22702
elsif ($acc_type eq 'filter') { } 4358 9509 13867
elsif ($acc_type eq 'multi') { } 9509 0 9509
39 8835 quote_sub "${class}::${rel}" => sprintf(<<'EOC', perlstring $rel);
40 my $self = shift;
41  
42 if (@_) {
43 $self->set_from_related( %1$s => @_ );
44  
45 # a plain hashref is column values, reduced to our own columns just
46 # above (ADR 0034) -- not a related object to keep
47 return $_[0] if ref $_[0] eq 'HASH';
48  
49 return $self->{_relationship_data}{%1$s} = $_[0];
50 }
51 elsif (exists $self->{_relationship_data}{%1$s}) {
52 return $self->{_relationship_data}{%1$s};
53 }
54 else {
55 return undef if $self->_relationship_null_fk_miss(%1$s);
56  
57 my $val = $self->search_related( %1$s )->single;
58 return $val unless $val; # $val instead of undef so that null-objects can go through
59  
60 return $self->{_relationship_data}{%1$s} = $val;
61 }
62 EOC
63  
64 8835 quote_sub "${class}::${rel}_async" => sprintf(<<'EOC', perlstring $rel, $rel);
65 my $self = shift;
66 $self->throw_exception("%2$s_async doesn't take arguments -- use ->%2$s(...) to set the relationship")
67 if @_;
68  
69 return $self->_row_run_async(%1$s,
70 sub {
71 my ($backend, $fc) = @_;
72  
73 return $fc->done($self->{_relationship_data}{%1$s})
74 if exists $self->{_relationship_data}{%1$s};
75  
76 return $fc->done(undef)
77 if $self->_relationship_null_fk_miss(%1$s);
78  
79 return $self->search_related(%1$s)->single_async->then(sub {
80 my ($val) = @_;
81 return $val unless $val; # $val instead of undef so that null-objects can go through
82 return $self->{_relationship_data}{%1$s} = $val;
83 });
84 },
85 sub { $self->%2$s },
86 );
87 EOC
88 }
89 elsif ($acc_type eq 'filter') {
90 4358 ▶ $class->throw_exception("No such column '$rel' to filter")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->has_column($rel) 0 4358 4358
91 unless $class->has_column($rel);
92  
93 4358 my $f_class = $class->relationship_info($rel)->{class};
94  
95 $class->inflate_column($rel, {
96 15 ▶ inflate => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
97 15 my ($val, $self) = @_;
98 # the row the foreign key names: found by the key the related
99 # resultset holds, a new row when there is none
100 15 my $found = $self->find_related($rel, {}, {});
101 15 ▶ return defined $found ? $found : $self->new_related($rel, {}, {});
Branch: 1 branch
BranchTrueFalseTotal
defined $found ? : 3 12 15
102 },
103 30 ▶ deflate => sub {
Subroutine __ANON__: called CC=6 SCAR=18.5
104 30 my ($val, $self) = @_;
105 30 ▶ $self->throw_exception("The value given for '$rel' isn't a $f_class")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($val) and $val->isa($f_class) 4 26 30
Condition66%2/3
builtin::blessed($val) and $val->isa($f_class)
!ll&&!rl&&r
4026
MC/DC50%1/2
builtin::blessed($val) and $val->isa($f_class)
builtin::blessed($val) $val->isa($f_class)
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa($f_class)
ABresult
0X0
100
111
106 unless blessed $val and $val->isa($f_class);
107  
108 # MASSIVE FIXME - this code assumes we pointed at the PK, but the belongs_to
109 # helper does not check any of this
110 # fixup the code a bit to make things saner, but ideally 'filter' needs to
111 # be deprecated ASAP and removed shortly after
112 # Not doing so before 0.08250 however, too many things in motion already
113 26 my ($pk_col, @rest) = $val->result_source->_pri_cols_or_die;
114 26 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if @rest 0 26 26
115 "Relationship '$rel' of type 'filter' can not work with a multicolumn primary key on source '$f_class'"
116 ) if @rest;
117  
118 26 my $pk_val = $val->get_column($pk_col);
119 26 ▶ carp_unique (
Branch: 1 branch
BranchTrueFalseTotal
if not defined $pk_val and $val->in_storage 0 26 26
Condition66%2/3
not defined $pk_val and $val->in_storage
!ll&&!rl&&r
2420
MC/DC0%0/2
not defined $pk_val and $val->in_storage
not defined $pk_val $val->in_storage
Truth table66%2/3
not(A) and B
A= not defined $pk_valB= $val->in_storage
ABresult
0X0
100
111
120 "Unable to deflate 'filter'-type relationship '$rel' (related object "
121 . "primary key not retrieved), assuming undef instead"
122 ) if ( ! defined $pk_val and $val->in_storage );
123  
124 26 return $pk_val;
125 },
126 4358 });
127 }
128 elsif ($acc_type eq 'multi') {
129  
130 9509 quote_sub "${class}::${rel}_rs", sprintf('shift->search_related_rs( %s => @_ )', perlstring $rel);
131 9509 quote_sub "${class}::add_to_${rel}", sprintf('shift->create_related( %s => @_ )', perlstring $rel);
132 9509 quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel, perlstring $rel );
133 my $self = shift;
134 DBIO::Util::assert_no_list_context('relationship accessor %s') if wantarray;
135 $self->search_related_rs( %s => @_ )
136 EOC
137 }
138 else {
139 0 $class->throw_exception("No such relationship accessor type '$acc_type'");
140 }
141  
142 }
143  
144 # Shared by the single-type accessor and its _async twin: true when the
145 # relationship's join-free condition is fully resolved but every value in it
146 # is undef (the FK is definitely NULL) and the relationship is configured to
147 # skip the query in that case (undef_on_null_fk -- the belongs_to default).
148 # See DBIO::Relationship/belongs_to.
149 24 ▶sub _relationship_null_fk_miss {
Subroutine _relationship_null_fk_miss: called CC=5 SCAR=17.8
150 24 my ($self, $rel) = @_;
151  
152 24 my $relcond = $self->result_source->_resolve_relationship_condition(
153 rel_name => $rel,
154 foreign_alias => $rel,
155 self_alias => 'me',
156 self_result_object => $self,
157 );
158  
159 return !!(
160 $relcond->{join_free_condition}
161 and
162 $relcond->{join_free_condition} ne DBIO::Util::UNRESOLVABLE_CONDITION()
163 and
164 24 ▶ scalar grep { not defined $_ } values %{ $relcond->{join_free_condition} || {} }
Condition50%1/2
$relcond->{'join_free_condition'} || {}
l!l
240
MC/DC0%0/1
$relcond->{'join_free_condition'} || {}
$relcond->{'join_free_condition'}
Truth table50%1/2
A
A= $relcond->{'join_free_condition'}
Aresult
11
00
165 and
166 $self->result_source->relationship_info($rel)->{attrs}{undef_on_null_fk}
167 24 ▶ );
Condition50%4/8
$relcond->{'join_free_condition'} && $relcond->{'join_free_condition'} ne \'1 = 0' && scalar grep({not defined $_;} values %{$relcond->{'join_free_condition'} or {};}) && $self->result_source->relationship_info($rel)->{'attrs'}{'undef_on_null_fk'}
!ll&&!rl&&r
2202
$relcond->{"join_free_condition"} and $relcond->{"join_free_condition"} ne \"1 = 0" and scalar grep({not defined $_;} values %{$relcond->{"join_free_condition"} or {};})
!ll
024
$relcond->{"join_free_condition"} and $relcond->{"join_free_condition"} ne \"1 = 0"
!ll&&!rl&&r
0024
MC/DC0%0/3
$relcond->{'join_free_condition'} && $relcond->{'join_free_condition'} ne \'1 = 0' && scalar grep({not defined $_;} values %{$relcond->{'join_free_condition'} or {};}) && $self->result_source->relationship_info($rel)->{'attrs'}{'undef_on_null_fk'}
$relcond->{"join_free_condition"} $relcond->{"join_free_condition"} ne \"1 = 0" $self->result_source->relationship_info($rel)->{'attrs'}{'undef_on_null_fk'}
Truth table25%1/4
A and B && C
A= $relcond->{"join_free_condition"}B= $relcond->{"join_free_condition"} ne \"1 = 0"C= $self->result_source->relationship_info($rel)->{'attrs'}{'undef_on_null_fk'}
ABCresult
0XX0
10X0
1100
1111
168 }
169  
170  
171 1543 ▶sub proxy_to_related {
Subroutine proxy_to_related: called CC=6 SCAR=18.6
Pod documented
172 1543 my ($class, $rel, $proxy_args) = @_;
173 1543 my %proxy_map = $class->_build_proxy_map_from($proxy_args);
174  
175 1543 for my $col (values %proxy_map) {
176 2312 ▶ $class->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col and not ref $col and $col =~ /\A\w+\z/ 6 2306 2312
Condition50%3/6
defined $col and not ref $col and $col =~ /\A\w+\z/
!ll&&!rl&&r
062306
defined $col and not ref $col
!ll&&!rl&&r
002312
MC/DC33%1/3
defined $col and not ref $col and $col =~ /\A\w+\z/
defined $col not ref $col $col =~ /\A\w+\z/
Truth table50%2/4
A and not(B) and C
A= defined $colB= not ref $colC= $col =~ /\A\w+\z/
ABCresult
0XX0
10X0
1100
1111
177 "proxy column of relationship '$rel' must be a plain column name"
178 ) unless defined $col and !ref $col and $col =~ /\A\w+\z/;
179 }
180  
181 quote_sub "${class}::$_", sprintf( <<'EOC', perlstring($rel), perlstring($proxy_map{$_}) )
182 my $self = shift;
183 my $relobj = $self->${\ %1$s};
184 if (@_ && !defined $relobj) {
185 $relobj = $self->create_related( %1$s => { %2$s => $_[0] } );
186 @_ = ();
187 }
188 $relobj ? $relobj->${\ %2$s}(@_) : undef;
189 EOC
190 1537 for keys %proxy_map
191 }
192  
193  
194 1543 ▶sub _build_proxy_map_from {
Subroutine _build_proxy_map_from: called CC=5 SCAR=16.2
195 1543 my ( $class, $proxy_arg ) = @_;
196 1543 my $ref = ref $proxy_arg;
197  
198 1543 ▶ if ($ref eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 775 768 1543
elsif ($ref eq 'ARRAY') { } 512 256 768
elsif ($ref) { } 0 256 256
199 775 return %$proxy_arg;
200 }
201 elsif ($ref eq 'ARRAY') {
202 return map {
203 512 ▶ (ref $_ eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 256 512 768
204 ? (%$_)
205 : ($_ => $_)
206 } @$proxy_arg;
207 }
208 elsif ($ref) {
209 0 $class->throw_exception("Unable to process the 'proxy' argument $proxy_arg");
210 }
211 else {
212 256 return ( $proxy_arg => $proxy_arg );
213 }
214 }
215  
216 378 1;
217  
218 __END__
« blib/lib/DBIO/Storage/Debug/PrettyTrace.pm ↑ Summary blib/lib/DBIO/Test/StorageLayer.pm »