Skip to content

Commit

Permalink
fix it (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasavila00 authored Jul 1, 2022
1 parent cb9f12e commit f52c160
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -82,10 +82,10 @@ const printTableInternal = <Selection extends string, Alias extends string>(
): 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}`,
};
Expand Down
4 changes: 2 additions & 2 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { castSafe } from "./safe-string";
import { wrapAlias } from "./wrap-alias";
import { wrapAliasSplitDots } from "./wrap-alias";

export const proxy = new Proxy(
{
SQL_PROXY_TARGET: true,
},
{
get: function (_target, prop, _receiver) {
return castSafe(String(prop).split(".").map(wrapAlias).join("."));
return castSafe(wrapAliasSplitDots(String(prop)));
},
}
) as any;
Expand Down
3 changes: 3 additions & 0 deletions src/wrap-alias.ts
Original file line number Diff line number Diff line change
@@ -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(".");
8 changes: 8 additions & 0 deletions tests/unit/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\`"`
);
});
});

0 comments on commit f52c160

Please sign in to comment.