Skip to content

Commit

Permalink
fix: added missing parentheses in where conditions (#10650)
Browse files Browse the repository at this point in the history
added missing parentheses in where conditions and with And/Or operators

Closes: #10534
  • Loading branch information
alenap93 committed Jan 26, 2024
1 parent dd8c0fd commit 4624930
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,9 +1130,9 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
true,
)}`
case "and":
return condition.parameters.join(" AND ")
return "(" + condition.parameters.join(" AND ") + ")"
case "or":
return condition.parameters.join(" OR ")
return "(" + condition.parameters.join(" OR ") + ")"

This comment has been minimized.

Copy link
@kirrg001

kirrg001 Apr 10, 2024

Hi @alenap93

I wanted to let you know that our test failed because of this change.

await repo.findOne({ where: { name: 'parapeter' } });

Assertion before your fix:

SELECT "UserTypeOrm"."id" AS "UserTypeOrm_id", "UserTypeOrm"."name" AS "UserTypeOrm_name" FROM "user_type_orm" "UserTypeOrm" WHERE ("UserTypeOrm"."name" = $1) LIMIT 1'

Assertion after:

SELECT "UserTypeOrm"."id" AS "UserTypeOrm_id", "UserTypeOrm"."name" AS "UserTypeOrm_name" FROM "user_type_orm" "UserTypeOrm" WHERE (("UserTypeOrm"."name" = $1)) LIMIT 1

We had to assert double braces. Looks wrong to me?
If this looks right to you, ignore me comment 👍

Thanks!

}

throw new TypeError(
Expand Down
33 changes: 16 additions & 17 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4226,21 +4226,18 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
// let parameterIndex = Object.keys(this.expressionMap.nativeParameters).length;
if (Array.isArray(where)) {
if (where.length) {
condition =
"(" +
where
.map((whereItem) => {
return this.buildWhere(
whereItem,
metadata,
alias,
embedPrefix,
)
})
.filter((condition) => !!condition)
.map((condition) => "(" + condition + ")")
.join(" OR ") +
")"
condition = where
.map((whereItem) => {
return this.buildWhere(
whereItem,
metadata,
alias,
embedPrefix,
)
})
.filter((condition) => !!condition)
.map((condition) => "(" + condition + ")")
.join(" OR ")
}
} else {
let andConditions: string[] = []
Expand Down Expand Up @@ -4510,8 +4507,10 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
}
}
}
condition = andConditions.join(" AND ")
condition = andConditions.length
? "(" + andConditions.join(") AND (") + ")"
: andConditions.join(" AND ")
}
return condition
return condition.length ? "(" + condition + ")" : condition
}
}

0 comments on commit 4624930

Please sign in to comment.