Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape backquotes in a query #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions examples/authors/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ WHERE id = ?;
/* name: Test :one */
SELECT * FROM node_mysql_types
LIMIT 1;

/* name: GetReservedWords :many */
SELECT `id`, `key`, `value` FROM reserved_words;
7 changes: 7 additions & 0 deletions examples/authors/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ CREATE TABLE node_mysql_types (

c_json JSON
);

/* https://dev.mysql.com/doc/refman/8.4/en/keywords.html#keywords-8-4-detailed-I */
CREATE TABLE reserved_words (
`id` BIGINT PRIMARY KEY AUTO_INCREMENT,
`key` TEXT,
`value` TEXT
);
24 changes: 24 additions & 0 deletions examples/bun-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,27 @@ export async function test(client: Client): Promise<TestRow | null> {
};
}

export const getReservedWordsQuery = `-- name: GetReservedWords :many
SELECT \`id\`, \`key\`, \`value\` FROM reserved_words`;

export interface GetReservedWordsRow {
id: number;
key: string | null;
value: string | null;
}

export async function getReservedWords(client: Client): Promise<GetReservedWordsRow[]> {
const [rows] = await client.query<RowDataPacket[]>({
sql: getReservedWordsQuery,
values: [],
rowsAsArray: true
});
return rows.map(row => {
return {
id: row[0],
key: row[1],
value: row[2]
};
});
}

24 changes: 24 additions & 0 deletions examples/node-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,27 @@ export async function test(client: Client): Promise<TestRow | null> {
};
}

export const getReservedWordsQuery = `-- name: GetReservedWords :many
SELECT \`id\`, \`key\`, \`value\` FROM reserved_words`;

export interface GetReservedWordsRow {
id: string;
key: string | null;
value: string | null;
}

export async function getReservedWords(client: Client): Promise<GetReservedWordsRow[]> {
const [rows] = await client.query<RowDataPacket[]>({
sql: getReservedWordsQuery,
values: [],
rowsAsArray: true
});
return rows.map(row => {
return {
id: row[0],
key: row[1],
value: row[2]
};
});
}

4 changes: 3 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ function readInput(): GenerateRequest {
}

function queryDecl(name: string, sql: string) {
const escaped = sql.replace(/`/g, '\\`');

return factory.createVariableStatement(
[factory.createToken(SyntaxKind.ExportKeyword)],
factory.createVariableDeclarationList(
Expand All @@ -229,7 +231,7 @@ function queryDecl(name: string, sql: string) {
factory.createIdentifier(name),
undefined,
undefined,
factory.createNoSubstitutionTemplateLiteral(sql, sql)
factory.createNoSubstitutionTemplateLiteral(escaped, escaped)
),
],
NodeFlags.Const //| NodeFlags.Constant | NodeFlags.Constant
Expand Down