Skip to content

Commit

Permalink
fix(pg): Remove unused import (#2)
Browse files Browse the repository at this point in the history
* fix(pg): Remove unused import

* Fix build
  • Loading branch information
kyleconroy committed Dec 7, 2023
1 parent ddce067 commit efa6671
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/bun-pg/src/db/query_sql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { QueryArrayConfig, QueryArrayResult } from "pg";

import { IPostgresInterval } from "postgres-interval";

interface Client {
query: (config: QueryArrayConfig) => Promise<QueryArrayResult>;
}
Expand Down
2 changes: 0 additions & 2 deletions examples/node-pg/src/db/query_sql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { QueryArrayConfig, QueryArrayResult } from "pg";

import { IPostgresInterval } from "postgres-interval";

interface Client {
query: (config: QueryArrayConfig) => Promise<QueryArrayResult>;
}
Expand Down
60 changes: 60 additions & 0 deletions examples/sqlc.dev.yaml
Original file line number Diff line number Diff line change
@@ -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
162 changes: 80 additions & 82 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Parameter,
Column,
File,
Query,
} from "./gen/plugin/codegen_pb";

import { argName, colName } from "./drivers/utlis";
Expand All @@ -44,7 +45,7 @@ interface Options {
}

interface Driver {
preamble: () => Node[];
preamble: (queries: Query[]) => Node[];
columnType: (c?: Column) => TypeNode;
execDecl: (
name: string,
Expand Down Expand Up @@ -98,104 +99,101 @@ function codegen(input: GenerateRequest): GenerateResponse {

// TODO: Verify options, parse them from protobuf honestly

const querymap = new Map<string, Node[]>();

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<string, Query[]>();

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<string, number>();
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<string, number>();
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,
});
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/mysql2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function columnType(column?: Column): TypeNode {
]);
}

export function preamble() {
export function preamble(queries: unknown) {
return [
factory.createImportDeclaration(
undefined,
Expand Down
58 changes: 37 additions & 21 deletions src/drivers/pg.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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"),
Expand Down Expand Up @@ -363,8 +377,10 @@ export function preamble() {
)
),
]
),
];
)
);

return imports;
}

function funcParamsDecl(iface: string | undefined, params: Parameter[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export function columnType(column?: Column): TypeNode {
]);
}

export function preamble() {
export function preamble(queries: unknown) {
return [
factory.createImportDeclaration(
undefined,
Expand Down

0 comments on commit efa6671

Please sign in to comment.