diff --git a/examples/authors/mysql/query.sql b/examples/authors/mysql/query.sql index 94c51b9..7357f3a 100644 --- a/examples/authors/mysql/query.sql +++ b/examples/authors/mysql/query.sql @@ -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; diff --git a/examples/authors/mysql/schema.sql b/examples/authors/mysql/schema.sql index 3004aa5..67dfb22 100644 --- a/examples/authors/mysql/schema.sql +++ b/examples/authors/mysql/schema.sql @@ -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 +); diff --git a/examples/bun-mysql2/src/db/query_sql.ts b/examples/bun-mysql2/src/db/query_sql.ts index 1673007..4bf4c1e 100644 --- a/examples/bun-mysql2/src/db/query_sql.ts +++ b/examples/bun-mysql2/src/db/query_sql.ts @@ -209,3 +209,27 @@ export async function test(client: Client): Promise { }; } +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 { + const [rows] = await client.query({ + sql: getReservedWordsQuery, + values: [], + rowsAsArray: true + }); + return rows.map(row => { + return { + id: row[0], + key: row[1], + value: row[2] + }; + }); +} + diff --git a/examples/node-mysql2/src/db/query_sql.ts b/examples/node-mysql2/src/db/query_sql.ts index c79988f..0116369 100644 --- a/examples/node-mysql2/src/db/query_sql.ts +++ b/examples/node-mysql2/src/db/query_sql.ts @@ -209,3 +209,27 @@ export async function test(client: Client): Promise { }; } +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 { + const [rows] = await client.query({ + sql: getReservedWordsQuery, + values: [], + rowsAsArray: true + }); + return rows.map(row => { + return { + id: row[0], + key: row[1], + value: row[2] + }; + }); +} + diff --git a/src/app.ts b/src/app.ts index e3a077c..b6dd47e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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( @@ -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