lib/CGI/OptimalQuery/FilterParser.pm

filter: Statementstmt 18.5% 10 / 54 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 3 / 4 Podpod 0.0% 0 / 1 totaltotal 11.6% 13 / 112 CC 21 SCAR 60.0
CC
21
Cov
10%
parseFilter
462.0
BEGIN
1.0
BEGIN
1.0
CRAP 404.4
« blib/lib/CGI/OptimalQuery/SaveSearchTool.pm ↑ Summary blib/lib/CGI/OptimalQuery/Base.pm »
1 package CGI::OptimalQuery::FilterParser;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings qw( uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # arguments: ($CgiOptimalQueryObjecto, $filterString)
8 # return value: is an arrayref contain zero or more components that look like:
9 # # logic operator
10 # 'AND'|'OR', # logic operator
11 # # type 1 - (selectalias operator literal)
12 # [1,$numLeftParen,$leftExpSelectAlias,$op,$rightExpLiteral,$numRightParen],
13 # # type 2 - (namedfilter, arguments)
14 # [2,$numLeftParen,$namedFilter,$argArray,$numRightParen]
15 # # type 3 - (selectalias operator selectalias)
16 # [3,$numLeftParen,$leftExpSelectAlias,$op,$rightExpSelectAlias,$numRightParen],
17 # dies on bad filter string
18 sub parseFilter {
19 # $o is optimalquery object, $f is the filter string
20 0 ▶ my ($o, $f) = @_;
Subroutine parseFilter: not called CC=21 SCAR=61.4
Pod undocumented
21 0 $f =~ /\G\s+/gc; # match all leading whitespace
22  
23 # initialize the return value which is an array of components
24 0 my @rv;
25 0 ▶ return \@rv if $f eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "" 0 0 0
26
27 0 while (1) {
28 0 my $numLeftParenthesis = 0;
29 0 my $numRightParenthesis = 0;
30  
31 # parse opening parenthesis
32 0 ▶ while ($f =~ /\G\(\s*/gc) { $numLeftParenthesis++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\G\(\s*/cg) 0 0 0
33  
34 # if this looks like a named filter
35 0 ▶ if ($f=~/\G(\w+)\s*\(\s*/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\G(\w+)\s*\(\s*/cg) { } 0 0 0
36 0 my $namedFilter = $1;
37 die "Invalid named filter $namedFilter at: ".substr($f, 0, pos($f)).' <*> '.substr($f,pos($f))
38 0 ▶ unless exists $$o{schema}{named_filters}{$namedFilter};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $o->{"schema"}{"named_filters"}{$namedFilter} 0 0 0
39  
40 # parse named filter arguments
41 0 my @args;
42 0 while (1) {
43 # closing paren so end
44 0 ▶ if ($f=~/\G\)\s*/gc) {
Branch: 3 branches
BranchTrueFalseTotal
if ($f =~ /\G\)\s*/cg) { } 0 0 0
elsif ($f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg or $f =~ /\G(\w+)\s*/cg) { } 0 0 0
elsif ($f =~ /\G(\,|\=\>|\:)\s*/cg) { } 0 0 0
45 0 last;
46 }
47  
48 # single quoted value OR double quoted value OR no whitespace literal
49 ▶ elsif ($f=~/\G\'([^\']*)\'\s*/gc || $f=~/\G\"([^\"]*)\"\s*/gc || $f=~/\G(\w+)\s*/gc) {
Condition0%0/6
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg or $f =~ /\G(\w+)\s*/cg
l!l&&r!l&&!r
000
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg
l!l&&r!l&&!r
000
MC/DC0%0/3
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg or $f =~ /\G(\w+)\s*/cg
$f =~ /\G\'([^\']*)\'\s*/cg $f =~ /\G\"([^\"]*)\"\s*/cg $f =~ /\G(\w+)\s*/cg
Truth table0%0/4
A or B or C
A= $f =~ /\G\'([^\']*)\'\s*/cgB= $f =~ /\G\"([^\"]*)\"\s*/cgC= $f =~ /\G(\w+)\s*/cg
ABCresult
1XX1
01X1
0011
0000
50 0 push @args, $1;
51 }
52  
53 # , => : separator so do nothing
54 elsif ($f =~ /\G(\,|\=\>|\:)\s*/gc) {
55 # noop
56 }
57 else {
58 0 die "Invalid named filter $namedFilter - missing right paren at: ".substr($f, 0, pos($f)).' <*> '.substr($f,pos($f));
59 }
60 }
61  
62 # parse closing parenthesis
63 0 ▶ while ($f =~ /\G\)\s*/gc) { $numRightParenthesis++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\G\)\s*/cg) 0 0 0
64 0 push @rv, [2,$numLeftParenthesis,$namedFilter,\@args,$numRightParenthesis];
65 }
66  
67 # else this is an expression
68 else {
69 0 my $lexp;
70 0 my $typeNum = 1;
71  
72 # grab select alias used on the left side of the expression
73 0 ▶ if ($f=~/\G\[([^\]]+)\]\s*/gc || $f=~/\G(\w+)\s*/gc) { $lexp = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\G\[([^\]]+)\]\s*/cg or $f =~ /\G(\w+)\s*/cg) { } 0 0 0
Condition0%0/3
$f =~ /\G\[([^\]]+)\]\s*/cg or $f =~ /\G(\w+)\s*/cg
l!l&&r!l&&!r
000
MC/DC0%0/2
$f =~ /\G\[([^\]]+)\]\s*/cg or $f =~ /\G(\w+)\s*/cg
$f =~ /\G\[([^\]]+)\]\s*/cg $f =~ /\G(\w+)\s*/cg
Truth table0%0/3
A or B
A= $f =~ /\G\[([^\]]+)\]\s*/cgB= $f =~ /\G(\w+)\s*/cg
ABresult
1X1
011
000
74 0 else { die 'Missing left expression: '.substr($f, 0, pos($f)).' <*> '.substr($f,pos($f)); }
75  
76 # make sure the select alias is valid
77 die "Invalid field $lexp at: ".substr($f, 0, pos($f)).' <*> '.substr($f,pos($f))
78 0 ▶ unless exists $$o{schema}{select}{$lexp};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $o->{"schema"}{"select"}{$lexp} 0 0 0
79  
80 # parse the operator
81 0 my $op;
82 0 ▶ if ($f =~ /\G(\!\=|\=|\<\=|\>\=|\<|\>|like|not\ like|contains|not\ contains)\s*/igc) { $op = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\G(\!\=|\=|\<\=|\>\=|\<|\>|like|not\ like|contains|not\ contains)\s*/cig) { } 0 0 0
83 0 else { die 'Missing operator: '.substr($f, 0, pos($f)).' <*> '.substr($f,pos($f)); }
84  
85 # parse the right side of expression
86 0 my $rexp;
87  
88 # if rexp is a select alias
89 0 ▶ if ($f=~/\G\[([^\]]+)\]\s*/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f =~ /\G\[([^\]]+)\]\s*/cg) { } 0 0 0
elsif ($f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg or $f =~ /\G(\w+)\s*/cg) { } 0 0 0
90 0 $rexp = $1;
91 0 $typeNum = 3;
92 }
93  
94 # else if rexp is a literal
95 ▶ elsif ($f=~/\G\'([^\']*)\'\s*/gc || $f=~/\G\"([^\"]*)\"\s*/gc || $f=~/\G(\w+)\s*/gc) {
Condition0%0/6
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg or $f =~ /\G(\w+)\s*/cg
l!l&&r!l&&!r
000
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg
l!l&&r!l&&!r
000
MC/DC0%0/3
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg or $f =~ /\G(\w+)\s*/cg
$f =~ /\G\'([^\']*)\'\s*/cg $f =~ /\G\"([^\"]*)\"\s*/cg $f =~ /\G(\w+)\s*/cg
Truth table0%0/4
A or B or C
A= $f =~ /\G\'([^\']*)\'\s*/cgB= $f =~ /\G\"([^\"]*)\"\s*/cgC= $f =~ /\G(\w+)\s*/cg
ABCresult
1XX1
01X1
0011
0000
96 0 $rexp = $1;
97 }
98  
99 0 else { die 'Missing right expression: '.substr($f, 0, pos($f)).' <*> '.substr($f,pos($f)); }
100  
101 # parse closing parenthesis
102 0 ▶ while ($f =~ /\G\)\s*/gc) { $numRightParenthesis++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\G\)\s*/cg) 0 0 0
103  
104 0 push @rv, [$typeNum, $numLeftParenthesis, $lexp, $op, $rexp, $numRightParenthesis];
105 }
106  
107 # parse logic operator
108 0 ▶ if ($f =~ /(AND|OR)\s*/gci) { push @rv, uc($1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /(AND|OR)\s*/cig) { } 0 0 0
109 0 else { last; }
110 }
111 0 return \@rv;
112 }
113  
114 1 1;
« blib/lib/CGI/OptimalQuery/SaveSearchTool.pm ↑ Summary blib/lib/CGI/OptimalQuery/Base.pm »