SQL/Abstract/Plugin/ExtraClauses.pm

filter: Statementstmt 97.9% 143 / 146 Branchbran 89.1% 66 / 74 Conditioncond 79.4% 27 / 34 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 100% 28 / 28 Podpod 100% 1 / 1 totaltotal 91.1% 278 / 305 CC 52 SCAR 16.6
CC
52
Cov
93%
_expand_from_list
12.0
_render_join
8.5
_expand_join
6.0
CRAP 5.2
« DBIx/Class/Storage/Debug/PrettyPrint.pm ↑ Summary SQL/Abstract/Parts.pm »
1 package SQL::Abstract::Plugin::ExtraClauses;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 with 'SQL::Abstract::Role::Plugin';
6  
7 1 ▶sub register_extensions {
Subroutine register_extensions: called CC=5 SCAR=16.1
Pod documented
8 1 my ($self, $sqla) = @_;
9  
10 1 my @clauses = $sqla->clauses_of('select');
11 1 my @before_setop;
12 1 CLAUSE: foreach my $idx (0..$#clauses) {
13 4 ▶ if ($clauses[$idx] eq 'order_by') {
Branch: 1 branch
BranchTrueFalseTotal
if ($clauses[$idx] eq "order_by") 1 3 4
14 1 @before_setop = @clauses[0..$idx-1];
15 1 splice(@clauses, $idx, 0, qw(setop group_by having));
16 1 last CLAUSE;
17 }
18 }
19  
20 1 ▶ die "Huh?" unless @before_setop;
Branch: 1 branch
BranchTrueFalseTotal
unless @before_setop 0 1 1
21 1 $sqla->clauses_of(select => @clauses);
22  
23 1 ▶ $sqla->clauses_of(update => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 my ($self, @clauses) = @_;
25 1 splice(@clauses, 2, 0, 'from');
26 1 @clauses;
27 1 });
28  
29 1 ▶ $sqla->clauses_of(delete => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 my ($self, @clauses) = @_;
31 1 splice(@clauses, 1, 0, 'using');
32 1 @clauses;
33 1 });
34  
35 $self->register(
36 (map +(
37 "${_}er" => [
38 do {
39 2 my $x = $_;
40 2 (map +($_ => "_${x}_${_}"), qw(join from_list alias))
41 }
42 ]
43 ), qw(expand render)),
44 binop_expander => [ as => '_expand_op_as' ],
45 renderer => [ as => '_render_as' ],
46 expander => [ cast => '_expand_cast' ],
47 clause_expanders => [
48 'select.group_by'
49 2 ▶ => sub { $_[0]->expand_expr({ -list => $_[2] }, -ident) },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 'select.having'
51 2 ▶ => sub { $_[0]->expand_expr($_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 'update.from' => '_expand_from_list',
53 "update.target", '_expand_update_clause_target',
54 "update.update", '_expand_update_clause_target',
55 'delete.using' => '_expand_from_list',
56 1 ▶ 'insert.rowvalues' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 +(from => $_[0]->expand_expr({ -values => $_[2] }));
58 },
59 3 ▶ 'insert.select' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 3 +(from => $_[0]->expand_expr({ -select => $_[2] }));
61 },
62 1 ],
63 );
64  
65 1 $sqla->expander(old_from => $sqla->clause_expander('select.from'));
66 1 ▶ $sqla->wrap_clause_expander('select.from', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 1 my ($orig) = @_;
68 29 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
69 29 my ($sqla, undef, $args) = @_;
70 29 ▶ if (ref($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 5 24 29
71 5 return $self->_expand_from_list(undef, $args);
72 }
73 24 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "ARRAY" and grep {not ref $_ and $_ =~ /^-/;} @$args) 9 15 24
Condition100%3/3
ref $args eq "ARRAY" and grep {not ref $_ and $_ =~ /^-/;} @$args
!ll&&!rl&&r
1419
MC/DC100%2/2
ref $args eq "ARRAY" and grep {not ref $_ and $_ =~ /^-/;} @$args
ref $args eq "ARRAY" grep {not ref $_ and $_ =~ /^-/;} @$args
Truth table100%3/3
A and B
A= ref $args eq "ARRAY"B= grep {not ref $_ and $_ =~ /^-/;} @$args
ABresult
0X0
100
111
74 ref($args) eq 'ARRAY'
75 42 ▶ and grep { !ref($_) and $_ =~ /^-/ } @$args
Condition100%3/3
!ref($_) && $_ =~ /^-/
!ll&&!rl&&r
131316
MC/DC100%2/2
!ref($_) && $_ =~ /^-/
!ref($_) $_ =~ /^-/
Truth table100%3/3
not(A) && B
A= !ref($_)B= $_ =~ /^-/
ABresult
0X0
100
111
76 ) {
77 9 return $self->_expand_from_list(undef, $args);
78 }
79 15 return $sqla->$orig(undef, $args);
80 }
81 1 });
82  
83 # set ops
84 1 ▶ $sqla->wrap_expander(select => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 $self->cb('_expand_select', $_[0], \@before_setop);
86 1 });
87  
88 $self->register(
89 clause_renderer => [
90 3 ▶ 'select.setop' => sub { $_[0]->render_aqt($_[2]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
91 1 ],
92 expander => [
93 map +($_ => '_expand_setop', "${_}_all" => '_expand_setop'), qw(union intersect except) ],
94 renderer => [ map +($_ => '_render_setop'), qw(union intersect except) ],
95 );
96  
97 1 my $setop_expander = $self->cb('_expand_clause_setop');
98  
99 1 $sqla->clause_expanders(
100 map +($_ => $setop_expander),
101 map "select.${_}",
102 map +($_, "${_}_all", "${_}_distinct"),
103 qw(union intersect except)
104 );
105  
106 1 foreach my $stmt (qw(select insert update delete)) {
107 4 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
108 4 $self->register(
109 clause_expanders => [
110 "${stmt}.with" => '_expand_with',
111 "${stmt}.with_recursive" => '_expand_with',
112 ],
113 clause_renderer => [ "${stmt}.with" => '_render_with' ],
114 );
115 }
116  
117 1 return $sqla;
118 }
119  
120 33 ▶sub _expand_select {
Subroutine _expand_select: called CC=3 SCAR=11.0
121 33 my ($self, $orig, $before_setop, @args) = @_;
122 33 my $exp = $self->sqla->$orig(@args);
123 33 ▶ return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
Branch: 1 branch
BranchTrueFalseTotal
unless my $setop = +(my $sel = $exp->{"-select"})->{"setop"} 29 4 33
124 4 ▶ if (my @keys = grep $sel->{$_}, @$before_setop) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@keys) = grep($sel->{$_}, @$before_setop)) 3 1 4
125 3 my %inner; @inner{@keys} = delete @{$sel}{@keys};
126 3 unshift @{(values(%$setop))[0]{queries}},
127 { -select => \%inner };
128 }
129 4 return $exp;
130 }
131  
132 44 ▶sub _expand_from_list {
Subroutine _expand_from_list: called CC=12 SCAR=24.9
133 44 my ($self, undef, $args) = @_;
134 44 ▶ if (ref($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 18 26 44
135 18 ▶ return $args if $args->{-from_list};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-from_list"} 3 15 18
136 15 return { -from_list => [ $self->expand_expr($args) ] };
137 }
138 26 my @list;
139 26 ▶ my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 14 12 26
140 26 ▶ while (my $entry = shift @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @args) 45 26 71
141 45 ▶ if (!ref($entry) and $entry =~ /^-(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $entry and $entry =~ /^-(.*)/) 19 26 45
Condition100%3/3
not ref $entry and $entry =~ /^-(.*)/
!ll&&!rl&&r
42219
MC/DC100%2/2
not ref $entry and $entry =~ /^-(.*)/
not ref $entry $entry =~ /^-(.*)/
Truth table100%3/3
not(A) and B
A= not ref $entryB= $entry =~ /^-(.*)/
ABresult
0X0
100
111
142 19 ▶ if ($1 eq 'as') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "as") 8 11 19
143 8 ▶ $list[-1] = $self->expand_expr({ -as => [
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 1 7 8
144 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
145 ]});
146 8 next;
147 }
148 11 $entry = { $entry => shift @args };
149 }
150 37 my $aqt = $self->expand_expr($entry, -ident);
151 37 ▶ if ($aqt->{-join} and not $aqt->{-join}{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aqt->{"-join"} and not $aqt->{"-join"}{"from"}) 10 27 37
Condition66%2/3
$aqt->{"-join"} and not $aqt->{"-join"}{"from"}
!ll&&!rl&&r
27010
MC/DC50%1/2
$aqt->{"-join"} and not $aqt->{"-join"}{"from"}
$aqt->{"-join"} not $aqt->{"-join"}{"from"}
Truth table66%2/3
A and not(B)
A= $aqt->{"-join"}B= not $aqt->{"-join"}{"from"}
ABresult
0X0
100
111
152 10 $aqt->{-join}{from} = pop @list;
153 }
154 37 push @list, $aqt;
155 }
156 26 ▶ return $list[0] if @list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 1 25 1 26
157 1 return { -from_list => \@list };
158 }
159  
160 13 ▶sub _expand_join {
Subroutine _expand_join: called CC=6 SCAR=18.0
161 13 my ($self, undef, $args) = @_;
162 13 ▶ my %proto = (
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 11 2 13
163 ref($args) eq 'HASH'
164 ? %$args
165 : (to => @$args)
166 );
167 13 ▶ if (my $as = delete $proto{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $as = delete $proto{"as"}) 9 4 13
168 $proto{to} = $self->expand_expr(
169 9 { -as => [ { -from_list => $proto{to} }, $as ] }
170 );
171 }
172 13 ▶ if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto{"using"} and ref(my $using = $proto{"using"}) ne "HASH") 1 12 13
Condition66%2/3
defined $proto{"using"} and ref(my $using = $proto{"using"}) ne "HASH"
!ll&&!rl&&r
1201
MC/DC50%1/2
defined $proto{"using"} and ref(my $using = $proto{"using"}) ne "HASH"
defined $proto{"using"} ref(my $using = $proto{"using"}) ne "HASH"
Truth table66%2/3
A and B
A= defined $proto{"using"}B= ref(my $using = $proto{"using"}) ne "HASH"
ABresult
0X0
100
111
173 $proto{using} = [
174 1 ▶ map [ $self->expand_expr($_, -ident) ],
Branch: 1 branch
BranchTrueFalseTotal
ref $using eq 'ARRAY' ? : 0 1 1
175 ref($using) eq 'ARRAY' ? @$using: $using
176 ];
177 }
178 my %ret = (
179 type => delete $proto{type},
180 13 to => $self->expand_expr({ -from_list => delete $proto{to} }, -ident)
181 );
182 %ret = (%ret,
183 13 map +($_ => $self->expand_expr($proto{$_}, -ident)),
184 sort keys %proto
185 );
186 13 return +{ -join => \%ret };
187 }
188  
189 6 ▶sub _render_from_list {
Subroutine _render_from_list: called CC=1 SCAR=0.0
190 6 my ($self, undef, $list) = @_;
191 6 return $self->join_query_parts(', ', @$list);
192 }
193  
194 10 ▶sub _render_join {
Subroutine _render_join: called CC=8 SCAR=21.4
195 10 my ($self, undef, $args) = @_;
196  
197 my @parts = (
198 $args->{from},
199 { -keyword => join '_', ($args->{type}||()), 'join' },
200 (map +($_->{-ident} || $_->{-as}
201 ? $_
202 : ('(', $self->render_aqt($_, 1), ')')),
203 10 ▶ map +(@{$_->{-from_list}||[]} == 1 ? $_->{-from_list}[0] : $_),
Branch: 2 branches
BranchTrueFalseTotal
$args->{'on'} ? : 9 1 10
$args->{'using'} ? : 1 9 10
Condition100%2/2
$_->{'-from_list'} || []
l!l
91
MC/DC100%1/1
$_->{'-from_list'} || []
$_->{'-from_list'}
Truth table100%2/2
A
A= $_->{'-from_list'}
Aresult
11
00
204 $args->{to}
205 ),
206 ($args->{on} ? (
207 { -keyword => 'on' },
208 $args->{on},
209 ) : ()),
210 ($args->{using} ? (
211 { -keyword => 'using' },
212 10 ▶ '(', $args->{using}, ')',
Branch: 2 branches
BranchTrueFalseTotal
$_->{'-ident'} || $_->{'-as'} ? : 10 0 10
@{$_->{'-from_list'} or [];} == 1 ? : 9 1 10
Condition50%3/6
$args->{'type'} || ()
l!l&&r!l&&!r
0100
$_->{"-ident"} or $_->{"-as"}
l!l&&r!l&&!r
190
MC/DC0%0/2
$args->{'type'} || ()
$args->{'type'} ()
MC/DC0%0/2
$_->{"-ident"} or $_->{"-as"}
$_->{"-ident"} $_->{"-as"}
Truth table33%1/3
A || B
A= $args->{'type'}B= ()
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $_->{"-ident"}B= $_->{"-as"}
ABresult
1X1
011
000
213 ) : ()),
214 );
215 10 return $self->join_query_parts(' ', @parts);
216 }
217  
218 36 ▶sub _expand_op_as {
Subroutine _expand_op_as: called CC=4 SCAR=13.9
219 36 my ($self, undef, $vv, $k) = @_;
220 36 ▶ my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
Branch: 1 branch
BranchTrueFalseTotal
ref $vv eq 'ARRAY' ? : 35 1 36
221 36 my $ik = $self->expand_expr($k, -ident);
222 36 ▶ return +{ -as => [ $ik, $self->expand_expr($vv[0], -ident) ] }
Branch: 1 branch
BranchTrueFalseTotal
if @vv == 1 and ref $vv[0] eq "HASH" 13 23 36
Condition100%3/3
@vv == 1 and ref $vv[0] eq "HASH"
!ll&&!rl&&r
22113
MC/DC100%2/2
@vv == 1 and ref $vv[0] eq "HASH"
@vv == 1 ref $vv[0] eq "HASH"
Truth table100%3/3
A and B
A= @vv == 1B= ref $vv[0] eq "HASH"
ABresult
0X0
100
111
223 if @vv == 1 and ref($vv[0]) eq 'HASH';
224  
225 23 my @as = map $self->expand_expr($_, -ident), @vv;
226 23 return { -as => [ $ik, $self->expand_expr({ -alias => \@as }) ] };
227 }
228  
229 23 ▶sub _render_as {
Subroutine _render_as: called CC=1 SCAR=0.0
230 23 my ($self, undef, $args) = @_;
231 23 my ($thing, $alias) = @$args;
232 23 return $self->join_query_parts(
233 ' ',
234 $thing,
235 { -keyword => 'as' },
236 $alias,
237 );
238 }
239  
240 3 ▶sub _render_alias {
Subroutine _render_alias: called CC=2 SCAR=7.1
241 3 my ($self, undef, $args) = @_;
242 3 my ($as, @cols) = @$args;
243 return (@cols
244 3 ▶ ? $self->join_query_parts('',
Branch: 1 branch
BranchTrueFalseTotal
@cols ? : 3 0 3
245 $as,
246 '(',
247 $self->join_query_parts(
248 ', ',
249 @cols
250 ),
251 ')',
252 )
253 : $self->render_aqt($as)
254 );
255 }
256  
257 4 ▶sub _expand_update_clause_target {
Subroutine _expand_update_clause_target: called CC=1 SCAR=0.0
258 4 my ($self, undef, $target) = @_;
259 4 +(target => $self->_expand_from_list(undef, $target));
260 }
261  
262 1 ▶sub _expand_cast {
Subroutine _expand_cast: called CC=2 SCAR=10.1
263 1 my ($self, undef, $thing) = @_;
264 1 ▶ return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $thing eq "HASH" 1 0 1
265 0 my ($cast, $to) = @{$thing};
266 0 +{ -func => [ cast => { -as => [
267 $self->expand_expr($cast),
268 $self->expand_expr($to, -ident),
269 ] } ] };
270 }
271  
272 30 ▶sub _expand_alias {
Subroutine _expand_alias: called CC=5 SCAR=16.1
273 30 my ($self, undef, $args) = @_;
274 30 ▶ if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH" and my $alias = $args->{"-alias"}) 1 29 30
Condition66%2/3
ref $args eq "HASH" and my $alias = $args->{"-alias"}
!ll&&!rl&&r
2901
MC/DC50%1/2
ref $args eq "HASH" and my $alias = $args->{"-alias"}
ref $args eq "HASH" my $alias = $args->{"-alias"}
Truth table66%2/3
A and B
A= ref $args eq "HASH"B= my $alias = $args->{"-alias"}
ABresult
0X0
100
111
275 1 $args = $alias;
276 }
277 my @parts = map $self->expand_expr($_, -ident),
278 30 ▶ ref($args) eq 'ARRAY' ? @{$args} : $args;
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 25 5 30
279 30 ▶ return $parts[0] if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 26 4 30
280 4 return { -alias => \@parts };
281 }
282  
283 5 ▶sub _expand_with {
Subroutine _expand_with: called CC=4 SCAR=13.9
284 5 my ($self, $name, $with) = @_;
285 5 my (undef, $type) = split '_', $name;
286 5 ▶ if (ref($with) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $with eq "HASH") 1 4 5
287 return +{
288 %$with,
289 queries => [
290 map +[
291 $self->expand_expr({ -alias => $_->[0] }, -ident),
292 $self->expand_expr($_->[1]),
293 1 ], @{$with->{queries}}
294 ]
295 }
296 }
297 4 my @with = @$with;
298 4 my @exp;
299 4 ▶ while (my ($alias, $query) = splice @with, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@with, 0, 2)) 6 4 10
300 6 push @exp, [
301 $self->expand_expr({ -alias => $alias }, -ident),
302 $self->expand_expr($query)
303 ];
304 }
305 4 ▶ return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 1 3 4
306 }
307  
308 4 ▶sub _render_with {
Subroutine _render_with: called CC=2 SCAR=6.9
309 4 my ($self, undef, $with) = @_;
310 my $q_part = $self->join_query_parts(', ',
311 map {
312 6 my ($alias, $query) = @$_;
313 6 $self->join_query_parts(' ',
314 $alias,
315 { -keyword => 'as' },
316 $query,
317 )
318 4 } @{$with->{queries}}
319 );
320 return $self->join_query_parts(' ',
321 4 ▶ { -keyword => join '_', 'with', ($with->{type}||'') },
Condition100%2/2
$with->{'type'} || ''
l!l
13
MC/DC100%1/1
$with->{'type'} || ''
$with->{'type'}
Truth table100%2/2
A
A= $with->{'type'}
Aresult
11
00
322 $q_part,
323 );
324 }
325  
326 4 ▶sub _expand_setop {
Subroutine _expand_setop: called CC=3 SCAR=11.4
327 4 my ($self, $setop, $args) = @_;
328 4 my $is_all = $setop =~ s/_all$//;
329 +{ "-${setop}" => {
330 ($is_all ? (type => 'all') : ()),
331 (ref($args) eq 'ARRAY'
332 ? (queries => [ map $self->expand_expr($_), @$args ])
333 : (
334 %$args,
335 4 ▶ queries => [ map $self->expand_expr($_), @{$args->{queries}} ]
Branch: 2 branches
BranchTrueFalseTotal
$is_all ? : 0 4 4
ref $args eq 'ARRAY' ? : 0 4 4
336 )
337 ),
338 } };
339 }
340  
341 3 ▶sub _render_setop {
Subroutine _render_setop: called CC=2 SCAR=7.0
342 3 my ($self, $setop, $args) = @_;
343 $self->join_query_parts(
344 { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' },
345 3 ▶ @{$args->{queries}}
Condition66%2/3
$args->{'type'} || ()
l!l&&r!l&&!r
120
MC/DC0%0/2
$args->{'type'} || ()
$args->{'type'} ()
Truth table66%2/3
A || B
A= $args->{'type'}B= ()
ABresult
1X1
011
000
346 );
347 }
348  
349 3 ▶sub _expand_clause_setop {
Subroutine _expand_clause_setop: called CC=3 SCAR=11.1
350 3 my ($self, $setop, $args) = @_;
351 3 my ($op, $type) = split '_', $setop;
352 3 ▶ +(setop => $self->expand_expr({
Branch: 2 branches
BranchTrueFalseTotal
$type ? : 1 2 3
ref $args eq 'ARRAY' ? : 0 3 3
353 "-${op}" => {
354 ($type ? (type => $type) : ()),
355 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
356 }
357 }));
358 }
359  
360 1 1;
361  
362 __END__
« DBIx/Class/Storage/Debug/PrettyPrint.pm ↑ Summary SQL/Abstract/Parts.pm »