From b248da246da85a27967ac0e6cf5c9940d9183fe0 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Thu, 7 Dec 2023 10:56:04 -0800 Subject: [PATCH 1/2] fix(pg): Remove unused import --- Makefile | 4 +- examples/bun-pg/src/db/query_sql.ts | 2 - examples/node-pg/src/db/query_sql.ts | 2 - examples/sqlc.dev.yaml | 60 ++++++++++ src/app.ts | 162 +++++++++++++-------------- src/drivers/mysql2.ts | 2 +- src/drivers/pg.ts | 58 ++++++---- src/drivers/postgres.ts | 2 +- 8 files changed, 181 insertions(+), 111 deletions(-) create mode 100644 examples/sqlc.dev.yaml diff --git a/Makefile b/Makefile index 5d07c74..45c89dc 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: generate -generate: examples/plugin.wasm examples/sqlc.yaml - cd examples && sqlc-dev generate +generate: examples/plugin.wasm examples/sqlc.dev.yaml + cd examples && sqlc-dev -f sqlc.dev.yaml generate # https://github.com/bytecodealliance/javy/releases/tag/v1.2.0 examples/plugin.wasm: out.js diff --git a/examples/bun-pg/src/db/query_sql.ts b/examples/bun-pg/src/db/query_sql.ts index d03cf27..ffae8a9 100644 --- a/examples/bun-pg/src/db/query_sql.ts +++ b/examples/bun-pg/src/db/query_sql.ts @@ -1,7 +1,5 @@ import { QueryArrayConfig, QueryArrayResult } from "pg"; -import { IPostgresInterval } from "postgres-interval"; - interface Client { query: (config: QueryArrayConfig) => Promise; } diff --git a/examples/node-pg/src/db/query_sql.ts b/examples/node-pg/src/db/query_sql.ts index d03cf27..ffae8a9 100644 --- a/examples/node-pg/src/db/query_sql.ts +++ b/examples/node-pg/src/db/query_sql.ts @@ -1,7 +1,5 @@ import { QueryArrayConfig, QueryArrayResult } from "pg"; -import { IPostgresInterval } from "postgres-interval"; - interface Client { query: (config: QueryArrayConfig) => Promise; } diff --git a/examples/sqlc.dev.yaml b/examples/sqlc.dev.yaml new file mode 100644 index 0000000..df52a0a --- /dev/null +++ b/examples/sqlc.dev.yaml @@ -0,0 +1,60 @@ +version: "2" +plugins: +- name: ts + wasm: + url: file://plugin.wasm +sql: +- schema: "authors/postgresql/schema.sql" + queries: "authors/postgresql/query.sql" + engine: "postgresql" + codegen: + - plugin: ts + out: node-pg/src/db + options: + runtime: node + driver: pg +- schema: "authors/postgresql/schema.sql" + queries: "authors/postgresql/query.sql" + engine: "postgresql" + codegen: + - plugin: ts + out: bun-pg/src/db + options: + runtime: bun + driver: pg +- schema: "authors/postgresql/schema.sql" + queries: "authors/postgresql/query.sql" + engine: "postgresql" + codegen: + - plugin: ts + out: node-postgres/src/db + options: + runtime: node + driver: postgres +- schema: "authors/postgresql/schema.sql" + queries: "authors/postgresql/query.sql" + engine: "postgresql" + codegen: + - plugin: ts + out: bun-postgres/src/db + options: + runtime: bun + driver: postgres +- schema: "authors/mysql/schema.sql" + queries: "authors/mysql/query.sql" + engine: "mysql" + codegen: + - plugin: ts + out: node-mysql2/src/db + options: + runtime: node + driver: mysql2 +- schema: "authors/mysql/schema.sql" + queries: "authors/mysql/query.sql" + engine: "mysql" + codegen: + - plugin: ts + out: bun-mysql2/src/db + options: + runtime: bun + driver: mysql2 \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 2545943..4006c39 100644 --- a/src/app.ts +++ b/src/app.ts @@ -24,6 +24,7 @@ import { Parameter, Column, File, + Query, } from "./gen/plugin/codegen_pb"; import { argName, colName } from "./drivers/utlis"; @@ -44,7 +45,7 @@ interface Options { } interface Driver { - preamble: () => Node[]; + preamble: (queries: Query[]) => Node[]; columnType: (c?: Column) => TypeNode; execDecl: ( name: string, @@ -98,104 +99,101 @@ function codegen(input: GenerateRequest): GenerateResponse { // TODO: Verify options, parse them from protobuf honestly - const querymap = new Map(); - - const filenames = new Set(input.queries.map((q) => q.filename)); - for (const filename of filenames) { - const nodes = driver.preamble(); - querymap.set(filename, nodes); - } + const querymap = new Map(); for (const query of input.queries) { - let nodes = querymap.get(query.filename); - if (!nodes) { - continue; + if (!querymap.has(query.filename)) { + querymap.set(query.filename, []); } + const qs = querymap.get(query.filename); + qs?.push(query); + } - const colmap = new Map(); - for (let column of query.columns) { - if (!column.name) { - continue; - } - const count = colmap.get(column.name) || 0; - if (count > 0) { - column.name = `${column.name}_${count + 1}`; + for (const [filename, queries] of querymap.entries()) { + const nodes = driver.preamble(queries); + + for (const query of queries) { + const colmap = new Map(); + for (let column of query.columns) { + if (!column.name) { + continue; + } + const count = colmap.get(column.name) || 0; + if (count > 0) { + column.name = `${column.name}_${count + 1}`; + } + colmap.set(column.name, count + 1); } - colmap.set(column.name, count + 1); - } - const lowerName = query.name[0].toLowerCase() + query.name.slice(1); - const textName = `${lowerName}Query`; + const lowerName = query.name[0].toLowerCase() + query.name.slice(1); + const textName = `${lowerName}Query`; - nodes.push( - queryDecl( - textName, - `-- name: ${query.name} ${query.cmd} + nodes.push( + queryDecl( + textName, + `-- name: ${query.name} ${query.cmd} ${query.text}` - ) - ); + ) + ); - const ctype = driver.columnType; + const ctype = driver.columnType; - let argIface = undefined; - let returnIface = undefined; - if (query.params.length > 0) { - argIface = `${query.name}Args`; - nodes.push(argsDecl(argIface, ctype, query.params)); - } - if (query.columns.length > 0) { - returnIface = `${query.name}Row`; - nodes.push(rowDecl(returnIface, ctype, query.columns)); - } - - switch (query.cmd) { - case ":exec": { - nodes.push( - driver.execDecl(lowerName, textName, argIface, query.params) - ); - break; + let argIface = undefined; + let returnIface = undefined; + if (query.params.length > 0) { + argIface = `${query.name}Args`; + nodes.push(argsDecl(argIface, ctype, query.params)); } - case ":one": { - nodes.push( - driver.oneDecl( - lowerName, - textName, - argIface, - returnIface ?? "void", - query.params, - query.columns - ) - ); - break; + if (query.columns.length > 0) { + returnIface = `${query.name}Row`; + nodes.push(rowDecl(returnIface, ctype, query.columns)); + } + + switch (query.cmd) { + case ":exec": { + nodes.push( + driver.execDecl(lowerName, textName, argIface, query.params) + ); + break; + } + case ":one": { + nodes.push( + driver.oneDecl( + lowerName, + textName, + argIface, + returnIface ?? "void", + query.params, + query.columns + ) + ); + break; + } + case ":many": { + nodes.push( + driver.manyDecl( + lowerName, + textName, + argIface, + returnIface ?? "void", + query.params, + query.columns + ) + ); + break; + } } - case ":many": { - nodes.push( - driver.manyDecl( - lowerName, - textName, - argIface, - returnIface ?? "void", - query.params, - query.columns - ) + if (nodes) { + files.push( + new File({ + name: `${filename.replace(".", "_")}.ts`, + contents: new TextEncoder().encode(printNode(nodes)), + }) ); - break; } } } - for (const filename of filenames) { - const nodes = querymap.get(filename); - if (nodes) { - files.push( - new File({ - name: `${filename.replace(".", "_")}.ts`, - contents: new TextEncoder().encode(printNode(nodes)), - }) - ); - } - } - return new GenerateResponse({ files: files, }); diff --git a/src/drivers/mysql2.ts b/src/drivers/mysql2.ts index cc5a978..d6f943b 100644 --- a/src/drivers/mysql2.ts +++ b/src/drivers/mysql2.ts @@ -165,7 +165,7 @@ export function columnType(column?: Column): TypeNode { ]); } -export function preamble() { +export function preamble(queries: unknown) { return [ factory.createImportDeclaration( undefined, diff --git a/src/drivers/pg.ts b/src/drivers/pg.ts index 8c3d03a..508260a 100644 --- a/src/drivers/pg.ts +++ b/src/drivers/pg.ts @@ -1,6 +1,6 @@ -import { SyntaxKind, NodeFlags, TypeNode, factory } from "typescript"; +import { SyntaxKind, NodeFlags, Node, TypeNode, factory } from "typescript"; -import { Parameter, Column } from "../gen/plugin/codegen_pb"; +import { Parameter, Column, Query } from "../gen/plugin/codegen_pb"; import { argName, colName } from "./utlis"; export function columnType(column?: Column): TypeNode { @@ -287,8 +287,8 @@ export function columnType(column?: Column): TypeNode { ]); } -export function preamble() { - return [ +export function preamble(queries: Query[]) { + const imports: Node[] = [ factory.createImportDeclaration( undefined, factory.createImportClause( @@ -310,22 +310,36 @@ export function preamble() { factory.createStringLiteral("pg"), undefined ), - factory.createImportDeclaration( - undefined, - factory.createImportClause( - false, + ]; + + const hasInterval = queries.some( + (query) => + query.params.some((p) => p.column?.type?.name === "interval") || + query.columns.some((c) => c.type?.name === "interval") + ); + + if (hasInterval) { + imports.push( + factory.createImportDeclaration( undefined, - factory.createNamedImports([ - factory.createImportSpecifier( - false, - undefined, - factory.createIdentifier("IPostgresInterval") - ), - ]) - ), - factory.createStringLiteral("postgres-interval"), - undefined - ), + factory.createImportClause( + false, + undefined, + factory.createNamedImports([ + factory.createImportSpecifier( + false, + undefined, + factory.createIdentifier("IPostgresInterval") + ), + ]) + ), + factory.createStringLiteral("postgres-interval"), + undefined + ) + ); + } + + imports.push( factory.createInterfaceDeclaration( undefined, factory.createIdentifier("Client"), @@ -363,8 +377,10 @@ export function preamble() { ) ), ] - ), - ]; + ) + ); + + return imports; } function funcParamsDecl(iface: string | undefined, params: Parameter[]) { diff --git a/src/drivers/postgres.ts b/src/drivers/postgres.ts index 92b422e..6a273cd 100644 --- a/src/drivers/postgres.ts +++ b/src/drivers/postgres.ts @@ -246,7 +246,7 @@ export function columnType(column?: Column): TypeNode { ]); } -export function preamble() { +export function preamble(queries: unknown) { return [ factory.createImportDeclaration( undefined, From 912e1e0bf4fb60569ce25eea4be36918086c5b68 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Thu, 7 Dec 2023 10:58:21 -0800 Subject: [PATCH 2/2] Fix build --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce3e9b5..2781563 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,5 +16,5 @@ jobs: - run: npx tsc --noEmit - run: npx esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js - run: ./javy-x86_64-linux-v1.2.0 compile out.js -o examples/plugin.wasm - - run: sqlc diff + - run: sqlc -f sqlc.dev.yaml diff working-directory: examples \ No newline at end of file