Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
]
});
```

<a id="querysettings"></a>
### QuerySettings

Expand Down
18 changes: 14 additions & 4 deletions src/formatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -157,7 +165,7 @@ export class QueryFormatter {
}
}

sql += "]";
sql += suffix;

return sql;
}
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
23 changes: 22 additions & 1 deletion test/unit/statement.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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'"`
);
});
});