Simplified filter construction

This commit is contained in:
Daniel
2021-01-29 19:22:11 +01:00
parent 2782cf0d6d
commit 683b785ac5

View File

@@ -38,25 +38,6 @@
ge: '>=', ge: '>=',
} }
function FilterExpr() {
let parsedWhereClause = [];
function appendWhereClause(body) {
if(!parsedWhereClause) {
parsedWhereClause = body;
} else {
parsedWhereClause = [...parsedWhereClause, ...body];
}
}
function getParsedWhereClause() {
return parsedWhereClause;
}
return {
appendWhereClause,
getParsedWhereClause,
}
}
} }
start = ODataRelativeURI start = ODataRelativeURI
@@ -66,26 +47,23 @@ ODataRelativeURI // Note: case-sensitive!
{return { SELECT }} {return { SELECT }}
QueryOptions = ( QueryOptions = (
"$expand=" expand / "$expand=" expand /
"$select=" select / "$select=" select /
"$top=" top / "$top=" top /
"$skip=" skip / "$skip=" skip /
"$count=" count / "$count=" count /
"$orderby=" orderby / "$filter=" filter /
(beforeFilter FilterExprSequence aflterFilter) "$orderby=" orderby
)( o'&'o QueryOptions )? )( o'&'o QueryOptions )?
filter
= (o { SELECT.where = [] })
FilterExprSequence
// ---------- Grouped $filter expression ---------- // ---------- Grouped $filter expression ----------
// ---------- // ----------
beforeFilter = "$filter=" {
console.log('starting $filter');
filterExpr = new FilterExpr();
}
aflterFilter = "" {
console.log('end of $filter');
SELECT.where = filterExpr.getParsedWhereClause();
}
FilterExprSequence = (Expr (SP logicalOperator SP Expr)*) FilterExprSequence = (Expr (SP logicalOperator SP Expr)*)
GroupedExpr = (startGroup FilterExprSequence closeGroup) GroupedExpr = (startGroup FilterExprSequence closeGroup)
@@ -94,10 +72,10 @@ Expr = (
) / ( commonExp ) ) / ( commonExp )
startGroup startGroup
= OPEN = OPEN
{ filterExpr.appendWhereClause(['(']); } { SELECT.where.push('(') }
closeGroup closeGroup
= CLOSE = CLOSE
{ filterExpr.appendWhereClause([')']); } { SELECT.where.push(')') }
// ---------- Function expressions ---------- // ---------- Function expressions ----------
@@ -115,7 +93,7 @@ commonExp = val:(
) )
{ {
const res = val.filter(cur => cur !== ' '); const res = val.filter(cur => cur !== ' ');
filterExpr.appendWhereClause([...res]); SELECT.where.push(...res)
} }
compStrExpr compStrExpr
@@ -179,27 +157,17 @@ boolFunc
= funcName:( "contains" / "endswith" / "startswith" ) = funcName:( "contains" / "endswith" / "startswith" )
OPEN OPEN
fieldRef:strArg COMMA fieldRef:strArg COMMA
containsStrArg:strArg value:strArg
CLOSE CLOSE
{ {
function getLikeArgs (value) { const args = {
const funcArgs = {
contains: [ "'%'", value, "'%'" ], contains: [ "'%'", value, "'%'" ],
endswith: [ "'%'", value ], endswith: [ "'%'", value ],
startswith: [ value, "'%'" ] startswith: [ value, "'%'" ]
}; }[funcName]
return funcArgs[funcName]; SELECT.where.push(
}; fieldRef, 'like', {func:'concat', args }, 'escape', "'^'"
filterExpr.appendWhereClause([ )
fieldRef,
'like',
{
func: 'concat',
args: getLikeArgs(containsStrArg)
},
'escape',
"'^'"
]);
} }
// ---------- "length" ---------- // ---------- "length" ----------
lengthFunc lengthFunc
@@ -420,7 +388,7 @@ skip
other "other query options" other "other query options"
= o:$([^=]+) "=" x:todo = o:$([^=]+) "=" x:todo
{ SELECT[o.slice(1)] = x; console.log('another option was called') } { SELECT[o.slice(1)] = x }
ref "a reference" ref "a reference"
= p:$[^,?&()]+ { = p:$[^,?&()]+ {
@@ -503,7 +471,6 @@ number = val:$(
int32Value / int32Value /
int16Value int16Value
) { ) {
console.log('number', val)
return { val: Number(val) }; return { val: Number(val) };
} }
@@ -564,10 +531,10 @@ numCompOperator
= operatorVal:("lt" / "gt" / "le" / "ge") = operatorVal:("lt" / "gt" / "le" / "ge")
{ return compOperators[operatorVal]; } { return compOperators[operatorVal]; }
logicalOperator = operator:$("and" / "or") logicalOperator = operator:$("and" / "or")
{ filterExpr.appendWhereClause([operator]); } { SELECT.where.push(operator); }
notOperator notOperator
= notOperator:$("not") = "not"
{ filterExpr.appendWhereClause([notOperator]); } { SELECT.where.push('not') }
// //
@@ -586,4 +553,5 @@ RWS = ( SP / HTAB )+ // "required" whitespace
//-- Whitespaces //-- Whitespaces
o "optional whitespaces" = $[ \t\n]* o "optional whitespaces" = $[ \t\n]*
_ "mandatory whitespaces" = $[ \t\n]+