Skip to content

Commit cd28dc1

Browse files
committed
tuple
1 parent 62e6a97 commit cd28dc1

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ const statement = await connection.execute("select ?, ?", {
197197

198198
will produce `select 'foo', 1` query
199199

200+
Format `Tuple`:
201+
202+
```typescript
203+
import { Tuple } from 'firebolt-sdk'
204+
205+
const statement = await connection.execute("select ? where bar in ?", {
206+
parameters: [
207+
1,
208+
new Tuple(['foo'])
209+
]
210+
});
211+
```
212+
200213
<a id="querysettings"></a>
201214
### QuerySettings
202215

src/formatter/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ const zeroPad = (param: number, length: number) => {
4141
return paded;
4242
};
4343

44+
export class Tuple {
45+
value: unknown[];
46+
47+
constructor(value: unknown[]) {
48+
this.value = value;
49+
}
50+
}
51+
4452
export class QueryFormatter {
4553
private format(query: string, params: unknown[]) {
4654
params = [...params];
@@ -143,8 +151,8 @@ export class QueryFormatter {
143151
return "X" + this.escapeString(param.toString("hex"));
144152
}
145153

146-
private escapeArray(param: unknown[]) {
147-
let sql = "[";
154+
private escapeArray(param: unknown[], prefix = "[", suffix = "]") {
155+
let sql = prefix;
148156

149157
for (let i = 0; i < param.length; i++) {
150158
const value = param[i];
@@ -157,7 +165,7 @@ export class QueryFormatter {
157165
}
158166
}
159167

160-
sql += "]";
168+
sql += suffix;
161169

162170
return sql;
163171
}
@@ -195,7 +203,9 @@ export class QueryFormatter {
195203
}
196204

197205
private escapeObject(param: unknown) {
198-
if (BigNumber.isBigNumber(param)) {
206+
if (param instanceof Tuple) {
207+
return this.escapeArray(param.value, "(", ")");
208+
} else if (BigNumber.isBigNumber(param)) {
199209
return param.toString();
200210
} else if (Object.prototype.toString.call(param) === "[object Date]") {
201211
return this.escapeDate(param as Date);

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type {
3030
export { OutputFormat } from "./types";
3131
export { EngineStatusSummary } from "./service/engine/types";
3232
export { isDateType, isNumberType } from "./statement/dataTypes";
33+
export { Tuple } from "./formatter";
3334

3435
export type { Connection } from "./connection";
3536
export type { Meta } from "./meta";

test/unit/statement.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import BigNumber from "bignumber.js";
2-
import { QueryFormatter } from "../../src/formatter";
2+
import { QueryFormatter, Tuple } from "../../src/formatter";
33

44
describe("format query", () => {
55
it("format", () => {
@@ -122,4 +122,25 @@ describe("format query", () => {
122122
expect(true).toEqual(true);
123123
}
124124
});
125+
it("format tuple", () => {
126+
const queryFormatter = new QueryFormatter();
127+
const query = "select foo from bar where foo in ?";
128+
const formattedQuery = queryFormatter.formatQuery(query, [
129+
new Tuple(["some", "other"])
130+
]);
131+
expect(formattedQuery).toMatchInlineSnapshot(
132+
`"select foo from bar where foo in ('some', 'other')"`
133+
);
134+
});
135+
it("format tuple 2", () => {
136+
const queryFormatter = new QueryFormatter();
137+
const query = "select foo, bar from baz where foo in ? and bar = ?";
138+
const formattedQuery = queryFormatter.formatQuery(query, [
139+
new Tuple(["some", "other"]),
140+
"str"
141+
]);
142+
expect(formattedQuery).toMatchInlineSnapshot(
143+
`"select foo, bar from baz where foo in ('some', 'other') and bar = 'str'"`
144+
);
145+
});
125146
});

0 commit comments

Comments
 (0)