Smooth/FilterItem.pm

filter: Statementstmt 27.6% 34 / 123 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 38.1% 8 / 21 Podpod 0.0% 0 / 7 totaltotal 16.3% 42 / 257 CC 45 SCAR 66.1
CC
45
Cov
16%
parse
1332.0
__ANON__
30.0
shift_parts
20.0
CRAP 742.0
↑ Summary Smooth/Helper/ResultSet/Shortcut/RemoveColumns.pm »
1 25 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DBIx::Class::Smooth::FilterItem;
6  
7 # ABSTRACT: Short intro
8 25 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 25 our $VERSION = '0.0104';
10  
11 25 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Safe::Isa qw/$_isa/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 25 ▶use experimental qw/signatures postderef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # parts, resultset and value (and nothing else) are constructor args
19 has parts => (
20 is => 'rw',
21 required => 1,
22 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 25 );
24 has resultset => (
25 is => 'rw',
26 required => 1,
27 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 25 );
29 25 has value => (
30 is => 'rw',
31 required => 1,
32 );
33  
34 # so these are *not* constructor args
35 25 for my $scalar_arg (qw/left_hand_prefix operator sql_operator column_name/) {
36 has $scalar_arg => (
37 is => 'rw',
38 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 100 );
40 }
41 has left_hand_functions => (
42 is => 'rw',
43 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 25 );
45 has quote_value => (
46 # it's a boolean!
47 is => 'rw',
48 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 25 );
50  
51 ▶around BUILDARGS => sub ($orig, $class, %args) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
52 0 ▶ if(exists $args{'resultset'} && exists $args{'value'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"resultset"} and exists $args{"value"}) 0 0 0
Condition0%0/3
exists $args{"resultset"} and exists $args{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"resultset"} and exists $args{"value"}
exists $args{"resultset"} exists $args{"value"}
Truth table0%0/3
A and B
A= exists $args{"resultset"}B= exists $args{"value"}
ABresult
0X0
100
111
53 0 ▶ if(blessed $args{'value'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($args{"value"})) 0 0 0
54 0 my $flatten_method = sprintf 'smooth__flatten__%s', blessed $args{'value'};
55 0 ▶ if($args{'resultset'}->can($flatten_method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"resultset"}->can($flatten_method)) 0 0 0
56 0 $args{'value'} = $args{'resultset'}->$flatten_method($args{'value'});
57 }
58 }
59 }
60 0 $class->$orig(%args);
61 25 };
62  
63 ▶sub get_part($self, $index) {
Subroutine get_part: not called CC=1 SCAR=6.9
Pod undocumented
64 0 return $self->parts->[$index];
65 }
66 ▶sub get_all_parts($self) {
Subroutine get_all_parts: not called CC=1 SCAR=6.9
Pod undocumented
67 0 return $self->parts->@*;
68 }
69 0 ▶sub shift_parts($self, $repeats = 1) {
Subroutine shift_parts: not called CC=4 SCAR=30.0
Pod undocumented
70 0 ▶ if($repeats == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($repeats == 1) { } 0 0 0
elsif ($repeats > 1) { } 0 0 0
71 0 return shift $self->parts->@*;
72 }
73 elsif($repeats > 1) {
74 0 return splice $self->parts->@*, 0, $repeats;
75 }
76 }
77  
78 ▶sub set_left_hand_prefix($self, $prefix) {
Subroutine set_left_hand_prefix: not called CC=2 SCAR=17.9
Pod undocumented
79 0 ▶ if(defined $self->left_hand_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->left_hand_prefix) 0 0 0
80 0 die "Trying to set left hand prefix ($prefix), but it is already set to (@{[ $self->left_hand_prefix ]})";
81 }
82 0 $self->left_hand_prefix($prefix);
83 }
84  
85 ▶sub add_left_hand_function($self, $data) {
Subroutine add_left_hand_function: not called CC=1 SCAR=6.9
Pod undocumented
86 0 push $self->left_hand_functions->@* => $data;
87 }
88 ▶sub left_hand_functions_get_all($self) {
Subroutine left_hand_functions_get_all: not called CC=2 SCAR=17.9
Pod undocumented
89 0 ▶ if(!$self->left_hand_functions) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->left_hand_functions) 0 0 0
90 0 return ();
91 }
92 0 return $self->left_hand_functions->@*;
93 }
94  
95 ▶sub parse($self) {
Subroutine parse: not called CC=36 SCAR=71.9
Pod undocumented
96 # Ordinary column name, like 'me.first_name' or 'that_relation.whatever', then we keep that as the column name
97 0 ▶ if($self->get_part(0) =~ m/\./) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_part(0) =~ /\./u) { } 0 0 0
elsif ($self->resultset->result_source->has_column($self->get_part(0))) { } 0 0 0
98 0 $self->column_name($self->shift_parts);
99 }
100 # Otherwise we make it into an ordinary column name
101 elsif($self->resultset->result_source->has_column($self->get_part(0))) {
102 0 $self->column_name(sprintf '%s.%s', $self->resultset->current_source_alias, $self->shift_parts);
103 }
104 else {
105 0 my $possible_relation = $self->get_part(0);
106 0 my $possible_column = $self->get_part(1);
107  
108 0 my $has_relationship = $self->resultset->result_source->has_relationship($possible_relation);
109  
110 0 ▶ if($has_relationship && defined $possible_column && $self->resultset->result_source->relationship_info($possible_relation)->{'class'}->has_column($possible_column)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_relationship and defined $possible_column and $self->resultset->result_source->relationship_info($possible_relation)->{'class'}->has_column($possible_column)) { } 0 0 0
elsif ($has_relationship and $self->value->$DBIx::Class::Smooth::FilterItem::_isa('DBIx::Class::Row')) { } 0 0 0
Condition0%0/6
$has_relationship and defined $possible_column and $self->resultset->result_source->relationship_info($possible_relation)->{"class"}->has_column($possible_column)
!ll&&!rl&&r
000
$has_relationship and defined $possible_column
!ll&&!rl&&r
000
MC/DC0%0/3
$has_relationship and defined $possible_column and $self->resultset->result_source->relationship_info($possible_relation)->{"class"}->has_column($possible_column)
$has_relationship defined $possible_column $self->resultset->result_source->relationship_info($possible_relation)->{"class"}->has_column($possible_column)
Truth table0%0/4
A and B and C
A= $has_relationshipB= defined $possible_columnC= $self->resultset->result_source->relationship_info($possible_relation)->{"class"}->has_column($possible_column)
ABCresult
0XX0
10X0
1100
1111
111 0 ▶ if($self->value->$_isa('DBIx::Class::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->value->$DBIx::Class::Smooth::FilterItem::_isa("DBIx::Class::Row")) 0 0 0
112 0 confess "Don't pass a row object to a column";
113 }
114 0 $self->column_name(sprintf '%s.%s', $self->shift_parts(2));
115 }
116 ▶ elsif($has_relationship && $self->value->$_isa('DBIx::Class::Row')) {
Condition0%0/3
$has_relationship and $self->value->$DBIx::Class::Smooth::FilterItem::_isa("DBIx::Class::Row")
!ll&&!rl&&r
000
MC/DC0%0/2
$has_relationship and $self->value->$DBIx::Class::Smooth::FilterItem::_isa("DBIx::Class::Row")
$has_relationship $self->value->$DBIx::Class::Smooth::FilterItem::_isa("DBIx::Class::Row")
Truth table0%0/3
A and B
A= $has_relationshipB= $self->value->$DBIx::Class::Smooth::FilterItem::_isa("DBIx::Class::Row")
ABresult
0X0
100
111
117 0 $self->column_name(sprintf '%s.id', $possible_relation);
118 0 $self->value($self->value->id);
119 0 $self->shift_parts;
120 }
121 else {
122 0 confess "Has no relation <$possible_relation> or that has no column <$possible_column>";
123 }
124 }
125  
126 0 for my $part ($self->get_all_parts) {
127 0 my @params = ();
128 0 ▶ if($part =~ m{^ (\w+) \( ([^)]+) \) $}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^ (\w+) \( ([^)]+) \) $/ux) 0 0 0
129 0 $part = $1;
130 0 @params = split /\s*,\s*/ => $2;
131 }
132 0 my $method = "smooth__lookup__$part";
133  
134 0 my $lookup_result;
135 0 ▶ if($self->resultset->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->resultset->can($method)) { } 0 0 0
136 0 $lookup_result = $self->resultset->$method($self->column_name, $self->value, \@params);
137 }
138 else {
139 0 confess "Can't do <$method>, find suitable Lookup and add it to load_components";
140 }
141  
142 0 ▶ if(!exists $lookup_result->{'value'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $lookup_result->{"value"}) 0 0 0
143 0 confess "Lookup for <$part> is expected to return { value => ... }, can't proceed";
144 }
145 0 $self->value(delete $lookup_result->{'value'});
146 0 ▶ if(exists $lookup_result->{'left_hand_function'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup_result->{"left_hand_function"}) 0 0 0
147 0 $self->add_left_hand_function(delete $lookup_result->{'left_hand_function'});
148 }
149 0 ▶ if(exists $lookup_result->{'left_hand_prefix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup_result->{"left_hand_prefix"}) 0 0 0
150 0 $self->set_left_hand_prefix(delete $lookup_result->{'left_hand_prefix'});
151 }
152 0 ▶ if(exists $lookup_result->{'sql_operator'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup_result->{"sql_operator"}) 0 0 0
153 0 $self->sql_operator(delete $lookup_result->{'sql_operator'});
154 }
155 0 ▶ if(exists $lookup_result->{'operator'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup_result->{"operator"}) 0 0 0
156 0 $self->operator(delete $lookup_result->{'operator'});
157 }
158 0 ▶ if(exists $lookup_result->{'quote_value'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup_result->{'quote_value'}) { } 0 0 0
159 0 $self->quote_value(delete $lookup_result->{'quote_value'});
160 }
161 else {
162 0 $self->quote_value(1);
163 }
164 0 ▶ if(scalar keys $lookup_result->%*) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$lookup_result) 0 0 0
165 0 die sprintf "Unexpected keys returned from lookup for <$part>: %s", join(', ' => sort keys $lookup_result->%*);
166 }
167 }
168  
169 # Happy case
170 0 ▶ if((!defined $self->left_hand_functions || !scalar $self->left_hand_functions->@*) && !defined $self->left_hand_prefix && $self->quote_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->left_hand_functions && scalar @{$self->left_hand_functions} || defined $self->left_hand_prefix and $self->quote_value) { } 0 0 0
Condition0%0/9
not defined $self->left_hand_functions && scalar @{$self->left_hand_functions} || defined $self->left_hand_prefix and $self->quote_value
!ll&&!rl&&r
000
defined $self->left_hand_functions && scalar @{$self->left_hand_functions} || defined $self->left_hand_prefix
l!l&&r!l&&!r
000
defined $self->left_hand_functions and scalar @{$self->left_hand_functions}
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $self->left_hand_functions && scalar @{$self->left_hand_functions} || defined $self->left_hand_prefix and $self->quote_value
defined $self->left_hand_functions scalar @{$self->left_hand_functions} defined $self->left_hand_prefix $self->quote_value
Truth table0%0/7
not(A and B || C) and D
A= defined $self->left_hand_functionsB= scalar @{$self->left_hand_functions}C= defined $self->left_hand_prefixD= $self->quote_value
ABCDresult
11XX0
0X1X0
101X0
0X000
10000
0X011
10011
171 0 my $column_name = $self->column_name;
172  
173 0 ▶ if($self->operator && $self->operator ne '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->operator and $self->operator ne '=') { } 0 0 0
Condition0%0/3
$self->operator and $self->operator ne "="
!ll&&!rl&&r
000
MC/DC0%0/2
$self->operator and $self->operator ne "="
$self->operator $self->operator ne "="
Truth table0%0/3
A and B
A= $self->operatorB= $self->operator ne "="
ABresult
0X0
100
111
174 0 return ($self->column_name, { $self->operator => $self->value });
175 }
176 else {
177 0 return ($self->column_name, $self->value);
178 }
179 }
180 else {
181 0 my @left_hand = ();
182 0 ▶ if($self->left_hand_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->left_hand_prefix) 0 0 0
183 0 push @left_hand => $self->left_hand_prefix;
184 }
185 0 my $function_call_string = $self->column_name;
186 0 for my $lhf ($self->left_hand_functions_get_all) {
187 0 ▶ if(exists $lhf->{'complete'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $lhf->{'complete'}) { } 0 0 0
elsif (exists $lhf->{'name'}) { } 0 0 0
elsif (exists $lhf->{'start'} and exists $lhf->{'end'}) { } 0 0 0
188 0 $function_call_string = delete $lhf->{'complete'};
189 }
190 elsif(exists $lhf->{'name'}) {
191 0 $function_call_string = sprintf '%s(%s)', delete ($lhf->{'name'}), $function_call_string;
192 }
193 ▶ elsif(exists $lhf->{'start'} && exists $lhf->{'end'}) {
Condition0%0/3
exists $lhf->{"start"} and exists $lhf->{"end"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $lhf->{"start"} and exists $lhf->{"end"}
exists $lhf->{"start"} exists $lhf->{"end"}
Truth table0%0/3
A and B
A= exists $lhf->{"start"}B= exists $lhf->{"end"}
ABresult
0X0
100
111
194 0 $function_call_string = sprintf '%s%s%s', delete ($lhf->{'start'}), $function_call_string, delete ($lhf->{'end'});
195 }
196 }
197 0 push @left_hand => $function_call_string;
198 0 ▶ push @left_hand => $self->sql_operator ? $self->sql_operator : $self->operator ? $self->operator : '=';
Branch: 2 branches
BranchTrueFalseTotal
$self->sql_operator ? : 0 0 0
$self->operator ? : 0 0 0
199  
200 0 ▶ if($self->quote_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->quote_value) { } 0 0 0
201 # Either ? or (?, ?, ...., ?)
202 0 ▶ my $placeholders = ref $self->value eq 'ARRAY' ? '(' . join(', ', split (//, ('?' x scalar $self->value->@*))) . ')' : ' ? ';
Branch: 1 branch
BranchTrueFalseTotal
ref $self->value eq 'ARRAY' ? : 0 0 0
203 0 push @left_hand => $placeholders;
204  
205 0 my $left_hand = join ' ' => @left_hand;
206 0 ▶ return (undef, \[$left_hand, (ref $self->value eq 'ARRAY' ? $self->value->@* : $self->value)]);
Branch: 1 branch
BranchTrueFalseTotal
ref $self->value eq 'ARRAY' ? : 0 0 0
207 }
208 else {
209 0 push @left_hand => $self->value;
210 0 my $left_hand = join ' ' => @left_hand;
211 0 return (undef, \[$left_hand]);
212 }
213 }
214 }
215  
216 25 1;
217  
218 __END__
219  
220 =pod
221  
222 =encoding UTF-8
223  
224 =head1 NAME
225  
226 DBIx::Class::Smooth::FilterItem - Short intro
227  
228 =head1 VERSION
229  
230 Version 0.0104, released 2020-08-30.
231  
232 =head1 SOURCE
233  
234 L<https://github.com/Csson/p5-DBIx-Class-Smooth>
235  
236 =head1 HOMEPAGE
237  
238 L<https://metacpan.org/release/DBIx-Class-Smooth>
239  
240 =head1 AUTHOR
241  
242 Erik Carlsson <info@code301.com>
243  
244 =head1 COPYRIGHT AND LICENSE
245  
246 This software is copyright (c) 2018 by Erik Carlsson.
247  
248 This is free software; you can redistribute it and/or modify it under
249 the same terms as the Perl 5 programming language system itself.
250  
251 =cut
↑ Summary Smooth/Helper/ResultSet/Shortcut/RemoveColumns.pm »