Skip to content
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

fix: handle AExpr and NullTest nodes in SQL parsing #354

Merged
merged 2 commits into from
Jan 1, 2025
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
6 changes: 6 additions & 0 deletions .changeset/olive-coins-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ts-safeql/eslint-plugin": patch
"@ts-safeql/generate": patch
---

fixed an issue when dealing with AExpr and NullTest nodes
21 changes: 21 additions & 0 deletions packages/eslint-plugin/src/rules/check-sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,27 @@ RuleTester.describe("check-sql", () => {
}),
code: "sql<{ phone_number: PhoneNumber }>`select phone_number from caregiver_phone`",
},
{
name: "select case when then jsonb with not like with type reference",
filename,
options: withConnection(connections.withTag, {
targets: [{ tag: "sql", transform: "{type}[]" }],
}),
code: `
type Meta = { is_test: boolean; };
type Caregiver = { meta: Meta | null };

await sql<Caregiver[]>\`
SELECT
CASE WHEN caregiver.id IS NOT NULL
THEN jsonb_build_object('is_test', caregiver.middle_name NOT LIKE '%test%')
ELSE NULL
END AS meta
FROM
caregiver
\`;
`,
},
],
invalid: [
{
Expand Down
40 changes: 40 additions & 0 deletions packages/generate/src/ast-describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,49 @@ function getDescribedNode(params: {
return getDescribedCaseExpr({ alias: alias, node: node.CaseExpr, context });
}

if (node.NullTest !== undefined) {
return getDescribedNullTest({ alias: alias, node: node.NullTest, context });
}

if (node.A_Expr !== undefined) {
return getDescribedAExpr({ alias: alias, node: node.A_Expr, context });
}

return [];
}

function getDescribedAExpr({
alias,
context,
}: GetDescribedParamsOf<LibPgQueryAST.AExpr>): ASTDescribedColumn[] {
return [
{
name: alias ?? "?column?",
type: resolveType({
context: context,
nullable: false,
type: context.toTypeScriptType({ name: "boolean" }),
}),
},
];
}

function getDescribedNullTest({
alias,
context,
}: GetDescribedParamsOf<LibPgQueryAST.NullTest>): ASTDescribedColumn[] {
return [
{
name: alias ?? "?column?",
type: resolveType({
context: context,
nullable: false,
type: context.toTypeScriptType({ name: "boolean" }),
}),
},
];
}

function getDescribedCaseExpr({
alias,
node,
Expand Down
43 changes: 43 additions & 0 deletions packages/generate/src/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1500,3 +1500,46 @@ test("select case when expr with returned string literals", async () => {
expected: [["case", { kind: "type", value: "boolean" }]],
});
});

test("select case when expr is not null else is not null", async () => {
await testQuery({
query: `
SELECT
CASE WHEN TRUE
THEN caregiver.id IS NOT NULL
ELSE caregiver.id IS NOT NULL
END
FROM caregiver`,
expected: [["case", { kind: "type", value: "boolean" }]],
});
});

test("select case when with jsonb_build_object", async () => {
await testQuery({
query: `
SELECT
CASE
WHEN caregiver.id IS NOT NULL THEN (
jsonb_build_object(
'is_test',
caregiver.first_name NOT LIKE '%test%'
)
)
ELSE NULL
END AS col
FROM
caregiver`,
expected: [
[
"col",
{
kind: "union",
value: [
{ kind: "object", value: [["is_test", { kind: "type", value: "boolean" }]] },
{ kind: "type", value: "null" },
],
},
],
],
});
});
Loading