Easy/Record/Collection.pm

filter: Statementstmt 61.2% 114 / 186 Branchbran 43.7% 21 / 48 Conditioncond 46.9% 31 / 66 MC/DCmcdc 25.0% 9 / 36 Subroutinesub 66.6% 12 / 18 Podpod 0.0% 0 / 15 totaltotal 50.6% 187 / 369 CC 55 SCAR 34.9
CC
55
Cov
55%
ordered_list
156.0
records
14.7
tree
12.0
CRAP 32.9
↑ Summary Easy/Record.pm »
1 package DBI::Easy::Record::Collection;
2  
3 6 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use base qw(DBI::Easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $wrapper = 1;
8  
9 6 has 'filter', is => 'rw', default => {};
10 6 has 'join_table', is => 'rw';
11  
12 4 ▶sub new_record {
Subroutine new_record: called CC=3 SCAR=11.3
Pod undocumented
13 4 my $self = shift;
14 4 ▶ my $params = shift || {};
Condition50%1/2
shift() || {}
l!l
40
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
15
16 4 my $rec_pack = $self->record_package;
17
18 4 ▶ my $rec = $rec_pack->new ({%$params, %{$self->filter || {}}});
Condition50%1/2
$self->filter || {}
l!l
40
MC/DC0%0/1
$self->filter || {}
$self->filter
Truth table50%1/2
A
A= $self->filter
Aresult
11
00
19 }
20  
21 1 ▶sub natural_join {
Subroutine natural_join: called CC=1 SCAR=0.0
Pod undocumented
22 1 my $self = shift;
23
24 1 my $join = join ' ', map {'natural join ' . $_->table_quoted} @_;
25 1 $self->join_table ($join);
26 }
27  
28 39 ▶sub make_sql_and_bind {
Subroutine make_sql_and_bind: called CC=11 SCAR=24.3
Pod undocumented
29 39 my $self = shift;
30 39 my $method = shift;
31
32 39 my $set;
33 39 my $where = {};
34 39 my $suffix = '';
35 39 my $bind_suffix;
36
37 my %args;
38
39 # legacy syntax
40 39 ▶ if (! defined $_[0] or ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0] or ref $_[0]) { } 37 2 39
Condition66%2/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
3702
MC/DC50%1/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
41 37 $set = shift;
42 37 ▶ $where = shift || {};
Condition100%2/2
shift() || {}
l!l
298
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
43 37 ▶ $suffix = shift || '';
Condition50%1/2
shift() || ''
l!l
037
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
44  
45 37 $bind_suffix = shift;
46 37 %args = (@_);
47 } else {
48 2 %args = (@_);
49 2 ▶ $where = delete $args{where} || {};
Condition50%1/2
delete $args{'where'} || {}
l!l
20
MC/DC0%0/1
delete $args{'where'} || {}
delete $args{'where'}
Truth table50%1/2
A
A= delete $args{'where'}
Aresult
11
00
50 2 $set = delete $args{set};
51 2 ▶ $suffix = delete $args{suffix} || '';
Condition50%1/2
delete $args{'suffix'} || ''
l!l
02
MC/DC0%0/1
delete $args{'suffix'} || ''
delete $args{'suffix'}
Truth table50%1/2
A
A= delete $args{'suffix'}
Aresult
11
00
52 2 $bind_suffix = delete $args{bind};
53 }
54  
55 # if we call collection method from package name, we must create collection
56 # object automatically
57 39 ▶ $self = $self->new
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 7 32 39
58 unless ref $self;
59
60 39 my $filter = $self->filter;
61
62 39 my %params = (
63 where => [
64 $self->fields_to_columns ($filter),
65 $self->fields_to_columns ($where)
66 ],
67 suffix => $suffix,
68 %args
69 );
70
71 39 ▶ if ($method eq 'sql_update') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "sql_update") 0 39 39
72 0 $params{set} = $self->fields_to_columns ($set);
73 }
74
75 # use Data::Dumper;
76 # warn "$method => " . Dumper \%params;
77
78 39 my ($select, $bind) = $self->$method (%params);
79
80 39 ▶ push @$bind, @{$bind_suffix || []};
Condition100%2/2
$bind_suffix || []
l!l
2415
MC/DC100%1/1
$bind_suffix || []
$bind_suffix
Truth table100%2/2
A
A= $bind_suffix
Aresult
11
00
81
82 39 ▶ debug 'sql: \'', $select, '\' => ', defined $bind ? join ', ', @$bind : '[empty]';
Branch: 1 branch
BranchTrueFalseTotal
defined $bind ? : 39 0 39
83
84 39 return ($select, $bind);
85
86 }
87  
88 # legacy
89 16 ▶sub list {
Subroutine list: called CC=4 SCAR=13.9
Pod undocumented
90 16 my $self = shift;
91 16 ▶ my $where = shift || {};
Condition100%2/2
shift() || {}
l!l
115
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
92 16 ▶ my $suffix = shift || '';
Condition50%1/2
shift() || ''
l!l
016
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
93 16 ▶ my $bind_suffix = shift || [];
Condition100%2/2
shift() || []
l!l
313
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
94 16 my %params = @_;
95
96 16 return $self->records (where => $where, suffix => [$suffix, @$bind_suffix], %params);
97 }
98  
99 22 ▶sub records {
Subroutine records: called CC=11 SCAR=26.9
Pod undocumented
100 22 my $self = shift;
101 22 my $where;
102 my %params;
103  
104 22 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 1 21 22
105 1 $where = shift;
106 1 %params = @_;
107 } else {
108 21 %params = @_;
109 21 ▶ $where = delete $params{where} || {};
Condition100%2/2
delete $params{'where'} || {}
l!l
192
MC/DC100%1/1
delete $params{'where'} || {}
delete $params{'where'}
Truth table100%2/2
A
A= delete $params{'where'}
Aresult
11
00
110 }
111
112 22 my $suffix = '';
113 22 my $bind_suffix = [];
114
115 #TODO: REGRESSION FIX !!!
116
117 22 ▶ if ($params{suffix} and ref $params{suffix} and ref $params{suffix} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($params{'suffix'} and ref $params{'suffix'} and ref $params{'suffix'} eq 'ARRAY') { } 16 6 22
elsif ($params{'suffix'}) { } 0 6 6
Condition66%4/6
$params{"suffix"} and ref $params{"suffix"} and ref $params{"suffix"} eq "ARRAY"
!ll&&!rl&&r
6016
$params{"suffix"} and ref $params{"suffix"}
!ll&&!rl&&r
6016
MC/DC33%1/3
$params{"suffix"} and ref $params{"suffix"} and ref $params{"suffix"} eq "ARRAY"
$params{"suffix"} ref $params{"suffix"} ref $params{"suffix"} eq "ARRAY"
Truth table50%2/4
A and B and C
A= $params{"suffix"}B= ref $params{"suffix"}C= ref $params{"suffix"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
118 16 ▶ $suffix = shift @{$params{suffix}} || '';
Condition50%1/2
shift @{$params{'suffix'};} || ''
l!l
016
MC/DC0%0/1
shift @{$params{'suffix'};} || ''
shift @{$params{'suffix'};}
Truth table50%1/2
A
A= shift @{$params{'suffix'};}
Aresult
11
00
119 16 $bind_suffix = delete $params{suffix};
120 } elsif ($params{suffix}) {
121 0 ▶ $suffix = delete $params{suffix} || '';
Condition0%0/2
delete $params{'suffix'} || ''
l!l
00
MC/DC0%0/1
delete $params{'suffix'} || ''
delete $params{'suffix'}
Truth table0%0/2
A
A= delete $params{'suffix'}
Aresult
11
00
122 }
123
124 22 my @fetch_params = $self->make_sql_and_bind ('sql_select', undef, $where, $suffix, $bind_suffix, %params);
125
126 22 ▶ if ($params{fetch_handler} and ref $params{fetch_handler} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'fetch_handler'} and ref $params{'fetch_handler'} eq 'CODE') { } 0 22 22
Condition33%1/3
$params{"fetch_handler"} and ref $params{"fetch_handler"} eq "CODE"
!ll&&!rl&&r
2200
MC/DC0%0/2
$params{"fetch_handler"} and ref $params{"fetch_handler"} eq "CODE"
$params{"fetch_handler"} ref $params{"fetch_handler"} eq "CODE"
Truth table33%1/3
A and B
A= $params{"fetch_handler"}B= ref $params{"fetch_handler"} eq "CODE"
ABresult
0X0
100
111
127
128 0 debug "fetch by record";
129
130 0 ▶ $self->fetch_handled (@fetch_params, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 my $row = shift;
132
133 0 my $rec = $self->record_package->new (column_values => $row);
134
135 0 return $params{fetch_handler}->($rec);
136 0 });
137
138
139 } else {
140 22 my $db_result = $self->fetch_arrayref (@fetch_params);
141
142 22 debug "result count: ", $#$db_result+1;
143
144 22 $self->columns_to_fields_in_place ($db_result);
145
146 22 return $db_result;
147 }
148 }
149  
150 1 ▶sub list_of_record_hashes {
Subroutine list_of_record_hashes: called CC=1 SCAR=0.0
Pod undocumented
151 1 my $self = shift;
152 1 my $records = $self->records (@_);
153
154 1 my $list_of_hashes = [map {$_->hash} @$records];
155
156 1 return $list_of_hashes;
157 }
158  
159 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod undocumented
160 0 my $self = shift;
161
162 0 my ($sql, $bind) = $self->make_sql_and_bind ('sql_update', @_);
163
164 0 my $db_result = $self->no_fetch ($sql, $bind);
165
166 0 debug "rows affected: ", $db_result;
167
168 0 return $db_result;
169 }
170  
171  
172 16 ▶sub count {
Subroutine count: called CC=3 SCAR=11.0
Pod undocumented
173 16 my $self = shift;
174
175 16 my ($select, $bind);
176
177 16 ▶ if (ref $_[0] or @_ % 2) { # make_sql_and_bind (set, where, suffix, bind)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] or @_ % 2) { } 7 9 16
Condition100%3/3
ref $_[0] or @_ % 2
l!l&&r!l&&!r
349
MC/DC100%2/2
ref $_[0] or @_ % 2
ref $_[0] @_ % 2
Truth table100%3/3
A or B
A= ref $_[0]B= @_ % 2
ABresult
1X1
011
000
178 7 ($select, $bind) = $self->make_sql_and_bind ('sql_select_count', undef, @_);
179
180 } else { # make_sql_and_bind (set => set, where => where, ...)
181 9 ($select, $bind) = $self->make_sql_and_bind ('sql_select_count', @_);
182 }
183
184 16 my $db_result = $self->fetch_single ($select, $bind);
185
186 16 debug "result count: ", $db_result;
187
188 16 return $db_result;
189
190 }
191  
192 1 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.8
Pod undocumented
193 1 my $self = shift;
194
195 1 my ($select, $bind);
196
197 1 ▶ if (ref $_[0] or @_ % 2) { # make_sql_and_bind (set, where, suffix, bind)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] or @_ % 2) { } 0 1 1
Condition33%1/3
ref $_[0] or @_ % 2
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $_[0] or @_ % 2
ref $_[0] @_ % 2
Truth table33%1/3
A or B
A= ref $_[0]B= @_ % 2
ABresult
1X1
011
000
198 0 ($select, $bind) = $self->make_sql_and_bind ('sql_delete', undef, @_);
199
200 } else { # make_sql_and_bind (set => set, where => where, ...)
201 1 ($select, $bind) = $self->make_sql_and_bind ('sql_delete', @_);
202 }
203
204 1 my $db_result = $self->no_fetch ($select, $bind);
205
206 1 debug "rows affected: ", $db_result;
207
208 1 return $db_result;
209 }
210  
211 0 ▶sub tree {
Subroutine tree: not called CC=3 SCAR=24.8
Pod undocumented
212 0 my $self = shift;
213 0 my $keys = shift;
214 0 my $where = shift;
215 0 my $suffix = shift;
216
217 0 my $ref = ref $self;
218  
219 0 my $where_w_filter = $where;
220 0 my $filter = $self->filter;
221 0 ▶ $where_w_filter = {%$where, %$filter}
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter and ref $filter eq "HASH" 0 0 0
Condition0%0/3
defined $filter and ref $filter eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filter and ref $filter eq "HASH"
defined $filter ref $filter eq "HASH"
Truth table0%0/3
A and B
A= defined $filterB= ref $filter eq "HASH"
ABresult
0X0
100
111
222 if defined $filter and ref $filter eq 'HASH';
223
224 0 my $where_prefixed = $self->fields_to_columns ($where_w_filter);
225
226 0 my ($select, $bind) = $self->sql_select (where => $where_prefixed, suffix => $suffix);
227
228 # warn $select, ' => ', defined $bind ? join ', ', @$bind : '[empty]';
229
230 0 my $db_result = $self->fetch_hashref ($select, $keys, $bind);
231 #my $db_result = $self->fetch_arrayref ($select, $bind);
232
233 0 $self->columns_to_fields_in_place ($db_result, $keys);
234
235 0 return $db_result;
236
237 }
238  
239 0 ▶sub item {
Subroutine item: not called CC=2 SCAR=17.9
Pod undocumented
240 0 my $self = shift;
241 0 my $where = shift;
242 0 ▶ my $suffix = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
243
244 0 my $result = $self->list ($where, $suffix . ' limit 1');
245
246 # programmer must be warned about multiple values
247 0 return $result->[0];
248 }
249  
250 0 ▶sub new_record_from_request {
Subroutine new_record_from_request: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my $self = shift;
252 0 my $request = shift;
253  
254 0 my $rec_pack = $self->record_package;
255
256 0 my $rec = $rec_pack->new ({%{$self->filter}});
257 0 $rec->apply_request_params ($request);
258
259 0 return $rec;
260 }
261  
262 22 ▶sub columns_to_fields_in_place {
Subroutine columns_to_fields_in_place: called CC=5 SCAR=19.4
Pod undocumented
263 22 my $self = shift;
264 22 my $rows = shift;
265
266 22 my $rec_pack = $self->record_package;
267
268 22 ▶ if (UNIVERSAL::isa ($rows, 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($rows, 'ARRAY')) { } 22 0 22
elsif (UNIVERSAL::isa($rows, 'HASH')) { } 0 0 0
269
270 22 foreach my $row_counter (0 .. $#$rows) {
271
272 39 my $row = $rows->[$row_counter];
273
274 39 $rows->[$row_counter] = $rec_pack->new (column_values => $row);
275 }
276 } elsif (UNIVERSAL::isa ($rows, 'HASH')) {
277
278 0 foreach my $row_key (keys %$rows) {
279
280 0 my $row = $rows->{$row_key};
281
282 0 $rows->{$row_key} = $rec_pack->new (column_values => $row);
283 }
284 }
285 }
286  
287 6 our $MAX_LIMIT = 300;
288  
289 0 ▶sub ordered_list {
Subroutine ordered_list: not called CC=12 SCAR=50.5
Pod undocumented
290 0 my $self = shift;
291
292 0 my $order = shift;
293 0 my $dir = shift;
294 0 my $limit = shift;
295 0 my $start = shift;
296
297 0 my $filter = shift;
298 0 ▶ my $bind = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
299
300 0 my $fields = $self->fields;
301
302 0 my $sort_col;
303 0 ▶ if (exists $fields->{$order}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $fields->{$order}) { } 0 0 0
elsif ($self->_pk_) { } 0 0 0
304 0 $sort_col = $fields->{$order}->{quoted_column_name};
305 } elsif ($self->_pk_) {
306 # we assume primary key ordering unless ordered column known
307 0 $sort_col = $fields->{$self->_pk_}->{quoted_column_name};
308 }
309
310 0 ▶ if ($dir =~ /^(asc|desc)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /^(asc|desc)$/i) { } 0 0 0
311 0 $dir = lc($1);
312 } else {
313 0 $dir = ''; # default sort
314 }
315
316 # When using LIMIT, it is important to use an ORDER BY clause that
317 # constrains the result rows into a unique order. Otherwise you will
318 # get an unpredictable subset of the query's rows. You might be asking
319 # for the tenth through twentieth rows, but tenth through twentieth
320 # in what ordering? The ordering is unknown, unless you specified ORDER BY.
321 0 ▶ if (!$sort_col or $start !~ /\d+/ or $limit !~ /\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sort_col and $start =~ /\d+/ and $limit =~ /\d+/) 0 0 0
Condition0%0/6
$sort_col and $start =~ /\d+/ and $limit =~ /\d+/
!ll&&!rl&&r
000
$sort_col and $start =~ /\d+/
!ll&&!rl&&r
000
MC/DC0%0/3
$sort_col and $start =~ /\d+/ and $limit =~ /\d+/
$sort_col $start =~ /\d+/ $limit =~ /\d+/
Truth table0%0/4
A and B and C
A= $sort_colB= $start =~ /\d+/C= $limit =~ /\d+/
ABCresult
0XX0
10X0
1100
1111
322 return {
323 0 count => 0,
324 error => "ordering-undefined"
325 };
326 }
327
328 0 $start =~ s/(\d+)/$1/;
329 0 $limit =~ s/(\d+)/$1/;
330
331 0 my $count = $self->count ($filter, '', $bind);
332  
333 0 ▶ if ($start > $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $count) 0 0 0
334 0 $start = $count - $limit;
335 0 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 0 0
336 }
337
338 0 ▶ if ($limit > $MAX_LIMIT or ! $limit > 0) { # try undef -)
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $DBI::Easy::Record::Collection::MAX_LIMIT or !$limit > 0) 0 0 0
Condition0%0/3
$limit > $DBI::Easy::Record::Collection::MAX_LIMIT or !$limit > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$limit > $DBI::Easy::Record::Collection::MAX_LIMIT or !$limit > 0
$limit > $DBI::Easy::Record::Collection::MAX_LIMIT !$limit > 0
Truth table0%0/3
A or not(B)
A= $limit > $DBI::Easy::Record::Collection::MAX_LIMITB= !$limit > 0
ABresult
1X1
011
000
339 0 $limit = $MAX_LIMIT;
340 }
341  
342 0 my $suffix = "order by $sort_col $dir limit $limit offset $start";
343 # debug "suffix: $suffix";
344
345 0 my $list = $self->list ($filter, $suffix, $bind);
346  
347 return {
348 0 items => $list,
349 total_count => $count,
350 version => 1,
351 };
352 }
353  
354 # page_size, count, page_num, pages_to_show
355 4 ▶sub pager {
Subroutine pager: called CC=8 SCAR=22.3
Pod undocumented
356 4 my $self = shift;
357 4 my $param = shift;
358  
359 4 ▶ my $page_size = $param->{page_size} || 20;
Condition50%1/2
$param->{'page_size'} || 20
l!l
40
MC/DC0%0/1
$param->{'page_size'} || 20
$param->{'page_size'}
Truth table50%1/2
A
A= $param->{'page_size'}
Aresult
11
00
360 4 my %pager;
361  
362 4 my $number_of_pages = int(($param->{count} + $page_size - 1) / $page_size);
363  
364 4 $pager{pager_needed} = ($param->{count} > $page_size);
365
366 4 ▶ unless ($pager{pager_needed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pager{"pager_needed"}) 0 4 4
367 0 return;
368 }
369
370 4 ▶ my $page_number = $param->{page_num} || 0;
Condition50%1/2
$param->{'page_num'} || 0
l!l
40
MC/DC0%0/1
$param->{'page_num'} || 0
$param->{'page_num'}
Truth table50%1/2
A
A= $param->{'page_num'}
Aresult
11
00
371  
372 4 ▶ my $pages_to_show = $param->{pages_to_show} || 10;
Condition50%1/2
$param->{'pages_to_show'} || 10
l!l
40
MC/DC0%0/1
$param->{'pages_to_show'} || 10
$param->{'pages_to_show'}
Truth table50%1/2
A
A= $param->{'pages_to_show'}
Aresult
11
00
373 4 my $quarter_to_show = int ($pages_to_show / 4);
374  
375 4 my @pages;
376
377 4 ▶ if ($param->{count} <= $pages_to_show) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"count"} <= $pages_to_show) 0 4 4
378 0 return [1 .. $param->{count}];
379 }
380
381 4 ▶ if ($page_number <= $quarter_to_show * 2 + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_number <= $quarter_to_show * 2 + 1) 3 1 4
382 return [
383 3 1 .. $quarter_to_show * 3 + 1,
384 undef,
385 $number_of_pages - $quarter_to_show + 1 .. $number_of_pages
386 ];
387 }
388  
389 1 ▶ if ($page_number >= $number_of_pages - ($quarter_to_show * 2 + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_number >= $number_of_pages - ($quarter_to_show * 2 + 1)) 0 1 1
390 return [
391 0 1 .. $quarter_to_show,
392 undef,
393 $number_of_pages - ($quarter_to_show * 3 + 1) .. $number_of_pages
394 ];
395 }
396
397 return [
398 1 1 .. $quarter_to_show,
399 undef,
400 $page_number - $quarter_to_show .. $page_number + $quarter_to_show,
401 undef,
402 $number_of_pages - $quarter_to_show + 1 .. $number_of_pages
403 ];
404
405 }
406  
407  
408 6 1;
↑ Summary Easy/Record.pm »