emehrkay / Pypher

Python Cypher Querybuilder
MIT License
170 stars 29 forks source link

Howto do WHERE #34

Closed hudecof closed 4 years ago

hudecof commented 4 years ago

Hi,

I'm trying to write pypher statement for function like

def list(db: Graph, type: str=None, okres:str = None):
    q = Pypher()
    q.Match.node('school', labels='School')
    if type is not None:
        q.WHERE.school.property('type') == type
    if okres is not None:
        q.AND.school.property('okres') == okres
    q.RETURN.school

If both conditions are NULL it works. If type is set nad okres is NULL, it works.

Otherwise it do not works.

Is there any smart way hw to write WHERE statment, when I do not known the number of condition before? Like chaining AND, using Partials ..

regards Peter

emehrkay commented 4 years ago

I typically collect where clauses in a list and if that list has a length run a ConditionalAND

...
wheres = []

if type:
    wheres.append(__.school.property('type') == type)

if okres:
    wheres.append(__.school.property('okres') == okres)

if len(wheres):
    q.WHERE.CAND(*wheres)

q.RETURN.school
hudecof commented 4 years ago

great, thanks this way I'm using the sqlalchemy, but pypher I'm using about 2 hours