Skip to content
Closed
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
7 changes: 7 additions & 0 deletions examples/authors/mysql/query.sql
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,13 @@ INSERT INTO authors (
?, ?
);

/* name: CreateAuthors :copyfrom */
INSERT INTO authors (
name, bio
) VALUES (
?, ?
);

/* name: CreateAuthorReturnId :execlastid */
INSERT INTO authors (
name, bio
Expand Down
25 changes: 21 additions & 4 deletions src/app.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,6 +77,12 @@ interface Driver{
params: Parameter[],
columns: Column[]
) => Node;
copyfromDecl: (
name: string,
text: string,
iface: string | undefined,
params: Parameter[]
) => Node;
}

function createNodeGenerator(options: Options): Driver{
Expand DownExpand Up@@ -143,7 +149,8 @@ function codegen(input: GenerateRequest): GenerateResponse{
queryDecl(
textName,
`-- name: ${query.name} ${query.cmd}
${query.text}`
${query.text}`,
query.cmd === ":copyfrom"
)
);

Expand DownExpand Up@@ -197,6 +204,12 @@ ${query.text}`
);
break;
}
case ":copyfrom":{
nodes.push(
driver.copyfromDecl(lowerName, textName, argIface, query.params)
);
break;
}
}
if (nodes){
files.push(
Expand All@@ -220,7 +233,11 @@ function readInput(): GenerateRequest{
return GenerateRequest.fromBinary(buffer);
}

function queryDecl(name: string, sql: string){
function queryDecl(name: string, sql: string, isCopyFrom = false){
const modifiedSql = isCopyFrom
? sql.replace(/\(\s*\?(?:\s*,\s*\?)*\s*\)/g, "?")
: sql;

return factory.createVariableStatement(
[factory.createToken(SyntaxKind.ExportKeyword)],
factory.createVariableDeclarationList(
Expand All@@ -229,10 +246,10 @@ function queryDecl(name: string, sql: string){
factory.createIdentifier(name),
undefined,
undefined,
factory.createNoSubstitutionTemplateLiteral(sql, sql)
factory.createNoSubstitutionTemplateLiteral(modifiedSql, modifiedSql)
),
],
NodeFlags.Const //| NodeFlags.Constant | NodeFlags.Constant
NodeFlags.Const
)
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/drivers/better-sqlite3.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -435,4 +435,15 @@ export class Driver{
"better-sqlite3 driver currently does not support :execlastid"
);
}

copyfromDecl(
funcName: string,
queryName: string,
argIface: string | undefined,
params: Parameter[]
): Node{
throw new Error(
"better-sqlite3 driver currently does not support :copyfrom"
);
}
}
114 changes: 114 additions & 0 deletions src/drivers/mysql2.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -749,4 +749,118 @@ export class Driver{
)
);
}

copyfromDecl(
funcName: string,
queryName: string,
argIface: string | undefined,
params: Parameter[]
){
const funcParams = [
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier("client"),
undefined,
factory.createTypeReferenceNode(
factory.createIdentifier("Client"),
undefined
),
undefined
),
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier("args"),
undefined,
factory.createArrayTypeNode(
argIface
? factory.createTypeReferenceNode(
factory.createIdentifier(argIface),
undefined
)
: factory.createTypeLiteralNode([])
),
undefined
),
];

return factory.createFunctionDeclaration(
[
factory.createToken(SyntaxKind.ExportKeyword),
factory.createToken(SyntaxKind.AsyncKeyword),
],
undefined,
factory.createIdentifier(funcName),
undefined,
funcParams,
factory.createTypeReferenceNode(factory.createIdentifier("Promise"), [
factory.createKeywordTypeNode(SyntaxKind.VoidKeyword),
]),
factory.createBlock(
[
factory.createExpressionStatement(
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier("client"),
factory.createIdentifier("query")
),
undefined,
[
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier("sql"),
factory.createIdentifier(queryName)
),
factory.createPropertyAssignment(
factory.createIdentifier("values"),
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier("args"),
factory.createIdentifier("map")
),
undefined,
[
factory.createArrowFunction(
undefined,
undefined,
[
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier("arg"),
undefined,
undefined,
undefined
),
],
undefined,
factory.createToken(SyntaxKind.EqualsGreaterThanToken),
factory.createArrayLiteralExpression(
params.map((param, i) =>
factory.createPropertyAccessExpression(
factory.createIdentifier("arg"),
factory.createIdentifier(argName(i, param.column))
)
),
false
)
),
]
)
),
],
true
),
]
)
)
),
],
true
)
);
}
}
9 changes: 9 additions & 0 deletions src/drivers/pg.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -794,4 +794,13 @@ export class Driver{
): FunctionDeclaration{
throw new Error("pg driver currently does not support :execlastid");
}

copyfromDecl(
funcName: string,
queryName: string,
argIface: string | undefined,
params: Parameter[]
): Node{
throw new Error("pg driver currently does not support :copyfrom");
}
}
10 changes: 10 additions & 0 deletions src/drivers/postgres.ts
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
import{
SyntaxKind,
NodeFlags,
Node,
TypeNode,
factory,
FunctionDeclaration,
Expand DownExpand Up@@ -619,4 +620,13 @@ export class Driver{
): FunctionDeclaration{
throw new Error("postgres driver currently does not support :execlastid");
}

copyfromDecl(
funcName: string,
queryName: string,
argIface: string | undefined,
params: Parameter[]
): Node{
throw new Error("postgres driver currently does not support :copyfrom");
}
}