node-querybuilder icon indicating copy to clipboard operation
node-querybuilder copied to clipboard

Add a parenthesis stack for where

Open Flamenco opened this issue 7 years ago • 2 comments

It would be helpful if we could push and pop where statements.

qb.where({foo:'bar'}
qb.pushWhere()
qb.or_where({v1: 1})
qb.or_where({v2: 2})
qb.popWhere()
where foo='bar' and (v1=1 or v2=2)

Flamenco avatar Jun 24 '18 14:06 Flamenco

Query grouping would be a great feature. We would probably syntactically follow the concept in Codeigniter: https://codeigniter.com/user_guide/database/query_builder.html#query-grouping

So, like this:

qb.from('my_table')
    .group_start()
        .where('a', 'a')
        .or_group_start()
            .where({b: 'b', c: 'c'})
         .group_end()
     .group_end()
.where('d', 'd').get(callback);

Which should yield a query like this:

SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'

I'm focusing on getting some bugs fixed and adding support for MS SQL at the moment, but, we can add this as an enhancement request for a later release of v2... say, maybe, a v2.1 or something.

Once v2 is out, I'd be open to pull requests from anyone wanting this feature.

kylefarris avatar Jun 25 '18 15:06 kylefarris

I use version 2.1.1 but plugin not support this problem. Pls update new version fix problem

anphamhoai133 avatar Mar 28 '22 10:03 anphamhoai133