Skip to content

Surround where clauses with parentheses #6170

@diogodomanski

Description

@diogodomanski

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[x] mssql
[x] mysql / mariadb
[x] oracle
[x] postgres
[ ] cockroachdb
[x] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[x] 0.2.25

It would be nice if the expression passed to where() and andWhere() methods of QueryBuilder was surrounded by parentheses.

For example, the following code

qb("User", "user")
  .where("user.deleteDate IS NULL")  // expression (A)
  .andWhere("user.name = 'foo' OR user.birthDate IS NULL"); // expression (B or C) 

currently generates the following SQL sentence:

SELECT `user`.`id`, `user`.`name`, `user`.`birthDate`, `user`.`deleteDate`
FROM `User` `user`
WHERE `user`.`deleteDate` IS NULL
AND `user`.`name` = 'foo'
OR `user`.`birthDate` IS NULL

As can be seen, the resulting WHERE clause is A and B or C. But it would make more sense if it was (A) and (B or C) - because A was passed to where() and B or C was passed to andWhere().

I know that I can achieve this result by manually including the parentheses to every where condition. But, in my opinion, adding the parentheses automagically in SQL sentence generation would be better.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions