Skip to content

Commit 813fdec

Browse files
committed
null check fixed
1 parent 632b2a0 commit 813fdec

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/query/ast/ExpressionToSql.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,21 +493,43 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
493493
}
494494
}
495495

496+
const [r1] = right;
496497
switch(operator) {
497-
case "!==":
498-
case "!=":
499-
return prepare `${left} <> ${right}`;
500498
case "===":
501499
case "==":
500+
case "=":
501+
if (right.length === 1 && typeof r1 === "function") {
502+
const t = (p) => r1(p) === null
503+
? [ ... left, " IS NULL"]
504+
: [ ... left, " = ", ... right.flatMap((x) => typeof x === "function" ? () => x(p) : x)];
505+
return [t];
506+
}
502507
return prepare `${left} = ${right}`;
508+
case "<>":
509+
case "!==":
510+
case "!=":
511+
if (right.length === 1 && typeof r1 === "function") {
512+
const t = (p) => r1(p) === null
513+
? [ ... left, " IS NOT NULL"]
514+
: [ ... left, " <> ", ... right.flatMap((x) => typeof x === "function" ? () => x(p) : x)];
515+
return [t];
516+
}
517+
return prepare `${left} <> ${right}`;
503518
}
504519

505520
} else {
506521
switch(operator) {
507522
case "!==":
508523
case "!=":
509-
operator = "<>";
510-
break;
524+
case "<>":
525+
const [r1] = right;
526+
if (right.length === 1 && typeof r1 === "function") {
527+
const t = (p) => r1(p) === null
528+
? [ ... left, " IS NOT NULL"]
529+
: [ ... left, " <> ", ... right.flatMap((x) => typeof x === "function" ? () => x(p) : x)];
530+
return [t];
531+
}
532+
return prepare `${left} <> ${right}`;
511533
case "===":
512534
case "==":
513535
operator = "=";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import assert from "assert";
2+
import Sql from "../../../sql/Sql.js";
3+
import QueryCompiler from "../../../compiler/QueryCompiler.js";
4+
5+
export default function () {
6+
7+
const compiler = QueryCompiler.instance;
8+
9+
let name = "1";
10+
11+
let r = compiler.execute({ name }, (p) => (x) => x.firstName === p.name);
12+
assert.equal(`x."firstName" = $1`, r.text);
13+
14+
name = null;
15+
r = compiler.execute({ name }, (p) => (x) => x.firstName === p.name);
16+
assert.equal(`x."firstName" IS NULL`, r.text);
17+
18+
r = compiler.execute({ name }, (p) => (x) => x.firstName === null);
19+
assert.equal(`x."firstName" IS NULL`, r.text);
20+
21+
name = "1";
22+
23+
r = compiler.execute({ name }, (p) => (x) => x.firstName !== p.name);
24+
assert.equal(`x."firstName" <> $1`, r.text);
25+
26+
name = null;
27+
r = compiler.execute({ name }, (p) => (x) => x.firstName !== p.name);
28+
assert.equal(`x."firstName" IS NOT NULL`, r.text);
29+
30+
r = compiler.execute({ name }, (p) => (x) => x.firstName !== null);
31+
assert.equal(`x."firstName" IS NOT NULL`, r.text);
32+
}
33+

0 commit comments

Comments
 (0)