Skip to content

Commit 10b31b2

Browse files
committed
fix: escape backquotes in a query
1 parent 54496e9 commit 10b31b2

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
@@ -209,3 +209,27 @@ export async function test(client: Client): Promise<TestRow | null> {
209209
};
210210
}
211211

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

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

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

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

src/app.ts

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

223223
function queryDecl(name: string, sql: string) {
224+
const escaped = sql.replace(/`/g, '\\`');
225+
224226
return factory.createVariableStatement(
225227
[factory.createToken(SyntaxKind.ExportKeyword)],
226228
factory.createVariableDeclarationList(
@@ -229,7 +231,7 @@ function queryDecl(name: string, sql: string) {
229231
factory.createIdentifier(name),
230232
undefined,
231233
undefined,
232-
factory.createNoSubstitutionTemplateLiteral(sql, sql)
234+
factory.createNoSubstitutionTemplateLiteral(escaped, escaped)
233235
),
234236
],
235237
NodeFlags.Const //| NodeFlags.Constant | NodeFlags.Constant

0 commit comments

Comments
 (0)