| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$queue{$1} and not ref $queue{$1}
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}
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$queue{$param} and ref $queue{$param}
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}
| |||||||||||||||||||||||||||||
| 70 | 131 | ▶ | $param = 'sort' if $param eq 'order'; # XXX back-compat | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 79 | 553 | my $value = delete $queue{$param}; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 553 | my $method = "_handle_${param}_param"; | |||||||||||||||||||||||||||
| 82 | 553 | ▶ | unless ($self->can($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 142 | 60 | ▶ | Dwarn { $prefetch_or_join => $prefetch } if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 60 | ▶ | $self->set( $self->set->search_rs(undef, { $prefetch_or_join => $prefetch })) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $prefetch and length $prefetch
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
| |||||||||||||||||||||||||||||
| 159 | 79 | my @return; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 79 | ▶ | if (ref $prefetch eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 13 | ▶ | if (my @validate_errors = $self->_validate_relationship($result_class, $rel)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%2/2
$rel ne "self" and scalar @validate_errors
MC/DC100%1/1
$rel ne "self" and scalar @validate_errors
$rel ne "self"
Truth table100%2/2
A
A= $rel ne "self"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 276 | 162 | ▶ | ($field, $dir) = ($1, $2 || 'asc'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$2 || 'asc'
MC/DC100%1/1
$2 || 'asc'
$2
Truth table100%2/2
A
A= $2
| |||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | elsif ($clause =~ /^ (-?) ([a-z0-9_\.]*)$/xi) { | ||||||||||||||||||||||||||||
| 279 | 3 | ▶ | ($field, $dir) = ($2, ($1) ? 'desc' : 'asc'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 165 | ▶ | unless (defined $field) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||||||||||
| 306 | 2 | ▶ | push @errors, "distinct param requires sort (or order) param" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | unless $sort; | ||||||||||||||||||||||||||||
| 308 | 2 | ▶ | push @errors, "distinct param requires fields param" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | unless $self->param('fields'); | ||||||||||||||||||||||||||||
| 310 | 2 | ▶ | push @errors, "distinct param requires fields and orders parameters to have same value" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||