From cd28dc1b781cea6998176c9c835bbd8f9b64c1e1 Mon Sep 17 00:00:00 2001 From: Vitalii Hrytsiienko Date: Wed, 7 Sep 2022 18:00:22 +0300 Subject: [PATCH] tuple --- README.md | 13 +++++++++++++ src/formatter/index.ts | 18 ++++++++++++++---- src/index.ts | 1 + test/unit/statement.test.ts | 23 ++++++++++++++++++++++- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 28a88cac..c87c1012 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,19 @@ const statement = await connection.execute("select ?, ?", { will produce `select 'foo', 1` query +Format `Tuple`: + +```typescript +import { Tuple } from 'firebolt-sdk' + +const statement = await connection.execute("select ? where bar in ?", { + parameters: [ + 1, + new Tuple(['foo']) + ] +}); +``` + ### QuerySettings diff --git a/src/formatter/index.ts b/src/formatter/index.ts index 6edf7ccd..26261e39 100644 --- a/src/formatter/index.ts +++ b/src/formatter/index.ts @@ -41,6 +41,14 @@ const zeroPad = (param: number, length: number) => { return paded; }; +export class Tuple { + value: unknown[]; + + constructor(value: unknown[]) { + this.value = value; + } +} + export class QueryFormatter { private format(query: string, params: unknown[]) { params = [...params]; @@ -143,8 +151,8 @@ export class QueryFormatter { return "X" + this.escapeString(param.toString("hex")); } - private escapeArray(param: unknown[]) { - let sql = "["; + private escapeArray(param: unknown[], prefix = "[", suffix = "]") { + let sql = prefix; for (let i = 0; i < param.length; i++) { const value = param[i]; @@ -157,7 +165,7 @@ export class QueryFormatter { } } - sql += "]"; + sql += suffix; return sql; } @@ -195,7 +203,9 @@ export class QueryFormatter { } private escapeObject(param: unknown) { - if (BigNumber.isBigNumber(param)) { + if (param instanceof Tuple) { + return this.escapeArray(param.value, "(", ")"); + } else if (BigNumber.isBigNumber(param)) { return param.toString(); } else if (Object.prototype.toString.call(param) === "[object Date]") { return this.escapeDate(param as Date); diff --git a/src/index.ts b/src/index.ts index 720bfa93..73004dac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ export type { export { OutputFormat } from "./types"; export { EngineStatusSummary } from "./service/engine/types"; export { isDateType, isNumberType } from "./statement/dataTypes"; +export { Tuple } from "./formatter"; export type { Connection } from "./connection"; export type { Meta } from "./meta"; diff --git a/test/unit/statement.test.ts b/test/unit/statement.test.ts index 7699770f..eb2f3f93 100644 --- a/test/unit/statement.test.ts +++ b/test/unit/statement.test.ts @@ -1,5 +1,5 @@ import BigNumber from "bignumber.js"; -import { QueryFormatter } from "../../src/formatter"; +import { QueryFormatter, Tuple } from "../../src/formatter"; describe("format query", () => { it("format", () => { @@ -122,4 +122,25 @@ describe("format query", () => { expect(true).toEqual(true); } }); + it("format tuple", () => { + const queryFormatter = new QueryFormatter(); + const query = "select foo from bar where foo in ?"; + const formattedQuery = queryFormatter.formatQuery(query, [ + new Tuple(["some", "other"]) + ]); + expect(formattedQuery).toMatchInlineSnapshot( + `"select foo from bar where foo in ('some', 'other')"` + ); + }); + it("format tuple 2", () => { + const queryFormatter = new QueryFormatter(); + const query = "select foo, bar from baz where foo in ? and bar = ?"; + const formattedQuery = queryFormatter.formatQuery(query, [ + new Tuple(["some", "other"]), + "str" + ]); + expect(formattedQuery).toMatchInlineSnapshot( + `"select foo, bar from baz where foo in ('some', 'other') and bar = 'str'"` + ); + }); });