Skip to content

Commit

Permalink
fix: Logical operators should include parens. (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer authored Oct 12, 2023
1 parent c2200da commit f4ab517
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/sql/src/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ function visit(callback) {
function logical(op, clauses) {
const children = clauses.filter(x => x != null).map(asColumn);
const strings = children.map((c, i) => i ? ` ${op} ` : '');
if (clauses.length) strings.push('');
if (children.length === 1) {
strings.push('')
} else if (children.length > 1) {
strings[0] = '(';
strings.push(')');
}
return sql(strings, ...children).annotate({ op, children, visit });
}

Expand Down
6 changes: 4 additions & 2 deletions packages/sql/test/operator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe('Logical operators', () => {
assert.strictEqual(String(and()), '');
assert.strictEqual(String(and('foo')), '"foo"');
assert.strictEqual(String(and(null, true)), 'TRUE');
assert.strictEqual(String(and(true, true)), 'TRUE AND TRUE');
assert.strictEqual(String(and(true, true)), '(TRUE AND TRUE)');
assert.strictEqual(String(and(true, null, false)), '(TRUE AND FALSE)');
assert.strictEqual(and().op, 'AND');
assert.strictEqual(and().children.length, 0);
assert.strictEqual(and('foo').children.length, 1);
Expand All @@ -23,7 +24,8 @@ describe('Logical operators', () => {
assert.strictEqual(String(or()), '');
assert.strictEqual(String(or('foo')), '"foo"');
assert.strictEqual(String(or(null, true)), 'TRUE');
assert.strictEqual(String(or(false, true)), 'FALSE OR TRUE');
assert.strictEqual(String(or(false, true)), '(FALSE OR TRUE)');
assert.strictEqual(String(or(false, null, false)), '(FALSE OR FALSE)');
assert.strictEqual(or().op, 'OR');
assert.strictEqual(or().children.length, 0);
assert.strictEqual(or('foo').children.length, 1);
Expand Down

0 comments on commit f4ab517

Please sign in to comment.