SQL/Abstract/Tree.pm

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