SQL/Abstract/Tree.pm

filter: Statementstmt 94.5% 277 / 293 Branchbran 90.3% 141 / 156 Conditioncond 87.0% 168 / 193 MC/DCmcdc 71.7% 66 / 92 Subroutinesub 81.2% 26 / 32 Podpod 87.5% 7 / 8 totaltotal 88.5% 685 / 774 CC 148 SCAR 34.0
CC
148
Cov
91%
_recurse_parse
50.5
_parenthesis_unroll
45.4
_unparse
23.0
CRAP 30.1
« DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm ↑ Summary SQL/Abstract.pm »
1 package SQL::Abstract::Tree;
2  
3 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶no warnings 'qw';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Sub::Quote 'quote_sub';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 my $op_look_ahead = '(?: (?= [\s\)\(\;] ) | \z)';
10 22 my $op_look_behind = '(?: (?<= [\,\s\)\(] ) | \A )';
11  
12 22 my $quote_left = qr/[\`\'\"\[]/;
13 22 my $quote_right = qr/[\`\'\"\]]/;
14  
15 22 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 22 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 'BEGIN \s+ WORK',
52 'COMMIT',
53 'ROLLBACK \s+ TO \s+ SAVEPOINT',
54 'ROLLBACK',
55 'SAVEPOINT',
56 'RELEASE \s+ SAVEPOINT',
57 'RETURNING',
58 );
59  
60 22 my $expr_start_re = join ("\n\t|\n", @expression_start_keywords );
61 22 $expr_start_re = qr/ $op_look_behind (?i: $expr_start_re ) $op_look_ahead /x;
62  
63 # These are binary operator keywords always a single LHS and RHS
64 # * AND/OR are handled separately as they are N-ary
65 # * so is NOT as being unary
66 # * BETWEEN without parentheses around the ANDed arguments (which
67 # makes it a non-binary op) is detected and accommodated in
68 # _recurse_parse()
69 # * AS is not really an operator but is handled here as it's also LHS/RHS
70  
71 # this will be included in the $binary_op_re, the distinction is interesting during
72 # testing as one is tighter than the other, plus alphanum cmp ops have different
73 # look ahead/behind (e.g. "x"="y" )
74 22 my @alphanum_cmp_op_keywords = (qw/< > != <> = <= >= /);
75 my $alphanum_cmp_op_re = join ("\n\t|\n", map
76 22 { "(?: (?<= [\\w\\s] | $quote_right ) | \\A )" . quotemeta ($_) . "(?: (?= [\\w\\s] | $quote_left ) | \\z )" }
77 @alphanum_cmp_op_keywords
78 );
79 22 $alphanum_cmp_op_re = qr/$alphanum_cmp_op_re/x;
80  
81 22 my $binary_op_re = '(?: NOT \s+)? (?:' . join ('|', qw/IN BETWEEN [RI]?LIKE REGEXP/) . ')';
82 22 $binary_op_re = join "\n\t|\n",
83 "$op_look_behind (?i: $binary_op_re | AS ) $op_look_ahead",
84 $alphanum_cmp_op_re,
85 $op_look_behind . 'IS (?:\s+ NOT)?' . "(?= \\s+ NULL \\b | $op_look_ahead )",
86 ;
87 22 $binary_op_re = qr/$binary_op_re/x;
88  
89 22 my $rno_re = qr/ROW_NUMBER \s* \( \s* \) \s+ OVER/ix;
90  
91 22 my $unary_op_re = 'NOT \s+ EXISTS | NOT | ' . $rno_re;
92 22 $unary_op_re = join "\n\t|\n",
93 "$op_look_behind (?i: $unary_op_re ) $op_look_ahead",
94 ;
95 22 $unary_op_re = qr/$unary_op_re/x;
96  
97 22 my $asc_desc_re = qr/$op_look_behind (?i: ASC | DESC ) $op_look_ahead /x;
98 22 my $and_or_re = qr/$op_look_behind (?i: AND | OR ) $op_look_ahead /x;
99  
100 my $tokenizer_re = join("\n\t|\n",
101 $expr_start_re,
102 $binary_op_re,
103 $unary_op_re,
104 $asc_desc_re,
105 $and_or_re,
106 $op_look_behind . ' \* ' . $op_look_ahead,
107 22 (map { quotemeta $_ } qw/, ( )/),
108 $placeholder_re,
109 );
110  
111 # this one *is* capturing for the split below
112 # splits on whitespace if all else fails
113 # has to happen before the composing qr's are anchored (below)
114 22 $tokenizer_re = qr/ \s* ( $tokenizer_re ) \s* | \s+ /x;
115  
116 # Parser states for _recurse_parse()
117 22 ▶use constant PARSE_TOP_LEVEL => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 22 ▶use constant PARSE_IN_EXPR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 22 ▶use constant PARSE_IN_PARENS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 22 ▶use constant PARSE_IN_FUNC => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 22 ▶use constant PARSE_RHS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 22 ▶use constant PARSE_LIST_ELT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 22 my $expr_term_re = qr/$expr_start_re | \)/x;
125 22 my $rhs_term_re = qr/ $expr_term_re | $binary_op_re | $unary_op_re | $asc_desc_re | $and_or_re | \, /x;
126 22 my $all_std_keywords_re = qr/ $rhs_term_re | \( | $placeholder_re /x;
127  
128 # anchor everything - even though keywords are separated by the tokenizer, leakage may occur
129 22 for (
130 $quote_left,
131 $quote_right,
132 $placeholder_re,
133 $expr_start_re,
134 $alphanum_cmp_op_re,
135 $binary_op_re,
136 $unary_op_re,
137 $asc_desc_re,
138 $and_or_re,
139 $expr_term_re,
140 $rhs_term_re,
141 $all_std_keywords_re,
142 ) {
143 264 $_ = qr/ \A $_ \z /x;
144 }
145  
146 # what can be bunched together under one MISC in an AST
147 22 my $compressable_node_re = qr/^ \- (?: MISC | LITERAL | PLACEHOLDER ) $/x;
148  
149 22 my %indents = (
150 select => 0,
151 update => 0,
152 'insert into' => 0,
153 'delete from' => 0,
154 from => 1,
155 where => 0,
156 join => 1,
157 'left join' => 1,
158 on => 2,
159 having => 0,
160 'group by' => 0,
161 'order by' => 0,
162 set => 1,
163 into => 1,
164 values => 1,
165 limit => 1,
166 offset => 1,
167 skip => 1,
168 first => 1,
169 );
170  
171  
172 22 has [qw(
173 newline indent_string indent_amount fill_in_placeholders placeholder_surround
174 )] => (is => 'ro');
175  
176 22 has [qw( indentmap colormap )] => ( is => 'ro', default => quote_sub('{}') );
177  
178 # class global is in fact desired
179 22 my $merger;
180  
181 25 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=10 SCAR=25.3
Pod undocumented
182 25 my $class = shift;
183 25 ▶ my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 7 18 25
184  
185 25 ▶ if (my $p = delete $args->{profile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = delete $args->{"profile"}) 2 23 25
186 2 my %extra_args;
187 2 ▶ if ($p eq 'console') {
Branch: 4 branches
BranchTrueFalseTotal
if ($p eq 'console') { } 1 1 2
elsif ($p eq 'console_monochrome') { } 0 1 1
elsif ($p eq 'html') { } 1 0 1
elsif ($p eq 'none') { } 0 0 0
188 %extra_args = (
189 fill_in_placeholders => 1,
190 placeholder_surround => ['?/', ''],
191 indent_string => ' ',
192 indent_amount => 2,
193 newline => "\n",
194 colormap => {},
195 indentmap => \%indents,
196  
197 1 ▶ ! ( eval { require Term::ANSIColor } ) ? () : do {
Branch: 1 branch
BranchTrueFalseTotal
!eval { do { require Term::ANSIColor } } ? : 0 1 1
198 1 my $c = \&Term::ANSIColor::color;
199  
200 1 my $red = [$c->('red') , $c->('reset')];
201 1 my $cyan = [$c->('cyan') , $c->('reset')];
202 1 my $green = [$c->('green') , $c->('reset')];
203 1 my $yellow = [$c->('yellow') , $c->('reset')];
204 1 my $blue = [$c->('blue') , $c->('reset')];
205 1 my $magenta = [$c->('magenta'), $c->('reset')];
206 1 my $b_o_w = [$c->('black on_white'), $c->('reset')];
207 (
208 1 placeholder_surround => [$c->('black on_magenta'), $c->('reset')],
209 colormap => {
210 'begin work' => $b_o_w,
211 commit => $b_o_w,
212 rollback => $b_o_w,
213 savepoint => $b_o_w,
214 'rollback to savepoint' => $b_o_w,
215 'release savepoint' => $b_o_w,
216  
217 select => $red,
218 'insert into' => $red,
219 update => $red,
220 'delete from' => $red,
221  
222 set => $cyan,
223 from => $cyan,
224  
225 where => $green,
226 values => $yellow,
227  
228 join => $magenta,
229 'left join' => $magenta,
230 on => $blue,
231  
232 'group by' => $yellow,
233 having => $yellow,
234 'order by' => $yellow,
235  
236 skip => $green,
237 first => $green,
238 limit => $green,
239 offset => $green,
240 }
241 );
242 },
243 );
244 }
245 elsif ($p eq 'console_monochrome') {
246 0 %extra_args = (
247 fill_in_placeholders => 1,
248 placeholder_surround => ['?/', ''],
249 indent_string => ' ',
250 indent_amount => 2,
251 newline => "\n",
252 indentmap => \%indents,
253 );
254 }
255 elsif ($p eq 'html') {
256 %extra_args = (
257 fill_in_placeholders => 1,
258 placeholder_surround => ['<span class="placeholder">', '</span>'],
259 indent_string => '&nbsp;',
260 indent_amount => 2,
261 newline => "<br />\n",
262 colormap => { map {
263 1 (my $class = $_) =~ s/\s+/-/g;
264 25 ( $_ => [ qq|<span class="$class">|, '</span>' ] )
265 } (
266 keys %indents,
267 qw(commit rollback savepoint),
268 'begin work', 'rollback to savepoint', 'release savepoint',
269 ) },
270 indentmap => \%indents,
271 );
272 }
273 elsif ($p eq 'none') {
274 # nada
275 }
276 else {
277 0 croak "No such profile '$p'";
278 }
279  
280 # see if we got any duplicates and merge if needed
281 2 ▶ if (scalar grep { exists $args->{$_} } keys %extra_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({exists $args->{$_};} keys %extra_args)) { } 1 1 2
282 # heavy-duty merge
283 1 ▶ $args = ($merger ||= do {
Condition33%1/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
010
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 table33%1/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
284 1 require Hash::Merge;
285 1 my $m = Hash::Merge->new;
286  
287 $m->specify_behavior({
288 SCALAR => {
289 0 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
290 0 ▶ ARRAY => sub { [ $_[0], @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
291 0 ▶ HASH => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
292 },
293 ARRAY => {
294 1 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
295 1 ▶ ARRAY => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
296 0 ▶ HASH => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
297 },
298 HASH => {
299 0 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
300 0 ▶ ARRAY => sub { [ values %{$_[0]}, @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
301 2 ▶ HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
302 },
303 1 }, 'SQLA::Tree Behavior' );
304  
305 1 $m;
306 })->merge(\%extra_args, $args );
307  
308 }
309 else {
310 1 $args = { %extra_args, %$args };
311 }
312 }
313  
314 25 $args;
315 }
316  
317 4583 ▶sub parse {
Subroutine parse: called CC=6 SCAR=18.0
Pod documented
318 4583 my ($self, $s) = @_;
319  
320 4583 ▶ return [] unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 4583 4583
321  
322 # tokenize string, and remove all optional whitespace
323 4583 my $tokens = [];
324 4583 foreach my $token (split $tokenizer_re, $s) {
325 106824 ▶ push @$tokens, $token if (
Branch: 1 branch
BranchTrueFalseTotal
if defined $token and length $token and $token =~ /\S/ 81820 25004 106824
Condition83%5/6
defined $token and length $token and $token =~ /\S/
!ll&&!rl&&r
25004081820
defined $token and length $token
!ll&&!rl&&r
4032460181820
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
326 defined $token
327 and
328 length $token
329 and
330 $token =~ /\S/
331 );
332 }
333  
334 4583 return [ $self->_recurse_parse($tokens, PARSE_TOP_LEVEL) ];
335 }
336  
337 45754 ▶sub _recurse_parse {
Subroutine _recurse_parse: called CC=50 SCAR=39.2
338 45754 my ($self, $tokens, $state) = @_;
339  
340 45754 my @left;
341 45754 while (1) { # left-associative parsing
342  
343 110190 ▶ 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) 35974 74216 110190
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
31976399874216
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
319235378214
not @$tokens or $state == 2 and $tokens->[0] eq ")" or $state == 1 and $tokens->[0] =~ /$expr_term_re/x
l!l&&r!l&&!r
174821444178267
not @$tokens or $state == 2 and $tokens->[0] eq ")"
l!l&&r!l&&!r
10368711492708
$state == 2 and $tokens->[0] eq ")"
!ll&&!rl&&r
78980137287114
$state == 1 and $tokens->[0] =~ /$expr_term_re/x
!ll&&!rl&&r
464803178714441
$state == 4 and $tokens->[0] =~ /$rhs_term_re/x
!ll&&!rl&&r
68434978053
$state == 5 and $tokens->[0] eq "," || $tokens->[0] =~ /$expr_term_re/x
!ll&&!rl&&r
7000042163998
$tokens->[0] eq "," or $tokens->[0] =~ /$expr_term_re/x
l!l&&r!l&&!r
33166824216
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
344 or
345 ($state == PARSE_IN_PARENS && $tokens->[0] eq ')')
346 or
347 ($state == PARSE_IN_EXPR && $tokens->[0] =~ $expr_term_re )
348 or
349 ($state == PARSE_RHS && $tokens->[0] =~ $rhs_term_re )
350 or
351 ($state == PARSE_LIST_ELT && ( $tokens->[0] eq ',' or $tokens->[0] =~ $expr_term_re ) )
352 ) {
353 35974 return @left;
354 }
355  
356 74216 my $token = shift @$tokens;
357  
358 # nested expression in ()
359 74216 ▶ if ($token eq '(' ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($token eq '(') { } 7114 67102 74216
elsif ($token =~ /$and_or_re/x) { } 3579 63523 67102
elsif ($token eq ',') { } 4121 59402 63523
elsif ($token =~ /$binary_op_re/x) { } 9295 50107 59402
elsif ($token =~ /$unary_op_re/x) { } 85 50022 50107
elsif ($token =~ /$expr_start_re/x) { } 16524 33498 50022
elsif ($token =~ /$placeholder_re/x) { } 2319 31179 33498
elsif (@$tokens and $tokens->[0] eq '(' || $tokens->[0] =~ /$placeholder_re/x) { } 453 30726 31179
360 7114 my @right = $self->_recurse_parse($tokens, PARSE_IN_PARENS);
361 7114 ▶ $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)) 7114 0 7114
362 7114 ▶ $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)) 7114 0 7114
363  
364 7114 push @left, [ '-PAREN' => \@right ];
365 }
366  
367 # AND/OR
368 elsif ($token =~ $and_or_re) {
369 3579 my $op = uc $token;
370  
371 3579 my @right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
372  
373 # Merge chunks if "logic" matches
374 @left = [ $op => [ @left, (@right and $op eq $right[0][0])
375 3579 ▶ ? @{ $right[0][1] }
Branch: 1 branch
BranchTrueFalseTotal
@right && $op eq $right[0][0] ? : 440 3139 3579
Condition100%3/3
@right and $op eq $right[0][0]
!ll&&!rl&&r
13138440
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
376 : @right
377 ] ];
378 }
379  
380 # LIST (,)
381 elsif ($token eq ',') {
382  
383 4121 my @right = $self->_recurse_parse($tokens, PARSE_LIST_ELT);
384  
385 # deal with malformed lists ( foo, bar, , baz )
386 4121 ▶ @right = [] unless @right;
Branch: 1 branch
BranchTrueFalseTotal
unless @right 29 4092 4121
387  
388 4121 ▶ @right = [ -MISC => [ @right ] ] if @right > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @right > 1 0 4121 4121
389  
390 4121 ▶ if (!@left) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @left) { } 9 4112 4121
elsif ($left[0][0] eq '-LIST') { } 3316 796 4112
391 9 @left = [ -LIST => [ [], @right ] ];
392 }
393 elsif ($left[0][0] eq '-LIST') {
394 3316 ▶ push @{$left[0][1]}, (@{$right[0]} and $right[0][0] eq '-LIST')
Branch: 1 branch
BranchTrueFalseTotal
@{$right[0];} && $right[0][0] eq '-LIST' ? : 0 3316 3316
Condition66%2/3
@{$right[0];} and $right[0][0] eq "-LIST"
!ll&&!rl&&r
2232940
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
395 0 ? @{$right[0][1]}
396 : @right
397 ;
398 }
399 else {
400 796 @left = [ -LIST => [ @left, @right ] ];
401 }
402 }
403  
404 # binary operator keywords
405 elsif ($token =~ $binary_op_re) {
406 9295 my $op = uc $token;
407  
408 9295 my @right = $self->_recurse_parse($tokens, PARSE_RHS);
409  
410 # A between with a simple LITERAL for a 1st RHS argument needs a
411 # rerun of the search to (hopefully) find the proper AND construct
412 9295 ▶ if ($op eq 'BETWEEN' and $right[0] eq '-LITERAL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "BETWEEN" and $right[0] eq "-LITERAL") 0 9295 9295
Condition66%2/3
$op eq "BETWEEN" and $right[0] eq "-LITERAL"
!ll&&!rl&&r
9197980
MC/DC0%0/2
$op eq "BETWEEN" and $right[0] eq "-LITERAL"
$op eq "BETWEEN" $right[0] eq "-LITERAL"
Truth table66%2/3
A and B
A= $op eq "BETWEEN"B= $right[0] eq "-LITERAL"
ABresult
0X0
100
111
413 0 unshift @$tokens, $right[1][0];
414 0 @right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
415 }
416  
417 9295 ▶ push @left, [$op => [ (@left ? pop @left : ''), @right ]];
Branch: 1 branch
BranchTrueFalseTotal
@left ? : 9289 6 9295
418 }
419  
420 # unary op keywords
421 elsif ($token =~ $unary_op_re) {
422 85 my $op = uc $token;
423  
424 # normalize RNO explicitly
425 85 ▶ $op = 'ROW_NUMBER() OVER' if $op =~ /^$rno_re$/;
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ /^$rno_re$/ 5 80 85
426  
427 85 my @right = $self->_recurse_parse($tokens, PARSE_RHS);
428  
429 85 push @left, [ $op => \@right ];
430 }
431  
432 # expression terminator keywords
433 elsif ($token =~ $expr_start_re) {
434 16524 my $op = uc $token;
435 16524 my @right = $self->_recurse_parse($tokens, PARSE_IN_EXPR);
436  
437 16524 push @left, [ $op => \@right ];
438 }
439  
440 # a '?'
441 elsif ($token =~ $placeholder_re) {
442 2319 push @left, [ -PLACEHOLDER => [ $token ] ];
443 }
444  
445 # check if the current token is an unknown op-start
446 ▶ 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
267228054453
$tokens->[0] eq "(" or $tokens->[0] =~ /$placeholder_re/x
l!l&&r!l&&!r
4361728054
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
447 453 push @left, [ $token => [ $self->_recurse_parse($tokens, PARSE_RHS) ] ];
448 }
449  
450 # we're now in "unknown token" land - start eating tokens until
451 # we see something familiar, OR in the case of RHS (binop) stop
452 # after the first token
453 # Also stop processing when we could end up with an unknown func
454 else {
455 30726 my @lits = [ -LITERAL => [$token] ];
456  
457 30726 ▶ unshift @lits, pop @left if @left == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @left == 1 153 30573 30726
458  
459 30726 ▶ unless ( $state == PARSE_RHS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state == 4) 23253 7473 30726
460 23253 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens and not $tokens->[0] =~ /$all_std_keywords_re/x and not @$tokens > 1 && $tokens->[1] eq "(") 312 23253 23565
Condition100%9/9
@$tokens and not $tokens->[0] =~ /$all_std_keywords_re/x and not @$tokens > 1 && $tokens->[1] eq "("
!ll&&!rl&&r
2323914312
@$tokens and not $tokens->[0] =~ /$all_std_keywords_re/x
!ll&&!rl&&r
133821901326
@$tokens > 1 && $tokens->[1] eq '('
!ll&&!rl&&r
1030214
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
461 @$tokens
462 and
463 $tokens->[0] !~ $all_std_keywords_re
464 and
465 ! (@$tokens > 1 and $tokens->[1] eq '(')
466 ) {
467 312 push @lits, [ -LITERAL => [ shift @$tokens ] ];
468 }
469 }
470  
471 30726 ▶ @lits = [ -MISC => [ @lits ] ] if @lits > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @lits > 1 398 30328 30726
472  
473 30726 push @left, @lits;
474 }
475  
476 # compress -LITERAL -MISC and -PLACEHOLDER pieces into a single
477 # -MISC container
478 74216 ▶ if (@left > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@left > 1) 11475 62741 74216
479 11475 my $i = 0;
480 11475 ▶ while ($#left > $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#left > $i) 22893 11475 34368
481 22893 ▶ 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/) { } 39 22854 22893
Condition100%3/3
$left[$i][0] =~ /$compressable_node_re/x and $left[$i + 1][0] =~ /$compressable_node_re/
!ll&&!rl&&r
2257228239
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
482 splice @left, $i, 2, [ -MISC => [
483 39 ▶ map { $_->[0] eq '-MISC' ? @{$_->[1]} : $_ } (@left[$i, $i+1])
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq '-MISC' ? : 39 39 78
484 ]];
485 }
486 else {
487 22854 $i++;
488 }
489 }
490 }
491  
492 74216 ▶ return @left if $state == PARSE_RHS;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 4 9780 64436 74216
493  
494 # deal with post-fix operators
495 64436 ▶ if (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 54163 10273 64436
496 # asc/desc
497 54163 ▶ if ($tokens->[0] =~ $asc_desc_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->[0] =~ /$asc_desc_re/x) 178 53985 54163
498 178 @left = [ ('-' . uc (shift @$tokens)) => [ @left ] ];
499 }
500 }
501 }
502 }
503  
504 696 ▶sub format_keyword {
Subroutine format_keyword: called CC=2 SCAR=6.9
Pod documented
505 696 my ($self, $keyword) = @_;
506  
507 696 ▶ if (my $around = $self->colormap->{lc $keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $around = $self->colormap->{lc $keyword}) 10 686 696
508 10 $keyword = "$around->[0]$keyword$around->[1]";
509 }
510  
511 696 return $keyword
512 }
513  
514 22 my %starters = (
515 select => 1,
516 update => 1,
517 'insert into' => 1,
518 'delete from' => 1,
519 );
520  
521 697 ▶sub pad_keyword {
Subroutine pad_keyword: called CC=4 SCAR=13.9
Pod documented
522 697 my ($self, $keyword, $depth) = @_;
523  
524 697 my $before = '';
525 697 ▶ if (defined $self->indentmap->{lc $keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->indentmap->{lc $keyword}) 11 686 697
526 11 $before = $self->newline . $self->indent($depth + $self->indentmap->{lc $keyword});
527 }
528 697 ▶ $before = '' if $depth == 0 and defined $starters{lc $keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 and defined $starters{lc $keyword} 150 547 697
Condition100%3/3
$depth == 0 and defined $starters{lc $keyword}
!ll&&!rl&&r
64483150
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
529 697 return [$before, ''];
530 }
531  
532 11 ▶sub indent { ($_[0]->indent_string||'') x ( ( $_[0]->indent_amount || 0 ) * $_[1] ) }
Subroutine indent: called CC=3 SCAR=12.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->indent_string or "" 11 0 11
Condition50%1/2
$_[0]->indent_amount || 0
l!l
110
MC/DC0%0/1
$_[0]->indent_amount || 0
$_[0]->indent_amount
Truth table50%1/2
A
A= $_[0]->indent_amount
Aresult
11
00
533  
534 1485 ▶sub _is_key {
Subroutine _is_key: called CC=3 SCAR=11.0
535 1485 my ($self, $tree) = @_;
536 1485 ▶ $tree = $tree->[0] while ref $tree;
Branch: 1 branch
BranchTrueFalseTotal
ref $tree and do { $tree = $tree->[0] } 2963 1485 4448
537  
538 1485 ▶ defined $tree && defined $self->indentmap->{lc $tree};
Branch: 1 branch
BranchTrueFalseTotal
defined $tree and defined $self->indentmap->{lc $tree} 1478 7 1485
539 }
540  
541 70 ▶sub fill_in_placeholder {
Subroutine fill_in_placeholder: called CC=4 SCAR=13.9
Pod documented
542 70 my ($self, $bindargs) = @_;
543  
544 70 ▶ if ($self->fill_in_placeholders) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fill_in_placeholders) 9 61 70
545 9 ▶ my $val = shift @{$bindargs} || '';
Condition50%1/2
shift @{$bindargs;} || ''
l!l
90
MC/DC0%0/1
shift @{$bindargs;} || ''
shift @{$bindargs;}
Truth table50%1/2
A
A= shift @{$bindargs;}
Aresult
11
00
546 9 my $quoted = $val =~ s/^(['"])(.*)\1$/$2/;
547 9 my ($left, $right) = @{$self->placeholder_surround};
548 9 $val =~ s/\\/\\\\/g;
549 9 $val =~ s/'/\\'/g;
550 9 ▶ $val = qq('$val') if $quoted;
Branch: 1 branch
BranchTrueFalseTotal
if $quoted 0 9 9
551 9 return qq($left$val$right)
552 }
553 61 return '?'
554 }
555  
556 # FIXME - terrible name for a user facing API
557 2605 ▶sub unparse {
Subroutine unparse: called CC=2 SCAR=6.9
Pod documented
558 2605 my ($self, $tree, $bindargs) = @_;
559 2605 ▶ $self->_unparse($tree, [@{$bindargs||[]}], 0);
Condition100%2/2
$bindargs || []
l!l
22603
MC/DC100%1/1
$bindargs || []
$bindargs
Truth table100%2/2
A
A= $bindargs
Aresult
11
00
560 }
561  
562 13964 ▶sub _unparse {
Subroutine _unparse: called CC=22 SCAR=31.3
563 13964 my ($self, $tree, $bindargs, $depth) = @_;
564  
565 13964 ▶ if (not $tree or not @$tree) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree and @$tree) 17 13947 13964
Condition100%3/3
$tree and @$tree
!ll&&!rl&&r
21513947
MC/DC100%2/2
$tree and @$tree
$tree @$tree
Truth table100%3/3
A and B
A= $treeB= @$tree
ABresult
0X0
100
111
566 17 return '';
567 }
568  
569 # FIXME - needs a config switch to disable
570 13947 $self->_parenthesis_unroll($tree);
571  
572 13947 my ($op, $args) = @{$tree}[0,1];
573  
574 13947 ▶ if (! defined $op or (! ref $op and ! defined $args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $op and ref $op || defined $args) 0 13947 13947
Condition50%3/6
defined $op and ref $op || defined $args
!ll&&!rl&&r
0013947
ref $op or defined $args
l!l&&r!l&&!r
2502114450
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
575 0 require Data::Dumper;
576 0 Carp::confess( sprintf ( "Internal error - malformed branch at depth $depth:\n%s",
577 Data::Dumper::Dumper($tree)
578 ) );
579 }
580  
581 13947 ▶ if (ref $op) {
Branch: 8 branches
BranchTrueFalseTotal
if (ref $op) { } 2502 11445 13947
elsif ($op eq '-LITERAL') { } 8108 3337 11445
elsif ($op eq '-PLACEHOLDER') { } 68 3269 3337
elsif ($op eq '-PAREN') { } 1485 1784 3269
elsif ($op eq 'AND' or $op eq 'OR' or $op =~ /$binary_op_re/) { } 908 876 1784
elsif ($op eq '-LIST') { } 37 839 876
elsif ($op eq '-MISC') { } 132 707 839
elsif ($op =~ qr/^-(ASC|DESC)$/) { } 18 689 707
582 2502 return join (' ', map $self->_unparse($_, $bindargs, $depth), @$tree);
583 }
584 elsif ($op eq '-LITERAL') { # literal has different sig
585 8108 return $args->[0];
586 }
587 elsif ($op eq '-PLACEHOLDER') {
588 68 return $self->fill_in_placeholder($bindargs);
589 }
590 elsif ($op eq '-PAREN') {
591 return sprintf ('( %s )',
592 1485 ▶ join (' ', map { $self->_unparse($_, $bindargs, $depth + 2) } @{$args} )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_key($args) ? : 0 1485 1485
Condition0%0/2
$self->newline || ''
l!l
00
MC/DC0%0/1
$self->newline || ''
$self->newline
Truth table0%0/2
A
A= $self->newline
Aresult
11
00
593 .
594 ($self->_is_key($args)
595 ? ( $self->newline||'' ) . $self->indent($depth + 1)
596 : ''
597 )
598 );
599 }
600 ▶ elsif ($op eq 'AND' or $op eq 'OR' or $op =~ $binary_op_re ) {
Condition100%6/6
$op eq "AND" or $op eq "OR" or $op =~ /$binary_op_re/
l!l&&r!l&&!r
203705876
$op eq "AND" or $op eq "OR"
l!l&&r!l&&!r
177261581
MC/DC100%3/3
$op eq "AND" or $op eq "OR" or $op =~ /$binary_op_re/
$op eq "AND" $op eq "OR" $op =~ /$binary_op_re/
Truth table100%4/4
A or B or C
A= $op eq "AND"B= $op eq "OR"C= $op =~ /$binary_op_re/
ABCresult
1XX1
01X1
0011
0000
601 908 return join (" $op ", map $self->_unparse($_, $bindargs, $depth), @{$args});
602 }
603 elsif ($op eq '-LIST' ) {
604 37 return join (', ', map $self->_unparse($_, $bindargs, $depth), @{$args});
605 }
606 elsif ($op eq '-MISC' ) {
607 132 return join (' ', map $self->_unparse($_, $bindargs, $depth), @{$args});
608 }
609 elsif ($op =~ qr/^-(ASC|DESC)$/ ) {
610 18 my $dir = $1;
611 18 return join (' ', (map $self->_unparse($_, $bindargs, $depth), @{$args}), $dir);
612 }
613 else {
614 689 my ($l, $r) = @{$self->pad_keyword($op, $depth)};
615  
616 689 my $rhs = $self->_unparse($args, $bindargs, $depth);
617  
618 return sprintf "$l%s$r", join(
619 689 ▶ ( ref $args eq 'ARRAY' and @{$args} == 1 and $args->[0][0] eq '-PAREN' )
Branch: 2 branches
BranchTrueFalseTotal
ref $args eq 'ARRAY' && @{$args;} == 1 && $args->[0][0] eq '-PAREN' ? : 63 626 689
length $rhs ? : 683 6 689
Condition83%5/6
ref $args eq "ARRAY" and @{$args;} == 1 and $args->[0][0] eq "-PAREN"
!ll&&!rl&&r
1261463
ref $args eq "ARRAY" and @{$args;} == 1
!ll&&!rl&&r
012677
MC/DC66%2/3
ref $args eq "ARRAY" and @{$args;} == 1 and $args->[0][0] eq "-PAREN"
ref $args eq "ARRAY" @{$args;} == 1 $args->[0][0] eq "-PAREN"
Truth table75%3/4
A and B and C
A= ref $args eq "ARRAY"B= @{$args;} == 1C= $args->[0][0] eq "-PAREN"
ABCresult
0XX0
10X0
1100
1111
620 ? '' # mysql--
621 : ' '
622 ,
623 $self->format_keyword($op),
624 (length $rhs ? $rhs : () ),
625 );
626 }
627 }
628  
629 # All of these keywords allow their parameters to be specified with or without parenthesis without changing the semantics
630 22 my @unrollable_ops = (
631 'ON',
632 'WHERE',
633 'GROUP \s+ BY',
634 'HAVING',
635 'ORDER \s+ BY',
636 'I?LIKE',
637 );
638 22 my $unrollable_ops_re = join ' | ', @unrollable_ops;
639 22 $unrollable_ops_re = qr/$unrollable_ops_re/xi;
640  
641 62836 ▶sub _parenthesis_unroll {
Subroutine _parenthesis_unroll: called CC=45 SCAR=38.1
642 62836 my $self = shift;
643 62836 my $ast = shift;
644  
645 62836 ▶ return unless (ref $ast and ref $ast->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ast and ref $ast->[1] 3080 59756 62836
Condition66%2/3
ref $ast and ref $ast->[1]
!ll&&!rl&&r
0308059756
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
646  
647 59756 my $changes;
648 59756 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$changes and 2699 59756 62455
649 62455 my @children;
650 62455 $changes = 0;
651  
652 62455 for my $child (@{$ast->[1]}) {
653  
654 # the current node in this loop is *always* a PAREN
655 81564 ▶ if (! ref $child or ! @$child or $child->[0] ne '-PAREN') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $child && @$child or $child->[0] ne "-PAREN") 75186 6378 81564
Condition100%6/6
not ref $child && @$child or $child->[0] ne "-PAREN"
l!l&&r!l&&!r
31049441376378
ref $child && @$child
!ll&&!rl&&r
310123750515
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
656 75186 push @children, $child;
657 75186 next;
658 }
659  
660 6378 my $parent_op = $ast->[0];
661  
662 # unroll nested parenthesis
663 6378 ▶ 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") 386 6378 6764
Condition100%6/6
$parent_op ne "IN" and @{$child->[1];} == 1 and $child->[1][0][0] eq "-PAREN"
!ll&&!rl&&r
7545624386
$parent_op ne "IN" and @{$child->[1];} == 1
!ll&&!rl&&r
1076476010
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
664 386 $child = $child->[1][0];
665 386 $changes++;
666 }
667  
668 # set to CHILD in the case of PARENT ( CHILD )
669 # but NOT in the case of PARENT( CHILD1, CHILD2 )
670 6378 ▶ my $single_child_op = (@{$child->[1]} == 1) ? $child->[1][0][0] : '';
Branch: 1 branch
BranchTrueFalseTotal
@{$child->[1];} == 1 ? : 5713 665 6378
671  
672 6378 ▶ my $child_op_argc = $single_child_op ? scalar @{$child->[1][0][1]} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$single_child_op ? : 5713 665 6378
673  
674 6378 ▶ my $single_grandchild_op
Branch: 1 branch
BranchTrueFalseTotal
($child_op_argc || !1) && ref $child->[1][0][1][0] eq 'ARRAY' ? : 4077 2301 6378
Condition100%5/5
and ref $child->[1][0][1][0] eq "ARRAY"
!ll&&!rl&&r
66516364077
$child_op_argc or
l!l
5713665
MC/DC100%2/2
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
675 = ( $child_op_argc||0 == 1 and ref $child->[1][0][1][0] eq 'ARRAY' )
676 ? $child->[1][0][1][0][0]
677 : ''
678 ;
679  
680 # if the parent operator explicitly allows it AND the child isn't a subselect
681 # nuke the parenthesis
682 6378 ▶ 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') { } 1460 4918 6378
elsif ($single_child_op eq $parent_op and $parent_op eq 'AND' || $parent_op eq 'OR') { } 147 4771 4918
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 4766 4771
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) { } 1715 3051 4766
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 3044 3051
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') { } 15 3029 3044
Condition100%3/3
$parent_op =~ /$unrollable_ops_re/xi and $single_child_op ne "SELECT"
!ll&&!rl&&r
491171460
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
683 1460 push @children, @{$child->[1]};
684 1460 $changes++;
685 }
686  
687 # if the parenthesis are wrapped around an AND/OR matching the parent AND/OR - open the parenthesis up and merge the list
688 ▶ elsif (
Condition100%6/6
$single_child_op eq $parent_op and $parent_op eq "AND" || $parent_op eq "OR"
!ll&&!rl&&r
475318147
$parent_op eq "AND" or $parent_op eq "OR"
l!l&&r!l&&!r
1064118
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
689 $single_child_op eq $parent_op
690 and
691 ( $parent_op eq 'AND' or $parent_op eq 'OR')
692 ) {
693 147 push @children, @{$child->[1][0][1]};
694 147 $changes++;
695 }
696  
697 # only *ONE* LITERAL or placeholder element
698 # as an AND/OR/NOT argument
699 ▶ 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
316216045
$single_child_op eq "-LITERAL" or $single_child_op eq "-PLACEHOLDER"
l!l&&r!l&&!r
15021073162
$parent_op eq "AND" or $parent_op eq "OR" or $parent_op eq "NOT"
l!l&&r!l&&!r
321604
$parent_op eq "AND" or $parent_op eq "OR"
l!l&&r!l&&!r
301606
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
700 ( $single_child_op eq '-LITERAL' or $single_child_op eq '-PLACEHOLDER' )
701 and
702 ( $parent_op eq 'AND' or $parent_op eq 'OR' or $parent_op eq 'NOT' )
703 ) {
704 5 push @children, @{$child->[1]};
705 5 $changes++;
706 }
707  
708 # an AND/OR expression with only one binop in the parenthesis
709 # with exactly two grandchildren
710 # the only time when we can *not* unroll this is when both
711 # the parent and the child are mathops (in which case we'll
712 # break precedence) or when the child is BETWEEN (special
713 # case)
714 ▶ 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
305101715
$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
305101715
$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
304741715
$parent_op eq "AND" || $parent_op eq "OR" and $single_child_op =~ /$binary_op_re/x
!ll&&!rl&&r
27742731719
$parent_op eq "AND" or $parent_op eq "OR"
l!l&&r!l&&!r
18341582774
$single_child_op =~ /$alphanum_cmp_op_re/x && $parent_op =~ /$alphanum_cmp_op_re/x
!ll&&!rl&&r
3316820
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 table61%8/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
715 ($parent_op eq 'AND' or $parent_op eq 'OR')
716 and
717 $single_child_op =~ $binary_op_re
718 and
719 $single_child_op ne 'BETWEEN'
720 and
721 $child_op_argc == 2
722 and
723 ! (
724 $single_child_op =~ $alphanum_cmp_op_re
725 and
726 $parent_op =~ $alphanum_cmp_op_re
727 )
728 ) {
729 1715 push @children, @{$child->[1]};
730 1715 $changes++;
731 }
732  
733 # a function binds tighter than a mathop - see if our ancestor is a
734 # mathop, and our content is:
735 # a single non-mathop child with a single PAREN grandchild which
736 # would indicate mathop ( nonmathop ( ... ) )
737 # or a single non-mathop with a single LITERAL ( nonmathop foo )
738 # or a single non-mathop with a single PLACEHOLDER ( nonmathop ? )
739 ▶ 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
304407
$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
304227
$single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x and not $single_child_op =~ /$alphanum_cmp_op_re/
!ll&&!rl&&r
303579
$single_child_op and $parent_op =~ /$alphanum_cmp_op_re/x
!ll&&!rl&&r
663237216
$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
740 $single_child_op
741 and
742 $parent_op =~ $alphanum_cmp_op_re
743 and
744 $single_child_op !~ $alphanum_cmp_op_re
745 and
746 $child_op_argc == 1
747 and
748 (
749 $single_grandchild_op eq '-PAREN'
750 or
751 $single_grandchild_op eq '-LITERAL'
752 or
753 $single_grandchild_op eq '-PLACEHOLDER'
754 )
755 ) {
756 7 push @children, @{$child->[1]};
757 7 $changes++;
758 }
759  
760 # a construct of ... ( somefunc ( ... ) ) ... can safely lose the outer parens
761 # except for the case of ( NOT ( ... ) ) which has already been handled earlier
762 # and except for the case of RNO, where the double are explicit syntax
763 ▶ 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
1402162715
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op and $single_child_op ne "NOT" and $child_op_argc == 1
!ll&&!rl&&r
6967061642
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op and $single_child_op ne "NOT"
!ll&&!rl&&r
666302348
$parent_op ne "ROW_NUMBER() OVER" and $single_child_op
!ll&&!rl&&r
36632378
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
764 $parent_op ne 'ROW_NUMBER() OVER'
765 and
766 $single_child_op
767 and
768 $single_child_op ne 'NOT'
769 and
770 $child_op_argc == 1
771 and
772 $single_grandchild_op eq '-PAREN'
773 ) {
774 15 push @children, @{$child->[1]};
775 15 $changes++;
776 }
777  
778  
779 # otherwise no more mucking for this pass
780 else {
781 3029 push @children, $child;
782 }
783 }
784  
785 62455 $ast->[1] = \@children;
786  
787 } while ($changes);
788 }
789  
790 49882 ▶sub _strip_asc_from_order_by {
Subroutine _strip_asc_from_order_by: called CC=8 SCAR=20.9
791 49882 my ($self, $ast) = @_;
792  
793 49882 ▶ return $ast if (
Branch: 1 branch
BranchTrueFalseTotal
if ref $ast ne "ARRAY" or $ast->[0] ne "ORDER BY" 49739 143 49882
Condition66%2/3
ref $ast ne "ARRAY" or $ast->[0] ne "ORDER BY"
l!l&&r!l&&!r
049739143
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
794 ref $ast ne 'ARRAY'
795 or
796 $ast->[0] ne 'ORDER BY'
797 );
798  
799  
800 143 my $to_replace;
801  
802 143 ▶ 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 135 143
elsif (@{$ast->[1];} == 1 and $ast->[1][0][0] eq '-LIST') { } 72 63 135
Condition66%2/3
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-ASC"
!ll&&!rl&&r
01358
MC/DC50%1/2
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-ASC"
@{$ast->[1];} == 1 $ast->[1][0][0] eq "-ASC"
Truth table66%2/3
A and B
A= @{$ast->[1];} == 1B= $ast->[1][0][0] eq "-ASC"
ABresult
0X0
100
111
803 8 $to_replace = [ $ast->[1][0] ];
804 }
805 135 ▶ elsif (@{$ast->[1]} == 1 and $ast->[1][0][0] eq '-LIST') {
Condition66%2/3
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-LIST"
!ll&&!rl&&r
06372
MC/DC50%1/2
@{$ast->[1];} == 1 and $ast->[1][0][0] eq "-LIST"
@{$ast->[1];} == 1 $ast->[1][0][0] eq "-LIST"
Truth table66%2/3
A and B
A= @{$ast->[1];} == 1B= $ast->[1][0][0] eq "-LIST"
ABresult
0X0
100
111
806 72 $to_replace = [ grep { $_->[0] eq '-ASC' } @{$ast->[1][0][1]} ];
807 }
808  
809 143 @$_ = @{$_->[1][0]} for @$to_replace;
810  
811 143 $ast;
812 }
813  
814 23 ▶sub format { my $self = shift; $self->unparse($self->parse($_[0]), $_[1]) }
Subroutine format: called CC=1 SCAR=0.0
Pod documented
815  
816 22 1;
817  
818 =pod
819  
820 =head1 NAME
821  
822 SQL::Abstract::Tree - Represent SQL as an AST
823  
824 =head1 SYNOPSIS
825  
826 my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' });
827  
828 print $sqla_tree->format('SELECT * FROM foo WHERE foo.a > 2');
829  
830 # SELECT *
831 # FROM foo
832 # WHERE foo.a > 2
833  
834 =head1 METHODS
835  
836 =head2 new
837  
838 my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' });
839  
840 $args = {
841 profile => 'console', # predefined profile to use (default: 'none')
842 fill_in_placeholders => 1, # true for placeholder population
843 placeholder_surround => # The strings that will be wrapped around
844 [GREEN, RESET], # populated placeholders if the above is set
845 indent_string => ' ', # the string used when indenting
846 indent_amount => 2, # how many of above string to use for a single
847 # indent level
848 newline => "\n", # string for newline
849 colormap => {
850 select => [RED, RESET], # a pair of strings defining what to surround
851 # the keyword with for colorization
852 # ...
853 },
854 indentmap => {
855 select => 0, # A zero means that the keyword will start on
856 # a new line
857 from => 1, # Any other positive integer means that after
858 on => 2, # said newline it will get that many indents
859 # ...
860 },
861 }
862  
863 Returns a new SQL::Abstract::Tree object. All arguments are optional.
864  
865 =head3 profiles
866  
867 There are four predefined profiles, C<none>, C<console>, C<console_monochrome>,
868 and C<html>. Typically a user will probably just use C<console> or
869 C<console_monochrome>, but if something about a profile bothers you, merely
870 use the profile and override the parts that you don't like.
871  
872 =head2 format
873  
874 $sqlat->format('SELECT * FROM bar WHERE x = ?', [1])
875  
876 Takes C<$sql> and C<\@bindargs>.
877  
878 Returns a formatting string based on the string passed in
879  
880 =head2 parse
881  
882 $sqlat->parse('SELECT * FROM bar WHERE x = ?')
883  
884 Returns a "tree" representing passed in SQL. Please do not depend on the
885 structure of the returned tree. It may be stable at some point, but not yet.
886  
887 =head2 unparse
888  
889 $sqlat->unparse($tree_structure, \@bindargs)
890  
891 Transform "tree" into SQL, applying various transforms on the way.
892  
893 =head2 format_keyword
894  
895 $sqlat->format_keyword('SELECT')
896  
897 Currently this just takes a keyword and puts the C<colormap> stuff around it.
898 Later on it may do more and allow for coderef based transforms.
899  
900 =head2 pad_keyword
901  
902 my ($before, $after) = @{$sqlat->pad_keyword('SELECT')};
903  
904 Returns whitespace to be inserted around a keyword.
905  
906 =head2 fill_in_placeholder
907  
908 my $value = $sqlat->fill_in_placeholder(\@bindargs)
909  
910 Removes last arg from passed arrayref and returns it, surrounded with
911 the values in placeholder_surround, and then surrounded with single quotes.
912  
913 =head2 indent
914  
915 Returns as many indent strings as indent amounts times the first argument.
916  
917 =head1 ACCESSORS
918  
919 =head2 colormap
920  
921 See L</new>
922  
923 =head2 fill_in_placeholders
924  
925 See L</new>
926  
927 =head2 indent_amount
928  
929 See L</new>
930  
931 =head2 indent_string
932  
933 See L</new>
934  
935 =head2 indentmap
936  
937 See L</new>
938  
939 =head2 newline
940  
941 See L</new>
942  
943 =head2 placeholder_surround
944  
945 See L</new>
946  
« DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm ↑ Summary SQL/Abstract.pm »