blib/lib/ClearPress/model.pm

filter: Statementstmt 79.6% 368 / 462 Branchbran 53.9% 82 / 152 Conditioncond 36.9% 17 / 46 MC/DCmcdc 13.7% 4 / 29 Subroutinesub 90.5% 48 / 53 Podpod 100% 31 / 31 totaltotal 71.1% 550 / 773 CC 99 SCAR 19.9
CC
99
Cov
71%
read
16.1
new
15.4
create
14.4
CRAP 7.3
« blib/lib/ClearPress/view/error.pm ↑ Summary blib/lib/ClearPress/driver/SQLite.pm »
1 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
2 # vim:ts=8:sw=2:et:sta:sts=2
3 #########
4 # Author: rmp
5 # Created: 2006-10-31
6 #
7 package ClearPress::model;
8 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use ClearPress::util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Lingua::EN::Inflect qw(PL);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Lingua::EN::PluralToSingular qw(to_singular);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 our $VERSION = q[2026.07.25];
21  
22 9 Readonly::Scalar our $DBI_CACHE_OVERWRITE => 3;
23  
24 9 our $EXPERIMENTAL_PL = 0;
25  
26 9 ▶sub fields { return (); }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
27  
28 37 ▶sub _plfunc {
Subroutine _plfunc: called CC=2 SCAR=7.2
29 37 my $thing = shift;
30 37 ▶ return $EXPERIMENTAL_PL ? PL(to_singular($thing)) : PL($thing);
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::model::EXPERIMENTAL_PL ? : 0 37 37
31 }
32  
33 207 ▶sub primary_key {
Subroutine primary_key: called CC=1 SCAR=0.0
Pod documented
34 207 my $self = shift;
35 207 return ($self->fields())[0];
36 }
37  
38 85 ▶sub secondary_key {
Subroutine secondary_key: called CC=1 SCAR=0.0
Pod documented
39 }
40  
41 47 ▶sub table {
Subroutine table: called CC=3 SCAR=11.0
Pod documented
42 47 my $self = shift;
43 47 ▶ my $tbl = (ref $self) || $self;
Condition100%3/3
ref $self || $self
l!l&&r!l&&!r
36101
MC/DC100%2/2
ref $self || $self
ref $self $self
Truth table100%3/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
44 47 ▶ if(!$tbl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tbl) 1 46 47
45 1 return;
46 }
47 46 ($tbl) = $tbl =~ /.*::([^:]+)/smx;
48 46 return $tbl;
49 }
50  
51 79 ▶sub init { }
Subroutine init: called CC=1 SCAR=0.0
Pod documented
52  
53 79 ▶sub new {
Subroutine new: called CC=8 SCAR=27.3
Pod documented
54 79 my ($class, $ref) = @_;
55 79 ▶ $ref ||= {};
Condition100%2/2
$ref ||= {}
l!l
5524
MC/DC100%1/1
$ref ||= {}
$ref
Truth table100%2/2
A
A= $ref
Aresult
11
00
56  
57 79 my $pk = $class->primary_key();
58  
59 79 ▶ if(!ref $ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref) 2 77 79
60 2 ▶ if($pk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk) { } 2 0 2
61 2 $ref = {
62 $pk => $ref,
63 };
64 } else {
65 0 croak q[Could not set primary key in an object with no fields];
66 }
67 }
68  
69 79 bless $ref, $class;
70  
71 79 my $sk = $ref->secondary_key();
72 79 ▶ if($sk && $ref->{$sk} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($sk and $ref->{$sk} and not $ref->{$pk}) 0 79 79
Condition33%2/6
$sk and $ref->{$sk} and not $ref->{$pk}
!ll&&!rl&&r
7900
$sk and $ref->{$sk}
!ll&&!rl&&r
7900
MC/DC0%0/3
$sk and $ref->{$sk} and not $ref->{$pk}
$sk $ref->{$sk} not $ref->{$pk}
Truth table25%1/4
A and B and not(C)
A= $skB= $ref->{$sk}C= not $ref->{$pk}
ABCresult
0XX0
10X0
1100
1111
73 !$ref->{$pk}) {
74  
75 0 my $table = $ref->table;
76 0 my $util = $ref->util;
77 0 my $dbh = $util->dbh;
78 eval {
79 0 my $id = $dbh->selectall_arrayref(qq[/* model::new */ SELECT $pk FROM $table WHERE $sk=?], {}, $ref->{$sk})->[0]->[0];
80 0 $ref->{$pk} = $id;
81 0 1;
82  
83 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $id = $dbh->selectall_arrayref("/* model::new */ SELECT $pk FROM $table WHERE $sk=?", {}, $ref->{$sk})->[0][0]; $ref->{$pk} = $id; 1 } } or do { carp($ClearPress::model::EVAL_ERROR); return } 0 0 0
84 0 carp $EVAL_ERROR;
85 0 return;
86 };
87 }
88  
89 79 $ref->init($ref);
90  
91 79 return $ref;
92 }
93  
94 59 ▶sub util {
Subroutine util: called CC=7 SCAR=20.0
Pod documented
95 59 my ($self, $util) = @_;
96  
97 59 ▶ if(!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 1 58 59
98 #########
99 # If we're being accessed as a class method (e.g. for retrieving
100 # type dictionaries) Then play nicely and return a util.
101 #
102 # Note, this currently needs subclassing if you want class-method
103 # support in your own namespace.
104 #
105 1 return ClearPress::util->new();
106 }
107  
108 58 ▶ if($util) {
Branch: 1 branch
BranchTrueFalseTotal
if ($util) 1 57 58
109 1 $self->{util} = $util;
110 1 return $util;
111 }
112  
113 57 ▶ if($self->{util}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"util"}) 54 3 57
114 54 return $self->{util};
115 }
116  
117 #########
118 # attempt to instantiate a util using $self's namespace
119 #
120 3 my ($ref) = (ref $self) =~ /^([^:]+)/smx;
121 3 my $nsutil;
122 eval {
123 3 my $ns = "${ref}::util";
124 3 $nsutil = $ns->new();
125  
126 3 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $ns = "${ref}::util"; $nsutil = $ns->new } } or do { carp("Failed to construct a util from the current namespace ($ref).") } 2 1 3
127 1 carp qq[Failed to construct a util from the current namespace ($ref).];
128 };
129  
130 3 ▶ if($nsutil) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nsutil) 2 1 3
131 2 $self->{util} = $nsutil;
132 2 return $self->{util};
133 }
134  
135 #########
136 # ClearPress::driver is now a Class::Singleton so, casually ignoring
137 # the state of any open transactions, we should be able to
138 # instantiate more copies on demand
139 #
140 1 my $cputil = ClearPress::util->new();
141 1 my $config = $cputil->config();
142 0 ▶ my $namespace = $config->val('application', 'namespace') ||
Condition0%0/3
$config->val('application', 'namespace') || $config->val('application', 'name')
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->val('application', 'namespace') || $config->val('application', 'name')
$config->val('application', 'namespace') $config->val('application', 'name')
Truth table0%0/3
A || B
A= $config->val('application', 'namespace')B= $config->val('application', 'name')
ABresult
1X1
011
000
143 $config->val('application', 'name');
144 0 $util = "${namespace}::util"->new();
145 0 $self->{util} = $util;
146 0 return $util;
147 }
148  
149 99 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
150 99 my ($self, $field) = @_;
151  
152 99 ▶ if(!exists $self->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$field}) 18 81 99
153 18 $self->read();
154 }
155  
156 99 return $self->SUPER::get($field);
157 }
158  
159 9 ▶sub gen_getarray {
Subroutine gen_getarray: called CC=5 SCAR=18.8
Pod documented
160 9 my ($self, $class, $query, @args) = @_;
161 9 my $util = $self->util();
162  
163 9 ▶ if(!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 9 9
164 0 $self = $self->new({
165 util => $util,
166 });
167 }
168  
169 9 my $res = [];
170 9 my $sth;
171  
172 eval {
173 9 my $dbh = $util->dbh();
174 #########
175 # statement caching removed as cache conflicts are dangerous
176 # and could be easily generated by accident
177 9 $sth = $dbh->prepare($query);
178 9 $sth->execute(@args);
179 9 1; # sth->execute() does not return true!
180  
181 9 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $dbh = $util->dbh; $sth = $dbh->prepare($query); $sth->execute(@args); 1 } } or do { $query =~ s/\s+/ /gmsx; local $ClearPress::model::LIST_SEPARATOR = ", "; carp(qq[GEN_GETARRAY ERROR\nEVAL_ERROR: $ClearPress::model::EVAL_ERROR\nCaller: @{['' . (caller)];}\nQuery:\n$query\nDBH: @{[$util->dbh];}\nUTIL: $util\nParams: @{[map({defined $_ ? $_ : "NULL";} @args)];}]); return } 9 0 9
182 0 $query =~ s/\s+/ /smxg;
183 0 local $LIST_SEPARATOR = q[, ];
184 0 ▶ carp qq[GEN_GETARRAY ERROR\nEVAL_ERROR: $EVAL_ERROR\nCaller: @{[q[].caller]}\nQuery:\n$query\nDBH: @{[$util->dbh]}\nUTIL: $util\nParams: @{[map { (defined $_)?$_:'NULL' } @args]}];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
185 0 return;
186 };
187  
188 9 ▶ while(my $ref = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $sth->fetchrow_hashref) 7 9 16
189 7 $ref->{util} = $util;
190 7 push @{$res}, $class->new($ref);
191 }
192 9 $sth->finish();
193  
194 9 return $res;
195 }
196  
197 6 ▶sub gen_getall {
Subroutine gen_getall: called CC=5 SCAR=16.6
Pod documented
198 6 my ($self, $class, $cachekey) = @_;
199 6 ▶ $class ||= ref $self;
Condition33%1/3
$class ||= ref $self
l!l&&r!l&&!r
060
MC/DC0%0/2
$class ||= ref $self
$class ref $self
Truth table33%1/3
A ||= B
A= $classB= ref $self
ABresult
1X1
011
000
200  
201 6 ▶ if(!$cachekey) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 6 0 6
202 6 ($cachekey) = $class =~ /([^:]+)$/smx;
203 6 $cachekey = _plfunc($cachekey);
204 }
205  
206 6 ▶ if(!$self->{$cachekey}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$cachekey}) 6 0 6
207 6 my $sortk = $self->secondary_key;
208 6 ▶ if(!$sortk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sortk) 6 0 6
209 6 $sortk = $self->primary_key;
210 }
211 6 my $query = <<"EOT";
212 /* model::gen_getall */
213 6 SELECT @{[join q(, ), $class->fields()]}
214 6 FROM @{[$class->table()]}
215 ORDER BY $sortk
216 EOT
217 6 $self->{$cachekey} = $self->gen_getarray($class, $query);
218 }
219  
220 6 return $self->{$cachekey};
221 }
222  
223 1 ▶sub gen_getfriends {
Subroutine gen_getfriends: called CC=4 SCAR=15.0
Pod documented
224 1 my ($self, $class, $cachekey) = @_;
225 1 ▶ $class ||= ref $self;
Condition33%1/3
$class ||= ref $self
l!l&&r!l&&!r
100
MC/DC0%0/2
$class ||= ref $self
$class ref $self
Truth table33%1/3
A ||= B
A= $classB= ref $self
ABresult
1X1
011
000
226  
227 1 ▶ if(!$cachekey) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 1 1
228 0 ($cachekey) = $class =~ /([^:]+)$/smx;
229 0 $cachekey = _plfunc($cachekey);
230 }
231  
232 1 ▶ if(!$self->{$cachekey}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$cachekey}) 1 0 1
233 1 my $link = $self->primary_key();
234 1 my $query = <<"EOT";
235 /* model::gen_getfriends */
236 1 SELECT @{[join q(, ), $class->fields()]}
237 1 FROM @{[$class->table()]}
238 WHERE $link=?
239 ORDER BY $link
240 EOT
241 1 $self->{$cachekey} = $self->gen_getarray($class, $query, $self->$link());
242 }
243  
244 1 return $self->{$cachekey};
245 }
246  
247 4 ▶sub gen_getfriends_through {
Subroutine gen_getfriends_through: called CC=4 SCAR=14.2
Pod documented
248 4 my ($self, $class, $through, $cachekey) = @_;
249 4 ▶ $class ||= ref $self;
Condition33%1/3
$class ||= ref $self
l!l&&r!l&&!r
400
MC/DC0%0/2
$class ||= ref $self
$class ref $self
Truth table33%1/3
A ||= B
A= $classB= ref $self
ABresult
1X1
011
000
250  
251 4 ▶ if(!$cachekey) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 4 4
252 0 ($cachekey) = $class =~ /([^:]+)$/smx;
253 0 $cachekey = _plfunc($cachekey);
254 }
255  
256 4 ▶ if(!$self->{$cachekey}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$cachekey}) 1 3 4
257 1 my ($through_pkg) = (ref $self) =~ /^(.*::)[^:]+$/smx;
258 1 $through_pkg .= $through;
259 1 my $through_key = $self->primary_key();
260 1 my $friend_key = $class->primary_key();
261 1 my $query = <<"EOT";
262 /* model::gen_getfriends_through */
263 SELECT @{[join q(, ),
264 2 (map { "f.$_" } $class->fields()),
265 1 (map { "t.$_" } $through_pkg->fields())]}
266 1 FROM @{[$class->table()]} f,
267 $through t
268 WHERE t.$through_key = ?
269 AND t.$friend_key = f.$friend_key
270 EOT
271 1 $self->{$cachekey} = $self->gen_getarray($class, $query, $self->$through_key());
272 }
273  
274 4 return $self->{$cachekey};
275 }
276  
277 1 ▶sub gen_getobj {
Subroutine gen_getobj: called CC=3 SCAR=12.0
Pod documented
278 1 my ($self, $class) = @_;
279 1 ▶ $class ||= ref $self;
Condition33%1/3
$class ||= ref $self
l!l&&r!l&&!r
100
MC/DC0%0/2
$class ||= ref $self
$class ref $self
Truth table33%1/3
A ||= B
A= $classB= ref $self
ABresult
1X1
011
000
280 1 my $pk = $class->primary_key();
281 1 my ($cachekey) = $class =~ /([^:]+)$/smx;
282 1 ▶ $self->{$cachekey} ||= $class->new({
Condition33%1/3
$self->{$cachekey} ||= $class->new({"util", $self->util, $pk, $self->$pk})
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{$cachekey} ||= $class->new({"util", $self->util, $pk, $self->$pk})
$self->{$cachekey} $class->new({"util", $self->util, $pk, $self->$pk})
Truth table33%1/3
A ||= B
A= $self->{$cachekey}B= $class->new({"util", $self->util, $pk, $self->$pk})
ABresult
1X1
011
000
283 util => $self->util(),
284 $pk => $self->$pk(),
285 });
286 1 return $self->{$cachekey};
287 }
288  
289 2 ▶sub gen_getobj_through {
Subroutine gen_getobj_through: called CC=4 SCAR=14.0
Pod documented
290 2 my ($self, $class, $through, $cachekey) = @_;
291 2 ▶ $class ||= ref $self;
Condition33%1/3
$class ||= ref $self
l!l&&r!l&&!r
200
MC/DC0%0/2
$class ||= ref $self
$class ref $self
Truth table33%1/3
A ||= B
A= $classB= ref $self
ABresult
1X1
011
000
292  
293 2 ▶ if(!$cachekey) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 2 0 2
294 2 ($cachekey) = $class =~ /([^:]+)$/smx;
295 }
296  
297 2 ▶ if(!$self->{$cachekey}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$cachekey}) 1 1 2
298 # todo: use $through class to determine $through_key
299 # - but $through class may not always be implemented
300 1 my $through_key = q(id_).$through;
301 1 my $friend_key = $class->primary_key();
302 1 my $query = <<"EOT";
303 /* model::gen_getobj_through */
304 1 SELECT @{[join q(, ), map { "f.$_" } $class->fields()]}
305 1 FROM @{[$class->table()]} f,
306 $through t
307 WHERE t.$through_key = ?
308 AND t.$friend_key = f.$friend_key
309 EOT
310  
311 1 $self->{$cachekey} = $self->gen_getarray($class, $query, $self->$through_key())->[0];
312 }
313  
314 2 return $self->{$cachekey};
315 }
316  
317 0 ▶sub belongs_to {
Subroutine belongs_to: not called CC=1 SCAR=6.9
Pod documented
318 0 my ($class, @args) = @_;
319 0 return $class->has_a(@args);
320 }
321  
322 1 ▶sub hasa {
Subroutine hasa: called CC=1 SCAR=0.0
Pod documented
323 1 my ($class, @args) = @_;
324 1 carp q[hasa is deprecated. Use has_a];
325 1 return $class->has_a(@args);
326 }
327  
328 11 ▶sub has_a {
Subroutine has_a: called CC=6 SCAR=19.6
Pod documented
329 11 my ($class, $attr) = @_;
330  
331 11 ▶ if(ref $attr ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr ne "ARRAY") 6 5 11
332 6 $attr = [$attr];
333 }
334  
335 11 for my $single (@{$attr}) {
336 11 my $pkg = $single;
337  
338 11 ▶ if(ref $single eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $single eq "HASH") 0 11 11
339 0 ($pkg) = values %{$single};
340 0 ($single) = keys %{$single};
341 }
342  
343 11 my $namespace = "${class}::$pkg";
344 11 my $yield = $class;
345 11 ▶ if($yield !~ /model/smx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($yield =~ /model/msx) 0 11 11
346 0 croak qq[$pkg ($namespace) is not under a model:: namespace. Friend relationships will not work.];
347 }
348  
349 11 $yield =~ s/^(.*model::).*$/$1$pkg/smx;
350  
351 11 ▶ if (defined &{$namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$namespace;}) 0 11 11
352 0 next;
353 }
354  
355 9 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
356 11 ▶ *{$namespace} = sub {
Subroutine __ANON__: not called CC=1 SCAR=2.6
357 0 my $self = shift;
358 0 return $self->gen_getobj($yield);
359 11 };
360 }
361  
362 11 return;
363 }
364  
365 1 ▶sub hasmany {
Subroutine hasmany: called CC=1 SCAR=0.0
Pod documented
366 1 my ($class, @args) = @_;
367 1 carp q[hasmany is deprecated. Use has_many];
368 1 return $class->has_many(@args);
369 }
370  
371 6 ▶sub has_many {
Subroutine has_many: called CC=6 SCAR=18.1
Pod documented
372 6 my ($class, $attr) = @_;
373  
374 6 ▶ if(ref $attr ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr ne "ARRAY") 6 0 6
375 6 $attr = [$attr];
376 }
377  
378 6 for my $single (@{$attr}) {
379 6 my $pkg = $single;
380  
381 6 ▶ if(ref $single eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $single eq "HASH") 5 1 6
382 5 ($pkg) = values %{$single};
383 5 ($single) = keys %{$single};
384 }
385  
386 6 my $plural = _plfunc($single);
387 6 my $namespace = "${class}::$plural";
388 6 my $yield = $class;
389 6 $yield =~ s/^(.*model::).*$/$1$pkg/smx;
390  
391 6 ▶ if($yield !~ /model/smx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($yield =~ /model/msx) 0 6 6
392 0 croak qq[$pkg ($namespace) is not under a model:: namespace. Friend relationships will not work.];
393 }
394  
395 6 ▶ if (defined &{$namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$namespace;}) 0 6 6
396 0 next;
397 }
398  
399 9 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
400 6 ▶ *{$namespace} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
401 1 my $self = shift;
402 1 return $self->gen_getfriends($yield, $plural);
403 6 };
404 }
405  
406 6 return;
407 }
408  
409 0 ▶sub belongs_to_through {
Subroutine belongs_to_through: not called CC=1 SCAR=6.9
Pod documented
410 0 my ($class, @args) = @_;
411 0 return $class->has_a_through(@args);
412 }
413  
414 5 ▶sub has_a_through {
Subroutine has_a_through: called CC=7 SCAR=21.0
Pod documented
415 5 my ($class, $attr) = @_;
416  
417 5 ▶ if(ref $attr ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr ne "ARRAY") 5 0 5
418 5 $attr = [$attr];
419 }
420  
421 5 for my $single (@{$attr}) {
422 5 my $pkg = $single;
423  
424 5 ▶ if(ref $single eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $single eq "HASH") 0 5 5
425 0 ($pkg) = values %{$single};
426 0 ($single) = keys %{$single};
427 }
428 5 $pkg =~ s/[|].*//smx;
429  
430 5 my $through;
431 5 ($single, $through) = split /[|]/smx, $single;
432  
433 5 ▶ if(!$through) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($through) 0 5 5
434 0 croak qq(Cannot build belongs_to_through for $single);
435 }
436  
437 5 my $namespace = "${class}::$pkg";
438 5 my $yield = $class;
439 5 $yield =~ s/^(.*model::).*$/$1$pkg/smx;
440  
441 5 ▶ if($yield !~ /model/smx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($yield =~ /model/msx) 0 5 5
442 0 croak qq[$pkg ($namespace) is not under a model:: namespace. Friend relationships will not work.];
443 }
444  
445 5 ▶ if (defined &{$namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$namespace;}) 0 5 5
446 0 next;
447 }
448  
449 9 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
450 5 ▶ *{$namespace} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
451 2 my $self = shift;
452 2 return $self->gen_getobj_through($yield, $through);
453 5 };
454 }
455  
456 5 return;
457 }
458  
459 5 ▶sub has_many_through {
Subroutine has_many_through: called CC=7 SCAR=20.9
Pod documented
460 5 my ($class, $attr) = @_;
461  
462 5 ▶ if(ref $attr ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr ne "ARRAY") 5 0 5
463 5 $attr = [$attr];
464 }
465  
466 5 for my $single (@{$attr}) {
467 5 my $pkg = $single;
468  
469 5 ▶ if(ref $single eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $single eq "HASH") 0 5 5
470 0 ($pkg) = values %{$single};
471 0 ($single) = keys %{$single};
472 }
473 5 $pkg =~ s/[|].*//smx;
474  
475 5 my $through;
476 5 ($single, $through) = split /[|]/smx, $single;
477  
478 5 ▶ if(!$through) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($through) 0 5 5
479 0 croak qq(Cannot build has_many_through for $single);
480 }
481  
482 5 my $plural = _plfunc($single);
483 5 my $namespace = "${class}::$plural";
484 5 my $yield = $class;
485 5 $yield =~ s/^(.*model::).*$/$1$pkg/smx;
486  
487 5 ▶ if($yield !~ /model/smx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($yield =~ /model/msx) 0 5 5
488 0 croak qq[$pkg ($namespace) is not under a model:: namespace. Friend relationships will not work.];
489 }
490  
491 5 ▶ if (defined &{$namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$namespace;}) 0 5 5
492 0 next;
493 }
494  
495 9 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
496 5 ▶ *{$namespace} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
497 4 my $self = shift;
498  
499 4 return $self->gen_getfriends_through($yield, $through, $plural);
500 5 };
501 }
502  
503 5 return;
504 }
505  
506 20 ▶sub has_all {
Subroutine has_all: called CC=2 SCAR=7.0
Pod documented
507 20 my ($class) = @_;
508  
509 20 my ($single) = $class =~ /([^:]+)$/smx;
510 20 my $plural = _plfunc($single);
511 20 my $namespace = "${class}::$plural";
512  
513 20 ▶ if (defined &{$namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$namespace;}) 0 20 20
514 0 return;
515 }
516  
517 9 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
518 20 ▶ *{$namespace} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
519 6 my $self = shift;
520 6 return $self->gen_getall();
521 20 };
522  
523 20 return 1;
524 }
525  
526 16 ▶sub create {
Subroutine create: called CC=9 SCAR=26.6
Pod documented
527 16 my $self = shift;
528 16 my $util = $self->util();
529 16 my $dbh = $util->dbh();
530 16 my $pk = $self->primary_key();
531 16 my $tr_state = $util->transactions();
532 16 my $table = $self->table();
533  
534 16 ▶ if(!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 16 16
535 0 croak q(No table defined);
536 }
537  
538 #########
539 # disallow saving against zero
540 #
541 16 ▶ if(!$self->$pk()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->$pk) 16 0 16
542 16 delete $self->{$pk};
543 }
544  
545 16 my $query = <<"EOT";
546 16 INSERT INTO $table (@{[join q(, ), $self->fields()]})
547 16 VALUES (@{[join q(, ), map { q(?) } $self->fields()]})
548 EOT
549  
550 16 my @args = map { $self->{$_} } $self->fields();
551 eval {
552 16 my $drv = $util->driver();
553 16 my $id = $drv->create($query, @args);
554 16 $self->$pk($id);
555  
556 16 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $drv = $util->driver; my $id = $drv->create($query, @args); $self->$pk($id) } } or do { $tr_state and $dbh->rollback; carp(qq[CREATE Query was:\n$query\n\nParams: @{[map({defined $_ ? $_ : "NULL";} @args)];}]); croak($ClearPress::model::EVAL_ERROR) } 16 0 16
557 0 ▶ $tr_state and $dbh->rollback();
Branch: 1 branch
BranchTrueFalseTotal
$tr_state and $dbh->rollback 0 0 0
558 0 ▶ carp qq[CREATE Query was:\n$query\n\nParams: @{[map { (defined $_)?$_:'NULL' } @args]}];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
559 0 croak $EVAL_ERROR;
560 };
561  
562 eval {
563 16 ▶ $tr_state and $dbh->commit();
Branch: 1 branch
BranchTrueFalseTotal
$tr_state and $dbh->commit 16 0 16
564 16 1;
565  
566 16 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $tr_state and $dbh->commit; 1 } } or do { $tr_state and $dbh->rollback; croak($ClearPress::model::EVAL_ERROR) } 16 0 16
567 0 ▶ $tr_state and $dbh->rollback();
Branch: 1 branch
BranchTrueFalseTotal
$tr_state and $dbh->rollback 0 0 0
568 0 croak $EVAL_ERROR;
569 };
570  
571 16 return 1;
572 }
573  
574 25 ▶sub read { ## no critic (homonym)
Subroutine read: called CC=13 SCAR=27.8
Pod documented
575 25 my ($self, $query, @args) = @_;
576 25 my $pk = $self->primary_key();
577 25 my $util = $self->util;
578  
579 25 ▶ if(!$query && !$self->{$pk}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query or $self->{$pk}) 16 9 25
Condition66%2/3
$query or $self->{$pk}
l!l&&r!l&&!r
0916
MC/DC50%1/2
$query or $self->{$pk}
$query $self->{$pk}
Truth table66%2/3
A or B
A= $queryB= $self->{$pk}
ABresult
1X1
011
000
580 # carp q(No primary key);
581 16 return;
582 }
583  
584 9 my $table = $self->table();
585 9 ▶ if(!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 9 9
586 0 croak q(No table defined);
587 }
588  
589 9 ▶ if(!$self->{_loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_loaded"}) 9 0 9
590 9 ▶ if(!$query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 9 0 9
591 9 $query = <<"EOT";
592 /* model::read */
593 9 SELECT @{[join q(, ), $self->fields()]}
594 FROM $table
595 WHERE $pk=?
596 EOT
597 9 @args = ($self->{$pk});
598 }
599  
600 eval {
601 9 my $sth = $util->dbh->prepare($query);
602 9 $sth->execute(@args);
603  
604 9 my $ref = $sth->fetchrow_hashref();
605  
606 9 ▶ if(!$sth->rows()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->rows) 1 8 9
607 #########
608 # entity not in database
609 #
610 1 $sth->finish();
611 1 croak q[missing entity];
612 }
613  
614 8 $sth->finish();
615  
616 8 my $warnings = $util->driver->sth_has_warnings($sth);
617 8 ▶ if(!$warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $warnings) { } 8 0 8
618 8 for my $f ($self->fields()) {
619 52 $self->{$f} = $ref->{$f};
620 }
621  
622 } else {
623 0 for my $w (@{$warnings}) {
624 0 carp qq[ClearPress::model::read: mysql warning: $w->[2]];
625 }
626 }
627  
628 8 1;
629  
630 9 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $sth = $util->dbh->prepare($query); $sth->execute(@args); my $ref = $sth->fetchrow_hashref; unless ($sth->rows) { $sth->finish; croak("missing entity"); } ; $sth->finish; my $warnings = $util->driver->sth_has_warnings($sth); if (not $warnings) { foreach my $f ($self->fields) { $self->{$f} = $ref->{$f}; } ; } else { foreach my $w (@{$warnings;}) { carp("ClearPress::model::read: mysql warning: $w->[2]"); } ; } ; 1 } } or do { if ($ClearPress::model::EVAL_ERROR =~ /missing\sentity/msx) { return; } ; carp(qq[SELECT ERROR\nEVAL_ERROR: $ClearPress::model::EVAL_ERROR\nQuery:\n$query\n\nParams: @{[map({defined $_ ? $_ : "NULL";} @args)];}\n]) } 8 1 9
631 1 ▶ if($EVAL_ERROR =~ /missing\sentity/smx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ClearPress::model::EVAL_ERROR =~ /missing\sentity/msx) 1 0 1
632 1 return;
633 }
634 0 ▶ carp qq[SELECT ERROR\nEVAL_ERROR: $EVAL_ERROR\nQuery:\n$query\n\nParams: @{[map { (defined $_)?$_:'NULL' } @args]}\n];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
635 };
636 }
637 8 $self->{_loaded} = 1;
638 8 return 1;
639 }
640  
641 4 ▶sub update {
Subroutine update: called CC=8 SCAR=23.4
Pod documented
642 4 my $self = shift;
643 4 my $pk = $self->primary_key();
644  
645 4 ▶ if(!$pk || !$self->$pk()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk and $self->$pk) 0 4 4
Condition33%1/3
$pk and $self->$pk
!ll&&!rl&&r
004
MC/DC0%0/2
$pk and $self->$pk
$pk $self->$pk
Truth table33%1/3
A and B
A= $pkB= $self->$pk
ABresult
0X0
100
111
646 0 croak q(No primary key);
647 }
648  
649 4 my $table = $self->table();
650 4 ▶ if(!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 4 4
651 0 croak q(No table defined);
652 }
653  
654 4 my $util = $self->util();
655 4 my $tr_state = $util->transactions();
656 4 my $dbh = $util->dbh();
657 20 my @fields = grep { exists $self->{$_} }
658 4 grep { $_ ne $pk }
659 $self->fields();
660 4 my $query = <<"EOT";
661 4 UPDATE @{[$self->table()]}
662 SET @{[join q(, ),
663 4 map { qq[$_ = ?] }
664 @fields]}
665 WHERE $pk=?
666 EOT
667  
668 eval {
669 4 $dbh->do($query, {}, (map { $self->$_() } @fields), $self->$pk);
670  
671 4 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbh->do($query, {}, map({$self->$_;} @fields), $self->$pk) } } or do { $tr_state and $dbh->rollback; croak($ClearPress::model::EVAL_ERROR . ' ' . $query) } 4 0 4
672 0 ▶ $tr_state and $dbh->rollback();
Branch: 1 branch
BranchTrueFalseTotal
$tr_state and $dbh->rollback 0 0 0
673 0 croak $EVAL_ERROR.q[ ].$query;
674 };
675  
676 eval {
677 4 ▶ $tr_state and $dbh->commit();
Branch: 1 branch
BranchTrueFalseTotal
$tr_state and $dbh->commit 4 0 4
678 4 1;
679  
680 4 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $tr_state and $dbh->commit; 1 } } or do { croak($ClearPress::model::EVAL_ERROR) } 4 0 4
681 0 croak $EVAL_ERROR;
682 };
683  
684 4 return 1;
685 }
686  
687 1 ▶sub delete { ## no critic (homonym)
Subroutine delete: called CC=7 SCAR=22.9
Pod documented
688 1 my $self = shift;
689 1 my $util = $self->util();
690 1 my $tr_state = $util->transactions();
691 1 my $dbh = $util->dbh();
692 1 my $pk = $self->primary_key();
693  
694 1 ▶ if(!$pk || !$self->$pk()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk and $self->$pk) 0 1 1
Condition33%1/3
$pk and $self->$pk
!ll&&!rl&&r
001
MC/DC0%0/2
$pk and $self->$pk
$pk $self->$pk
Truth table33%1/3
A and B
A= $pkB= $self->$pk
ABresult
0X0
100
111
695 0 croak q(No primary key);
696 }
697  
698 1 my $query = <<"EOT";
699 1 DELETE FROM @{[$self->table()]}
700 WHERE $pk=?
701 EOT
702  
703 eval {
704 1 $dbh->do($query, {}, $self->$pk());
705  
706 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbh->do($query, {}, $self->$pk) } } or do { $tr_state and $dbh->rollback; croak($ClearPress::model::EVAL_ERROR . $query) } 1 0 1
707 0 ▶ $tr_state and $dbh->rollback();
Branch: 1 branch
BranchTrueFalseTotal
$tr_state and $dbh->rollback 0 0 0
708 0 croak $EVAL_ERROR.$query;
709 };
710  
711 eval {
712 1 ▶ $tr_state and $dbh->commit();
Branch: 1 branch
BranchTrueFalseTotal
$tr_state and $dbh->commit 1 0 1
713 1 1;
714  
715 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $tr_state and $dbh->commit; 1 } } or do { croak($ClearPress::model::EVAL_ERROR) } 1 0 1
716 0 croak $EVAL_ERROR;
717 };
718  
719 1 return 1;
720 }
721  
722 0 ▶sub save {
Subroutine save: not called CC=3 SCAR=24.8
Pod documented
723 0 my $self = shift;
724 0 my $pk = $self->primary_key();
725  
726 0 ▶ if($pk && defined $self->{$pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk and defined $self->{$pk}) 0 0 0
Condition0%0/3
$pk and defined $self->{$pk}
!ll&&!rl&&r
000
MC/DC0%0/2
$pk and defined $self->{$pk}
$pk defined $self->{$pk}
Truth table0%0/3
A and B
A= $pkB= defined $self->{$pk}
ABresult
0X0
100
111
727 0 return $self->update();
728 }
729  
730 0 return $self->create();
731 }
732  
733 1 ▶sub zdate {
Subroutine zdate: called CC=4 SCAR=16.8
Pod documented
734 1 my $self = shift;
735 1 my $date = q[];
736  
737 1 ▶ if(scalar grep { $_ eq 'date' } $self->fields()) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ eq "date";} $self->fields)) 0 1 1
738 0 ▶ $date = $self->date() || q[];
Condition0%0/2
$self->date || ''
l!l
00
MC/DC0%0/1
$self->date || ''
$self->date
Truth table0%0/2
A
A= $self->date
Aresult
11
00
739 0 $date =~ s/[ ]/T/smx;
740 0 $date .='Z';
741 }
742  
743 1 ▶ if(!$date) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date) 1 0 1
744 1 $date = strftime q(%Y-%m-%dT%H:%M:%SZ), gmtime;
745 }
746  
747 1 return $date;
748 }
749  
750 3 ▶sub isodate {
Subroutine isodate: called CC=1 SCAR=0.0
Pod documented
751 3 return strftime q(%Y-%m-%d %H:%M:%S), gmtime;
752 }
753  
754 0 ▶sub as_json {
Subroutine as_json: not called CC=2 SCAR=17.9
Pod documented
755 0 my ($self, $cb) = @_;
756 # $self->read;
757 my $obj = {
758 0 map { $_ => $self->{$_} } ($self->fields)
759 };
760 0 ▶ if($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
761 0 $cb->($obj);
762 }
763  
764 0 return JSON->new->encode($obj);
765 }
766  
767 9 1;
768 __END__
« blib/lib/ClearPress/view/error.pm ↑ Summary blib/lib/ClearPress/driver/SQLite.pm »