SQL/Abstract/Tree.pm

filter: Statementstmt 96.1% 328 / 341 Branchbran 92.9% 171 / 184 Conditioncond 89.3% 202 / 226 MC/DCmcdc 75.2% 79 / 105 Subroutinesub 85.2% 29 / 34 Podpod 87.5% 7 / 8 totaltotal 90.8% 816 / 898 CC 175 SCAR 35.0
CC
175
Cov
93%
_recurse_parse
57.3
_parenthesis_unroll
45.4
_unparse
35.0
CRAP 33.0
« DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm ↑ Summary SQL/Abstract/Test.pm »
1 package SQL::Abstract::Tree;
2  
3 27 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶no warnings 'qw';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Sub::Quote 'quote_sub';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 27 my $op_look_ahead = '(?: (?= [\s\)\(\;] ) | \z)';
10 27 my $op_look_behind = '(?: (?<= [\,\s\)\(] ) | \A )';
11  
12 27 my $quote_left = qr/[\`\'\"\[]/;
13 27 my $quote_right = qr/[\`\'\"\]]/;
14  
15 27 my $placeholder_re = qr/(?: \? | \$\d+ )/x;
16  
17 # These SQL keywords always signal end of the current expression (except inside
18 # of a parenthesized subexpression).
19 # Format: A list of strings that will be compiled to extended syntax ie.
20 # /.../x) regexes, without capturing parentheses. They will be automatically
21 # anchored to op boundaries (excluding quotes) to match the whole token.
22 27 my @expression_start_keywords = (
23 'SELECT',
24 'UPDATE',
25 'SET',
26 'INSERT \s+ INTO',
27 'DELETE \s+ FROM',
28 'FROM',
29 '(?:
30 (?:
31 (?: (?: LEFT | RIGHT | FULL ) \s+ )?
32 (?: (?: CROSS | INNER | OUTER ) \s+ )?
33 )?
34 JOIN
35 )',
36 'ON',
37 'WHERE',
38 '(?: DEFAULT \s+ )? VALUES',
39 'GROUP \s+ BY',
40 'HAVING',
41 'ORDER \s+ BY',
42 'SKIP',
43 'FETCH',
44 'FIRST',
45 'LIMIT',
46 'OFFSET',
47 'FOR',
48 'UNION',
49 'INTERSECT',
50 'EXCEPT',
51 'MINUS',
52 'BEGIN \s+ WORK',
53 'COMMIT',
54 'ROLLBACK \s+ TO \s+ SAVEPOINT',
55 'ROLLBACK',
56 'SAVEPOINT',
57 'RELEASE \s+ SAVEPOINT',
58 'RETURNING',
59 );
60  
61 27 my $expr_start_re = join ("\n\t|\n", @expression_start_keywords );
62 27 $expr_start_re = qr/ $op_look_behind (?i: $expr_start_re ) $op_look_ahead /x;
63  
64 # These are binary operator keywords always a single LHS and RHS
65 # * AND/OR are handled separately as they are N-ary
66 # * so is NOT as being unary
67 # * BETWEEN without parentheses around the ANDed arguments (which
68 # makes it a non-binary op) is detected and accommodated in
69 # _recurse_parse()
70 # * AS is not really an operator but is handled here as it's also LHS/RHS
71  
72 # this will be included in the $binary_op_re, the distinction is interesting during
73 # testing as one is tighter than the other, plus alphanum cmp ops have different
74 # look ahead/behind (e.g. "x"="y" )
75 27 my @alphanum_cmp_op_keywords = (qw/< > != <> = <= >= /);
76 my $alphanum_cmp_op_re = join ("\n\t|\n", map
77 27 { "(?: (?<= [\\w\\s] | $quote_right ) | \\A )" . quotemeta ($_) . "(?: (?= [\\w\\s] | $quote_left ) | \\z )" }
78 @alphanum_cmp_op_keywords
79 );
80 27 $alphanum_cmp_op_re = qr/$alphanum_cmp_op_re/x;
81  
82 27 my $binary_op_re = '(?: NOT \s+)? (?:' . join ('|', qw/IN BETWEEN [RI]?LIKE REGEXP/) . ')';
83 27 $binary_op_re = join "\n\t|\n",
84 "$op_look_behind (?i: $binary_op_re | AS ) $op_look_ahead",
85 $alphanum_cmp_op_re,
86 $op_look_behind . 'IS (?:\s+ NOT)?' . "(?= \\s+ NULL \\b | $op_look_ahead )",
87 ;
88 27 $binary_op_re = qr/$binary_op_re/x;
89  
90 27 my $rno_re = qr/ROW_NUMBER \s* \( \s* \) \s+ OVER/ix;
91  
92 27 my $unary_op_re = 'NOT \s+ EXISTS | NOT | ' . $rno_re;
93 27 $unary_op_re = join "\n\t|\n",
94 "$op_look_behind (?i: $unary_op_re ) $op_look_ahead",
95 ;
96 27 $unary_op_re = qr/$unary_op_re/x;
97  
98 27 my $asc_desc_re = qr/$op_look_behind (?i: ASC | DESC ) $op_look_ahead /x;
99 27 my $and_or_re = qr/$op_look_behind (?i: AND | OR ) $op_look_ahead /x;
100  
101 my $tokenizer_re = join("\n\t|\n",
102 $expr_start_re,
103 $binary_op_re,
104 $unary_op_re,
105 $asc_desc_re,
106 $and_or_re,
107 $op_look_behind . ' \* ' . $op_look_ahead,
108 27 (map { quotemeta $_ } qw/, ( )/),
109 # a backslash-escaped question mark is not a placeholder but (part of)
110 # an operator, e.g. PostgreSQL's ?| or ?-| written as \?| for DBD::Pg
111 ' \\\\ \? [\-\+\*\/<>=~!\@\#%\^&\|\?]* ',
112 $placeholder_re,
113 );
114  
115 # this one *is* capturing for the split below
116 # splits on whitespace if all else fails
117 # has to happen before the composing qr's are anchored (below)
118 27 $tokenizer_re = qr/ \s* ( $tokenizer_re ) \s* | \s+ /x;
119  
120 # Parser states for _recurse_parse()
121 27 ▶use constant PARSE_TOP_LEVEL => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 27 ▶use constant PARSE_IN_EXPR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 27 ▶use constant PARSE_IN_PARENS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 27 ▶use constant PARSE_IN_FUNC => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 27 ▶use constant PARSE_RHS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 27 ▶use constant PARSE_LIST_ELT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 27 my $expr_term_re = qr/$expr_start_re | \)/x;
129 27 my $rhs_term_re = qr/ $expr_term_re | $binary_op_re | $unary_op_re | $asc_desc_re | $and_or_re | \, /x;
130 27 my $all_std_keywords_re = qr/ $rhs_term_re | \( | $placeholder_re /x;
131  
132 # anchor everything - even though keywords are separated by the tokenizer, leakage may occur
133 27 for (
134 $quote_left,
135 $quote_right,
136 $placeholder_re,
137 $expr_start_re,
138 $alphanum_cmp_op_re,
139 $binary_op_re,
140 $unary_op_re,
141 $asc_desc_re,
142 $and_or_re,
143 $expr_term_re,
144 $rhs_term_re,
145 $all_std_keywords_re,
146 ) {
147 324 $_ = qr/ \A $_ \z /x;
148 }
149  
150 # what can be bunched together under one MISC in an AST
151 27 my $compressable_node_re = qr/^ \- (?: MISC | LITERAL | PLACEHOLDER ) $/x;
152  
153 27 my %indents = (
154 select => 0,
155 update => 0,
156 'insert into' => 0,
157 'delete from' => 0,
158 from => 1,
159 where => 0,
160 join => 1,
161 'left join' => 1,
162 on => 2,
163 having => 0,
164 'group by' => 0,
165 'order by' => 0,
166 set => 1,
167 into => 1,
168 values => 1,
169 limit => 1,
170 offset => 1,
171 skip => 1,
172 first => 1,
173 );
174  
175  
176 27 has [qw(
177 newline indent_string indent_amount fill_in_placeholders placeholder_surround
178 html_escape
179 )] => (is => 'ro');
180  
181 27 has [qw( indentmap colormap )] => ( is => 'ro', default => quote_sub('{}') );
182  
183 # class global is in fact desired
184 27 my $merger;
185  
186 52 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=10 SCAR=24.2
Pod undocumented
187 52 my $class = shift;
188 52 ▶ my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 30 22 52
189  
190 52 ▶ if (my $p = delete $args->{profile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = delete $args->{"profile"}) 21 31 52
191 21 my %extra_args;
192 21 ▶ if ($p eq 'console') {
Branch: 4 branches
BranchTrueFalseTotal
if ($p eq 'console') { } 3 18 21
elsif ($p eq 'console_monochrome') { } 15 3 18
elsif ($p eq 'html') { } 3 0 3
elsif ($p eq 'none') { } 0 0 0
193 %extra_args = (
194 fill_in_placeholders => 1,
195 placeholder_surround => ['?/', ''],
196 indent_string => ' ',
197 indent_amount => 2,
198 newline => "\n",
199 colormap => {},
200 indentmap => \%indents,
201  
202 3 ▶ ! ( eval { require Term::ANSIColor } ) ? () : do {
Branch: 1 branch
BranchTrueFalseTotal
!eval { do { require Term::ANSIColor } } ? : 0 3 3
203 3 my $c = \&Term::ANSIColor::color;
204  
205 3 my $red = [$c->('red') , $c->('reset')];
206 3 my $cyan = [$c->('cyan') , $c->('reset')];
207 3 my $green = [$c->('green') , $c->('reset')];
208 3 my $yellow = [$c->('yellow') , $c->('reset')];
209 3 my $blue = [$c->('blue') , $c->('reset')];
210 3 my $magenta = [$c->('magenta'), $c->('reset')];
211 3 my $b_o_w = [$c->('black on_white'), $c->('reset')];
212 (
213 3 placeholder_surround => [$c->('black on_magenta'), $c->('reset')],
214 colormap => {
215 'begin work' => $b_o_w,
216 commit => $b_o_w,
217 rollback => $b_o_w,
218 savepoint => $b_o_w,
219 'rollback to savepoint' => $b_o_w,
220 'release savepoint' => $b_o_w,
221  
222 select => $red,
223 'insert into' => $red,
224 update => $red,
225 'delete from' => $red,
226  
227 set => $cyan,
228 from => $cyan,
229  
230 where => $green,
231 values => $yellow,
232  
233 join => $magenta,
234 'left join' => $magenta,
235 on => $blue,
236  
237 'group by' => $yellow,
238 having => $yellow,
239 'order by' => $yellow,
240  
241 skip => $green,
242 first => $green,
243 limit => $green,
244 offset => $green,
245 }
246 );
247 },
248 );
249 }
250 elsif ($p eq 'console_monochrome') {
251 15 %extra_args = (
252 fill_in_placeholders => 1,
253 placeholder_surround => ['?/', ''],
254 indent_string => ' ',
255 indent_amount => 2,
256 newline => "\n",
257 indentmap => \%indents,
258 );
259 }
260 elsif ($p eq 'html') {
261 %extra_args = (
262 html_escape => 1,
263 fill_in_placeholders => 1,
264 placeholder_surround => ['<span class="placeholder">', '</span>'],
265 indent_string => '&nbsp;',
266 indent_amount => 2,
267 newline => "<br />\n",
268 colormap => { map {
269 3 (my $class = $_) =~ s/\s+/-/g;
270 75 ( $_ => [ qq|<span class="$class">|, '</span>' ] )
271 } (
272 keys %indents,
273 qw(commit rollback savepoint),
274 'begin work', 'rollback to savepoint', 'release savepoint',
275 ) },
276 indentmap => \%indents,
277 );
278 }
279 elsif ($p eq 'none') {
280 # nada
281 }
282 else {
283 0 croak "No such profile '$p'";
284 }
285  
286 # see if we got any duplicates and merge if needed
287 21 ▶ if (scalar grep { exists $args->{$_} } keys %extra_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({exists $args->{$_};} keys %extra_args)) { } 17 4 21
288 # heavy-duty merge
289 17 ▶ $args = ($merger ||= do {
Condition66%2/3
$merger ||= do { require Hash::Merge; my $m = "Hash::Merge"->new; $m->specify_behavior({"SCALAR", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [$_[0], @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "ARRAY", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { $_[1]; } , "HASH", sub { $_[1]; } }, "HASH", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [values %{$_[0];}, @{$_[1];}]; } , "HASH", sub { Hash::Merge::_merge_hashes($_[0], $_[1]); } }}, "SQLA::Tree Behavior"); $m }
l!l&&r!l&&!r
1340
MC/DC0%0/2
$merger ||= do { require Hash::Merge; my $m = "Hash::Merge"->new; $m->specify_behavior({"SCALAR", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [$_[0], @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "ARRAY", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { $_[1]; } , "HASH", sub { $_[1]; } }, "HASH", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [values %{$_[0];}, @{$_[1];}]; } , "HASH", sub { Hash::Merge::_merge_hashes($_[0], $_[1]); } }}, "SQLA::Tree Behavior"); $m }
$merger do { require Hash::Merge; my $m = "Hash::Merge"->new; $m->specify_behavior({"SCALAR", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [$_[0], @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "ARRAY", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { $_[1]; } , "HASH", sub { $_[1]; } }, "HASH", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [values %{$_[0];}, @{$_[1];}]; } , "HASH", sub { Hash::Merge::_merge_hashes($_[0], $_[1]); } }}, "SQLA::Tree Behavior"); $m }
Truth table66%2/3
A ||= B
A= $mergerB= do { require Hash::Merge; my $m = "Hash::Merge"->new; $m->specify_behavior({"SCALAR", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [$_[0], @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "ARRAY", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { $_[1]; } , "HASH", sub { $_[1]; } }, "HASH", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [values %{$_[0];}, @{$_[1];}]; } , "HASH", sub { Hash::Merge::_merge_hashes($_[0], $_[1]); } }}, "SQLA::Tree Behavior"); $m }
ABresult
1X1
011
000
290 4 require Hash::Merge;
291 4 my $m = Hash::Merge->new;
292  
293 $m->specify_behavior({
294 SCALAR => {
295 15 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
296 0 ▶ ARRAY => sub { [ $_[0], @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
297 0 ▶ HASH => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 },
299 ARRAY => {
300 1 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
301 1 ▶ ARRAY => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
302 0 ▶ HASH => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
303 },
304 HASH => {
305 0 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
306 0 ▶ ARRAY => sub { [ values %{$_[0]}, @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
307 31 ▶ HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
308 },
309 4 }, 'SQLA::Tree Behavior' );
310  
311 4 $m;
312 })->merge(\%extra_args, $args );
313  
314 }
315 else {
316 4 $args = { %extra_args, %$args };
317 }
318 }
319  
320 52 $args;
321 }
322  
323 4842 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.5
Pod documented
324 4842 my ($self, $s) = @_;
325  
326 4842 ▶ return [] unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 4842 4842
327  
328 # Swap the contents of quoted strings and identifiers for a NUL-delimited
329 # index before tokenizing, so the separators inside them (whitespace,
330 # commas, parenthesis, keywords) can not split them up. The quotes stay in
331 # place, so whatever is glued to them (N'..', '..'::date) still tokenizes
332 # as before. Doubled quotes are the SQL escape; backslashes are literal.
333 4842 my @quoted;
334 4842 $s =~ s{ ( ' (?: [^'] | '' )* ' | " (?: [^"] | "" )* " | ` (?: [^`] | `` )* ` ) }{
335 3903 push @quoted, $1;
336 3903 my $q = substr($1, 0, 1);
337 3903 "$q\x00$#quoted\x00$q";
338 }gex;
339  
340 # tokenize string, and remove all optional whitespace
341 4842 my $tokens = [];
342 4842 foreach my $token (split $tokenizer_re, $s) {
343 111615 ▶ next unless defined $token and length $token and $token =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token and $token =~ /\S/ 26276 85339 111615
Condition83%5/6
defined $token and length $token and $token =~ /\S/
!ll&&!rl&&r
26276085339
defined $token and length $token
!ll&&!rl&&r
4652581185339
MC/DC66%2/3
defined $token and length $token and $token =~ /\S/
defined $token length $token $token =~ /\S/
Truth table75%3/4
A and B and C
A= defined $tokenB= length $tokenC= $token =~ /\S/
ABCresult
0XX0
10X0
1100
1111
344 85339 ▶ $token =~ s{ (['"`]) \x00 (\d+) \x00 \1 }{$quoted[$2]}gx if @quoted;
Branch: 1 branch
BranchTrueFalseTotal
if @quoted 15191 70148 85339
345 85339 push @$tokens, $token;
346 }
347  
348 4842 return [ $self->_recurse_parse($tokens, PARSE_TOP_LEVEL) ];
349 }
350  
351 47742 ▶sub _recurse_parse {
Subroutine _recurse_parse: called CC=57 SCAR=40.5
352 47742 my ($self, $tokens, $state) = @_;
353  
354 47742 my @left;
355 47742 while (1) { # left-associative parsing
356  
357 114719 ▶ if (! @$tokens
Branch: 1 branch
BranchTrueFalseTotal
if (not @$tokens or $state == 2 and $tokens->[0] eq ")" or $state == 1 and $tokens->[0] =~ /$expr_term_re/x or $state == 4 and $tokens->[0] =~ /$rhs_term_re/x or $state == 5 and $tokens->[0] eq "," || $tokens->[0] =~ /$expr_term_re/x) 37461 77258 114719
Condition100%27/27
not @$tokens or $state == 2 and $tokens->[0] eq ")" or $state == 1 and $tokens->[0] =~ /$expr_term_re/x or $state == 4 and $tokens->[0] =~ /$rhs_term_re/x or $state == 5 and $tokens->[0] eq "," || $tokens->[0] =~ /$expr_term_re/x
l!l&&r!l&&!r
33283417877258
not @$tokens or $state == 2 and $tokens->[0] eq ")" or $state == 1 and $tokens->[0] =~ /$expr_term_re/x or $state == 4 and $tokens->[0] =~ /$rhs_term_re/x
l!l&&r!l&&!r
332305381436
not @$tokens or $state == 2 and $tokens->[0] eq ")" or $state == 1 and $tokens->[0] =~ /$expr_term_re/x
l!l&&r!l&&!r
183041492681489
not @$tokens or $state == 2 and $tokens->[0] eq ")"
l!l&&r!l&&!r
10916738896415
$state == 2 and $tokens->[0] eq ")"
!ll&&!rl&&r
82191142247388
$state == 1 and $tokens->[0] =~ /$expr_term_re/x
!ll&&!rl&&r
484933299614926
$state == 4 and $tokens->[0] =~ /$rhs_term_re/x
!ll&&!rl&&r
711551028153
$state == 5 and $tokens->[0] eq "," || $tokens->[0] =~ /$expr_term_re/x
!ll&&!rl&&r
7281444444178
$tokens->[0] eq "," or $tokens->[0] =~ /$expr_term_re/x
l!l&&r!l&&!r
33438354444
MC/DC100%10/10
not @$tokens or $state == 2 and $tokens->[0] eq ")" or $state == 1 and $tokens->[0] =~ /$expr_term_re/x or $state == 4 and $tokens->[0] =~ /$rhs_term_re/x or $state == 5 and $tokens->[0] eq "," || $tokens->[0] =~ /$expr_term_re/x
not @$tokens $state == 2 $tokens->[0] eq ")" $state == 1 $tokens->[0] =~ /$expr_term_re/x $state == 4 $tokens->[0] =~ /$rhs_term_re/x $state == 5 $tokens->[0] eq "," $tokens->[0] =~ /$expr_term_re/x
Truth table27%11/40
not(A) or B and C or D and E or F and G or H and I or J
A= not @$tokensB= $state == 2C= $tokens->[0] eq ")"D= $state == 1E= $tokens->[0] =~ /$expr_term_re/xF= $state == 4G= $tokens->[0] =~ /$rhs_term_re/xH= $state == 5I= $tokens->[0] eq ","J= $tokens->[0] =~ /$expr_term_re/x
ABCDEFGHIJresult
1XXXXXXXXX1
011XXXXXXX1
00X11XXXXX1
01011XXXXX1
00X0X11XXX1
00X1011XXX1
0100X11XXX1
0101011XXX1
00X0X0X11X1
00X0X0X1011
00X0X1011X1
00X0X101011
00X100X11X1
00X100X1011
00X101011X1
00X10101011
0100X0X11X1
0100X0X1011
0100X1011X1
0100X101011
010100X11X1
010100X1011
010101011X1
01010101011
00X0X0X0XX0
00X0X0X1000
00X0X100XX0
00X0X101000
00X100X0XX0
00X100X1000
00X10100XX0
00X10101000
0100X0X0XX0
0100X0X1000
0100X100XX0
0100X101000
010100X0XX0
010100X1000
01010100XX0
01010101000
358 or
359 ($state == PARSE_IN_PARENS && $tokens->[0] eq ')')
360 or
361 ($state == PARSE_IN_EXPR && $tokens->[0] =~ $expr_term_re )
362 or
363 ($state == PARSE_RHS && $tokens->[0] =~ $rhs_term_re )
364 or
365 ($state == PARSE_LIST_ELT && ( $tokens->[0] eq ',' or $tokens->[0] =~ $expr_term_re ) )
366 ) {
367 37461 return @left;
368 }
369  
370 77258 my $token = shift @$tokens;
371  
372 # nested expression in ()
373 77258 ▶ if ($token eq '(' ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($token eq '(') { } 7388 69870 77258
elsif ($token =~ /$and_or_re/x) { } 3593 66277 69870
elsif ($token eq ',') { } 4317 61960 66277
elsif ($token =~ /$binary_op_re/x) { } 9624 52336 61960
elsif ($token =~ /$unary_op_re/x) { } 85 52251 52336
elsif ($token =~ /$expr_start_re/x) { } 17268 34983 52251
elsif ($token =~ /$placeholder_re/x) { } 2594 32389 34983
elsif (@$tokens and $tokens->[0] eq '(' || $tokens->[0] =~ /$placeholder_re/x) { } 529 31860 32389
374 7388 my @right = $self->_recurse_parse($tokens, PARSE_IN_PARENS);
375 7388 ▶ $token = shift @$tokens or croak "missing closing ')' around block " . $self->unparse(\@right);
Branch: 1 branch
BranchTrueFalseTotal
$token = shift @$tokens or croak(q[missing closing ')' around block ] . $self->unparse(\@right)) 7388 0 7388
376 7388 ▶ $token eq ')' or croak "unexpected token '$token' terminating block " . $self->unparse(\@right);
Branch: 1 branch
BranchTrueFalseTotal
$token eq ")" or croak("unexpected token '${token}' terminating block " . $self->unparse(\@right)) 7388 0 7388
377  
378 7388 push @left, [ '-PAREN' => \@right ];
379 }
380  
381 # AND/OR
382 elsif ($token =~ $and_or_re) {
383 3593 my $op = uc $token;
384  
385 3593 my @right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
386  
387 # several nodes on one side (e.g. "x <@" followed by a function call)
388 # are a single operand, not one operand each
389 3593 for my $side (\@left, \@right) {
390 @$side = [ -MISC => [
391 7186 ▶ map { $_->[0] eq '-MISC' ? @{$_->[1]} : $_ } @$side
Branch: 2 branches
BranchTrueFalseTotal
if @$side > 1 27 7159 7186
$_->[0] eq '-MISC' ? : 17 38 55
392 ] ] if @$side > 1;
393 }
394  
395 # Merge chunks if "logic" matches
396 @left = [ $op => [ @left, (@right and $op eq $right[0][0])
397 3593 ▶ ? @{ $right[0][1] }
Branch: 1 branch
BranchTrueFalseTotal
@right && $op eq $right[0][0] ? : 443 3150 3593
Condition100%3/3
@right and $op eq $right[0][0]
!ll&&!rl&&r
13149443
MC/DC100%2/2
@right and $op eq $right[0][0]
@right $op eq $right[0][0]
Truth table100%3/3
A and B
A= @rightB= $op eq $right[0][0]
ABresult
0X0
100
111
398 : @right
399 ] ];
400 }
401  
402 # LIST (,)
403 elsif ($token eq ',') {
404  
405 4317 my @right = $self->_recurse_parse($tokens, PARSE_LIST_ELT);
406  
407 # deal with malformed lists ( foo, bar, , baz )
408 4317 ▶ @right = [] unless @right;
Branch: 1 branch
BranchTrueFalseTotal
unless @right 29 4288 4317
409  
410 4317 ▶ @right = [ -MISC => [ @right ] ] if @right > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @right > 1 0 4317 4317
411  
412 4317 ▶ if (!@left) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @left) { } 17 4300 4317
elsif ($left[0][0] eq '-LIST') { } 3343 957 4300
413 17 @left = [ -LIST => [ [], @right ] ];
414 }
415 elsif ($left[0][0] eq '-LIST') {
416 3343 ▶ push @{$left[0][1]}, (@{$right[0]} and $right[0][0] eq '-LIST')
Branch: 1 branch
BranchTrueFalseTotal
@{$right[0];} && $right[0][0] eq '-LIST' ? : 0 3343 3343
Condition66%2/3
@{$right[0];} and $right[0][0] eq "-LIST"
!ll&&!rl&&r
2233210
MC/DC0%0/2
@{$right[0];} and $right[0][0] eq "-LIST"
@{$right[0];} $right[0][0] eq "-LIST"
Truth table66%2/3
A and B
A= @{$right[0];}B= $right[0][0] eq "-LIST"
ABresult
0X0
100
111
417 0 ? @{$right[0][1]}
418 : @right
419 ;
420 }
421 else {
422 # several nodes before the comma (e.g. "x - ( y )") are one list
423 # element, not one element each
424 @left = [ -MISC => [
425 0 ▶ map { $_->[0] eq '-MISC' ? @{$_->[1]} : $_ } @left
Branch: 2 branches
BranchTrueFalseTotal
if @left > 1 4 953 957
$_->[0] eq '-MISC' ? : 0 8 8
426 ] ] if @left > 1;
427  
428 957 @left = [ -LIST => [ @left, @right ] ];
429 }
430 }
431  
432 # binary operator keywords
433 elsif ($token =~ $binary_op_re) {
434 9624 my $op = uc $token;
435  
436 9624 my @right = $self->_recurse_parse($tokens, PARSE_RHS);
437  
438 # BETWEEN's two bounds are joined by an AND, but PARSE_RHS stops at
439 # that AND, so only the lower bound has been parsed so far. Pull the
440 # upper bound in and group both under the BETWEEN as AND[low, high],
441 # otherwise the upper bound leaks out as a sibling of the BETWEEN in
442 # the enclosing expression. (The previous guard here compared an
443 # arrayref node to the string '-LITERAL' and so never fired.)
444 9624 ▶ if (($op eq 'BETWEEN' or $op eq 'NOT BETWEEN')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "BETWEEN" || $op eq "NOT BETWEEN" and @$tokens and $tokens->[0] =~ /\AAND\z/i) 96 9528 9624
Condition88%8/9
$op eq "BETWEEN" || $op eq "NOT BETWEEN" and @$tokens and $tokens->[0] =~ /\AAND\z/i
!ll&&!rl&&r
95181096
$op eq "BETWEEN" || $op eq "NOT BETWEEN" and @$tokens
!ll&&!rl&&r
95180106
$op eq "BETWEEN" or $op eq "NOT BETWEEN"
l!l&&r!l&&!r
10249518
MC/DC75%3/4
$op eq "BETWEEN" || $op eq "NOT BETWEEN" and @$tokens and $tokens->[0] =~ /\AAND\z/i
$op eq "BETWEEN" $op eq "NOT BETWEEN" @$tokens $tokens->[0] =~ /\AAND\z/i
Truth table57%4/7
A or B and C and D
A= $op eq "BETWEEN"B= $op eq "NOT BETWEEN"C= @$tokensD= $tokens->[0] =~ /\AAND\z/i
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
445 and @$tokens and $tokens->[0] =~ /\AAND\z/i) {
446 96 my $and = uc shift @$tokens;
447 96 @right = ([ $and => [ @right, $self->_recurse_parse($tokens, PARSE_RHS) ] ]);
448 }
449  
450 9624 ▶ push @left, [$op => [ (@left ? pop @left : ''), @right ]];
Branch: 1 branch
BranchTrueFalseTotal
@left ? : 9618 6 9624
451 }
452  
453 # unary op keywords
454 elsif ($token =~ $unary_op_re) {
455 85 my $op = uc $token;
456  
457 # normalize RNO explicitly
458 85 ▶ $op = 'ROW_NUMBER() OVER' if $op =~ /^$rno_re$/;
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ /^$rno_re$/ 5 80 85
459  
460 85 my @right = $self->_recurse_parse($tokens, PARSE_RHS);
461  
462 85 push @left, [ $op => \@right ];
463 }
464  
465 # expression terminator keywords
466 elsif ($token =~ $expr_start_re) {
467 17268 my $op = uc $token;
468 17268 my @right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
469  
470 17268 push @left, [ $op => \@right ];
471 }
472  
473 # a '?'
474 elsif ($token =~ $placeholder_re) {
475 2594 push @left, [ -PLACEHOLDER => [ $token ] ];
476 }
477  
478 # check if the current token is an unknown op-start
479 ▶ elsif (@$tokens and ($tokens->[0] eq '(' or $tokens->[0] =~ $placeholder_re ) ) {
Condition100%6/6
@$tokens and $tokens->[0] eq "(" || $tokens->[0] =~ /$placeholder_re/x
!ll&&!rl&&r
274129119529
$tokens->[0] eq "(" or $tokens->[0] =~ /$placeholder_re/x
l!l&&r!l&&!r
5082129119
MC/DC100%3/3
@$tokens and $tokens->[0] eq "(" || $tokens->[0] =~ /$placeholder_re/x
@$tokens $tokens->[0] eq "(" $tokens->[0] =~ /$placeholder_re/x
Truth table100%4/4
A and B or C
A= @$tokensB= $tokens->[0] eq "("C= $tokens->[0] =~ /$placeholder_re/x
ABCresult
0XX0
1000
11X1
1011
480 529 push @left, [ $token => [ $self->_recurse_parse($tokens, PARSE_RHS) ] ];
481 }
482  
483 # we're now in "unknown token" land - start eating tokens until
484 # we see something familiar, OR in the case of RHS (binop) stop
485 # after the first token
486 # Also stop processing when we could end up with an unknown func
487 else {
488 31860 my @lits = [ -LITERAL => [$token] ];
489  
490 31860 ▶ unshift @lits, pop @left if @left == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @left == 1 183 31677 31860
491  
492 31860 ▶ unless ( $state == PARSE_RHS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state == 4) 24211 7649 31860
493 24211 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens and not $tokens->[0] =~ /$all_std_keywords_re/x and not @$tokens > 1 && $tokens->[1] eq "(") 389 24211 24600
Condition100%9/9
@$tokens and not $tokens->[0] =~ /$all_std_keywords_re/x and not @$tokens > 1 && $tokens->[1] eq "("
!ll&&!rl&&r
2417239389
@$tokens and not $tokens->[0] =~ /$all_std_keywords_re/x
!ll&&!rl&&r
138722785428
@$tokens > 1 && $tokens->[1] eq '('
!ll&&!rl&&r
2036939
MC/DC100%4/4
@$tokens and not $tokens->[0] =~ /$all_std_keywords_re/x and not @$tokens > 1 && $tokens->[1] eq "("
@$tokens not $tokens->[0] =~ /$all_std_keywords_re/x @$tokens > 1 $tokens->[1] eq '('
Truth table100%5/5
A and not(B) and not(C && D)
A= @$tokensB= not $tokens->[0] =~ /$all_std_keywords_re/xC= @$tokens > 1D= $tokens->[1] eq '('
ABCDresult
0XXX0
10XX0
11110
110X1
11101
494 @$tokens
495 and
496 $tokens->[0] !~ $all_std_keywords_re
497 and
498 ! (@$tokens > 1 and $tokens->[1] eq '(')
499 ) {
500 389 push @lits, [ -LITERAL => [ shift @$tokens ] ];
501 }
502 }
503  
504 31860 ▶ @lits = [ -MISC => [ @lits ] ] if @lits > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @lits > 1 493 31367 31860
505  
506 31860 push @left, @lits;
507 }
508  
509 # compress -LITERAL -MISC and -PLACEHOLDER pieces into a single
510 # -MISC container
511 77258 ▶ if (@left > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@left > 1) 11987 65271 77258
512 11987 my $i = 0;
513 11987 ▶ while ($#left > $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#left > $i) 23749 11987 35736
514 23749 ▶ if ($left[$i][0] =~ $compressable_node_re and $left[$i+1][0] =~ $compressable_node_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left[$i][0] =~ /$compressable_node_re/x and $left[$i + 1][0] =~ /$compressable_node_re/) { } 71 23678 23749
Condition100%3/3
$left[$i][0] =~ /$compressable_node_re/x and $left[$i + 1][0] =~ /$compressable_node_re/
!ll&&!rl&&r
2337130771
MC/DC100%2/2
$left[$i][0] =~ /$compressable_node_re/x and $left[$i + 1][0] =~ /$compressable_node_re/
$left[$i][0] =~ /$compressable_node_re/x $left[$i + 1][0] =~ /$compressable_node_re/
Truth table100%3/3
A and B
A= $left[$i][0] =~ /$compressable_node_re/xB= $left[$i + 1][0] =~ /$compressable_node_re/
ABresult
0X0
100
111
515 splice @left, $i, 2, [ -MISC => [
516 71 ▶ map { $_->[0] eq '-MISC' ? @{$_->[1]} : $_ } (@left[$i, $i+1])
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq '-MISC' ? : 71 71 142
517 ]];
518 }
519 else {
520 23678 $i++;
521 }
522 }
523 }
524  
525 77258 ▶ return @left if $state == PARSE_RHS;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 4 10281 66977 77258
526  
527 # deal with post-fix operators
528 66977 ▶ if (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 56170 10807 66977
529 # asc/desc
530 56170 ▶ if ($tokens->[0] =~ $asc_desc_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->[0] =~ /$asc_desc_re/x) 208 55962 56170
531 208 @left = [ ('-' . uc (shift @$tokens)) => [ @left ] ];
532 }
533 }
534 }
535 }
536  
537 953 ▶sub format_keyword {
Subroutine format_keyword: called CC=2 SCAR=6.9
Pod documented
538 953 my ($self, $keyword) = @_;
539  
540 953 my $around = $self->colormap->{lc $keyword};
541 953 $keyword = $self->_escape($keyword);
542 953 ▶ $keyword = "$around->[0]$keyword$around->[1]" if $around;
Branch: 1 branch
BranchTrueFalseTotal
if $around 21 932 953
543  
544 953 return $keyword
545 }
546  
547 # SQL text and bind values only - never the markup from the profile
548 10335 ▶sub _escape {
Subroutine _escape: called CC=2 SCAR=6.9
549 10335 my ($self, $text) = @_;
550 10335 ▶ return $text unless $self->html_escape;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->html_escape 10303 32 10335
551 32 $text =~ s/&/&amp;/g;
552 32 $text =~ s/</&lt;/g;
553 32 $text =~ s/>/&gt;/g;
554 32 $text =~ s/"/&quot;/g;
555 32 $text =~ s/'/&#39;/g;
556 32 return $text;
557 }
558  
559 27 my %starters = (
560 select => 1,
561 update => 1,
562 'insert into' => 1,
563 'delete from' => 1,
564 );
565  
566 954 ▶sub pad_keyword {
Subroutine pad_keyword: called CC=5 SCAR=16.1
Pod documented
567 954 my ($self, $keyword, $depth) = @_;
568  
569 954 my $before = '';
570 954 ▶ if (defined $self->indentmap->{lc $keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->indentmap->{lc $keyword}) 87 867 954
571 # guard newline like _unparse does - a caller may set an indentmap
572 # without a newline (outside the canned profiles)
573 87 ▶ $before = ($self->newline||'') . $self->indent($depth + $self->indentmap->{lc $keyword});
Condition100%2/2
$self->newline || ''
l!l
861
MC/DC100%1/1
$self->newline || ''
$self->newline
Truth table100%2/2
A
A= $self->newline
Aresult
11
00
574 }
575 954 ▶ $before = '' if $depth == 0 and defined $starters{lc $keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 and defined $starters{lc $keyword} 212 742 954
Condition100%3/3
$depth == 0 and defined $starters{lc $keyword}
!ll&&!rl&&r
115627212
MC/DC100%2/2
$depth == 0 and defined $starters{lc $keyword}
$depth == 0 defined $starters{lc $keyword}
Truth table100%3/3
A and B
A= $depth == 0B= defined $starters{lc $keyword}
ABresult
0X0
100
111
576 954 return [$before, ''];
577 }
578  
579 113 ▶sub indent { ($_[0]->indent_string||'') x ( ( $_[0]->indent_amount || 0 ) * $_[1] ) }
Subroutine indent: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->indent_string or "" 112 1 113
Condition100%2/2
$_[0]->indent_amount || 0
l!l
1121
MC/DC100%1/1
$_[0]->indent_amount || 0
$_[0]->indent_amount
Truth table100%2/2
A
A= $_[0]->indent_amount
Aresult
11
00
580  
581 # indentmap entries that put things on lines of their own but are not
582 # clause keywords
583 27 my %line_break_keys = map { $_ => 1 } ('and', 'or', ',');
584  
585 # the indent level of the clause keyword currently being unparsed
586 27 our $Clause_Indent = 0;
587  
588 # the indent for a line break entry ('and', 'or', ','), or undef when the
589 # entry is not set or there is no newline to break with
590 320 ▶sub _line_break_indent {
Subroutine _line_break_indent: called CC=3 SCAR=11.0
591 320 my ($self, $key) = @_;
592 320 ▶ return undef unless length( $self->newline || '' );
Branch: 1 branch
BranchTrueFalseTotal
unless length($self->newline || "") 221 99 320
Condition100%2/2
$self->newline || ''
l!l
99221
MC/DC100%1/1
$self->newline || ''
$self->newline
Truth table100%2/2
A
A= $self->newline
Aresult
11
00
593 99 return $self->indentmap->{$key};
594 }
595  
596 1523 ▶sub _is_key {
Subroutine _is_key: called CC=4 SCAR=13.9
597 1523 my ($self, $tree) = @_;
598 1523 ▶ $tree = $tree->[0] while ref $tree;
Branch: 1 branch
BranchTrueFalseTotal
ref $tree and do { $tree = $tree->[0] } 3038 1523 4561
599  
600 1523 ▶ defined $tree && !$line_break_keys{lc $tree} && defined $self->indentmap->{lc $tree};
Branch: 1 branch
BranchTrueFalseTotal
defined $tree and not $line_break_keys{lc $tree} and defined $self->indentmap->{lc $tree} 1474 49 1523
Condition100%3/3
defined $tree and not $line_break_keys{lc $tree}
!ll&&!rl&&r
8411474
MC/DC100%2/2
defined $tree and not $line_break_keys{lc $tree}
defined $tree not $line_break_keys{lc $tree}
Truth table100%3/3
A and not(B)
A= defined $treeB= not $line_break_keys{lc $tree}
ABresult
0X0
100
111
601 }
602  
603 119 ▶sub fill_in_placeholder {
Subroutine fill_in_placeholder: called CC=6 SCAR=18.0
Pod documented
604 119 my ($self, $bindargs) = @_;
605  
606 119 ▶ if ($self->fill_in_placeholders) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fill_in_placeholders) 16 103 119
607 16 my $val = shift @{$bindargs};
608 16 ▶ $val = '' unless defined $val; # preserve a real 0/'0' bind value (5.6-safe defined-or)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 16 16
609 16 my $quoted = $val =~ s/^(['"])(.*)\1$/$2/;
610 16 ▶ my ($left, $right) = map { defined $_ ? $_ : '' } @{$self->placeholder_surround || []}[0, 1];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 30 2 32
Condition50%1/2
$self->placeholder_surround || []
l!l
160
MC/DC0%0/1
$self->placeholder_surround || []
$self->placeholder_surround
Truth table50%1/2
A
A= $self->placeholder_surround
Aresult
11
00
611 16 $val =~ s/\\/\\\\/g;
612 16 $val =~ s/'/\\'/g;
613 16 ▶ $val = qq('$val') if $quoted;
Branch: 1 branch
BranchTrueFalseTotal
if $quoted 0 16 16
614 16 $val = $self->_escape($val);
615 16 return qq($left$val$right)
616 }
617 103 return '?'
618 }
619  
620 # FIXME - terrible name for a user facing API
621 2670 ▶sub unparse {
Subroutine unparse: called CC=2 SCAR=6.9
Pod documented
622 2670 my ($self, $tree, $bindargs) = @_;
623 2670 ▶ $self->_unparse($tree, [@{$bindargs||[]}], 0);
Condition100%2/2
$bindargs || []
l!l
72663
MC/DC100%1/1
$bindargs || []
$bindargs
Truth table100%2/2
A
A= $bindargs
Aresult
11
00
624 }
625  
626 15309 ▶sub _unparse {
Subroutine _unparse: called CC=34 SCAR=35.6
627 15309 my ($self, $tree, $bindargs, $depth) = @_;
628  
629 15309 ▶ if (not $tree or not @$tree) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree and @$tree) 17 15292 15309
Condition100%3/3
$tree and @$tree
!ll&&!rl&&r
21515292
MC/DC100%2/2
$tree and @$tree
$tree @$tree
Truth table100%3/3
A and B
A= $treeB= @$tree
ABresult
0X0
100
111
630 17 return '';
631 }
632  
633 # FIXME - needs a config switch to disable
634 15292 $self->_parenthesis_unroll($tree);
635  
636 15292 my ($op, $args) = @{$tree}[0,1];
637  
638 15292 ▶ if (! defined $op or (! ref $op and ! defined $args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $op and ref $op || defined $args) 0 15292 15292
Condition50%3/6
defined $op and ref $op || defined $args
!ll&&!rl&&r
0015292
ref $op or defined $args
l!l&&r!l&&!r
2815124770
MC/DC0%0/3
defined $op and ref $op || defined $args
defined $op ref $op defined $args
Truth table50%2/4
A and B or C
A= defined $opB= ref $opC= defined $args
ABCresult
0XX0
1000
11X1
1011
639 0 require Data::Dumper;
640 0 Carp::confess( sprintf ( "Internal error - malformed branch at depth $depth:\n%s",
641 Data::Dumper::Dumper($tree)
642 ) );
643 }
644  
645 15292 ▶ if (ref $op) {
Branch: 9 branches
BranchTrueFalseTotal
if (ref $op) { } 2815 12477 15292
elsif ($op eq '-LITERAL') { } 8567 3910 12477
elsif ($op eq '-PLACEHOLDER') { } 115 3795 3910
elsif ($op eq '-PAREN') { } 1523 2272 3795
elsif ($op eq 'AND' or $op eq 'OR') { } 241 2031 2272
elsif ($op =~ /$binary_op_re/) { } 799 1232 2031
elsif ($op eq '-LIST') { } 87 1145 1232
elsif ($op eq '-MISC') { } 181 964 1145
elsif ($op =~ qr/^-(ASC|DESC)$/) { } 18 946 964
646 2815 return join (' ', map $self->_unparse($_, $bindargs, $depth), @$tree);
647 }
648 elsif ($op eq '-LITERAL') { # literal has different sig
649 8567 return $self->_escape($args->[0]);
650 }
651 elsif ($op eq '-PLACEHOLDER') {
652 115 return $self->fill_in_placeholder($bindargs);
653 }
654 elsif ($op eq '-PAREN') {
655 return sprintf ('( %s )',
656 1523 ▶ join (' ', map { $self->_unparse($_, $bindargs, $depth + 2) } @{$args} )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_key($args) ? : 2 1521 1523
Condition50%1/2
$self->newline || ''
l!l
20
MC/DC0%0/1
$self->newline || ''
$self->newline
Truth table50%1/2
A
A= $self->newline
Aresult
11
00
657 .
658 ($self->_is_key($args)
659 ? ( $self->newline||'' ) . $self->indent($depth + 1)
660 : ''
661 )
662 );
663 }
664 ▶ elsif ($op eq 'AND' or $op eq 'OR') {
Condition100%3/3
$op eq "AND" or $op eq "OR"
l!l&&r!l&&!r
204372031
MC/DC100%2/2
$op eq "AND" or $op eq "OR"
$op eq "AND" $op eq "OR"
Truth table100%3/3
A or B
A= $op eq "AND"B= $op eq "OR"
ABresult
1X1
011
000
665 241 my @parts = map $self->_unparse($_, $bindargs, $depth), @{$args};
666  
667 # every further condition on a line of its own, indented below the
668 # clause it belongs to (or below the parenthesis it is in)
669 241 ▶ if (defined (my $indent = $self->_line_break_indent(lc $op))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $indent = $self->_line_break_indent(lc $op))) 15 226 241
670 15 ▶ my $base = $depth > $Clause_Indent ? $depth : $Clause_Indent;
Branch: 1 branch
BranchTrueFalseTotal
$depth > $SQL::Abstract::Tree::Clause_Indent ? : 2 13 15
671 15 return join ($self->newline . $self->indent($base + $indent) . "$op ", @parts);
672 }
673  
674 226 return join (" $op ", @parts);
675 }
676 elsif ($op =~ $binary_op_re) {
677 799 my $sep = ' ' . $self->_escape($op) . ' ';
678 799 return join ($sep, map $self->_unparse($_, $bindargs, $depth), @{$args});
679 }
680 elsif ($op eq '-LIST' ) {
681 87 return join (', ', map $self->_unparse($_, $bindargs, $depth), @{$args});
682 }
683 elsif ($op eq '-MISC' ) {
684 181 return join (' ', map $self->_unparse($_, $bindargs, $depth), @{$args});
685 }
686 elsif ($op =~ qr/^-(ASC|DESC)$/ ) {
687 18 my $dir = $1;
688 18 return join (' ', (map $self->_unparse($_, $bindargs, $depth), @{$args}), $dir);
689 }
690 else {
691 946 my ($l, $r) = @{$self->pad_keyword($op, $depth)};
692  
693 946 my $clause_indent = $self->indentmap->{lc $op};
694 946 ▶ local $Clause_Indent = defined $clause_indent
Branch: 1 branch
BranchTrueFalseTotal
defined $clause_indent ? : 79 867 946
695 ? $depth + $clause_indent
696 : $Clause_Indent;
697  
698 946 my $rhs;
699 my $item_indent;
700 946 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $clause_indent and defined($item_indent = $self->_line_break_indent(',')) and ref $args eq 'ARRAY' and @$args == 1 and ref $args->[0] eq 'ARRAY' and ($args->[0][0] || '') eq '-LIST') { } 9 937 946
Condition76%13/17
defined $clause_indent and defined($item_indent = $self->_line_break_indent(",")) and ref $args eq "ARRAY" and @$args == 1 and ref $args->[0] eq "ARRAY" and ($args->[0][0] || "") eq "-LIST"
!ll&&!rl&&r
909289
defined $clause_indent and defined($item_indent = $self->_line_break_indent(",")) and ref $args eq "ARRAY" and @$args == 1 and ref $args->[0] eq "ARRAY"
!ll&&!rl&&r
909037
defined $clause_indent and defined($item_indent = $self->_line_break_indent(",")) and ref $args eq "ARRAY" and @$args == 1
!ll&&!rl&&r
909037
defined $clause_indent and defined($item_indent = $self->_line_break_indent(",")) and ref $args eq "ARRAY"
!ll&&!rl&&r
909037
defined $clause_indent and defined($item_indent = $self->_line_break_indent(","))
!ll&&!rl&&r
8674237
$args->[0][0] || ''
l!l
370
MC/DC33%2/6
defined $clause_indent and defined($item_indent = $self->_line_break_indent(",")) and ref $args eq "ARRAY" and @$args == 1 and ref $args->[0] eq "ARRAY" and ($args->[0][0] || "") eq "-LIST"
defined $clause_indent defined($item_indent = $self->_line_break_indent(",")) ref $args eq "ARRAY" @$args == 1 ref $args->[0] eq "ARRAY" $args->[0][0]
Truth table42%3/7
A and B and C and D and E and F
A= defined $clause_indentB= defined($item_indent = $self->_line_break_indent(","))C= ref $args eq "ARRAY"D= @$args == 1E= ref $args->[0] eq "ARRAY"F= $args->[0][0]
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
701 defined $clause_indent
702 and
703 defined ($item_indent = $self->_line_break_indent(','))
704 and
705 ref $args eq 'ARRAY' and @$args == 1
706 and
707 ref $args->[0] eq 'ARRAY' and ($args->[0][0]||'') eq '-LIST'
708 ) {
709 # the comma list right below a clause (select list, order by, set
710 # ...): every item on a line of its own. Lists further down (function
711 # arguments, IN lists) stay as they are.
712 9 $self->_parenthesis_unroll($args->[0]);
713 9 my $break = $self->newline . $self->indent($Clause_Indent + $item_indent);
714 9 $rhs = join ',', map { $break . $self->_unparse($_, $bindargs, $depth) } @{$args->[0][1]};
715 }
716 else {
717 937 $rhs = $self->_unparse($args, $bindargs, $depth);
718 }
719  
720 return sprintf "$l%s$r", join(
721 (
722 946 ▶ ( ref $args eq 'ARRAY' and @{$args} == 1 and $args->[0][0] eq '-PAREN' ) # mysql--
Branch: 2 branches
BranchTrueFalseTotal
ref $args eq 'ARRAY' && @{$args;} == 1 && $args->[0][0] eq '-PAREN' || defined $item_indent && substr($rhs, 0, length $self->newline) eq $self->newline ? : 102 844 946
length $rhs ? : 940 6 946
Condition91%11/12
ref $args eq "ARRAY" and @{$args;} == 1 and $args->[0][0] eq "-PAREN" or defined $item_indent and substr($rhs, 0, length $self->newline) eq $self->newline
l!l&&r!l&&!r
939844
ref $args eq "ARRAY" and @{$args;} == 1 and $args->[0][0] eq "-PAREN"
!ll&&!rl&&r
1384093
ref $args eq "ARRAY" and @{$args;} == 1
!ll&&!rl&&r
013933
defined $item_indent and substr($rhs, 0, length $self->newline) eq $self->newline
!ll&&!rl&&r
816289
MC/DC80%4/5
ref $args eq "ARRAY" and @{$args;} == 1 and $args->[0][0] eq "-PAREN" or defined $item_indent and substr($rhs, 0, length $self->newline) eq $self->newline
ref $args eq "ARRAY" @{$args;} == 1 $args->[0][0] eq "-PAREN" defined $item_indent substr($rhs, 0, length $self->newline) eq $self->newline
Truth table50%5/10
A and B and C or D and E
A= ref $args eq "ARRAY"B= @{$args;} == 1C= $args->[0][0] eq "-PAREN"D= defined $item_indentE= substr($rhs, 0, length $self->newline) eq $self->newline
ABCDEresult
111XX1
0XX111
10X111
110111
0XX0X0
0XX100
10X0X0
10X100
1100X0
110100
723 or
724 defined $item_indent and substr($rhs, 0, length $self->newline) eq $self->newline
725 )
726 ? ''
727 : ' '
728 ,
729 $self->format_keyword($op),
730 (length $rhs ? $rhs : () ),
731 );
732 }
733 }
734  
735 # All of these keywords allow their parameters to be specified with or without parenthesis without changing the semantics
736 27 my @unrollable_ops = (
737 'ON',
738 'WHERE',
739 'GROUP \s+ BY',
740 'HAVING',
741 'ORDER \s+ BY',
742 'I?LIKE',
743 );
744 27 my $unrollable_ops_re = join ' | ', @unrollable_ops;
745 # anchored, so that ON does not match inside function names like
746 # GROUP_CONCAT or CONCAT_WS and strip their parenthesis (RT#105563, RT#117234)
747 27 $unrollable_ops_re = qr/ \A (?: $unrollable_ops_re ) \z /xi;
748  
749 66290 ▶sub _parenthesis_unroll {
Subroutine _parenthesis_unroll: called CC=45 SCAR=38.1
750 66290 my $self = shift;
751 66290 my $ast = shift;
752  
753 66290 ▶ return unless (ref $ast and ref $ast->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ast and ref $ast->[1] 3493 62797 66290
Condition66%2/3
ref $ast and ref $ast->[1]
!ll&&!rl&&r
0349362797
MC/DC50%1/2
ref $ast and ref $ast->[1]
ref $ast ref $ast->[1]
Truth table66%2/3
A and B
A= ref $astB= ref $ast->[1]
ABresult
0X0
100
111
754  
755 62797 my $changes;
756 62797 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$changes and do { do { my @children; $changes = 0; foreach my $child (@{$$ast[1];}) { if (not ref $child && @$child or $child->[0] ne "-PAREN") { push @children, $child; next; } ; my $parent_op = $ast->[0]; while ($parent_op ne "IN" and @{$child->[1];} == 1 and $child->[1][0][0] eq "-PAREN") { $child = $child->[1][0]; ++$changes; } ; my $single_child_op = @{$child->[1];} == 1 ? $child->[1][0][0] : ""; my $child_op_argc = $single_child_op ? scalar @{$child->[1][0][1];} : undef; my $single_grandchild_op = ($child_op_argc || 0) == 1 && ref $child->[1][0][1][0] eq "ARRAY" ? $child->[1][0][1][0][0] : ""; if ($parent_op =~ /$unrollable_ops_re/xi and $single_child_op ne "SELECT") { push @children, @{$child->[1];}; ++$changes; } elsif ($single_child_op eq $parent_op and $parent_op eq "AND" || $parent_op eq "OR") { push @children, @{$child->[1][0][1];}; ++$changes; } elsif ($single_child_op eq "-LITERAL" || $single_child_op eq "-PLACEHOLDER" and $parent_op eq "AND" || $parent_op eq "OR" || $parent_op eq "NOT") { push @children, @{$child->[1];}; ++$changes; } elsif ($parent_op eq "AND" || $parent_op eq "OR" and $single_child_op =~ /$binary_op_re/x and $single_child_op ne "BETWEEN" and $child_op_argc == 2 and not $single_child_op =~ /$alphanum_cmp_op_re/x && $parent_op =~ /$alphanum_cmp_op_re/x) { push @children, @{$child->[1];}; ++$changes; } elsif ($single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x and not $single_child_op =~ /$alphanum_cmp_op_re/ and $child_op_argc == 1 and $single_grandchild_op eq "-PAREN" || $single_grandchild_op eq "-LITERAL" || $single_grandchild_op eq "-PLACEHOLDER") { push @children, @{$child->[1];}; ++$changes; } elsif ($parent_op ne "ROW_NUMBER() OVER" and $single_child_op and $single_child_op ne "NOT" and $child_op_argc == 1 and $single_grandchild_op eq "-PAREN") { push @children, @{$child->[1];}; ++$changes; } else { push @children, $child; } ; } ; $ast->[1] = \@children } } 2752 62797 65549
757 65549 my @children;
758 65549 $changes = 0;
759  
760 65549 for my $child (@{$ast->[1]}) {
761  
762 # the current node in this loop is *always* a PAREN
763 85489 ▶ if (! ref $child or ! @$child or $child->[0] ne '-PAREN') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $child && @$child or $child->[0] ne "-PAREN") 78875 6614 85489
Condition100%6/6
not ref $child && @$child or $child->[0] ne "-PAREN"
l!l&&r!l&&!r
32443464326614
ref $child && @$child
!ll&&!rl&&r
323984553046
MC/DC100%3/3
not ref $child && @$child or $child->[0] ne "-PAREN"
ref $child @$child $child->[0] ne "-PAREN"
Truth table100%4/4
not(A && B) or C
A= ref $childB= @$childC= $child->[0] ne "-PAREN"
ABCresult
0XX1
10X1
1111
1100
764 78875 push @children, $child;
765 78875 next;
766 }
767  
768 6614 my $parent_op = $ast->[0];
769  
770 # unroll nested parenthesis
771 6614 ▶ while ( $parent_op ne 'IN' and @{$child->[1]} == 1 and $child->[1][0][0] eq '-PAREN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_op ne "IN" and @{$child->[1];} == 1 and $child->[1][0][0] eq "-PAREN") 414 6614 7028
Condition100%6/6
$parent_op ne "IN" and @{$child->[1];} == 1 and $child->[1][0][0] eq "-PAREN"
!ll&&!rl&&r
8385776414
$parent_op ne "IN" and @{$child->[1];} == 1
!ll&&!rl&&r
1486906190
MC/DC100%3/3
$parent_op ne "IN" and @{$child->[1];} == 1 and $child->[1][0][0] eq "-PAREN"
$parent_op ne "IN" @{$child->[1];} == 1 $child->[1][0][0] eq "-PAREN"
Truth table100%4/4
A and B and C
A= $parent_op ne "IN"B= @{$child->[1];} == 1C= $child->[1][0][0] eq "-PAREN"
ABCresult
0XX0
10X0
1100
1111
772 414 $child = $child->[1][0];
773 414 $changes++;
774 }
775  
776 # set to CHILD in the case of PARENT ( CHILD )
777 # but NOT in the case of PARENT( CHILD1, CHILD2 )
778 6614 ▶ my $single_child_op = (@{$child->[1]} == 1) ? $child->[1][0][0] : '';
Branch: 1 branch
BranchTrueFalseTotal
@{$child->[1];} == 1 ? : 5902 712 6614
779  
780 6614 ▶ my $child_op_argc = $single_child_op ? scalar @{$child->[1][0][1]} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$single_child_op ? : 5902 712 6614
781  
782 6614 ▶ my $single_grandchild_op
Branch: 1 branch
BranchTrueFalseTotal
($child_op_argc || 0) == 1 && ref $child->[1][0][1][0] eq 'ARRAY' ? : 106 6508 6614
Condition100%5/5
($child_op_argc || 0) == 1 and ref $child->[1][0][1][0] eq "ARRAY"
!ll&&!rl&&r
48291679106
$child_op_argc || 0
l!l
5902712
MC/DC100%2/2
($child_op_argc || 0) == 1 and ref $child->[1][0][1][0] eq "ARRAY"
$child_op_argc ref $child->[1][0][1][0] eq "ARRAY"
Truth table100%3/3
A and B
A= $child_op_argcB= ref $child->[1][0][1][0] eq "ARRAY"
ABresult
0X0
100
111
783 = ( ($child_op_argc||0) == 1 and ref $child->[1][0][1][0] eq 'ARRAY' )
784 ? $child->[1][0][1][0][0]
785 : ''
786 ;
787  
788 # if the parent operator explicitly allows it AND the child isn't a subselect
789 # nuke the parenthesis
790 6614 ▶ if ($parent_op =~ $unrollable_ops_re and $single_child_op ne 'SELECT') {
Branch: 6 branches
BranchTrueFalseTotal
if ($parent_op =~ /$unrollable_ops_re/xi and $single_child_op ne 'SELECT') { } 1517 5097 6614
elsif ($single_child_op eq $parent_op and $parent_op eq 'AND' || $parent_op eq 'OR') { } 107 4990 5097
elsif ($single_child_op eq '-LITERAL' || $single_child_op eq '-PLACEHOLDER' and $parent_op eq 'AND' || $parent_op eq 'OR' || $parent_op eq 'NOT') { } 5 4985 4990
elsif ($parent_op eq 'AND' || $parent_op eq 'OR' and $single_child_op =~ /$binary_op_re/x and $single_child_op ne 'BETWEEN' and $child_op_argc == 2 and not $single_child_op =~ /$alphanum_cmp_op_re/x && $parent_op =~ /$alphanum_cmp_op_re/x) { } 1717 3268 4985
elsif ($single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x and not $single_child_op =~ /$alphanum_cmp_op_re/ and $child_op_argc == 1 and $single_grandchild_op eq '-PAREN' || $single_grandchild_op eq '-LITERAL' || $single_grandchild_op eq '-PLACEHOLDER') { } 7 3261 3268
elsif ($parent_op ne 'ROW_NUMBER() OVER' and $single_child_op and $single_child_op ne 'NOT' and $child_op_argc == 1 and $single_grandchild_op eq '-PAREN') { } 21 3240 3261
Condition100%3/3
$parent_op =~ /$unrollable_ops_re/xi and $single_child_op ne "SELECT"
!ll&&!rl&&r
509431517
MC/DC100%2/2
$parent_op =~ /$unrollable_ops_re/xi and $single_child_op ne "SELECT"
$parent_op =~ /$unrollable_ops_re/xi $single_child_op ne "SELECT"
Truth table100%3/3
A and B
A= $parent_op =~ /$unrollable_ops_re/xiB= $single_child_op ne "SELECT"
ABresult
0X0
100
111
791 1517 push @children, @{$child->[1]};
792 1517 $changes++;
793 }
794  
795 # if the parenthesis are wrapped around an AND/OR matching the parent AND/OR - open the parenthesis up and merge the list
796 ▶ elsif (
Condition100%6/6
$single_child_op eq $parent_op and $parent_op eq "AND" || $parent_op eq "OR"
!ll&&!rl&&r
496822107
$parent_op eq "AND" or $parent_op eq "OR"
l!l&&r!l&&!r
664122
MC/DC100%3/3
$single_child_op eq $parent_op and $parent_op eq "AND" || $parent_op eq "OR"
$single_child_op eq $parent_op $parent_op eq "AND" $parent_op eq "OR"
Truth table100%4/4
A and B or C
A= $single_child_op eq $parent_opB= $parent_op eq "AND"C= $parent_op eq "OR"
ABCresult
0XX0
1000
11X1
1011
797 $single_child_op eq $parent_op
798 and
799 ( $parent_op eq 'AND' or $parent_op eq 'OR')
800 ) {
801 107 push @children, @{$child->[1][0][1]};
802 107 $changes++;
803 }
804  
805 # only *ONE* LITERAL or placeholder element
806 # as an AND/OR/NOT argument
807 ▶ elsif (
Condition91%11/12
$single_child_op eq "-LITERAL" || $single_child_op eq "-PLACEHOLDER" and $parent_op eq "AND" || $parent_op eq "OR" || $parent_op eq "NOT"
!ll&&!rl&&r
332716585
$single_child_op eq "-LITERAL" or $single_child_op eq "-PLACEHOLDER"
l!l&&r!l&&!r
15301333327
$parent_op eq "AND" or $parent_op eq "OR" or $parent_op eq "NOT"
l!l&&r!l&&!r
321658
$parent_op eq "AND" or $parent_op eq "OR"
l!l&&r!l&&!r
301660
MC/DC60%3/5
$single_child_op eq "-LITERAL" || $single_child_op eq "-PLACEHOLDER" and $parent_op eq "AND" || $parent_op eq "OR" || $parent_op eq "NOT"
$single_child_op eq "-LITERAL" $single_child_op eq "-PLACEHOLDER" $parent_op eq "AND" $parent_op eq "OR" $parent_op eq "NOT"
Truth table55%5/9
A or B and C or D or E
A= $single_child_op eq "-LITERAL"B= $single_child_op eq "-PLACEHOLDER"C= $parent_op eq "AND"D= $parent_op eq "OR"E= $parent_op eq "NOT"
ABCDEresult
00XXX0
1X0000
010000
1X1XX1
1X01X1
1X0011
011XX1
0101X1
010011
808 ( $single_child_op eq '-LITERAL' or $single_child_op eq '-PLACEHOLDER' )
809 and
810 ( $parent_op eq 'AND' or $parent_op eq 'OR' or $parent_op eq 'NOT' )
811 ) {
812 5 push @children, @{$child->[1]};
813 5 $changes++;
814 }
815  
816 # an AND/OR expression with only one binop in the parenthesis
817 # with exactly two grandchildren
818 # the only time when we can *not* unroll this is when both
819 # the parent and the child are mathops (in which case we'll
820 # break precedence) or when the child is BETWEEN (special
821 # case)
822 ▶ elsif (
Condition83%15/18
$parent_op eq "AND" || $parent_op eq "OR" and $single_child_op =~ /$binary_op_re/x and $single_child_op ne "BETWEEN" and $child_op_argc == 2 and not $single_child_op =~ /$alphanum_cmp_op_re/x && $parent_op =~ /$alphanum_cmp_op_re/x
!ll&&!rl&&r
326801717
$parent_op eq "AND" || $parent_op eq "OR" and $single_child_op =~ /$binary_op_re/x and $single_child_op ne "BETWEEN" and $child_op_argc == 2
!ll&&!rl&&r
326801717
$parent_op eq "AND" || $parent_op eq "OR" and $single_child_op =~ /$binary_op_re/x and $single_child_op ne "BETWEEN"
!ll&&!rl&&r
3215531717
$parent_op eq "AND" || $parent_op eq "OR" and $single_child_op =~ /$binary_op_re/x
!ll&&!rl&&r
29372781770
$parent_op eq "AND" or $parent_op eq "OR"
l!l&&r!l&&!r
18901582937
$single_child_op =~ /$alphanum_cmp_op_re/x && $parent_op =~ /$alphanum_cmp_op_re/x
!ll&&!rl&&r
3516820
MC/DC57%4/7
$parent_op eq "AND" || $parent_op eq "OR" and $single_child_op =~ /$binary_op_re/x and $single_child_op ne "BETWEEN" and $child_op_argc == 2 and not $single_child_op =~ /$alphanum_cmp_op_re/x && $parent_op =~ /$alphanum_cmp_op_re/x
$parent_op eq "AND" $parent_op eq "OR" $single_child_op =~ /$binary_op_re/x $single_child_op ne "BETWEEN" $child_op_argc == 2 $single_child_op =~ /$alphanum_cmp_op_re/x $parent_op =~ /$alphanum_cmp_op_re/x
Truth table69%9/13
A or B and C and D and E and not(F && G)
A= $parent_op eq "AND"B= $parent_op eq "OR"C= $single_child_op =~ /$binary_op_re/xD= $single_child_op ne "BETWEEN"E= $child_op_argc == 2F= $single_child_op =~ /$alphanum_cmp_op_re/xG= $parent_op =~ /$alphanum_cmp_op_re/x
ABCDEFGresult
00XXXXX0
1X0XXXX0
010XXXX0
1X10XXX0
0110XXX0
1X110XX0
01110XX0
1X111110
01111110
1X1110X1
1X111101
011110X1
01111101
823 ($parent_op eq 'AND' or $parent_op eq 'OR')
824 and
825 $single_child_op =~ $binary_op_re
826 and
827 $single_child_op ne 'BETWEEN'
828 and
829 $child_op_argc == 2
830 and
831 ! (
832 $single_child_op =~ $alphanum_cmp_op_re
833 and
834 $parent_op =~ $alphanum_cmp_op_re
835 )
836 ) {
837 1717 push @children, @{$child->[1]};
838 1717 $changes++;
839 }
840  
841 # a function binds tighter than a mathop - see if our ancestor is a
842 # mathop, and our content is:
843 # a single non-mathop child with a single PAREN grandchild which
844 # would indicate mathop ( nonmathop ( ... ) )
845 # or a single non-mathop with a single LITERAL ( nonmathop foo )
846 # or a single non-mathop with a single PLACEHOLDER ( nonmathop ? )
847 ▶ elsif (
Condition77%14/18
$single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x and not $single_child_op =~ /$alphanum_cmp_op_re/ and $child_op_argc == 1 and $single_grandchild_op eq "-PAREN" || $single_grandchild_op eq "-LITERAL" || $single_grandchild_op eq "-PLACEHOLDER"
!ll&&!rl&&r
326107
$single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x and not $single_child_op =~ /$alphanum_cmp_op_re/ and $child_op_argc == 1
!ll&&!rl&&r
325927
$single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x and not $single_child_op =~ /$alphanum_cmp_op_re/
!ll&&!rl&&r
325279
$single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x
!ll&&!rl&&r
702255016
$single_grandchild_op eq "-PAREN" or $single_grandchild_op eq "-LITERAL" or $single_grandchild_op eq "-PLACEHOLDER"
l!l&&r!l&&!r
700
$single_grandchild_op eq "-PAREN" or $single_grandchild_op eq "-LITERAL"
l!l&&r!l&&!r
520
MC/DC57%4/7
$single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x and not $single_child_op =~ /$alphanum_cmp_op_re/ and $child_op_argc == 1 and $single_grandchild_op eq "-PAREN" || $single_grandchild_op eq "-LITERAL" || $single_grandchild_op eq "-PLACEHOLDER"
$single_child_op $parent_op =~ /$alphanum_cmp_op_re/x not $single_child_op =~ /$alphanum_cmp_op_re/ $child_op_argc == 1 $single_grandchild_op eq "-PAREN" $single_grandchild_op eq "-LITERAL" $single_grandchild_op eq "-PLACEHOLDER"
Truth table75%6/8
A and B and not(C) and D and E or F or G
A= $single_child_opB= $parent_op =~ /$alphanum_cmp_op_re/xC= not $single_child_op =~ /$alphanum_cmp_op_re/D= $child_op_argc == 1E= $single_grandchild_op eq "-PAREN"F= $single_grandchild_op eq "-LITERAL"G= $single_grandchild_op eq "-PLACEHOLDER"
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110000
11111XX1
111101X1
11110011
848 $single_child_op
849 and
850 $parent_op =~ $alphanum_cmp_op_re
851 and
852 $single_child_op !~ $alphanum_cmp_op_re
853 and
854 $child_op_argc == 1
855 and
856 (
857 $single_grandchild_op eq '-PAREN'
858 or
859 $single_grandchild_op eq '-LITERAL'
860 or
861 $single_grandchild_op eq '-PLACEHOLDER'
862 )
863 ) {
864 7 push @children, @{$child->[1]};
865 7 $changes++;
866 }
867  
868 # a construct of ... ( somefunc ( ... ) ) ... can safely lose the outer parens
869 # except for the case of ( NOT ( ... ) ) which has already been handled earlier
870 # and except for the case of RNO, where the double are explicit syntax
871 ▶ elsif (
Condition100%12/12
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op and $single_child_op ne "NOT" and $child_op_argc == 1 and $single_grandchild_op eq "-PAREN"
!ll&&!rl&&r
1557168321
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op and $single_child_op ne "NOT" and $child_op_argc == 1
!ll&&!rl&&r
7358221704
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op and $single_child_op ne "NOT"
!ll&&!rl&&r
705302526
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op
!ll&&!rl&&r
37022556
MC/DC100%5/5
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op and $single_child_op ne "NOT" and $child_op_argc == 1 and $single_grandchild_op eq "-PAREN"
$parent_op ne "ROW_NUMBER() OVER" $single_child_op $single_child_op ne "NOT" $child_op_argc == 1 $single_grandchild_op eq "-PAREN"
Truth table100%6/6
A and B and C and D and E
A= $parent_op ne "ROW_NUMBER() OVER"B= $single_child_opC= $single_child_op ne "NOT"D= $child_op_argc == 1E= $single_grandchild_op eq "-PAREN"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
872 $parent_op ne 'ROW_NUMBER() OVER'
873 and
874 $single_child_op
875 and
876 $single_child_op ne 'NOT'
877 and
878 $child_op_argc == 1
879 and
880 $single_grandchild_op eq '-PAREN'
881 ) {
882 21 push @children, @{$child->[1]};
883 21 $changes++;
884 }
885  
886  
887 # otherwise no more mucking for this pass
888 else {
889 3240 push @children, $child;
890 }
891 }
892  
893 65549 $ast->[1] = \@children;
894  
895 } while ($changes);
896 }
897  
898 52326 ▶sub _strip_asc_from_order_by {
Subroutine _strip_asc_from_order_by: called CC=8 SCAR=20.8
899 52326 my ($self, $ast) = @_;
900  
901 52326 ▶ return $ast if (
Branch: 1 branch
BranchTrueFalseTotal
if ref $ast ne "ARRAY" or $ast->[0] ne "ORDER BY" 52157 169 52326
Condition66%2/3
ref $ast ne "ARRAY" or $ast->[0] ne "ORDER BY"
l!l&&r!l&&!r
052157169
MC/DC50%1/2
ref $ast ne "ARRAY" or $ast->[0] ne "ORDER BY"
ref $ast ne "ARRAY" $ast->[0] ne "ORDER BY"
Truth table66%2/3
A or B
A= ref $ast ne "ARRAY"B= $ast->[0] ne "ORDER BY"
ABresult
1X1
011
000
902 ref $ast ne 'ARRAY'
903 or
904 $ast->[0] ne 'ORDER BY'
905 );
906  
907  
908 169 my $to_replace;
909  
910 169 ▶ if (@{$ast->[1]} == 1 and $ast->[1][0][0] eq '-ASC') {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$ast->[1];} == 1 and $ast->[1][0][0] eq '-ASC') { } 8 161 169
elsif (@{$ast->[1];} == 1 and $ast->[1][0][0] eq '-LIST') { } 78 89 167
Condition100%3/3
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-ASC"
!ll&&!rl&&r
61558
MC/DC100%2/2
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-ASC"
@{$ast->[1];} == 1 $ast->[1][0][0] eq "-ASC"
Truth table100%3/3
A and B
A= @{$ast->[1];} == 1B= $ast->[1][0][0] eq "-ASC"
ABresult
0X0
100
111
911 8 $to_replace = [ $ast->[1][0] ];
912 }
913 161 ▶ elsif (@{$ast->[1]} == 1 and $ast->[1][0][0] eq '-LIST') {
Condition100%3/3
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-LIST"
!ll&&!rl&&r
67778
MC/DC100%2/2
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-LIST"
@{$ast->[1];} == 1 $ast->[1][0][0] eq "-LIST"
Truth table100%3/3
A and B
A= @{$ast->[1];} == 1B= $ast->[1][0][0] eq "-LIST"
ABresult
0X0
100
111
914 78 $to_replace = [ grep { $_->[0] eq '-ASC' } @{$ast->[1][0][1]} ];
915 }
916  
917 169 @$_ = @{$_->[1][0]} for @$to_replace;
918  
919 169 $ast;
920 }
921  
922 59 ▶sub format { my $self = shift; $self->unparse($self->parse($_[0]), $_[1]) }
Subroutine format: called CC=1 SCAR=0.0
Pod documented
923  
924 27 1;
925  
926 =pod
927  
928 =head1 NAME
929  
930 SQL::Abstract::Tree - Represent SQL as an AST
931  
932 =head1 SYNOPSIS
933  
934 my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' });
935  
936 print $sqla_tree->format('SELECT * FROM foo WHERE foo.a > 2');
937  
938 # SELECT *
939 # FROM foo
940 # WHERE foo.a > 2
941  
942 =head1 METHODS
943  
944 =head2 new
945  
946 my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' });
947  
948 $args = {
949 profile => 'console', # predefined profile to use (default: 'none')
950 fill_in_placeholders => 1, # true for placeholder population
951 placeholder_surround => # The strings that will be wrapped around
952 [GREEN, RESET], # populated placeholders if the above is set
953 indent_string => ' ', # the string used when indenting
954 indent_amount => 2, # how many of above string to use for a single
955 # indent level
956 newline => "\n", # string for newline
957 html_escape => 0, # HTML-escape the SQL text and the filled in
958 # placeholders (but not the strings above)
959 colormap => {
960 select => [RED, RESET], # a pair of strings defining what to surround
961 # the keyword with for colorization
962 # ...
963 },
964 indentmap => {
965 select => 0, # A zero means that the keyword will start on
966 # a new line
967 from => 1, # Any other positive integer means that after
968 on => 2, # said newline it will get that many indents
969 # ...
970 and => 1, # optional, see "Lines of their own" below
971 or => 1,
972 ',' => 1,
973 },
974 }
975  
976 Returns a new SQL::Abstract::Tree object. All arguments are optional.
977  
978 =head3 Lines of their own
979  
980 Besides clause keywords, the C<indentmap> understands three more entries,
981 none of which is set by the predefined profiles:
982  
983 =over
984  
985 =item C<and>, C<or>
986  
987 Every further condition joined with C<AND> (or C<OR>) starts a new line,
988 indented by that many levels below its clause (or below the parenthesis it
989 is in).
990  
991 =item C<,>
992  
993 The items of a comma list right below a clause (the select list, C<ORDER BY>,
994 C<GROUP BY>, C<SET> ...) each get a line of their own, indented by that many
995 levels below the clause. Lists further down, like function arguments or an
996 C<IN> list, stay on one line.
997  
998 =back
999  
1000 Merge them into a profile like any other override:
1001  
1002 my $sqlat = SQL::Abstract::Tree->new({
1003 profile => 'console',
1004 indentmap => { and => 1, or => 1, ',' => 1 },
1005 });
1006  
1007 print $sqlat->format(
1008 'SELECT a, b FROM t WHERE x = ? AND y = ? ORDER BY a, b', [1, 2]
1009 );
1010  
1011 # SELECT
1012 # a,
1013 # b
1014 # FROM t
1015 # WHERE x = 1
1016 # AND y = 2
1017 # ORDER BY
1018 # a,
1019 # b
1020  
1021 They only have an effect when a L</newline> is set.
1022  
1023 =head3 profiles
1024  
1025 There are four predefined profiles, C<none>, C<console>, C<console_monochrome>,
1026 and C<html>. Typically a user will probably just use C<console> or
1027 C<console_monochrome>, but if something about a profile bothers you, merely
1028 use the profile and override the parts that you don't like.
1029  
1030 The C<html> profile sets L</html_escape>, so its output can go straight
1031 into an HTML page: bind values and SQL text are escaped, the profile's own
1032 C<< <span> >> and C<< <br /> >> markup is not.
1033  
1034 =head2 format
1035  
1036 $sqlat->format('SELECT * FROM bar WHERE x = ?', [1])
1037  
1038 Takes C<$sql> and C<\@bindargs>.
1039  
1040 Returns a formatting string based on the string passed in
1041  
1042 =head2 parse
1043  
1044 $sqlat->parse('SELECT * FROM bar WHERE x = ?')
1045  
1046 Returns a "tree" representing passed in SQL. Please do not depend on the
1047 structure of the returned tree. It may be stable at some point, but not yet.
1048  
1049 =head2 unparse
1050  
1051 $sqlat->unparse($tree_structure, \@bindargs)
1052  
1053 Transform "tree" into SQL, applying various transforms on the way.
1054  
1055 =head2 format_keyword
1056  
1057 $sqlat->format_keyword('SELECT')
1058  
1059 Currently this just takes a keyword and puts the C<colormap> stuff around it.
1060 Later on it may do more and allow for coderef based transforms.
1061  
1062 =head2 pad_keyword
1063  
1064 my ($before, $after) = @{$sqlat->pad_keyword('SELECT')};
1065  
1066 Returns whitespace to be inserted around a keyword.
1067  
1068 =head2 fill_in_placeholder
1069  
1070 my $value = $sqlat->fill_in_placeholder(\@bindargs)
1071  
1072 Removes last arg from passed arrayref and returns it, surrounded with
1073 the values in placeholder_surround, and then surrounded with single quotes.
1074  
1075 =head2 indent
1076  
1077 Returns as many indent strings as indent amounts times the first argument.
1078  
1079 =head1 ACCESSORS
1080  
1081 =head2 colormap
1082  
1083 See L</new>
1084  
1085 =head2 fill_in_placeholders
1086  
1087 See L</new>
1088  
1089 =head2 html_escape
1090  
1091 See L</new> and L</profiles>
1092  
1093 =head2 indent_amount
1094  
1095 See L</new>
1096  
1097 =head2 indent_string
1098  
1099 See L</new>
1100  
1101 =head2 indentmap
1102  
1103 See L</new>
1104  
1105 =head2 newline
1106  
1107 See L</new>
1108  
1109 =head2 placeholder_surround
1110  
1111 See L</new>
1112  
« DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm ↑ Summary SQL/Abstract/Test.pm »