From f52c160af71f9e20c6e6b1b19a19d08d64096f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20de=20=C3=81vila=20Martins?= Date: Fri, 1 Jul 2022 00:19:14 -0300 Subject: [PATCH] fix it (#29) --- package.json | 2 +- src/print.ts | 6 +++--- src/proxy.ts | 4 ++-- src/wrap-alias.ts | 3 +++ tests/unit/table.test.ts | 8 ++++++++ 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index c9726b9..aa2b2bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sql-select-ts", - "version": "0.0.4", + "version": "0.0.5", "description": "A modern, database-agnostic, composable SELECT query builder with great typescript support.", "main": "lib/index.js", "module": "es6/index.js", diff --git a/src/print.ts b/src/print.ts index 0a8f8a9..740e871 100644 --- a/src/print.ts +++ b/src/print.ts @@ -9,7 +9,7 @@ import { isTheProxyObject } from "./proxy"; import type { SafeString } from "./safe-string"; import { ClickhouseWith, JoinConstraint, TableOrSubquery } from "./types"; import { absurd } from "./utils"; -import { wrapAlias } from "./wrap-alias"; +import { wrapAlias, wrapAliasSplitDots } from "./wrap-alias"; // re-define to avoid circular dependency /* istanbul ignore next */ @@ -82,10 +82,10 @@ const printTableInternal = ( ): PrintInternalRet => { const final = table.__props.final ? ` FINAL` : ""; if (table.__props.name === table.__props.alias) { - return { content: wrapAlias(table.__props.name) + final }; + return { content: wrapAliasSplitDots(table.__props.name) + final }; } return { - content: `${wrapAlias(table.__props.name)} AS ${wrapAlias( + content: `${wrapAliasSplitDots(table.__props.name)} AS ${wrapAlias( table.__props.alias )}${final}`, }; diff --git a/src/proxy.ts b/src/proxy.ts index d992389..f8479e3 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -1,5 +1,5 @@ import { castSafe } from "./safe-string"; -import { wrapAlias } from "./wrap-alias"; +import { wrapAliasSplitDots } from "./wrap-alias"; export const proxy = new Proxy( { @@ -7,7 +7,7 @@ export const proxy = new Proxy( }, { get: function (_target, prop, _receiver) { - return castSafe(String(prop).split(".").map(wrapAlias).join(".")); + return castSafe(wrapAliasSplitDots(String(prop))); }, } ) as any; diff --git a/src/wrap-alias.ts b/src/wrap-alias.ts index b9180a2..b8b5423 100644 --- a/src/wrap-alias.ts +++ b/src/wrap-alias.ts @@ -1,3 +1,6 @@ const ID_GLOBAL_REGEXP = /`/g; export const wrapAlias = (alias: string) => "`" + alias.replace(ID_GLOBAL_REGEXP, "``") + "`"; + +export const wrapAliasSplitDots = (alias: string) => + alias.split(".").map(wrapAlias).join("."); diff --git a/tests/unit/table.test.ts b/tests/unit/table.test.ts index aa6f1f6..b40a72f 100644 --- a/tests/unit/table.test.ts +++ b/tests/unit/table.test.ts @@ -6,4 +6,12 @@ describe("table", () => { table(cols, "t"); expect(1).toBe(1); }); + + it("dot in name", () => { + const cols = ["a", "b"] as const; + const q = table(cols, "system.tables").selectStar(); + expect(q.stringify()).toMatchInlineSnapshot( + `"SELECT * FROM \`system\`.\`tables\`"` + ); + }); });