MoCo.pm

filter: Statementstmt 87.8% 534 / 608 Branchbran 61.6% 196 / 318 Conditioncond 43.1% 41 / 95 MC/DCmcdc 18.6% 11 / 59 Subroutinesub 93.5% 87 / 93 Podpod 56.6% 30 / 53 totaltotal 73.3% 899 / 1226 CC 219 SCAR 26.8
CC
219
Cov
76%
merge_with
132.0
inflate_column
48.3
object_id
27.6
CRAP 14.6
« MoCo/Relation.pm ↑ Summary MoCo/Column/utf8.pm »
1 package DBIx::MoCo;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use base qw (Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use DBIx::MoCo::Relation;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use DBIx::MoCo::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use DBIx::MoCo::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use DBIx::MoCo::Cache::Dummy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use DBIx::MoCo::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use DBIx::MoCo::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Class::Trigger;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 our $VERSION = '0.18';
20 12 our $AUTOLOAD;
21  
22 12 my $cache_status = {
23 retrieve_count => 0,
24 retrieve_cache_count => 0,
25 retrieve_icache_count => 0,
26 retrieve_all_count => 0,
27 has_many_count => 0,
28 has_many_cache_count => 0,
29 has_many_icache_count => 0,
30 retrieved_oids => [],
31 };
32 12 my ($db,$session,$schema);
33  
34 12 __PACKAGE__->mk_classdata($_) for
35 qw(cache_object default_cache_expiration icache_expiration
36 cache_null_object table cache_cols_only _db_object save_explicitly list_class);
37  
38 ## NOTE: INIT block does not work well under mod_perl or POE.
39 ## Please set cache_object() explicitly if you want to use transparent caching.
40 # INIT {
41 # unless (defined __PACKAGE__->cache_object) {
42 # if (Cache::FastMmap->require) {
43 # my $file = File::Spec->catfile('/tmp', __PACKAGE__);
44  
45 # File::Spec->require or die $@;
46 # __PACKAGE__->cache_object(
47 # Cache::FastMmap->new(
48 # share_file => $file,
49 # unlink_on_exit => 1,
50 # expire_time => 600, # sec
51 # ) or die $!
52 # );
53  
54 # chmod(0666, $file) or die $! if -e $file;
55 # } else {
56 # warn "Using DBIx::MoCo::Cache is now deprecated because of memory leak."
57 # . "Install Cache::FastMmap instead, or setup cache_object explicitly.";
58  
59 # DBIx::MoCo::Cache->require or die $@;
60 # __PACKAGE__->cache_object( DBIx::MoCo::Cache->new );
61 # }
62 # }
63 # }
64  
65 12 __PACKAGE__->default_cache_expiration(60 * 60 * 3); # 3 hours
66 12 __PACKAGE__->icache_expiration(0); # Instance cache
67 12 __PACKAGE__->cache_null_object(1);
68  
69 # SESSION & CACHE CONTROLLERS
70 23 ▶__PACKAGE__->add_trigger(after_create => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
71 23 my ($class, $self) = @_;
72 23 ▶ $self or confess '$self is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$self or confess("\$self is not specified") 23 0 23
73 23 $class->store_self_cache($self);
74 23 $class->flush_belongs_to($self);
75 12 });
76 12 ▶__PACKAGE__->add_trigger(before_update => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
77 12 my ($class, $self) = @_;
78 12 ▶ $self or confess '$self is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$self or confess("\$self is not specified") 12 0 12
79 12 $class->flush_self_cache($self);
80 12 });
81 12 ▶__PACKAGE__->add_trigger(after_update => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
82 12 my ($class, $self) = @_;
83 12 ▶ $self or confess '$self is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$self or confess("\$self is not specified") 12 0 12
84 12 $class->store_self_cache($self);
85 12 });
86 5 ▶__PACKAGE__->add_trigger(before_delete => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
87 5 my ($class, $self) = @_;
88 5 ▶ $self or confess '$self is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$self or confess("\$self is not specified") 5 0 5
89 5 $class->flush_self_cache($self);
90 5 $class->flush_belongs_to($self);
91 12 });
92  
93 104 ▶sub cache_status { $cache_status }
Subroutine cache_status: called CC=1 SCAR=0.0
Pod documented
94  
95 305 ▶sub cache {
Subroutine cache: called CC=21 SCAR=31.0
Pod undocumented
96 305 my $class = shift;
97 305 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 48 257 305
98  
99 ## It is no matter costs of creating Dummy objects because it is a singleton.
100 305 ▶ my $cache = $class->cache_object || DBIx::MoCo::Cache::Dummy->instance;
Condition66%2/3
$class->cache_object || 'DBIx::MoCo::Cache::Dummy'->instance
l!l&&r!l&&!r
239660
MC/DC0%0/2
$class->cache_object || 'DBIx::MoCo::Cache::Dummy'->instance
$class->cache_object 'DBIx::MoCo::Cache::Dummy'->instance
Truth table66%2/3
A || B
A= $class->cache_objectB= 'DBIx::MoCo::Cache::Dummy'->instance
ABresult
1X1
011
000
101  
102 305 my ($k,$v,$ex) = @_;
103 # warn "$cache in $class";
104 305 my $s = $class->is_in_session;
105 305 ▶ if (defined $v) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $v) { } 154 151 305
elsif ($k) { } 151 0 151
106 154 ▶ $ex ||= $class->default_cache_expiration;
Condition33%1/3
$ex ||= $class->default_cache_expiration
l!l&&r!l&&!r
01540
MC/DC0%0/2
$ex ||= $class->default_cache_expiration
$ex $class->default_cache_expiration
Truth table33%1/3
A ||= B
A= $exB= $class->default_cache_expiration
ABresult
1X1
011
000
107 154 ▶ $ex = "+$ex" if ($ex && ref($cache) eq 'Cache::Memory');
Branch: 1 branch
BranchTrueFalseTotal
if $ex and ref $cache eq "Cache::Memory" 2 152 154
Condition66%2/3
$ex and ref $cache eq "Cache::Memory"
!ll&&!rl&&r
01522
MC/DC50%1/2
$ex and ref $cache eq "Cache::Memory"
$ex ref $cache eq "Cache::Memory"
Truth table66%2/3
A and B
A= $exB= ref $cache eq "Cache::Memory"
ABresult
0X0
100
111
108 154 ▶ if ($v eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($v eq '') { } 26 128 154
109 26 ▶ if ($cache->can('remove')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->can("remove")) 26 0 26
110 26 $cache->remove($k);
111 }
112 26 ▶ if ($s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 1 25 26
113 1 ▶ delete $s->{cache}->{$k} if $k;
Branch: 1 branch
BranchTrueFalseTotal
if $k 1 0 1
114 }
115 } else {
116 128 ▶ if ($class->cache_cols_only && ref($v) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($class->cache_cols_only and ref $v and ref($v) =~ /::/ and $v->isa($class)) { } 42 86 128
Condition77%7/9
$class->cache_cols_only and ref $v and ref($v) =~ /::/ and $v->isa($class)
!ll&&!rl&&r
86042
$class->cache_cols_only and ref $v and ref($v) =~ /::/
!ll&&!rl&&r
761042
$class->cache_cols_only and ref $v
!ll&&!rl&&r
76052
MC/DC50%2/4
$class->cache_cols_only and ref $v and ref($v) =~ /::/ and $v->isa($class)
$class->cache_cols_only ref $v ref($v) =~ /::/ $v->isa($class)
Truth table60%3/5
A and B and C and D
A= $class->cache_cols_onlyB= ref $vC= ref($v) =~ /::/D= $v->isa($class)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
117 ref($v) =~ /::/ && $v->isa($class)) {
118 # remove additional elements
119 42 my @cols = @{$v->columns};
120 42 for (qw(changed_cols to_be_updated object_id)) {
121 126 ▶ push @cols, $_ if (defined $v->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $v->{$_} 84 42 126
122 }
123 42 my $hash = {map {$_ => $v->{$_}} @cols};
124 42 my $o = bless $hash, $class;
125 42 $cache->set($k,$o,$ex);
126 42 ▶ $s->{cache}->{$k} = $o if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 42 42
127 } else {
128 86 $cache->set($k,$v,$ex);
129 86 ▶ $s->{cache}->{$k} = $v if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 13 73 86
130 }
131 }
132 # warn $cache . '->set(' . $k . ')';
133 154 return $v;
134 } elsif ($k) {
135 # warn "hit session cache for $k" if ($s && $s->{cache}->{$k});
136 151 ▶ return $s->{cache}->{$k} || $cache->get($k);
Condition100%3/3
$s->{'cache'}{$k} || $cache->get($k)
l!l&&r!l&&!r
29950
MC/DC100%2/2
$s->{'cache'}{$k} || $cache->get($k)
$s->{'cache'}{$k} $cache->get($k)
Truth table100%3/3
A || B
A= $s->{'cache'}{$k}B= $cache->get($k)
ABresult
1X1
011
000
137 }
138 }
139  
140 0 ▶sub flush_belongs_to {} # it's delivered from MoCo::Relation
Subroutine flush_belongs_to: not called CC=1 SCAR=0.0
Pod documented
141  
142 17 ▶sub flush_self_cache {
Subroutine flush_self_cache: called CC=7 SCAR=21.8
Pod documented
143 17 my ($class, $self) = @_;
144 17 ▶ if (!$self && ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self and ref $class) 0 17 17
Condition33%1/3
not $self and ref $class
!ll&&!rl&&r
1700
MC/DC0%0/2
not $self and ref $class
not $self ref $class
Truth table33%1/3
not(A) and B
A= not $selfB= ref $class
ABresult
0X0
100
111
145 0 $self = $class;
146 0 $class = ref $self;
147 }
148 17 ▶ $self or confess '$self is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$self or confess("\$self is not specified") 17 0 17
149  
150 17 ▶ return unless $class->cache_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->cache_object 7 10 17
151  
152 10 ▶ my $rm = $class->cache_object->can('remove') ? 'remove' : 'delete';
Branch: 1 branch
BranchTrueFalseTotal
$class->cache_object->can('remove') ? : 10 0 10
153 10 for (@{$self->object_ids}) {
154 # warn "flush $_";
155 #weaken($class->cache($_));
156 18 $class->cache_object->$rm($_);
157 }
158 }
159  
160 65 ▶sub store_self_cache {
Subroutine store_self_cache: called CC=6 SCAR=18.2
Pod documented
161 65 my ($class, $self) = @_;
162 65 ▶ if (!$self && ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self and ref $class) 3 62 65
Condition66%2/3
not $self and ref $class
!ll&&!rl&&r
6203
MC/DC50%1/2
not $self and ref $class
not $self ref $class
Truth table66%2/3
not(A) and B
A= not $selfB= ref $class
ABresult
0X0
100
111
163 3 $self = $class;
164 3 $class = ref $self;
165 }
166 65 ▶ $self or confess '$self is not specified';
Branch: 1 branch
BranchTrueFalseTotal
$self or confess("\$self is not specified") 65 0 65
167 # warn "store $_" for @{$self->object_ids};
168 65 my $icache = $self->icache;
169 65 $self->flush_icache;
170 65 $class->cache($_, $self) for @{$self->object_ids};
171 65 ▶ $self->icache($icache) if $icache;
Branch: 1 branch
BranchTrueFalseTotal
if $icache 0 65 65
172 }
173  
174 87 ▶sub icache {
Subroutine icache: called CC=5 SCAR=24.7
Pod undocumented
175 87 my $self = shift;
176 87 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 87 87
177 0 $self->{_icache} = shift;
178 } else {
179 87 my $ex = $self->icache_expiration;
180 87 ▶ $ex > 0 or return;
Branch: 1 branch
BranchTrueFalseTotal
$ex > 0 or return 0 87 87
181 0 ▶ if (!$self->{_icache} ||
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_icache"} or $self->{"_icache"}{"_created"} + $ex < time) 0 0 0
Condition0%0/3
not $self->{"_icache"} or $self->{"_icache"}{"_created"} + $ex < time
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"_icache"} or $self->{"_icache"}{"_created"} + $ex < time
not $self->{"_icache"} $self->{"_icache"}{"_created"} + $ex < time
Truth table0%0/3
not(A) or B
A= not $self->{"_icache"}B= $self->{"_icache"}{"_created"} + $ex < time
ABresult
1X1
011
000
182 ($self->{_icache}->{_created} + $ex < time())) {
183 0 $self->{_icache} = {_created => time()};
184 }
185 }
186 0 return $self->{_icache};
187 }
188  
189 74 ▶sub flush_icache {
Subroutine flush_icache: called CC=3 SCAR=17.3
Pod undocumented
190 74 my $self = shift;
191 74 ▶ $self->{_icache} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_icache"} or return 0 74 74
192 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
193 # warn "flush icache $_[0] for " . $self;
194 0 delete $self->{_icache}->{$_[0]};
195 } else {
196 0 $self->{_icache} = undef;
197 }
198 }
199  
200 33 ▶sub has_many_keys_cache_name {
Subroutine has_many_keys_cache_name: called CC=3 SCAR=11.4
Pod undocumented
201 33 my $self = shift;
202 33 ▶ my $attr = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $attr = shift() or return 33 0 33
203 33 ▶ my $oid = $self->object_id or return;
Branch: 1 branch
BranchTrueFalseTotal
my $oid = $self->object_id or return 33 0 33
204 33 return sprintf('%s-%s_keys', $oid, $attr);
205 }
206  
207 10 ▶sub flush_has_many_keys {
Subroutine flush_has_many_keys: called CC=2 SCAR=7.0
Pod undocumented
208 10 my $self = shift;
209 10 ▶ my $attr = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $attr = shift() or return 10 0 10
210 # $self->flush($self->has_many_keys_name($attr));
211 # $self->flush($self->has_many_max_offset_name($attr));
212 10 my $key = $self->has_many_keys_cache_name($attr);
213 10 $self->cache($key, '');
214 }
215  
216 # session controllers
217 7 ▶sub start_session {
Subroutine start_session: called CC=2 SCAR=6.9
Pod documented
218 7 my $class = shift;
219 7 ▶ $class->end_session if $class->is_in_session;
Branch: 1 branch
BranchTrueFalseTotal
if $class->is_in_session 3 4 7
220 7 $session = {
221 changed_objects => [],
222 cache => {},
223 pid => $$,
224 created => time(),
225 };
226 }
227  
228 435 ▶sub is_in_session { $session }
Subroutine is_in_session: called CC=1 SCAR=0.0
Pod documented
229 15 ▶sub session { $session }
Subroutine session: called CC=1 SCAR=0.0
Pod undocumented
230 0 ▶sub session_cache {
Subroutine session_cache: not called CC=2 SCAR=17.9
Pod undocumented
231 0 ▶ my $s = shift->session or return;
Branch: 1 branch
BranchTrueFalseTotal
my $s = (shift())->session or return 0 0 0
232 0 return $s->{cache};
233 }
234  
235 6 ▶sub end_session {
Subroutine end_session: called CC=2 SCAR=7.0
Pod documented
236 6 my $class = shift;
237 6 ▶ $session or return;
Branch: 1 branch
BranchTrueFalseTotal
$session or return 6 0 6
238 6 $class->save_changed;
239 6 $cache_status->{retrieved_oids} = [];
240 6 $session = undef;
241 }
242  
243 86 ▶sub save_changed {
Subroutine save_changed: called CC=4 SCAR=13.9
Pod undocumented
244 86 my $class = shift;
245 86 ▶ $class->is_in_session or return;
Branch: 1 branch
BranchTrueFalseTotal
$class->is_in_session or return 11 75 86
246 11 for (@{$class->session->{changed_objects}}) {
247 4 ▶ $_ or next;
Branch: 1 branch
BranchTrueFalseTotal
$_ or next 4 0 4
248 4 $_->save;
249 }
250 }
251  
252 # CLASS DEFINISION METHODS
253 76 ▶sub relation { 'DBIx::MoCo::Relation' }
Subroutine relation: called CC=1 SCAR=0.0
Pod undocumented
254  
255 160 ▶sub db_object {
Subroutine db_object: called CC=2 SCAR=6.9
Pod undocumented
256 160 my $class = shift;
257 160 ▶ if (my $db = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $db = shift()) 16 144 160
258 16 $class->_db_object($db);
259 }
260 160 $class->_db_object;
261 }
262  
263 38 ▶sub has_a {
Subroutine has_a: called CC=1 SCAR=0.0
Pod documented
264 38 my $class = shift;
265 38 $class->relation->register($class, 'has_a', @_);
266 }
267 38 ▶sub has_many {
Subroutine has_many: called CC=1 SCAR=0.0
Pod documented
268 38 my $class = shift;
269 38 $class->relation->register($class, 'has_many', @_);
270 }
271  
272 485 ▶sub schema {
Subroutine schema: called CC=3 SCAR=11.0
Pod documented
273 485 my $class = shift;
274 485 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 136 349 485
275 485 ▶ unless ($schema->{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{$class}) 15 470 485
276 15 $schema->{$class} = DBIx::MoCo::Schema->new($class);
277 }
278 485 return $schema->{$class};
279 }
280  
281 12 for my $attr (qw/primary_keys unique_keys retrieve_keys columns/) {
282 48 my $classdata = "_" . $attr;
283 48 __PACKAGE__->mk_classdata($classdata);
284  
285 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
286 48 ▶ *{__PACKAGE__ . "\::$attr"} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
287 480 my $class = shift;
288 480 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 479 480
289 1 ▶ my @keys = (ref $_[0] and ref $_[0] eq 'ARRAY') ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq 'ARRAY' ? : 1 0 1
Condition33%1/3
ref $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "ARRAY"
ref $_[0] ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
290 1 $class->$classdata(\@keys);
291 } else {
292 479 ▶ $class->$classdata
Branch: 1 branch
BranchTrueFalseTotal
$class->$classdata ? : 1 478 479
293 ? $class->$classdata
294 : $class->schema->$attr;
295 }
296 48 };
297 }
298  
299 47 ▶sub has_muid {
Subroutine has_muid: called CC=2 SCAR=9.2
Pod undocumented
300 47 my $class = shift;
301 return ($class->has_column('muid') &&
302 0 ▶ scalar @{$class->primary_keys} == 1);
Condition33%1/3
$class->has_column('muid') && scalar @{$class->primary_keys;} == 1
!ll&&!rl&&r
4700
MC/DC0%0/2
$class->has_column('muid') && scalar @{$class->primary_keys;} == 1
$class->has_column('muid') scalar @{$class->primary_keys;} == 1
Truth table33%1/3
A && B
A= $class->has_column('muid')B= scalar @{$class->primary_keys;} == 1
ABresult
0X0
100
111
303 }
304  
305 54 ▶sub has_column {
Subroutine has_column: called CC=3 SCAR=11.0
Pod documented
306 54 my $class = shift;
307 54 ▶ my $col = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $col = shift() or return 54 0 54
308 54 ▶ $class->columns or return;
Branch: 1 branch
BranchTrueFalseTotal
$class->columns or return 53 1 54
309 53 grep { $col eq $_ } @{$class->columns};
310 }
311  
312 5 ▶sub utf8_columns {
Subroutine utf8_columns: called CC=1 SCAR=0.0
Pod documented
313 5 my $class = shift;
314 5 $class->schema->utf8_columns(@_);
315 }
316  
317 3 ▶sub is_utf8_column {
Subroutine is_utf8_column: called CC=4 SCAR=14.5
Pod undocumented
318 3 my $class = shift;
319 3 ▶ my $col = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $col = shift() or return 3 0 3
320 3 ▶ my $utf8 = $class->utf8_columns or return;
Branch: 1 branch
BranchTrueFalseTotal
my $utf8 = $class->utf8_columns or return 3 0 3
321 3 ▶ ref $utf8 eq 'ARRAY' or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $utf8 eq "ARRAY" or return 3 0 3
322 3 return grep { $_ eq $col } @$utf8;
323 }
324  
325 # DATA OPERATIONAL METHODS
326 539 ▶sub object_id {
Subroutine object_id: called CC=22 SCAR=33.2
Pod undocumented
327 539 my $self = shift;
328 539 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
2323070
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
329 539 ▶ $self = undef unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 307 232 539
330 539 ▶ if ($self && $self->{object_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $self->{"object_id"}) 185 354 539
Condition100%3/3
$self and $self->{"object_id"}
!ll&&!rl&&r
30747185
MC/DC100%2/2
$self and $self->{"object_id"}
$self $self->{"object_id"}
Truth table100%3/3
A and B
A= $selfB= $self->{"object_id"}
ABresult
0X0
100
111
331 185 return $self->{object_id};
332 }
333 354 ▶ my $prefix = $class->object_id_prefix || '';
Condition50%1/2
$class->object_id_prefix || ''
l!l
3540
MC/DC0%0/1
$class->object_id_prefix || ''
$class->object_id_prefix
Truth table50%1/2
A
A= $class->object_id_prefix
Aresult
11
00
334 354 my ($key, $col);
335 354 ▶ if ($self && @{$class->retrieve_keys || $class->primary_keys}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self and @{$class->retrieve_keys or $class->primary_keys;}) { } 47 307 354
elsif ($_[3]) { } 75 232 307
elsif (@{$class->primary_keys;} == 1) { } 210 22 232
Condition50%3/6
$self and @{$class->retrieve_keys or $class->primary_keys;}
!ll&&!rl&&r
307047
$class->retrieve_keys || $class->primary_keys
l!l&&r!l&&!r
0470
MC/DC50%1/2
$self and @{$class->retrieve_keys or $class->primary_keys;}
$self @{$class->retrieve_keys or $class->primary_keys;}
MC/DC0%0/2
$class->retrieve_keys || $class->primary_keys
$class->retrieve_keys $class->primary_keys
Truth table66%2/3
A and B
A= $selfB= @{$class->retrieve_keys or $class->primary_keys;}
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $class->retrieve_keysB= $class->primary_keys
ABresult
1X1
011
000
336 47 ▶ if ($self->has_muid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_muid) { } 0 47 47
337 0 $key = $self->muid;
338 } else {
339 47 ▶ for (sort @{$class->retrieve_keys || $class->primary_keys}) {
Condition33%1/3
$class->retrieve_keys || $class->primary_keys
l!l&&r!l&&!r
0470
MC/DC0%0/2
$class->retrieve_keys || $class->primary_keys
$class->retrieve_keys $class->primary_keys
Truth table33%1/3
A || B
A= $class->retrieve_keysB= $class->primary_keys
ABresult
1X1
011
000
340 56 ▶ defined($self->{$_}) or warn "$_ is undefined for $self" and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$_} or warn "$_ is undefined for $self" and return 56 0 56
Condition20%1/5
defined $self->{$_} || warn("$_ is undefined for $self") && (return)
l!l&&r!l&&!r
5600
warn "$_ is undefined for $self" and return
!ll
00
MC/DC0%0/2
defined $self->{$_} || warn("$_ is undefined for $self") && (return)
defined $self->{$_} warn "$_ is undefined for $self"
Truth table33%1/3
A || B
A= defined $self->{$_}B= warn "$_ is undefined for $self"
ABresult
1X1
011
000
341 56 $key .= "-$_-" . $self->{$_};
342 }
343 47 ▶ $key or die "couldn't create object_id for " . $self;
Branch: 1 branch
BranchTrueFalseTotal
$key or die q[couldn't create object_id for ] . $self 47 0 47
344 47 $key = $prefix . $key;
345 }
346 } elsif ($_[3]) {
347 75 my %args = @_;
348 75 $key .= "-$_-$args{$_}" for (sort keys %args);
349 75 $key = $prefix . $key;
350 232 } elsif (@{$class->primary_keys} == 1) {
351 210 my @args = @_;
352 210 ▶ $col = defined $args[1] ? $args[0] : $class->primary_keys->[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $args[1] ? : 175 35 210
353 210 ▶ my $value = defined $args[1] ? $args[1] : $args[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $args[1] ? : 175 35 210
354 210 ▶ if ($col eq 'muid') {
Branch: 1 branch
BranchTrueFalseTotal
if ($col eq 'muid') { } 0 210 210
355 0 $key = $value;
356 } else {
357 210 $key = $prefix . '-' . $col . '-' . $value;
358 }
359 }
360 354 ▶ $self->{object_id} = $key if $self;;
Branch: 1 branch
BranchTrueFalseTotal
if $self 47 307 354
361 354 return $key;
362 }
363  
364 354 ▶sub object_id_prefix {
Subroutine object_id_prefix: called CC=2 SCAR=7.1
Pod documented
365 354 my $class = shift;
366 354 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 354 354
367 354 return $class;
368 }
369  
370 144 ▶sub db { $_[0]->db_object }
Subroutine db: called CC=1 SCAR=0.0
Pod undocumented
371  
372 80 ▶sub retrieve {
Subroutine retrieve: called CC=6 SCAR=17.9
Pod documented
373 80 my $cs = $cache_status;
374 80 $cs->{retrieve_count}++;
375 80 my $class = shift;
376 80 ▶ $_[0] or carp "Retrieve keys not found";
Branch: 1 branch
BranchTrueFalseTotal
$_[0] or carp("Retrieve keys not found") 80 0 80
377 80 my $oid = $class->object_id(@_);
378 80 my $c = $class->cache($oid);
379 80 ▶ if (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c) { } 46 34 80
380 # warn "use cache $oid";
381 46 $cs->{retrieve_cache_count}++;
382 46 return $c;
383 } else {
384 # warn "use db $oid";
385 34 my $o = $class->retrieve_by_db(@_);
386 34 ▶ if ($o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o) { } 18 16 34
387 18 $class->store_self_cache($o);
388 18 ▶ push @{$cs->{retrieved_oids}}, $oid if $class->is_in_session;
Branch: 1 branch
BranchTrueFalseTotal
if $class->is_in_session 1 17 18
389 } else {
390 # $class->cache($oid => $o) if $o;
391 # cache null object for performance.
392 16 ▶ $class->cache($oid => $o) if $class->cache_null_object;
Branch: 1 branch
BranchTrueFalseTotal
if $class->cache_null_object 16 0 16
393 }
394 34 return $o;
395 }
396 }
397  
398 34 ▶sub retrieve_by_db {
Subroutine retrieve_by_db: called CC=3 SCAR=11.0
Pod documented
399 34 my $class = shift;
400 34 ▶ my %args = defined $_[1] ? @_ : ($class->primary_keys->[0] => $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 18 16 34
401 34 my $res = $class->db->select($class->table,'*',\%args);
402 34 my $h = $res->[0];
403 34 ▶ return $h ? $class->new(%$h) : '';
Branch: 1 branch
BranchTrueFalseTotal
$h ? : 18 16 34
404 }
405  
406 1 ▶sub restore_from_db {
Subroutine restore_from_db: called CC=4 SCAR=14.1
Pod documented
407 1 my $self = shift;
408 1 ▶ my $class = ref $self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or return 1 0 1
409 1 ▶ my $hash = $self->primary_keys_hash or return;
Branch: 1 branch
BranchTrueFalseTotal
my $hash = $self->primary_keys_hash or return 1 0 1
410 1 my $res = $class->db->select($class->table,'*',$hash);
411 1 ▶ my $h = $res->[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $h = $res->[0] or return 1 0 1
412 1 @{$self}{keys %$h} = @{$h}{keys %$h};
413 1 $class->store_self_cache($self);
414 1 return $self;
415 }
416  
417 22 ▶sub retrieve_multi {
Subroutine retrieve_multi: called CC=15 SCAR=32.5
Pod documented
418 22 my $class = shift;
419 22 ▶ my @list = @_ or return $class->_list([]);
Branch: 1 branch
BranchTrueFalseTotal
my(@list) = or return $class->_list([]) 21 1 22
420  
421 21 my (@cached_objects, @non_cached_queries);
422 21 ▶ if ($class->cache_object && $class->cache_object->can('get_multi')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->cache_object and $class->cache_object->can('get_multi')) { } 0 21 21
Condition33%1/3
$class->cache_object and $class->cache_object->can("get_multi")
!ll&&!rl&&r
0210
MC/DC0%0/2
$class->cache_object and $class->cache_object->can("get_multi")
$class->cache_object $class->cache_object->can("get_multi")
Truth table33%1/3
A and B
A= $class->cache_objectB= $class->cache_object->can("get_multi")
ABresult
0X0
100
111
423 0 my $ids = [ map { $class->object_id(%$_) } @list ];
424 0 ▶ my $hash = $class->cache_object->get_multi(@$ids) || {};
Condition0%0/2
$class->cache_object->get_multi(@$ids) || {}
l!l
00
MC/DC0%0/1
$class->cache_object->get_multi(@$ids) || {}
$class->cache_object->get_multi(@$ids)
Truth table0%0/2
A
A= $class->cache_object->get_multi(@$ids)
Aresult
11
00
425  
426 0 ▶ for (my $i = 0; $i <= $#list; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#list) 0 0 0
427 0 my $object = $hash->{$ids->[$i]};
428 0 ▶ $object
Branch: 1 branch
BranchTrueFalseTotal
$object ? : 0 0 0
429 ? push @cached_objects, $object
430 : push @non_cached_queries, $list[$i];
431 }
432 } else {
433 21 for (@list) {
434 47 my $cached_object = $class->cache( $class->object_id(%$_) );
435 47 ▶ $cached_object
Branch: 1 branch
BranchTrueFalseTotal
$cached_object ? : 41 6 47
436 ? push @cached_objects, $cached_object
437 : push @non_cached_queries, $_;
438 }
439 }
440  
441 ## Updating cache status
442 21 $class->cache_status->{retrieve_count} += scalar @list;
443 21 $class->cache_status->{retrieve_cache_count} += scalar @cached_objects;
444  
445 ## All objects were found in cache.
446 21 ▶ if (@cached_objects == @list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cached_objects == @list) 17 4 21
447 17 my @ordered= $class->_merge_objects(\@list, @cached_objects);
448 17 ▶ wantarray ? return @ordered : return $class->_list(\@ordered);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 17 17
449 }
450  
451 4 my (@clauses, @bind_values);
452 4 for my $cond (@non_cached_queries) {
453 my $subclause = join ' AND ', map {
454 6 push @bind_values, $cond->{$_};
455 10 sprintf "%s = ?", $_
456 } keys %$cond;
457  
458 6 push @clauses, $subclause;
459 }
460 4 my $where_clause = join ' OR ', map { sprintf "(%s)", $_ } @clauses;
461  
462 4 my @objects_from_db = $class->search( where => [ $where_clause, @bind_values ] );
463  
464 4 ▶ if ($class->is_in_session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->is_in_session) 0 4 4
465 0 push @{$class->cache_status->{retrieved_oids}}, map { $_->object_id } @objects_from_db;
466 }
467  
468 4 for my $object (@objects_from_db) {
469 6 $class->store_self_cache($object);
470 }
471  
472 4 my @merged = $class->_merge_objects(\@list, @cached_objects, @objects_from_db);
473 4 ▶ wantarray ? return @merged : return $class->_list(\@merged);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
474 }
475  
476 21 ▶sub _merge_objects {
Subroutine _merge_objects: called CC=4 SCAR=13.9
477 21 my $class = shift;
478 21 my $order = shift;
479  
480 21 my $tied = tie my %idt, 'Tie::IxHash'; ## orderd Hash
481 21 $tied->Push($class->object_id( %$_ ) => undef) for @$order;
482  
483 21 for (@_) {
484 47 my $id = $_->object_id;
485 47 ▶ die "assert" if not exists $idt{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $idt{$id} 0 47 47
486 47 $tied->Push($id => $_);
487 }
488  
489 ## cache_null_object() is now deprecated
490 # for (keys %idt) {
491 # if (not $idt{$_} and $class->cache_null_object) {
492 # $class->cache( $_ => '' );
493 # }
494 # }
495  
496 21 grep { defined $_ } values %idt;
497 }
498  
499 2 ▶sub retrieve_or_create {
Subroutine retrieve_or_create: called CC=2 SCAR=6.9
Pod documented
500 2 my $class = shift;
501 2 my %args = @_;
502 2 my %keys;
503 2 @keys{@{$class->primary_keys}} = @args{@{$class->primary_keys}};
504 2 ▶ $class->retrieve(%keys) || $class->create(%args);
Branch: 1 branch
BranchTrueFalseTotal
$class->retrieve(%keys) or $class->create(%args) 1 1 2
505 }
506  
507 4 ▶sub retrieve_all {
Subroutine retrieve_all: called CC=3 SCAR=11.0
Pod documented
508 4 my $cs = $cache_status;
509 4 $cs->{retrieve_all_count}++;
510 4 my $class = shift;
511 4 my %args = @_;
512 4 my $result = [];
513 4 my $list = $class->retrieve_all_id_hash(%args);
514 4 push @$result, $class->retrieve(%$_) for (@$list);
515 4 ▶ wantarray ? @$result :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
516 $class->_list($result);
517 }
518  
519 6 ▶sub retrieve_all_id_hash {
Subroutine retrieve_all_id_hash: called CC=2 SCAR=7.1
Pod undocumented
520 6 my $class = shift;
521 6 my %args = @_;
522 6 $args{table} = $class->table;
523 6 ▶ $args{field} = join(',', @{$class->retrieve_keys || $class->primary_keys});
Condition33%1/3
$class->retrieve_keys || $class->primary_keys
l!l&&r!l&&!r
060
MC/DC0%0/2
$class->retrieve_keys || $class->primary_keys
$class->retrieve_keys $class->primary_keys
Truth table33%1/3
A || B
A= $class->retrieve_keysB= $class->primary_keys
ABresult
1X1
011
000
524 6 my $res = $class->db->search(%args);
525 6 return $res;
526 }
527  
528 23 ▶sub create {
Subroutine create: called CC=5 SCAR=16.5
Pod documented
529 23 my $class = shift;
530 23 my %args = @_;
531 23 $class->call_trigger('before_create', \%args);
532 23 my $o = $class->new(%args);
533 # if ($class->is_in_session && $o->has_primary_keys) {
534 # $o->set(to_be_inserted => 1);
535 # $o->changed_cols->{$_}++ for (keys %args);
536 # push @{$class->session->{changed_objects}}, $o;
537 # } else {
538 23 ▶ if ($class->save_explicitly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->save_explicitly) { } 0 23 23
539 0 $o->set(to_be_inserted => 1);
540 0 $o->changed_cols->{$_}++ for keys %args;
541 } else {
542 23 ▶ $class->db->insert($class->table,\%args) or croak 'couldnt create';
Branch: 1 branch
BranchTrueFalseTotal
$class->db->insert($class->table, \%args) or croak("couldnt create") 23 0 23
543 23 my $pk = $class->primary_keys->[0];
544 23 ▶ unless (defined $args{$pk}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{$pk}) 6 17 23
545 6 my $id = $class->db->last_insert_id;
546 6 $o->set($pk => $id);
547 }
548 }
549 23 $class->call_trigger('after_create', $o);
550 23 return $o;
551 }
552  
553 3 ▶sub delete {
Subroutine delete: called CC=9 SCAR=22.4
Pod documented
554 3 my $self = shift;
555 3 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 1 3
556 3 ▶ $self = shift unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 2 3
557 3 ▶ $self or return;
Branch: 1 branch
BranchTrueFalseTotal
$self or return 3 0 3
558 3 $self->call_trigger('before_delete', $self);
559 3 ▶ $self->has_primary_keys or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_primary_keys or return 3 0 3
560 3 my %args;
561 3 for (@{$class->primary_keys}) {
562 3 $args{$_} = $self->{$_};
563 3 ▶ defined($args{$_}) or die "$self doesn't have $_";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} or die "$self doesn't have $_" 3 0 3
564 }
565 3 ▶ %args or die "$self doesn't have where condition";
Branch: 1 branch
BranchTrueFalseTotal
%args or die "$self doesn't have where condition" 3 0 3
566 3 ▶ my $res = $class->db->delete($class->table,\%args) or croak 'couldnt delete';
Branch: 1 branch
BranchTrueFalseTotal
my $res = $class->db->delete($class->table, \%args) or croak("couldnt delete") 3 0 3
567 3 $self = undef;
568 3 return $res;
569 }
570  
571 1 ▶sub delete_all {
Subroutine delete_all: called CC=6 SCAR=18.2
Pod documented
572 1 my $class = shift;
573 1 my %args = @_;
574 1 ▶ ref $args{where} eq 'HASH' or die 'please specify where in hash';
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"where"} eq "HASH" or die "please specify where in hash" 1 0 1
575 1 my $list = $class->retrieve_all_id_hash(%args);
576 1 my $caches = [];
577 1 for (@$list) {
578 2 my $oid = $class->object_id(%$_);
579 2 ▶ my $c = $class->cache($oid) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $c = $class->cache($oid) or next 2 0 2
580 2 push @$caches, $c;
581 }
582 1 $class->call_trigger('before_delete', $_) for (@$caches);
583 1 ▶ $class->db->delete($class->table,$args{where}) or croak 'couldnt delete';
Branch: 1 branch
BranchTrueFalseTotal
$class->db->delete($class->table, $args{"where"}) or croak("couldnt delete") 1 0 1
584 1 return 1;
585 }
586  
587 17 ▶sub search {
Subroutine search: called CC=4 SCAR=13.9
Pod documented
588 17 my $class = shift;
589 17 my %args = @_;
590  
591 17 my $with = delete $args{with};
592  
593 17 $args{table} = $class->table;
594 17 my $res = $class->db->search(%args);
595 17 $_ = $class->new(%$_) for @$res;
596 17 ▶ $class->merge_with($res, $with) if $with;
Branch: 1 branch
BranchTrueFalseTotal
if $with 0 17 17
597  
598 17 ▶ wantarray ? @$res : $class->_list($res);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 4 17
599 }
600  
601 0 ▶sub merge_with {
Subroutine merge_with: not called CC=11 SCAR=48.8
Pod undocumented
602 0 my ($class, $res, $with, $without) = @_;
603  
604 0 ▶ my @with_attrs = (ref $with and ref $with eq 'ARRAY') ? @$with : $with;
Branch: 1 branch
BranchTrueFalseTotal
ref $with && ref $with eq 'ARRAY' ? : 0 0 0
Condition0%0/3
ref $with and ref $with eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $with and ref $with eq "ARRAY"
ref $with ref $with eq "ARRAY"
Truth table0%0/3
A and B
A= ref $withB= ref $with eq "ARRAY"
ABresult
0X0
100
111
605  
606 0 ▶ if ($without) {
Branch: 1 branch
BranchTrueFalseTotal
if ($without) 0 0 0
607 0 ▶ my @withouts = (ref $without and ref $without eq 'ARRAY') ? @$without : $without;
Branch: 1 branch
BranchTrueFalseTotal
ref $without && ref $without eq 'ARRAY' ? : 0 0 0
Condition0%0/3
ref $without and ref $without eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $without and ref $without eq "ARRAY"
ref $without ref $without eq "ARRAY"
Truth table0%0/3
A and B
A= ref $withoutB= ref $without eq "ARRAY"
ABresult
0X0
100
111
608 0 my $regex = sprintf '(?:^%s$)', join '|', @withouts;
609 0 @with_attrs = grep { $_ !~ m/$regex/ } @with_attrs;
610 }
611  
612 0 for my $with_attr (@with_attrs) {
613 0 ▶ my $rel = $class->relation->find_relation_by_attr($class => $with_attr)
Branch: 1 branch
BranchTrueFalseTotal
my $rel = $class->relation->find_relation_by_attr($class, $with_attr) or croak("No such relation for attr '${with_attr}' in $class") 0 0 0
614 or croak "No such relation for attr '$with_attr' in $class";
615  
616 0 ▶ my $key = $rel->{option}->{key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $rel->{"option"}{"key"} or next 0 0 0
617  
618 0 my ($my_key, $other_key);
619 0 ▶ (ref $key and ref $key eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
ref $key && ref $key eq 'HASH' ? : 0 0 0
Condition0%0/3
ref $key and ref $key eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $key and ref $key eq "HASH"
ref $key ref $key eq "HASH"
Truth table0%0/3
A and B
A= ref $keyB= ref $key eq "HASH"
ABresult
0X0
100
111
620 ? ($my_key, $other_key) = %$key
621 : $my_key = $other_key = $key;
622  
623 0 my @queries = map { +{ $other_key => $_->$my_key } } @$res;
624  
625 ## Only creating caches for less SQL queries.
626 ## Those caches will be stored to the session cache if the session is activated.
627 0 $rel->{class}->retrieve_multi(@queries);
628 }
629  
630 0 $res;
631 }
632  
633 5 ▶sub count {
Subroutine count: called CC=4 SCAR=14.8
Pod documented
634 5 my $class = shift;
635 5 my $where = '';
636 5 ▶ if ($_[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[1]) { } 0 5 5
elsif ($_[0]) { } 3 2 5
637 0 my %args = @_;
638 0 $where = \%args;
639 } elsif ($_[0]) {
640 3 $where = shift;
641 }
642 5 my $res = $class->db->search(
643 table => $class->table,
644 field => 'COUNT(*) as count',
645 where => $where,
646 );
647 5 ▶ return $res->[0]->{count} || 0;
Condition50%1/2
$res->[0]{'count'} || 0
l!l
50
MC/DC0%0/1
$res->[0]{'count'} || 0
$res->[0]{'count'}
Truth table50%1/2
A
A= $res->[0]{'count'}
Aresult
11
00
648 }
649  
650 2 ▶sub find {
Subroutine find: called CC=3 SCAR=13.0
Pod documented
651 2 my $class = shift;
652 2 my $where;
653 2 ▶ if ($_[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[1]) { } 0 2 2
elsif ($_[0]) { } 2 0 2
654 0 my %args = @_;
655 0 $where = \%args;
656 } elsif ($_[0]) {
657 2 $where = shift;
658 } else {
659 0 return;
660 }
661 2 $class->search(
662 where => $where,
663 offset => 0,
664 limit => 1,
665 )->first;
666 }
667  
668 3 ▶sub quote {
Subroutine quote: called CC=1 SCAR=0.0
Pod documented
669 3 my $class = shift;
670 3 $class->db->dbh->quote(shift);
671 }
672  
673 0 ▶sub scalar {
Subroutine scalar: not called CC=1 SCAR=6.9
Pod undocumented
674 0 my ($class, $method, @args) = @_;
675 0 scalar $class->$method(@args);
676 }
677  
678 43 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=9 SCAR=22.0
679 43 my $self = $_[0];
680 43 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
31120
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
681 43 ▶ $self = undef unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 12 31 43
682 43 (my $method = $AUTOLOAD) =~ s!.+::!!;
683 43 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 43 43
684 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
685 43 ▶ if ($method =~ /^retrieve_by_(.+?)(_or_create)?$/o) {
Branch: 3 branches
BranchTrueFalseTotal
if ($method =~ /^retrieve_by_(.+?)(_or_create)?$/o) { } 9 34 43
elsif ($method =~ /^(\w+)_as_(\w+)$/o) { } 7 27 34
elsif (defined $self->{$method} or $class->has_column($method)) { } 21 6 27
686 9 my ($by, $create) = ($1,$2);
687 9 ▶ *$AUTOLOAD = $create ? $class->_retrieve_by_or_create_handler($by) :
Branch: 1 branch
BranchTrueFalseTotal
$create ? : 2 7 9
688 $class->_retrieve_by_handler($by);
689 } elsif ($method =~ /^(\w+)_as_(\w+)$/o) {
690 7 my ($col,$as) = ($1,$2);
691 7 *$AUTOLOAD = $class->_column_as_handler($col, $as);
692 ▶ } elsif (defined $self->{$method} || $class->has_column($method)) {
Condition100%3/3
defined $self->{$method} or $class->has_column($method)
l!l&&r!l&&!r
2016
MC/DC100%2/2
defined $self->{$method} or $class->has_column($method)
defined $self->{$method} $class->has_column($method)
Truth table100%3/3
A or B
A= defined $self->{$method}B= $class->has_column($method)
ABresult
1X1
011
000
693 21 ▶ *$AUTOLOAD = sub { shift->param($method, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
694 } else {
695 6 croak sprintf 'Can\'t locate object method "%s" via package %s', $method, $class;
696 }
697 37 goto &$AUTOLOAD;
698 }
699  
700 0 ▶sub inflate_column {
Subroutine inflate_column: not called CC=8 SCAR=38.8
Pod undocumented
701 0 my $class = shift;
702 0 ▶ @_ % 2 and croak "You gave me an odd number of parameters to inflate_column()";
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("You gave me an odd number of parameters to inflate_column()") 0 0 0
703  
704 0 my %args = @_;
705 0 ▶ while (my ($col, $as) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
706 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
707 12 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
708  
709 0 ▶ if (ref $as and ref $as eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $as and ref $as eq 'HASH') { } 0 0 0
Condition0%0/3
ref $as and ref $as eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $as and ref $as eq "HASH"
ref $as ref $as eq "HASH"
Truth table0%0/3
A and B
A= ref $asB= ref $as eq "HASH"
ABresult
0X0
100
111
710 0 for (qw/inflate deflate/) {
711 0 ▶ if ($as->{$_} and ref $as->{$_} ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($as->{$_} and ref $as->{$_} ne "CODE") 0 0 0
Condition0%0/3
$as->{$_} and ref $as->{$_} ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$as->{$_} and ref $as->{$_} ne "CODE"
$as->{$_} ref $as->{$_} ne "CODE"
Truth table0%0/3
A and B
A= $as->{$_}B= ref $as->{$_} ne "CODE"
ABresult
0X0
100
111
712 0 croak sprintf "parameter '%s' takes only CODE reference", $_
713 }
714 }
715  
716 0 ▶ *{"$class\::$col"} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
717 0 my $self = shift;
718 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
719 $as->{deflate}
720 0 ▶ ? $self->param( $col => $as->{deflate}->(@_) )
Branch: 1 branch
BranchTrueFalseTotal
$as->{'deflate'} ? : 0 0 0
721 : $self->param( $col => @_ );
722 } else {
723 $as->{inflate}
724 0 ▶ ? $as->{inflate}->( $self->param($col) )
Branch: 1 branch
BranchTrueFalseTotal
$as->{'inflate'} ? : 0 0 0
725 : $self->param( $col );
726 }
727 }
728 0 } else {
729 0 *{"$class\::$col"} = $class->_column_as_handler($col, $as);
730 }
731 }
732 }
733  
734 {
735 12 my $real_can = \&UNIVERSAL::can;
736 12 ▶ no warnings 'redefine', 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
737 84 ▶ *DBIx::MoCo::can = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
738 84 my ($class, $method) = @_;
739 84 ▶ if (my $sub = $real_can->(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = &$real_can(@_)) 75 9 84
740 # warn "found $method in $class";
741 75 return $sub;
742 }
743 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
744 9 ▶ if (my $auto = *{$class . '::AUTOLOAD'}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $auto = *{$class . '::AUTOLOAD';}{"CODE"}) 0 9 9
745 0 return $auto;
746 }
747 9 $AUTOLOAD = $class . '::' . $method;
748 9 ▶ eval {&DBIx::MoCo::AUTOLOAD(@_)} unless *$AUTOLOAD{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless *$DBIx::MoCo::AUTOLOAD{"CODE"} 9 0 9
749 9 return *$AUTOLOAD{CODE};
750 12 };
751 }
752  
753 9 ▶sub _column_as_handler {
Subroutine _column_as_handler: called CC=3 SCAR=11.0
754 9 my $class = shift;
755 9 my ($colname, $as) = @_;
756 9 ▶ unless (DBIx::MoCo::Column->can($as)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("DBIx::MoCo::Column"->can($as)) 6 3 9
757 6 my $plugin = "DBIx::MoCo::Column::$as";
758 6 $plugin->require;
759 6 ▶ croak "Couldn't load column plugin $plugin: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
760 {
761 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
762 6 push @{"DBIx::MoCo::Column::ISA"}, $plugin;
763 }
764 }
765 17 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
766 17 my $self = shift;
767 17 ▶ my $column = $self->column($colname) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $column = $self->column($colname) or return 16 0 16
768 16 ▶ if (my $new = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new = shift()) 5 11 16
769 5 my $as_string = $as . '_as_string'; # e.g. URI_as_string
770 5 ▶ my $v = $column->can($as_string) ?
Branch: 1 branch
BranchTrueFalseTotal
$column->can($as_string) ? : 5 0 5
771 $column->$as_string($new) : scalar $new;
772 5 $self->param($colname => $v);
773 }
774 16 $self->column($colname)->$as();
775 }
776 9 }
777  
778 34 ▶sub column {
Subroutine column: called CC=2 SCAR=7.1
Pod undocumented
779 34 my $self = shift;
780 34 ▶ my $col = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $col = shift() or return 34 0 34
781 34 return DBIx::MoCo::Column->new($self->{$col});
782 }
783  
784 7 ▶sub _retrieve_by_handler {
Subroutine _retrieve_by_handler: called CC=3 SCAR=11.0
785 7 my $class = shift;
786 7 ▶ my $by = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $by = shift() or return 7 0 7
787 7 ▶ if ($by =~ /.+_or_.+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($by =~ /.+_or_.+/) { } 2 5 7
788 2 my @keys = split('_or_', $by);
789 3 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
790 3 my $self = shift;
791 3 my $v = shift;
792 3 for (@keys) {
793 4 my $o = $self->retrieve($_ => $v);
794 4 ▶ return $o if $o;
Branch: 1 branch
BranchTrueFalseTotal
if $o 3 1 4
795 }
796 2 };
797 } else {
798 5 my @keys = split('_and_', $by);
799 7 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
800 7 my $self = shift;
801 7 my %args;
802 7 @args{@keys} = @_;
803 7 $self->retrieve(%args);
804 5 };
805 }
806 }
807  
808 2 ▶sub _retrieve_by_or_create_handler {
Subroutine _retrieve_by_or_create_handler: called CC=2 SCAR=7.0
809 2 my $class = shift;
810 2 ▶ my $by = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $by = shift() or return 2 0 2
811 2 my @keys = split('_and_', $by);
812 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
813 3 my $self = shift;
814 3 my %args;
815 3 @args{@keys} = @_;
816 3 ▶ return $self->retrieve(%args) || $class->create(%args);
Condition66%2/3
$self->retrieve(%args) || $class->create(%args)
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->retrieve(%args) || $class->create(%args)
$self->retrieve(%args) $class->create(%args)
Truth table66%2/3
A || B
A= $self->retrieve(%args)B= $class->create(%args)
ABresult
1X1
011
000
817 2 };
818 }
819  
820 30 ▶sub _list {
Subroutine _list: called CC=4 SCAR=22.9
821 30 my $class = shift;
822  
823 30 ▶ if ($class->list_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->list_class) { } 0 30 30
824 0 $class->list_class->require;
825 0 ▶ if ($@ and $@ !~ m/^Can\'t locate .+? in \@INC/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /^Can\'t locate .+? in \@INC/) 0 0 0
Condition0%0/3
$@ and not $@ =~ /^Can\'t locate .+? in \@INC/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $@ =~ /^Can\'t locate .+? in \@INC/
$@ not $@ =~ /^Can\'t locate .+? in \@INC/
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ /^Can\'t locate .+? in \@INC/
ABresult
0X0
100
111
826 0 die $@;
827 }
828 0 return $class->list_class->new(@_);
829 } else {
830 30 return DBIx::MoCo::List->new(@_);
831 }
832 }
833  
834 80 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
835 80 my $class = shift;
836 80 $class->save_changed;
837 }
838  
839 71 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
840 71 my $class = shift;
841 71 my %args = @_;
842 71 my $self = \%args;
843 71 $self->{changed_cols} = {};
844 71 bless $self, $class;
845 71 $self;
846 }
847  
848 2 ▶sub flush {
Subroutine flush: called CC=2 SCAR=7.0
Pod documented
849 2 my $self = shift;
850 2 ▶ my $attr = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $attr = shift() or return 2 0 2
851 # warn "flush " . $self->object_id . '->' . $attr;
852 2 $self->{$attr} = undef;
853 2 $self->store_self_cache($self);
854 }
855  
856 112 ▶sub param {
Subroutine param: called CC=13 SCAR=26.8
Pod documented
857 112 my $self = shift;
858 112 ▶ my $class = ref $self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or return 111 1 112
859 111 ▶ return unless(defined $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 111 111
860 # if (defined $_[1]) {
861 111 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 12 99 111
862 12 ▶ @_ % 2 and croak "You gave me an odd number of parameters to param()!";
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("You gave me an odd number of parameters to param()!") 0 12 12
863 12 my %args = @_;
864 12 $class->call_trigger('before_update', $self, \%args);
865 12 $self->{$_} = $args{$_} for (keys %args);
866 12 ▶ if ($class->is_in_session) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class->is_in_session) { } 2 10 12
elsif ($class->save_explicitly) { } 0 10 10
867 2 $self->{to_be_updated}++;
868 2 $self->{changed_cols}->{$_}++ for (keys %args);
869 2 push @{$class->session->{changed_objects}}, $self;
870 } elsif ($class->save_explicitly) {
871 0 $self->{to_be_updated}++;
872 0 $self->{changed_cols}->{$_}++ for keys %args;
873 } else {
874 10 ▶ my $where = $self->primary_keys_hash or return;
Branch: 1 branch
BranchTrueFalseTotal
my $where = $self->primary_keys_hash or return 10 0 10
875 10 ▶ %$where or return;
Branch: 1 branch
BranchTrueFalseTotal
%$where or return 10 0 10
876 10 ▶ $class->db->update($class->table,\%args,$where) or croak 'couldnt update';
Branch: 1 branch
BranchTrueFalseTotal
$class->db->update($class->table, \%args, $where) or croak("couldnt update") 10 0 10
877 }
878 12 $class->call_trigger('after_update', $self);
879 # return 1;
880 }
881 111 return $self->{$_[0]};
882 }
883  
884 13 ▶sub primary_keys_hash {
Subroutine primary_keys_hash: called CC=5 SCAR=16.4
Pod undocumented
885 13 my $self = shift;
886 13 ▶ my $class = ref $self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or return 13 0 13
887 13 ▶ @{$class->primary_keys} or return;
Branch: 1 branch
BranchTrueFalseTotal
@{$class->primary_keys;} or return 13 0 13
888 13 my $hash = {};
889 13 for (@{$class->primary_keys}) {
890 13 ▶ defined $self->{$_} or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$_} or return 13 0 13
891 13 $hash->{$_} = $self->{$_};
892 }
893 13 return $hash;
894 }
895  
896 10 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
897 10 my $self = shift;
898 10 my ($k,$v) = @_;
899 10 $self->{$k} = $v;
900 }
901  
902 3 ▶sub has_primary_keys {
Subroutine has_primary_keys: called CC=3 SCAR=11.0
Pod undocumented
903 3 my $self = shift;
904 3 my $class = ref $self;
905 3 for (@{$class->primary_keys}) {
906 3 ▶ defined $self->{$_} or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$_} or return 3 0 3
907 }
908 3 return 1;
909 }
910  
911 5 ▶sub save {
Subroutine save: called CC=8 SCAR=22.2
Pod documented
912 5 my $self = shift;
913 5 my $class = ref $self;
914 5 ▶ keys %{$self->{changed_cols}} or return;
Branch: 1 branch
BranchTrueFalseTotal
keys %{$$self{"changed_cols"};} or return 2 3 5
915 2 my %args;
916 2 for (keys %{$self->{changed_cols}}) {
917 # defined $self->{$_} or croak "$_ is undefined";
918 2 ▶ exists $self->{$_} or croak "$_ is undefined";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$_} or croak("$_ is undefined") 2 0 2
919 2 $args{$_} = $self->{$_};
920 }
921 2 ▶ if ($self->{to_be_inserted}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'to_be_inserted'}) { } 0 2 2
elsif ($self->{'to_be_updated'}) { } 2 0 2
922 0 $class->db->insert($class->table,\%args);
923 0 $self->{changed_cols} = {};
924 0 $self->{to_be_inserted} = undef;
925 } elsif ($self->{to_be_updated}) {
926 2 ▶ my $where = $self->primary_keys_hash or return;
Branch: 1 branch
BranchTrueFalseTotal
my $where = $self->primary_keys_hash or return 2 0 2
927 2 ▶ %$where or return;
Branch: 1 branch
BranchTrueFalseTotal
%$where or return 2 0 2
928 2 $class->db->update($class->table,\%args,$where);
929 2 $self->{changed_cols} = {};
930 2 $self->{to_be_updated} = undef;
931 }
932 }
933  
934 76 ▶sub object_ids { # returns all possible oids
Subroutine object_ids: called CC=6 SCAR=18.0
Pod undocumented
935 76 my $self = shift;
936 76 ▶ my $class = ref $self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or return 76 0 76
937 76 my $oids = {};
938 76 ▶ $oids->{$self->object_id} = 1 if $self->object_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->object_id 76 0 76
939 76 for my $key (@{$class->unique_keys}) {
940 130 ▶ next unless defined $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$key} 2 128 130
941 128 ▶ my $oid = $class->object_id($key => $self->{$key}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $oid = $class->object_id($key, $self->{$key}) or next 106 22 128
942 106 $oids->{$oid}++;
943 }
944 76 return [sort keys %$oids];
945 }
946  
947 12 1;
948  
949 __END__
« MoCo/Relation.pm ↑ Summary MoCo/Column/utf8.pm »