MoCo/Relation.pm

filter: Statementstmt 85.2% 127 / 149 Branchbran 56.0% 37 / 66 Conditioncond 57.4% 31 / 54 MC/DCmcdc 26.6% 8 / 30 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 5 totaltotal 68.5% 222 / 324 CC 59 SCAR 32.2
CC
59
Cov
72%
__ANON__
52.6
__ANON__
42.0
__ANON__
7.8
CRAP 25.1
↑ Summary MoCo.pm »
1 package DBIx::MoCo::Relation;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 my $relation = {};
7  
8 76 ▶sub register {
Subroutine register: called CC=4 SCAR=14.0
Pod undocumented
9 76 my $class = shift;
10 76 my ($klass, $type, $attr, $model, $option) = @_;
11  
12 76 ▶ $model->require or die $@;
Branch: 1 branch
BranchTrueFalseTotal
$model->require or die $@ 76 0 76
13 76 $model->import;
14  
15 76 ▶ $relation->{$klass} ||= {has_a => {},has_many => {}};
Condition100%2/2
$relation->{$klass} ||= {"has_a", {}, "has_many", {}}
l!l
3838
MC/DC100%1/1
$relation->{$klass} ||= {"has_a", {}, "has_many", {}}
$relation->{$klass}
Truth table100%2/2
A
A= $relation->{$klass}
Aresult
11
00
16 76 ▶ $relation->{$klass}->{$type}->{$attr} = {
Condition50%1/2
$option || ''
l!l
760
MC/DC0%0/1
$option || ''
$option
Truth table50%1/2
A
A= $option
Aresult
11
00
17 class => $model,
18 option => $option || '',
19 };
20 76 my $registry = 'register_' . $type;
21 76 $class->$registry(@_);
22 76 $class->register_flusher(@_);
23 }
24  
25 38 ▶sub register_has_a {
Subroutine register_has_a: called CC=4 SCAR=17.9
Pod undocumented
26 38 my $class = shift;
27 38 my ($klass, $type, $attr, $model, $option) = @_;
28 38 my ($my_key, $other_key);
29 38 ▶ $option->{key} or return;
Branch: 1 branch
BranchTrueFalseTotal
$option->{"key"} or return 38 0 38
30 38 ▶ if (ref $option->{key} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option->{'key'} eq 'HASH') { } 0 38 38
31 0 ($my_key, $other_key) = %{$option->{key}};
32 } else {
33 38 $my_key = $other_key = $option->{key};
34 }
35 38 my $icache_key = $attr;
36 38 my $cs = $klass->cache_status;
37 38 my $subname = $klass . '::' . $attr;
38 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 12 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 38 ▶ if ($klass->icache_expiration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($klass->icache_expiration) { } 0 38 38
41 0 ▶ *$subname = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
42 0 my $self = shift;
43 0 my $ic = $self->icache;
44 0 ▶ if ($ic && defined $ic->{$icache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ic and defined $ic->{$icache_key}) { } 0 0 0
Condition0%0/3
$ic and defined $ic->{$icache_key}
!ll&&!rl&&r
000
MC/DC0%0/2
$ic and defined $ic->{$icache_key}
$ic defined $ic->{$icache_key}
Truth table0%0/3
A and B
A= $icB= defined $ic->{$icache_key}
ABresult
0X0
100
111
45 0 $cs->{retrieve_count}++;
46 0 $cs->{retrieve_icache_count}++;
47 0 return $ic->{$icache_key};
48 } else {
49 0 ▶ defined $self->{$my_key} or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$my_key} or return 0 0 0
50 0 ▶ my $o = $model->retrieve($other_key => $self->{$my_key}) || undef;
Condition0%0/2
$model->retrieve($other_key, $self->{$my_key}) || undef
l!l
00
MC/DC0%0/1
$model->retrieve($other_key, $self->{$my_key}) || undef
$model->retrieve($other_key, $self->{$my_key})
Truth table0%0/2
A
A= $model->retrieve($other_key, $self->{$my_key})
Aresult
11
00
51 0 ▶ $ic->{$icache_key} = $o if $o;
Branch: 1 branch
BranchTrueFalseTotal
if $o 0 0 0
52 0 return $o;
53 }
54 0 };
55 } else {
56 25 ▶ *$subname = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
57 25 my $self = shift;
58 25 ▶ defined $self->{$my_key} or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$my_key} or return 22 3 25
59 22 ▶ $model->retrieve($other_key => $self->{$my_key}) || undef;
Branch: 1 branch
BranchTrueFalseTotal
$model->retrieve($other_key, $self->{$my_key}) or undef 14 8 22
60 38 };
61 }
62 }
63  
64 38 ▶sub register_has_many {
Subroutine register_has_many: called CC=2 SCAR=7.4
Pod undocumented
65 38 my $class = shift;
66 38 my ($klass, $type, $attr, $model, $option) = @_;
67 # my $array_key = $klass->has_many_keys_name($attr);
68 # my $max_key = $klass->has_many_max_offset_name($attr);
69  
70 38 ▶ $option->{key} or confess 'key is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$option->{"key"} or confess("key is not specified") 38 0 38
71  
72 {
73 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 12 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 38 ▶ *{"$klass\::$attr"} = sub {
Subroutine __ANON__: called CC=29 SCAR=39.6
77 22 ▶ my $extend = pop if ref $_[-1] and ref $_[-1] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] and ref $_[-1] eq "HASH" 0 22 22
Condition66%2/3
ref $_[-1] and ref $_[-1] eq "HASH"
!ll&&!rl&&r
5170
MC/DC0%0/2
ref $_[-1] and ref $_[-1] eq "HASH"
ref $_[-1] ref $_[-1] eq "HASH"
Truth table66%2/3
A and B
A= ref $_[-1]B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
78 22 my ($self, $off, $lt) = @_;
79  
80 22 ▶ $extend ||= {};
Condition100%2/2
$extend ||= {}
l!l
193
MC/DC100%1/1
$extend ||= {}
$extend
Truth table100%2/2
A
A= $extend
Aresult
11
00
81 22 ▶ $off ||= 0;
Condition100%2/2
$off ||= 0
l!l
220
MC/DC100%1/1
$off ||= 0
$off
Truth table100%2/2
A
A= $off
Aresult
11
00
82 22 ▶ my $max_off = defined $lt ? $off + $lt : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $lt ? : 5 17 22
83 22 my $icache = $self->icache;
84 22 my $cache_key = $self->has_many_keys_cache_name($attr);
85 22 my $icache_key = $attr;
86 22 my $cs = $klass->cache_status;
87  
88 22 $cs->{has_many_count}++;
89  
90 22 ▶ my $cond_cached = sub {
Subroutine __ANON__: called CC=7 SCAR=20.5
91 22 my ($keys, $max_offset) = @_;
92 22 ▶ defined $keys or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $keys or return 12 10 22
93 12 ▶ defined $keys->{array} or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $keys->{"array"} or return 12 0 12
94  
95 return (defined $keys->{max_offset} && $keys->{max_offset} == -1)
96 12 ▶ || ($max_off && 0 <= $max_off && $max_off <= $keys->{max_offset});
Condition66%8/12
defined $keys->{'max_offset'} && $keys->{'max_offset'} == -1 || $max_off && 0 <= $max_off && $max_off <= $keys->{'max_offset'}
l!l&&r!l&&!r
822
defined $keys->{"max_offset"} and $keys->{"max_offset"} == -1
!ll&&!rl&&r
048
$max_off and 0 <= $max_off and $max_off <= $keys->{"max_offset"}
!ll&&!rl&&r
022
$max_off and 0 <= $max_off
!ll&&!rl&&r
004
MC/DC40%2/5
defined $keys->{'max_offset'} && $keys->{'max_offset'} == -1 || $max_off && 0 <= $max_off && $max_off <= $keys->{'max_offset'}
defined $keys->{"max_offset"} $keys->{"max_offset"} == -1 $max_off 0 <= $max_off $max_off <= $keys->{"max_offset"}
Truth table33%3/9
A and B || C and D and E
A= defined $keys->{"max_offset"}B= $keys->{"max_offset"} == -1C= $max_offD= 0 <= $max_offE= $max_off <= $keys->{"max_offset"}
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
97 22 };
98  
99 22 my $keys = $self->cache($cache_key);
100  
101 22 ▶ if ( $cond_cached->($keys, $max_off) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$cond_cached($keys, $max_off)) { } 10 12 22
102 # warn "use cache for $cache_key";
103 10 $cs->{has_many_cache_count}++;
104  
105 10 ▶ if ($icache && $icache->{$icache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icache and $icache->{$icache_key}) 0 10 10
Condition33%1/3
$icache and $icache->{$icache_key}
!ll&&!rl&&r
1000
MC/DC0%0/2
$icache and $icache->{$icache_key}
$icache $icache->{$icache_key}
Truth table33%1/3
A and B
A= $icacheB= $icache->{$icache_key}
ABresult
0X0
100
111
106 0 $cs->{has_many_icache_count}++;
107 # warn "use icache $icache_key for " . $self;
108 0 ▶ return $icache->{$icache_key}->slice( $off, defined $max_off ? $max_off - 1 : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $max_off ? : 0 0 0
109 }
110  
111 # warn "$attr cache($keys->{max_offset}) is in range $max_off";
112 } else {
113 # warn "use db for $cache_key";
114 12 my ($my_key, $other_key);
115 ref $option->{key} eq 'HASH'
116 0 ? ($my_key, $other_key) = %{$option->{key}}
117 12 ▶ : $my_key = $other_key = $option->{key};
Branch: 1 branch
BranchTrueFalseTotal
ref $option->{'key'} eq 'HASH' ? : 0 12 12
118  
119 12 ▶ defined $self->{$my_key} or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$my_key} or return 12 0 12
120  
121 12 my $where_clause = "$other_key = ?";
122 12 ▶ $where_clause = join ' AND ', "$other_key = ?", $option->{condition} if $option->{condition};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"condition"} 0 12 12
123  
124 $keys = {
125 array => $model->db->search(
126 where => [ $where_clause, $self->{$my_key} ],
127 12 ▶ field => join(',', @{$model->retrieve_keys || $model->primary_keys}),
Branch: 3 branches
BranchTrueFalseTotal
$option ? : 12 0 12
$option ? : 12 0 12
defined $max_off && $max_off > 0 ? : 3 9 12
Condition58%7/12
$model->retrieve_keys || $model->primary_keys
l!l&&r!l&&!r
0120
$option->{"order"} or ""
l!l
012
$option->{"group"} or ""
l!l
012
defined $max_off and $max_off > 0
!ll&&!rl&&r
903
$max_off || -1
l!l
39
MC/DC0%0/2
$model->retrieve_keys || $model->primary_keys
$model->retrieve_keys $model->primary_keys
MC/DC0%0/1
$option->{"order"} or ""
$option->{"order"}
MC/DC0%0/1
$option->{"group"} or ""
$option->{"group"}
MC/DC50%1/2
defined $max_off and $max_off > 0
defined $max_off $max_off > 0
MC/DC100%1/1
$max_off || -1
$max_off
Truth table33%1/3
A || B
A= $model->retrieve_keysB= $model->primary_keys
ABresult
1X1
011
000
Truth table50%1/2
A
A= $option->{"order"}
Aresult
11
00
Truth table50%1/2
A
A= $option->{"group"}
Aresult
11
00
Truth table66%2/3
A and B
A= defined $max_offB= $max_off > 0
ABresult
0X0
100
111
Truth table100%2/2
A
A= $max_off
Aresult
11
00
128 table => $model->table,
129 order => $option ? $option->{order} || '' : '',
130 12 group => $option ? $option->{group} || '' : '',
131 limit => (defined $max_off && $max_off > 0) ? $max_off : '',
132 ),
133 max_offset => $max_off || -1,
134 };
135  
136 12 $self->cache($cache_key, $keys);
137 # warn @{$self->{$array_key}};
138 }
139  
140 22 ▶ my $last = ($max_off && $max_off <= $#{$keys->{array}}) ? $max_off - 1 : $#{$keys->{array}};
Branch: 1 branch
BranchTrueFalseTotal
$max_off && $max_off <= $#{$$keys{'array'};} ? : 0 22 22
Condition66%2/3
$max_off and $max_off <= $#{$$keys{"array"};}
!ll&&!rl&&r
1750
MC/DC0%0/2
$max_off and $max_off <= $#{$$keys{"array"};}
$max_off $max_off <= $#{$$keys{"array"};}
Truth table66%2/3
A and B
A= $max_offB= $max_off <= $#{$$keys{"array"};}
ABresult
0X0
100
111
141  
142 22 my $res;
143 22 ▶ if ($icache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icache) { } 0 22 22
144 # warn "set icache and return";
145 0 $icache->{$icache_key} = $model->retrieve_multi(@{$keys->{array}});
146 0 $res = $icache->{$icache_key}->slice($off, $last);
147 } else {
148 22 ▶ $res = $model->retrieve_multi(@{$keys->{array}}[$off || 0 .. $last]);
Condition100%2/2
$off || 0
l!l
220
MC/DC100%1/1
$off || 0
$off
Truth table100%2/2
A
A= $off
Aresult
11
00
149 }
150  
151 22 ▶ my $with = $extend->{with} || $class->find_relation_by_attr($klass => $attr)->{option}->{with};
Condition33%1/3
$extend->{'with'} || $class->find_relation_by_attr($klass, $attr)->{'option'}{'with'}
l!l&&r!l&&!r
0022
MC/DC0%0/2
$extend->{'with'} || $class->find_relation_by_attr($klass, $attr)->{'option'}{'with'}
$extend->{'with'} $class->find_relation_by_attr($klass, $attr)->{'option'}{'with'}
Truth table33%1/3
A || B
A= $extend->{'with'}B= $class->find_relation_by_attr($klass, $attr)->{'option'}{'with'}
ABresult
1X1
011
000
152 22 my $without = $extend->{without};
153  
154 22 ▶ if ($with and $res->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with and $res->size > 0) 0 22 22
Condition33%1/3
$with and $res->size > 0
!ll&&!rl&&r
2200
MC/DC0%0/2
$with and $res->size > 0
$with $res->size > 0
Truth table33%1/3
A and B
A= $withB= $res->size > 0
ABresult
0X0
100
111
155 0 $model->merge_with($res, $with, $without);
156 }
157  
158 22 ▶ wantarray ? @$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 22 22
159 38 };
160 }
161 }
162  
163 76 ▶sub register_flusher {
Subroutine register_flusher: called CC=1 SCAR=0.0
Pod undocumented
164 76 shift; # Relation
165 76 my ($klass, $type, $attr, $model, $option) = @_;
166 76 my $flusher = $klass . '::flush_belongs_to';
167 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
168 12 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
169 8 ▶ *$flusher = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
170 # warn "level 1 flusher called for $flusher";
171 8 my ($class, $self) = @_;
172 8 ▶ $self or confess '$self is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$self or confess("\$self is not specified") 8 0 8
173 8 my $has_a = $relation->{$klass}->{has_a};
174 8 for my $attr (keys %$has_a) {
175 5 my $ha = $has_a->{$attr};
176 5 my $oa = [];
177 5 my $other = $relation->{$ha->{class}};
178 5 for my $oattr (keys %{$other->{has_many}}) {
179 9 my $hm = $other->{has_many}->{$oattr};
180 9 ▶ if ($hm->{class} eq $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hm->{"class"} eq $class) 5 4 9
181 # push @$oa, $ha->{class}->has_many_keys_name($oattr);
182 5 push @$oa, $oattr;
183 }
184 }
185 5 $ha->{other_attrs} = $oa;
186 # warn join(' / ', %$ha);
187 }
188 28 ▶ *$flusher = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
189 # warn "level 2 flusher called for $flusher";
190 28 my ($class, $self) = @_;
191 28 for my $attr (keys %$has_a) {
192 18 ▶ my $parent = $self->$attr() or next;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->$attr or next 9 9 18
193 9 for my $oattr (@{$has_a->{$attr}->{other_attrs}}) {
194 # warn "call $self->$attr->flush($oattr)";
195 9 $parent->flush_has_many_keys($oattr);
196 9 $parent->flush_icache($oattr);
197 }
198 }
199 8 };
200 8 goto &$flusher;
201 76 };
202 }
203  
204 22 ▶sub find_relation_by_attr {
Subroutine find_relation_by_attr: called CC=4 SCAR=15.9
Pod undocumented
205 22 my ($class, $klass, $attr) = @_;
206 22 ▶ $relation->{$klass} or return;
Branch: 1 branch
BranchTrueFalseTotal
$relation->{$klass} or return 22 0 22
207  
208 22 ▶ if (my $has_a = $relation->{$klass}->{has_a}->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $has_a = $relation->{$klass}{"has_a"}{$attr}) 0 22 22
209 0 return $has_a;
210 }
211  
212 22 ▶ if (my $has_many = $relation->{$klass}->{has_many}->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $has_many = $relation->{$klass}{"has_many"}{$attr}) 22 0 22
213 22 return $has_many;
214 }
215  
216 0 return;
217 }
218  
219 12 1;
220  
221 __END__
↑ Summary MoCo.pm »