| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | # if this looks like a named filter | ||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($f=~/\G(\w+)\s*\(\s*/gc) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$f =~ /\G\[([^\]]+)\]\s*/cg or $f =~ /\G(\w+)\s*/cg
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
$f =~ /\G\'([^\']*)\'\s*/cg or $f =~ /\G\"([^\"]*)\"\s*/cg
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 109 | 0 | else { last; } | |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | 0 | return \@rv; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 1 | 1; | |||||||||||||||||||||||||||||||||