DBIC/Resource/Role/DBICParams.pm

filter: Statementstmt 94.2% 130 / 138 Branchbran 66.2% 53 / 80 Conditioncond 43.7% 7 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 3 totaltotal 79.4% 213 / 268 CC 53 SCAR 23.2
CC
53
Cov
81%
handle_request_params
21.6
_resolve_prefetch
14.2
_handle_sort_param
11.0
CRAP 10.1
« DBIC/TypeNamer.pm ↑ Summary DBIC/Resource/Role/ItemInvoke.pm »
1 package WebAPI::DBIC::Resource::Role::DBICParams;
2 25 $WebAPI::DBIC::Resource::Role::DBICParams::VERSION = '0.004002';
3  
4 25 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use Devel::Dwarn;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 25 requires 'set';
12 25 requires 'throwable';
13 25 requires 'prefetch';
14  
15 # TODO the params supported by a resource should be determined by the roles
16 # consumed by that resource, plus any extra params it wants to declare support for.
17 # So this should be reworked to enable that.
18  
19  
20 # we use malformed_request() call from Web::Machine to trigger parameter processing
21 149 ▶sub malformed_request {
Subroutine malformed_request: called CC=1 SCAR=0.0
Pod undocumented
22 149 my $self = shift;
23  
24 149 $self->handle_request_params;
25  
26 146 return 0;
27 }
28  
29  
30 # used to a) define order that params are handled,
31 # and b) to force calling of a handler even if param is missing
32 149 ▶sub get_param_order {
Subroutine get_param_order: called CC=1 SCAR=0.0
Pod undocumented
33 149 return qw(page rows sort);
34 }
35  
36  
37 # call _handle_${basename}_param methods for each parameter
38 # where basename is the name with any .suffix removed ('me.id' => 'me')
39 149 ▶sub handle_request_params {
Subroutine handle_request_params: called CC=15 SCAR=30.7
Pod undocumented
40 149 my $self = shift;
41  
42 149 my %queue;
43 149 for my $param ($self->param) {
44 154 ▶ next if $param eq ""; # ignore empty parameters
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "" 0 154 154
45  
46 154 my @v = $self->param($param);
47 # XXX we don't handle multiple params which appear more than once
48 154 ▶ die "Multiple $param parameters are not supported\n" if @v > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @v > 1 0 154 154
49  
50 # parameters with names containing a '.' are assumed to be search criteria
51 # this covers both 'me.field=foo' and 'relname.field=bar'
52 154 ▶ if ($param =~ /^\w+\.\w+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^\w+\.\w+/) 23 131 154
53 23 $param =~ s/^me\.(\w+\.\w+)/$1/; # handle deprecated 'me.relname.fieldname' form
54 23 $queue{search_criteria}->{$param} = $v[0];
55 23 next;
56 }
57 131 ▶ die "Explicit search_criteria param not allowed"
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "search_criteria" 0 131 131
58 if $param eq 'search_criteria';
59  
60 # for parameters with names like foo[x]=3&foo[y]=4
61 # we accumulate the value as a hash { x => 3, y => 4 }
62 131 ▶ if ($param =~ /^(\w+)\[(\w+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(\w+)\[(\w+)\]$/) { } 0 131 131
63 die "$param=$v[0] can't follow $param=$queue{$param} parameter\n"
64 0 ▶ if $queue{$1} and not ref $queue{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $queue{$1} and not ref $queue{$1} 0 0 0
Condition0%0/3
$queue{$1} and not ref $queue{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$queue{$1} and not ref $queue{$1}
$queue{$1} not ref $queue{$1}
Truth table0%0/3
A and not(B)
A= $queue{$1}B= not ref $queue{$1}
ABresult
0X0
100
111
65 0 $queue{$1}{$2} = $v[0];
66 }
67 else {
68 die "$param=$v[0] can't follow $param=$queue{$param} parameter\n"
69 131 ▶ if $queue{$param} and ref $queue{$param};
Branch: 1 branch
BranchTrueFalseTotal
if $queue{$param} and ref $queue{$param} 0 131 131
Condition33%1/3
$queue{$param} and ref $queue{$param}
!ll&&!rl&&r
13100
MC/DC0%0/2
$queue{$param} and ref $queue{$param}
$queue{$param} ref $queue{$param}
Truth table33%1/3
A and B
A= $queue{$param}B= ref $queue{$param}
ABresult
0X0
100
111
70 131 ▶ $param = 'sort' if $param eq 'order'; # XXX back-compat
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "order" 13 118 131
71 131 $queue{$param} = $v[0];
72 }
73 }
74  
75 # call handlers in desired order, then any remaining ones
76 149 my %done;
77 149 for my $param ($self->get_param_order, keys %queue) {
78 598 ▶ next if $done{$param}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$param}++ 45 553 598
79 553 my $value = delete $queue{$param};
80  
81 553 my $method = "_handle_${param}_param";
82 553 ▶ unless ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($method)) 0 553 553
83 0 die "The $param parameter is not supported by the $self resource\n";
84 }
85 553 $self->$method($value, $param);
86 }
87  
88 146 return 0;
89 }
90  
91  
92 ## no critic (ProhibitUnusedPrivateSubroutines)
93  
94 149 ▶sub _handle_rows_param {
Subroutine _handle_rows_param: called CC=2 SCAR=6.9
95 149 my ($self, $value) = @_;
96 149 ▶ $value = 30 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 131 18 149
97 149 $self->set( $self->set->search_rs(undef, { rows => $value }) );
98 149 return;
99 }
100  
101  
102 149 ▶sub _handle_page_param {
Subroutine _handle_page_param: called CC=2 SCAR=6.9
103 149 my ($self, $value) = @_;
104 149 ▶ $value = 1 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 141 8 149
105 149 $self->set( $self->set->search_rs(undef, { page => $value }) );
106 149 return;
107 }
108  
109  
110 1 ▶sub _handle_with_param { }
Subroutine _handle_with_param: called CC=1 SCAR=0.0
111  
112  
113 3 ▶sub _handle_rollback_param { }
Subroutine _handle_rollback_param: called CC=1 SCAR=0.0
114  
115  
116 20 ▶sub _handle_search_criteria_param {
Subroutine _handle_search_criteria_param: called CC=1 SCAR=0.0
117 20 my ($self, $value) = @_;
118 20 $self->set( $self->set->search_rs($value) );
119 20 return;
120 }
121  
122 63 ▶sub _handle_prefetch_param {
Subroutine _handle_prefetch_param: called CC=5 SCAR=16.2
123 63 my ($self, $value) = @_;
124  
125 # Prefetchs/join in DBIC accepts either:
126 # prefetch => relname OR
127 # prefetch => [relname1, relname2] OR
128 # prefetch => {relname1 => relname_on_relname1} OR
129 # prefetch => [{relname1 => [{relname_on_relname1 => relname_on_relname_on_relname1}, other_relname_on_relaname1]},relname2] ETC
130  
131 # Noramalise all prefetches to most complicated form.
132 # eg &prefetch=foo,bar or &prefetch.json={...}
133 63 my $prefetch = $self->_resolve_prefetch($value, $self->set->result_source);
134  
135 60 ▶ return unless scalar @$prefetch;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$prefetch 0 60 60
136 # XXX hack?: perhaps use {embedded}{$key} = sub { ... };
137 # see lib/WebAPI/DBIC/Resource/Role/DBIC.pm
138 60 $self->prefetch( $prefetch ); # include self, even if deleted below
139 60 $prefetch = [grep { !defined $_->{self}} @$prefetch];
140  
141 60 ▶ my $prefetch_or_join = $self->param('fields') ? 'join' : 'prefetch';
Branch: 1 branch
BranchTrueFalseTotal
$self->param('fields') ? : 6 54 60
142 60 ▶ Dwarn { $prefetch_or_join => $prefetch } if $ENV{WEBAPI_DBIC_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"WEBAPI_DBIC_DEBUG"} 0 60 60
143 60 ▶ $self->set( $self->set->search_rs(undef, { $prefetch_or_join => $prefetch }))
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$prefetch 49 11 60
144 if scalar @$prefetch;
145  
146 60 return;
147 }
148  
149 79 ▶sub _resolve_prefetch {
Subroutine _resolve_prefetch: called CC=13 SCAR=26.5
150 79 my ($self, $prefetch, $result_class) = @_;
151 79 my @errors;
152  
153 # Here we recursively resolve each of the prefetches to normalise them all to the most complicated
154 # form that can exist. The results will be a ArrayRef of HashRefs that can be passed to DBIC
155 # directly.
156 # This code is largely taken from the _resolve_join subroutine in DBIx::Class
157  
158 79 ▶ return [] unless defined $prefetch and length $prefetch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefetch and length $prefetch 0 79 79
Condition33%1/3
defined $prefetch and length $prefetch
!ll&&!rl&&r
0079
MC/DC0%0/2
defined $prefetch and length $prefetch
defined $prefetch length $prefetch
Truth table33%1/3
A and B
A= defined $prefetchB= length $prefetch
ABresult
0X0
100
111
159 79 my @return;
160  
161 79 ▶ if (ref $prefetch eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $prefetch eq 'ARRAY') { } 3 76 79
elsif (ref $prefetch eq 'HASH') { } 13 63 76
elsif (ref $prefetch) { } 0 63 63
162 push @return, map {
163 3 @{$self->_resolve_prefetch($_, $result_class)}
164 } @$prefetch;
165 } elsif (ref $prefetch eq 'HASH') {
166 13 for my $rel (keys %$prefetch) {
167 13 ▶ next if $rel eq 'self';
Branch: 1 branch
BranchTrueFalseTotal
if $rel eq "self" 0 13 13
168  
169 13 ▶ if (my @validate_errors = $self->_validate_relationship($result_class, $rel)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@validate_errors) = $self->_validate_relationship($result_class, $rel)) { } 0 13 13
170 0 push @errors, @validate_errors;
171 } else {
172 push @return, {
173 13 $rel => $self->_resolve_prefetch($prefetch->{$rel}, $result_class->related_source($rel))
174 };
175 }
176 }
177 } elsif (ref $prefetch) {
178 0 push @errors,
179 "No idea how to resolve prefetch reftype ".ref $prefetch;
180 } else {
181 63 for my $rel (split ',', $prefetch) {
182 83 my @validate_errors = $self->_validate_relationship($result_class, $rel);
183 83 ▶ if ($rel ne 'self' && scalar @validate_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel ne 'self' and scalar @validate_errors) { } 3 80 83
Condition100%2/2
$rel ne "self" and scalar @validate_errors
!ll
1865
MC/DC100%1/1
$rel ne "self" and scalar @validate_errors
$rel ne "self"
Truth table100%2/2
A
A= $rel ne "self"
Aresult
00
11
184 3 push @errors, @validate_errors;
185 } else {
186 80 push @return, {
187 $rel => [{}],
188 };
189 }
190 }
191 }
192  
193 79 ▶ $self->throwable->throw_bad_request(400, errors => \@errors)
Branch: 1 branch
BranchTrueFalseTotal
if @errors 3 76 79
194 if @errors;
195  
196 76 return \@return;
197 }
198  
199 96 ▶sub _validate_relationship {
Subroutine _validate_relationship: called CC=1 SCAR=0.0
200 96 my ($self, $result_class, $rel) = @_;
201 96 my @errors;
202  
203 my $rel_info;
204 96 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
205 96 $rel_info = $result_class->relationship_info($rel);
206 96 local $SIG{__DIE__}; # avoid strack trace from these dies:
207 96 ▶ die "no relationship with that name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_info 21 75 96
208 if not $rel_info;
209 die "relationship is $rel_info->{attrs}{accessor} but only single, filter and multi are supported\n"
210 75 ▶ if not $rel_info->{attrs}{accessor} =~ m/^(?:single|filter|multi)$/; # sanity
Branch: 1 branch
BranchTrueFalseTotal
if not $rel_info->{"attrs"}{"accessor"} =~ /^(?:single|filter|multi)$/ 0 75 75
211 }
212 21 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
213 21 push @errors, {
214 $rel => $_,
215 _meta => {
216 relationship => $rel_info,
217 relationships => [ sort $result_class->relationships ]
218 }, # XXX
219 };
220 96 };
221  
222 96 return @errors;
223 }
224  
225 17 ▶sub _handle_fields_param {
Subroutine _handle_fields_param: called CC=5 SCAR=16.8
226 17 my ($self, $value) = @_;
227 17 my @columns;
228  
229 17 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 17 17
230 0 @columns = @$value;
231 }
232 else {
233 17 @columns = split /\s*,\s*/, $value;
234 }
235  
236 17 for my $clause (@columns) {
237 # we take care to avoid injection risks
238 52 my ($field) = ($clause =~ /^ ([a-z0-9_\.]*) $/x);
239 52 ▶ $self->throwable->throw_bad_request(400, errors => [{
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 52 52
240 parameter => "invalid fields clause",
241 _meta => { fields => $field, }, # XXX
242 }]) if not defined $field;
243 }
244  
245 17 ▶ $self->set( $self->set->search_rs(undef, { columns => \@columns }) )
Branch: 1 branch
BranchTrueFalseTotal
if @columns 17 0 17
246 if @columns;
247  
248 17 return;
249 }
250  
251  
252 149 ▶sub _handle_sort_param {
Subroutine _handle_sort_param: called CC=10 SCAR=23.9
253 149 my ($self, $value) = @_;
254 149 my @order_spec;
255  
256 # to support sort[typename]=... we need to be able to make type names
257 # to relationship names that map to the type and are included in the query
258 # (there might be more than one relationship on 'me' that leads to
259 # the same resource type so there's a potential ambiguity)
260 149 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 149 149
261 0 $self->throwable->throw_bad_request(400, errors => [{
262 parameter => "per-type sort specifiers are not supported yet",
263 _meta => { sort => $value, }, # XXX
264 }]);
265 }
266  
267 149 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 130 19 149
268 130 $value = (join ",", map { "me.$_" } $self->set->result_source->primary_columns);
269 }
270  
271 149 for my $clause (split /,/, $value) {
272  
273 # we take care to avoid injection risks
274 165 my ($field, $dir);
275 165 ▶ if ($clause =~ /^ ([a-z0-9_\.]*)\b (?:\s+(asc|desc))? $/xi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($clause =~ /^ ([a-z0-9_\.]*)\b (?:\s+(asc|desc))? $/xi) { } 162 3 165
elsif ($clause =~ /^ (-?) ([a-z0-9_\.]*)$/xi) { } 3 0 3
276 162 ▶ ($field, $dir) = ($1, $2 || 'asc');
Condition100%2/2
$2 || 'asc'
l!l
6156
MC/DC100%1/1
$2 || 'asc'
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
277 }
278 elsif ($clause =~ /^ (-?) ([a-z0-9_\.]*)$/xi) {
279 3 ▶ ($field, $dir) = ($2, ($1) ? 'desc' : 'asc');
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 3 0 3
280 }
281  
282 165 ▶ unless (defined $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field) 0 165 165
283 0 $self->throwable->throw_bad_request(400, errors => [{
284 parameter => "invalid order clause",
285 _meta => { order => $clause, }, # XXX
286 }]);
287 }
288  
289 # https://metacpan.org/pod/SQL::Abstract#ORDER-BY-CLAUSES
290 165 push @order_spec, { "-$dir" => $field };
291 }
292  
293 149 ▶ $self->set( $self->set->search_rs(undef, { order_by => \@order_spec }) )
Branch: 1 branch
BranchTrueFalseTotal
if @order_spec 149 0 149
294 if @order_spec;
295  
296 149 return;
297 }
298  
299  
300 2 ▶sub _handle_distinct_param {
Subroutine _handle_distinct_param: called CC=6 SCAR=19.2
301 2 my ($self, $value) = @_;
302 2 my @errors;
303  
304 # these restrictions avoid edge cases we don't want to deal with yet
305 2 ▶ my $sort = $self->param('sort') || $self->param('order'); # XXX insufficient
Condition33%1/3
$self->param('sort') || $self->param('order')
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->param('sort') || $self->param('order')
$self->param('sort') $self->param('order')
Truth table33%1/3
A || B
A= $self->param('sort')B= $self->param('order')
ABresult
1X1
011
000
306 2 ▶ push @errors, "distinct param requires sort (or order) param"
Branch: 1 branch
BranchTrueFalseTotal
unless $sort 0 2 2
307 unless $sort;
308 2 ▶ push @errors, "distinct param requires fields param"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param("fields") 0 2 2
309 unless $self->param('fields');
310 2 ▶ push @errors, "distinct param requires fields and orders parameters to have same value"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->param("fields") eq $sort 0 2 2
311 unless $self->param('fields') eq $sort;
312 2 my $errors = join(", ", @errors);
313 2 ▶ die "$errors\n" if $errors; # TODO throw?
Branch: 1 branch
BranchTrueFalseTotal
if $errors 0 2 2
314  
315 2 $self->set( $self->set->search_rs(undef, { distinct => $value }) );
316  
317 2 return;
318 }
319  
320  
321  
322 25 1;
323  
324 __END__
« DBIC/TypeNamer.pm ↑ Summary DBIC/Resource/Role/ItemInvoke.pm »