ReviewOS

stacks/bun-query-builder

Multiple having() calls emit a second HAVING keyword instead of AND

#1034
Closed glennmichael123 opened this 2 months ago ·
2 months ago

Summary

Each having() appends ... HAVING ... to text; reorderSelectClauses groups same-key fragments by joining with a space, so two calls become HAVING COUNT(id) > $1 HAVING SUM(age) < $2 — invalid SQL.

Evidence (repro)

.having(['COUNT(id)','>',3]).having(['SUM(age)','<',100]).toSQL()
-> ... HAVING COUNT(id) > $1 HAVING SUM(age) < $2

Location

src/client.ts:~4429-4458.

Suggested fix

Detect an existing HAVING (like orderBy/groupBy do) and append subsequent conditions with AND.

Severity: medium. Found via internal code audit.

9 days ago

Fixed in b21dd32. having()/havingRaw() now switch to AND when a HAVING clause already exists, so chained calls produce a single HAVING a AND b instead of the invalid HAVING a HAVING b. Regression test added; full suite green.

Sign in to comment on this issue.