auth0 / node-odata-parser

OData query string parser for node.js.
MIT License
105 stars 72 forks source link

How to handle complex or and ands #18

Open ishwor-mis opened 9 years ago

ishwor-mis commented 9 years ago

Is it possible to handle a case where we need to handle complex or and and filters as the filter below?

$filter=Name eq 'John' and LastName eq 'Doe' or MiddleName eq 'mdle' and emailAddress eq 'someeqm@gmail.com'

Here I am trying to do the following

select * from sometable where Name='John' and (LastName='Doe' or MiddleName='mdle') and emailAddress='someeqm@gmail.com'

Hakaze commented 9 years ago

So far it seems this parser is not capable of having (a and b) or (c and d). Since this is not a farfetched use case, it would be great to have this feature!

armw4 commented 9 years ago

@Hakaze agreed

armw4 commented 9 years ago

It needs to handle operator precedence properly.

DannyDouglass commented 9 years ago

:+1:

DannyDouglass commented 9 years ago

@ishwor-mis can you provide any insight on this issue?

armw4 commented 9 years ago

What I have found is that if you want to do something like:

$filter=Name eq 'John' and LastName eq 'Doe' or MiddleName eq 'mdle' and emailAddress eq 'someeqm@gmail.com'

You must help this parser out by using grouping operators (, and ). If you do this, the parser will do a much better job at constructing the Abstract Syntax Tree (AST).

So I would change the aforementioned filter to:

$filter=(Name eq 'John' and LastName eq 'Doe') or (MiddleName eq 'mdle' and emailAddress eq 'someeqm@gmail.com')

The resulting AST will be much different (Array based), but will be straightforward enough to decompose/map (via recursion).

mkasberg commented 8 years ago

The PEG grammar used still doesn't handle operator precedence correctly. But now that #23 is merged, there is a simple workaround - just use parenthesis in your $filter clause.

armw4 commented 8 years ago

You mean like I did above? That's worked for some time now