Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What are you doing to handle more complicated WHERE clauses, such as WHERE last_name = 'Doe' AND (first_name = 'Jane' OR first_name = 'John')?


That's what the fancier __init__() at the end of the article[1] is for :)

Here's the tl;dr of a real-world example[2]:

    query = Query().SELECT(...).WHERE(...)
    for subtags in tags:
        tag_query = BaseQuery({'(': [], ')': ['']}, {'(': 'OR'})
        tag_add = partial(tag_query.add, '(')
        for subtag in subtags:
            tag_add(subtag)
        query.WHERE(str(tag_query))
It can be shortened by making a special subclass, but I only needed this once or twice so I didn't bother yet:

    for subtags in tags:
        tag_query = QList('OR')
        for subtag in subtags:
            tag_query.append(subtag)
        query.WHERE(str(tag_query))

[1]: https://death.andgravity.com/query-builder-how#more-init

[2]: https://github.com/lemon24/reader/blob/10ccabb9186f531da04db...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: