Skip to content

Commit 1181d34

Browse files
authored
Merge pull request #26 from reload/backquotes
fix: escape backquotes in a query #minor
2 parents 692ebb5 + ed381b7 commit 1181d34

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

examples/authors/mysql/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ WHERE id = ?;
2727
/* name: Test :one */
2828
SELECT * FROM node_mysql_types
2929
LIMIT 1;
30+
31+
/* name: GetReservedWords :many */
32+
SELECT `id`, `key`, `value` FROM reserved_words;

examples/authors/mysql/schema.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ CREATE TABLE node_mysql_types (
5050

5151
c_json JSON
5252
);
53+
54+
/* https://dev.mysql.com/doc/refman/8.4/en/keywords.html#keywords-8-4-detailed-I */
55+
CREATE TABLE reserved_words (
56+
`id` BIGINT PRIMARY KEY AUTO_INCREMENT,
57+
`key` TEXT,
58+
`value` TEXT
59+
);

examples/bun-mysql2/src/db/query_sql.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,27 @@ export async function test(client: Client): Promise<TestRow | null> {
215215
};
216216
}
217217

218+
export const getReservedWordsQuery = `-- name: GetReservedWords :many
219+
SELECT \`id\`, \`key\`, \`value\` FROM reserved_words`;
220+
221+
export interface GetReservedWordsRow {
222+
id: number;
223+
key: string | null;
224+
value: string | null;
225+
}
226+
227+
export async function getReservedWords(client: Client): Promise<GetReservedWordsRow[]> {
228+
const [rows] = await client.query<RowDataPacket[]>({
229+
sql: getReservedWordsQuery,
230+
values: [],
231+
rowsAsArray: true
232+
});
233+
return rows.map(row => {
234+
return {
235+
id: row[0],
236+
key: row[1],
237+
value: row[2]
238+
};
239+
});
240+
}
241+

examples/node-mysql2/src/db/query_sql.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,27 @@ export async function test(client: Client): Promise<TestRow | null> {
215215
};
216216
}
217217

218+
export const getReservedWordsQuery = `-- name: GetReservedWords :many
219+
SELECT \`id\`, \`key\`, \`value\` FROM reserved_words`;
220+
221+
export interface GetReservedWordsRow {
222+
id: string;
223+
key: string | null;
224+
value: string | null;
225+
}
226+
227+
export async function getReservedWords(client: Client): Promise<GetReservedWordsRow[]> {
228+
const [rows] = await client.query<RowDataPacket[]>({
229+
sql: getReservedWordsQuery,
230+
values: [],
231+
rowsAsArray: true
232+
});
233+
return rows.map(row => {
234+
return {
235+
id: row[0],
236+
key: row[1],
237+
value: row[2]
238+
};
239+
});
240+
}
241+

src/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ function readInput(): GenerateRequest {
225225
}
226226

227227
function queryDecl(name: string, sql: string) {
228+
const escaped = sql.replace(/`/g, '\\`');
229+
228230
return factory.createVariableStatement(
229231
[factory.createToken(SyntaxKind.ExportKeyword)],
230232
factory.createVariableDeclarationList(
@@ -233,7 +235,7 @@ function queryDecl(name: string, sql: string) {
233235
factory.createIdentifier(name),
234236
undefined,
235237
undefined,
236-
factory.createNoSubstitutionTemplateLiteral(sql, sql)
238+
factory.createNoSubstitutionTemplateLiteral(escaped, escaped)
237239
),
238240
],
239241
NodeFlags.Const //| NodeFlags.Constant | NodeFlags.Constant

0 commit comments

Comments
 (0)