Skip to content

Commit

Permalink
add execlastid implementation for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
yshrsmz committed Feb 17, 2024
1 parent daaf539 commit 456b2a5
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 8 deletions.
11 changes: 9 additions & 2 deletions examples/authors/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ ORDER BY name;
INSERT INTO authors (
name, bio
) VALUES (
?, ?
?, ?
);

/* name: CreateAuthorReturnId :execlastid */
INSERT INTO authors (
name, bio
) VALUES (
?, ?
);

/* name: DeleteAuthor :exec */
Expand All @@ -19,4 +26,4 @@ WHERE id = ?;

/* name: Test :one */
SELECT * FROM node_mysql_types
LIMIT 1;
LIMIT 1;
24 changes: 22 additions & 2 deletions examples/bun-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mysql, { RowDataPacket } from "mysql2/promise";
import mysql, { RowDataPacket, ResultSetHeader } from "mysql2/promise";

type Client = mysql.Connection | mysql.Pool;

Expand Down Expand Up @@ -62,7 +62,7 @@ export const createAuthorQuery = `-- name: CreateAuthor :exec
INSERT INTO authors (
name, bio
) VALUES (
?, ?
?, ?
)`;

export interface CreateAuthorArgs {
Expand All @@ -77,6 +77,26 @@ export async function createAuthor(client: Client, args: CreateAuthorArgs): Prom
});
}

export const createAuthorReturnIdQuery = `-- name: CreateAuthorReturnId :execlastid
INSERT INTO authors (
name, bio
) VALUES (
?, ?
)`;

export interface CreateAuthorReturnIdArgs {
name: string;
bio: string | null;
}

export async function createAuthorReturnId(client: Client, args: CreateAuthorReturnIdArgs): Promise<number> {
const [result] = await client.query<ResultSetHeader>({
sql: createAuthorReturnIdQuery,
values: [args.name, args.bio]
});
return result?.insertId ?? 0;
}

export const deleteAuthorQuery = `-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = ?`;
Expand Down
24 changes: 22 additions & 2 deletions examples/node-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mysql, { RowDataPacket } from "mysql2/promise";
import mysql, { RowDataPacket, ResultSetHeader } from "mysql2/promise";

type Client = mysql.Connection | mysql.Pool;

Expand Down Expand Up @@ -62,7 +62,7 @@ export const createAuthorQuery = `-- name: CreateAuthor :exec
INSERT INTO authors (
name, bio
) VALUES (
?, ?
?, ?
)`;

export interface CreateAuthorArgs {
Expand All @@ -77,6 +77,26 @@ export async function createAuthor(client: Client, args: CreateAuthorArgs): Prom
});
}

export const createAuthorReturnIdQuery = `-- name: CreateAuthorReturnId :execlastid
INSERT INTO authors (
name, bio
) VALUES (
?, ?
)`;

export interface CreateAuthorReturnIdArgs {
name: string;
bio: string | null;
}

export async function createAuthorReturnId(client: Client, args: CreateAuthorReturnIdArgs): Promise<number> {
const [result] = await client.query<ResultSetHeader>({
sql: createAuthorReturnIdQuery,
values: [args.name, args.bio]
});
return result?.insertId ?? 0;
}

export const deleteAuthorQuery = `-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = ?`;
Expand Down
16 changes: 14 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ interface Driver {
iface: string | undefined,
params: Parameter[]
) => Node;
execlastidDecl: (
name: string,
text: string,
iface: string | undefined,
params: Parameter[]
) => Node;
manyDecl: (
name: string,
text: string,
Expand All @@ -77,10 +83,10 @@ function createNodeGenerator(driver?: string): Driver {
return mysql2;
}
case "pg": {
return pg;
return pg as any;
}
case "postgres": {
return postgres;
return postgres as any;
}
}
throw new Error(`unknown driver: ${driver}`);
Expand Down Expand Up @@ -156,6 +162,12 @@ ${query.text}`
);
break;
}
case ":execlastid": {
nodes.push(
driver.execlastidDecl(lowerName, textName, argIface, query.params)
)
break;
}
case ":one": {
nodes.push(
driver.oneDecl(
Expand Down
109 changes: 109 additions & 0 deletions src/drivers/mysql2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ export function preamble(queries: unknown) {
undefined,
factory.createIdentifier("RowDataPacket")
),
factory.createImportSpecifier(
false,
undefined,
factory.createIdentifier("ResultSetHeader")
),
])
),
factory.createStringLiteral("mysql2/promise"),
Expand Down Expand Up @@ -611,10 +616,114 @@ export function oneDecl(
);
}

export function execlastidDecl(
funcName: string,
queryName: string,
argIface: string | undefined,
params: Parameter[]
) {
const funcParams = funcParamsDecl(argIface, params);

return factory.createFunctionDeclaration(
[
factory.createToken(SyntaxKind.ExportKeyword),
factory.createToken(SyntaxKind.AsyncKeyword),
],
undefined,
factory.createIdentifier(funcName),
undefined,
funcParams,
factory.createTypeReferenceNode(factory.createIdentifier("Promise"), [
factory.createTypeReferenceNode('number', undefined),
]),
factory.createBlock(
[
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createArrayBindingPattern([
factory.createBindingElement(
undefined,
undefined,
factory.createIdentifier("result"),
undefined
),
]),
undefined,
undefined,
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier("client"),
factory.createIdentifier("query")
),
[
factory.createTypeReferenceNode(
factory.createIdentifier("ResultSetHeader"),
undefined
)
],
[
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier("sql"),
factory.createIdentifier(queryName)
),
factory.createPropertyAssignment(
factory.createIdentifier("values"),
factory.createArrayLiteralExpression(
params.map((param, i) =>
factory.createPropertyAccessExpression(
factory.createIdentifier("args"),
factory.createIdentifier(
argName(i, param.column)
)
)
),
false
)
),
],
true
),
]
)
)
)
],
NodeFlags.Const |
// NodeFlags.Constant |
NodeFlags.AwaitContext |
// NodeFlags.Constant |
NodeFlags.ContextFlags |
NodeFlags.TypeExcludesFlags
)
),
factory.createReturnStatement(
factory.createBinaryExpression(
factory.createPropertyAccessChain(
factory.createIdentifier("result"),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier("insertId")
),
factory.createToken(SyntaxKind.QuestionQuestionToken),
factory.createNumericLiteral(0)
)
),
],
true
)
)
}

export default {
columnType,
preamble,
execDecl,
manyDecl,
oneDecl,
execlastidDecl,
};

0 comments on commit 456b2a5

Please sign in to comment.