Skip to content

Commit

Permalink
Accept safe string in clickhouse with (#79)
Browse files Browse the repository at this point in the history
* Accept safe string in clickhouse with

* update docs

* fix test

* fix ci

* fix ci
  • Loading branch information
lucasavila00 authored Aug 31, 2023
1 parent 436b49b commit 30ff217
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/api/classes/select-statement.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ clickhouse: {
with_: <NewSelection extends string>(
it: Record<
NewSelection,
| SafeString
| SelectStatement<any, any, any, any>
| AliasedSelectStatement<any, any, any, any>
| StringifiedSelectStatement<any, any, any, any>
Expand Down
12 changes: 10 additions & 2 deletions src/classes/select-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "../consume-fields";
import { AliasedRows, StarSymbol } from "../data-wrappers";
import { printAliasedSelectStatement, printSelectStatement } from "../print";
import { SafeString } from "../safe-string";
import { SafeString, isSafeString } from "../safe-string";
import {
ClickhouseWith,
CTE,
Expand Down Expand Up @@ -268,6 +268,7 @@ export class SelectStatement<
| AliasedSelectStatement<any, any, any, any>
| StringifiedSelectStatement<any, any, any, any>
| AliasedStringifiedSelectStatement<any, any, any, any>
| SafeString
>
): SelectStatement<
Selection | NewSelection,
Expand All @@ -277,7 +278,14 @@ export class SelectStatement<
> =>
this.copy().setClickhouseWith([
...this.__props.clickhouseWith,
it,
Object.fromEntries(
Object.entries(it).map(([key, value]) => [
key,
isSafeString(value)
? StringifiedSelectStatement.fromSafeString(value)
: value,
])
) as any,
]) as any,

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/clickhouse-suite/with.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ describe("clickhouse with", () => {
]
`);
});
it("from safe string", async () => {
const q = fromNothing({ it: dsql(10) })
.clickhouse.with_({
wont_use: dsql(20),
})
.stringify();
expect(q).toMatchInlineSnapshot(
`WITH (20) AS \`wont_use\` SELECT 10 AS \`it\``
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Array [
10,
],
]
`);
});

it("from stringified select statement", async () => {
const q = fromNothing({ it: dsql(10) })
Expand Down

0 comments on commit 30ff217

Please sign in to comment.