Skip to content

Fix v and nv filters in SQL #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,17 @@ function appendWhereEntry({type, operands}, args, escaper) {
if (operands.length < 1) throw new Error("Invalid operand length");

// Unary operations
if (operands.length === 1) {
// We treat `v` and `nv` as `NULL` and `NOT NULL` unary operations in SQL,
// since the database already validates column types.
if (operands.length === 1 || type === "v" || type === "nv") {
appendOperand(operands[0], args, escaper);
switch (type) {
case "n":
case "nv":
appendSql(` IS NULL`, args);
return;
case "nn":
case "v":
appendSql(` IS NOT NULL`, args);
return;
default:
Expand All @@ -398,7 +402,7 @@ function appendWhereEntry({type, operands}, args, escaper) {
if (operands.length === 2) {
if (["in", "nin"].includes(type)) {
// Fallthrough to next parent block.
} else if (["c", "nc", "v", "nv"].includes(type)) {
} else if (["c", "nc"].includes(type)) {
// TODO: Case (in)sensitive?
appendOperand(operands[0], args, escaper);
switch (type) {
Expand All @@ -408,14 +412,6 @@ function appendWhereEntry({type, operands}, args, escaper) {
case "nc":
appendSql(` NOT LIKE `, args);
break;
// JavaScript "not valid" filter translate to a SQL "IS NULL"
case "nv":
appendSql(` IS NULL`, args);
break;
// JavaScript "valid" filter translate to a SQL "IS NOT NULL"
case "v":
appendSql(` IS NOT NULL`, args);
break;
}
appendOperand(likeOperand(operands[1]), args, escaper);
return;
Expand Down
25 changes: 25 additions & 0 deletions test/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ describe("makeQueryTemplate", () => {
assert.deepStrictEqual(params, ["val1", "val2", "val3", "val4"]);
});

it("makeQueryTemplate filter valid and not valid", () => {
const source = {name: "db", dialect: "postgres"};
const operations = {
...baseOperations,
filter: [
{
type: "v",
operands: [
{type: "column", value: "col1"},
{type: "primitive", value: "string"}
]
},
{
type: "nv",
operands: [
{type: "column", value: "col2"},
{type: "primitive", value: "number"}
]
}
]
};
const [parts] = makeQueryTemplate(operations, source);
assert.deepStrictEqual(parts.join("?"), "SELECT col1, col2 FROM table1\nWHERE col1 IS NOT NULL\nAND col2 IS NULL");
});

it("makeQueryTemplate select", () => {
const source = {name: "db", dialect: "mysql"};
const operations = {
Expand Down