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

generated code is not usable if a query contains backquotes #40

Open
yshrsmz opened this issue Sep 26, 2024 · 1 comment · May be fixed by #41
Open

generated code is not usable if a query contains backquotes #40

yshrsmz opened this issue Sep 26, 2024 · 1 comment · May be fixed by #41

Comments

@yshrsmz
Copy link
Contributor

yshrsmz commented Sep 26, 2024

problem

Say I have MySQL database and users table below

CREATE TABLE users (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
  `comment` varchar(255) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
);

And this query.

-- name: FindAllUsers :many
SELECT
  `id`,
  `comment`
FROM
  users;

I wrap columns with backquotes as some are reserved words in MySQL (comment in this case).

And when I run sqlc, it generates the code below.

import mysql, { RowDataPacket } from "mysql2/promise";

type Client = mysql.Connection | mysql.Pool;

export const findAllUsersQuery = `-- name: FindAllUsers :many
SELECT
  `id`,
  `comment`
FROM
  users`;

export interface FindAllUsersRow {
    id: number;
    comment: string;
}

export async function findAllUsers(client: Client): Promise<FindAllUsersRow[]> {
    const [rows] = await client.query<RowDataPacket[]>({
        sql: findAllUsersQuery,
        values: [],
        rowsAsArray: true
    });
    return rows.map(row => {
        return {
            id: row[0],
            comment: row[1]
        };
    });
}

Apparently, it is broken as findAllUsersQuery contains unescaped backquotes, breaking its template literal.

solution

Escape the backquotes?

nodes.push(
queryDecl(
textName,
`-- name: ${query.name} ${query.cmd}
${query.text}`
)
);

Changing these lines could solve the problem, but these lines are common for all drivers, so I'm wondering if we can just escape it or we should abstract it.

@yshrsmz yshrsmz changed the title generated code is not usable if a query includes backquotes generated code is not usable if a query contains backquotes Sep 26, 2024
@yshrsmz yshrsmz linked a pull request Sep 27, 2024 that will close this issue
@yshrsmz
Copy link
Contributor Author

yshrsmz commented Sep 27, 2024

Changing these lines could solve the problem, but these lines are common for all drivers, so I'm wondering if we can just escape it or we should abstract it.

Backquotes in a query break the code anyway, so I think it's safe to include the fix in app.ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant