Skip to content

Commit 0d3a2bb

Browse files
author
Annie Zhang
authored
Fix v and nv filters in SQL (#340)
1 parent 1021684 commit 0d3a2bb

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/table.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,17 @@ function appendWhereEntry({type, operands}, args, escaper) {
380380
if (operands.length < 1) throw new Error("Invalid operand length");
381381

382382
// Unary operations
383-
if (operands.length === 1) {
383+
// We treat `v` and `nv` as `NULL` and `NOT NULL` unary operations in SQL,
384+
// since the database already validates column types.
385+
if (operands.length === 1 || type === "v" || type === "nv") {
384386
appendOperand(operands[0], args, escaper);
385387
switch (type) {
386388
case "n":
389+
case "nv":
387390
appendSql(` IS NULL`, args);
388391
return;
389392
case "nn":
393+
case "v":
390394
appendSql(` IS NOT NULL`, args);
391395
return;
392396
default:
@@ -398,7 +402,7 @@ function appendWhereEntry({type, operands}, args, escaper) {
398402
if (operands.length === 2) {
399403
if (["in", "nin"].includes(type)) {
400404
// Fallthrough to next parent block.
401-
} else if (["c", "nc", "v", "nv"].includes(type)) {
405+
} else if (["c", "nc"].includes(type)) {
402406
// TODO: Case (in)sensitive?
403407
appendOperand(operands[0], args, escaper);
404408
switch (type) {
@@ -408,14 +412,6 @@ function appendWhereEntry({type, operands}, args, escaper) {
408412
case "nc":
409413
appendSql(` NOT LIKE `, args);
410414
break;
411-
// JavaScript "not valid" filter translate to a SQL "IS NULL"
412-
case "nv":
413-
appendSql(` IS NULL`, args);
414-
break;
415-
// JavaScript "valid" filter translate to a SQL "IS NOT NULL"
416-
case "v":
417-
appendSql(` IS NOT NULL`, args);
418-
break;
419415
}
420416
appendOperand(likeOperand(operands[1]), args, escaper);
421417
return;

test/table-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,31 @@ describe("makeQueryTemplate", () => {
182182
assert.deepStrictEqual(params, ["val1", "val2", "val3", "val4"]);
183183
});
184184

185+
it("makeQueryTemplate filter valid and not valid", () => {
186+
const source = {name: "db", dialect: "postgres"};
187+
const operations = {
188+
...baseOperations,
189+
filter: [
190+
{
191+
type: "v",
192+
operands: [
193+
{type: "column", value: "col1"},
194+
{type: "primitive", value: "string"}
195+
]
196+
},
197+
{
198+
type: "nv",
199+
operands: [
200+
{type: "column", value: "col2"},
201+
{type: "primitive", value: "number"}
202+
]
203+
}
204+
]
205+
};
206+
const [parts] = makeQueryTemplate(operations, source);
207+
assert.deepStrictEqual(parts.join("?"), "SELECT col1, col2 FROM table1\nWHERE col1 IS NOT NULL\nAND col2 IS NULL");
208+
});
209+
185210
it("makeQueryTemplate select", () => {
186211
const source = {name: "db", dialect: "mysql"};
187212
const operations = {

0 commit comments

Comments
 (0)