Skip to content

Commit efa6671

Browse files
authored
fix(pg): Remove unused import (#2)
* fix(pg): Remove unused import * Fix build
1 parent ddce067 commit efa6671

File tree

9 files changed

+182
-112
lines changed

9 files changed

+182
-112
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ jobs:
1616
- run: npx tsc --noEmit
1717
- run: npx esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js
1818
- run: ./javy-x86_64-linux-v1.2.0 compile out.js -o examples/plugin.wasm
19-
- run: sqlc diff
19+
- run: sqlc -f sqlc.dev.yaml diff
2020
working-directory: examples

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.PHONY: generate
22

3-
generate: examples/plugin.wasm examples/sqlc.yaml
4-
cd examples && sqlc-dev generate
3+
generate: examples/plugin.wasm examples/sqlc.dev.yaml
4+
cd examples && sqlc-dev -f sqlc.dev.yaml generate
55

66
# https://github.com/bytecodealliance/javy/releases/tag/v1.2.0
77
examples/plugin.wasm: out.js

examples/bun-pg/src/db/query_sql.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { QueryArrayConfig, QueryArrayResult } from "pg";
22

3-
import { IPostgresInterval } from "postgres-interval";
4-
53
interface Client {
64
query: (config: QueryArrayConfig) => Promise<QueryArrayResult>;
75
}

examples/node-pg/src/db/query_sql.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { QueryArrayConfig, QueryArrayResult } from "pg";
22

3-
import { IPostgresInterval } from "postgres-interval";
4-
53
interface Client {
64
query: (config: QueryArrayConfig) => Promise<QueryArrayResult>;
75
}

examples/sqlc.dev.yaml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
version: "2"
2+
plugins:
3+
- name: ts
4+
wasm:
5+
url: file://plugin.wasm
6+
sql:
7+
- schema: "authors/postgresql/schema.sql"
8+
queries: "authors/postgresql/query.sql"
9+
engine: "postgresql"
10+
codegen:
11+
- plugin: ts
12+
out: node-pg/src/db
13+
options:
14+
runtime: node
15+
driver: pg
16+
- schema: "authors/postgresql/schema.sql"
17+
queries: "authors/postgresql/query.sql"
18+
engine: "postgresql"
19+
codegen:
20+
- plugin: ts
21+
out: bun-pg/src/db
22+
options:
23+
runtime: bun
24+
driver: pg
25+
- schema: "authors/postgresql/schema.sql"
26+
queries: "authors/postgresql/query.sql"
27+
engine: "postgresql"
28+
codegen:
29+
- plugin: ts
30+
out: node-postgres/src/db
31+
options:
32+
runtime: node
33+
driver: postgres
34+
- schema: "authors/postgresql/schema.sql"
35+
queries: "authors/postgresql/query.sql"
36+
engine: "postgresql"
37+
codegen:
38+
- plugin: ts
39+
out: bun-postgres/src/db
40+
options:
41+
runtime: bun
42+
driver: postgres
43+
- schema: "authors/mysql/schema.sql"
44+
queries: "authors/mysql/query.sql"
45+
engine: "mysql"
46+
codegen:
47+
- plugin: ts
48+
out: node-mysql2/src/db
49+
options:
50+
runtime: node
51+
driver: mysql2
52+
- schema: "authors/mysql/schema.sql"
53+
queries: "authors/mysql/query.sql"
54+
engine: "mysql"
55+
codegen:
56+
- plugin: ts
57+
out: bun-mysql2/src/db
58+
options:
59+
runtime: bun
60+
driver: mysql2

src/app.ts

+80-82
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Parameter,
2525
Column,
2626
File,
27+
Query,
2728
} from "./gen/plugin/codegen_pb";
2829

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

4647
interface Driver {
47-
preamble: () => Node[];
48+
preamble: (queries: Query[]) => Node[];
4849
columnType: (c?: Column) => TypeNode;
4950
execDecl: (
5051
name: string,
@@ -98,104 +99,101 @@ function codegen(input: GenerateRequest): GenerateResponse {
9899

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

101-
const querymap = new Map<string, Node[]>();
102-
103-
const filenames = new Set(input.queries.map((q) => q.filename));
104-
for (const filename of filenames) {
105-
const nodes = driver.preamble();
106-
querymap.set(filename, nodes);
107-
}
102+
const querymap = new Map<string, Query[]>();
108103

109104
for (const query of input.queries) {
110-
let nodes = querymap.get(query.filename);
111-
if (!nodes) {
112-
continue;
105+
if (!querymap.has(query.filename)) {
106+
querymap.set(query.filename, []);
113107
}
108+
const qs = querymap.get(query.filename);
109+
qs?.push(query);
110+
}
114111

115-
const colmap = new Map<string, number>();
116-
for (let column of query.columns) {
117-
if (!column.name) {
118-
continue;
119-
}
120-
const count = colmap.get(column.name) || 0;
121-
if (count > 0) {
122-
column.name = `${column.name}_${count + 1}`;
112+
for (const [filename, queries] of querymap.entries()) {
113+
const nodes = driver.preamble(queries);
114+
115+
for (const query of queries) {
116+
const colmap = new Map<string, number>();
117+
for (let column of query.columns) {
118+
if (!column.name) {
119+
continue;
120+
}
121+
const count = colmap.get(column.name) || 0;
122+
if (count > 0) {
123+
column.name = `${column.name}_${count + 1}`;
124+
}
125+
colmap.set(column.name, count + 1);
123126
}
124-
colmap.set(column.name, count + 1);
125-
}
126127

127-
const lowerName = query.name[0].toLowerCase() + query.name.slice(1);
128-
const textName = `${lowerName}Query`;
128+
const lowerName = query.name[0].toLowerCase() + query.name.slice(1);
129+
const textName = `${lowerName}Query`;
129130

130-
nodes.push(
131-
queryDecl(
132-
textName,
133-
`-- name: ${query.name} ${query.cmd}
131+
nodes.push(
132+
queryDecl(
133+
textName,
134+
`-- name: ${query.name} ${query.cmd}
134135
${query.text}`
135-
)
136-
);
136+
)
137+
);
137138

138-
const ctype = driver.columnType;
139+
const ctype = driver.columnType;
139140

140-
let argIface = undefined;
141-
let returnIface = undefined;
142-
if (query.params.length > 0) {
143-
argIface = `${query.name}Args`;
144-
nodes.push(argsDecl(argIface, ctype, query.params));
145-
}
146-
if (query.columns.length > 0) {
147-
returnIface = `${query.name}Row`;
148-
nodes.push(rowDecl(returnIface, ctype, query.columns));
149-
}
150-
151-
switch (query.cmd) {
152-
case ":exec": {
153-
nodes.push(
154-
driver.execDecl(lowerName, textName, argIface, query.params)
155-
);
156-
break;
141+
let argIface = undefined;
142+
let returnIface = undefined;
143+
if (query.params.length > 0) {
144+
argIface = `${query.name}Args`;
145+
nodes.push(argsDecl(argIface, ctype, query.params));
157146
}
158-
case ":one": {
159-
nodes.push(
160-
driver.oneDecl(
161-
lowerName,
162-
textName,
163-
argIface,
164-
returnIface ?? "void",
165-
query.params,
166-
query.columns
167-
)
168-
);
169-
break;
147+
if (query.columns.length > 0) {
148+
returnIface = `${query.name}Row`;
149+
nodes.push(rowDecl(returnIface, ctype, query.columns));
150+
}
151+
152+
switch (query.cmd) {
153+
case ":exec": {
154+
nodes.push(
155+
driver.execDecl(lowerName, textName, argIface, query.params)
156+
);
157+
break;
158+
}
159+
case ":one": {
160+
nodes.push(
161+
driver.oneDecl(
162+
lowerName,
163+
textName,
164+
argIface,
165+
returnIface ?? "void",
166+
query.params,
167+
query.columns
168+
)
169+
);
170+
break;
171+
}
172+
case ":many": {
173+
nodes.push(
174+
driver.manyDecl(
175+
lowerName,
176+
textName,
177+
argIface,
178+
returnIface ?? "void",
179+
query.params,
180+
query.columns
181+
)
182+
);
183+
break;
184+
}
170185
}
171-
case ":many": {
172-
nodes.push(
173-
driver.manyDecl(
174-
lowerName,
175-
textName,
176-
argIface,
177-
returnIface ?? "void",
178-
query.params,
179-
query.columns
180-
)
186+
if (nodes) {
187+
files.push(
188+
new File({
189+
name: `${filename.replace(".", "_")}.ts`,
190+
contents: new TextEncoder().encode(printNode(nodes)),
191+
})
181192
);
182-
break;
183193
}
184194
}
185195
}
186196

187-
for (const filename of filenames) {
188-
const nodes = querymap.get(filename);
189-
if (nodes) {
190-
files.push(
191-
new File({
192-
name: `${filename.replace(".", "_")}.ts`,
193-
contents: new TextEncoder().encode(printNode(nodes)),
194-
})
195-
);
196-
}
197-
}
198-
199197
return new GenerateResponse({
200198
files: files,
201199
});

src/drivers/mysql2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export function columnType(column?: Column): TypeNode {
165165
]);
166166
}
167167

168-
export function preamble() {
168+
export function preamble(queries: unknown) {
169169
return [
170170
factory.createImportDeclaration(
171171
undefined,

src/drivers/pg.ts

+37-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { SyntaxKind, NodeFlags, TypeNode, factory } from "typescript";
1+
import { SyntaxKind, NodeFlags, Node, TypeNode, factory } from "typescript";
22

3-
import { Parameter, Column } from "../gen/plugin/codegen_pb";
3+
import { Parameter, Column, Query } from "../gen/plugin/codegen_pb";
44
import { argName, colName } from "./utlis";
55

66
export function columnType(column?: Column): TypeNode {
@@ -287,8 +287,8 @@ export function columnType(column?: Column): TypeNode {
287287
]);
288288
}
289289

290-
export function preamble() {
291-
return [
290+
export function preamble(queries: Query[]) {
291+
const imports: Node[] = [
292292
factory.createImportDeclaration(
293293
undefined,
294294
factory.createImportClause(
@@ -310,22 +310,36 @@ export function preamble() {
310310
factory.createStringLiteral("pg"),
311311
undefined
312312
),
313-
factory.createImportDeclaration(
314-
undefined,
315-
factory.createImportClause(
316-
false,
313+
];
314+
315+
const hasInterval = queries.some(
316+
(query) =>
317+
query.params.some((p) => p.column?.type?.name === "interval") ||
318+
query.columns.some((c) => c.type?.name === "interval")
319+
);
320+
321+
if (hasInterval) {
322+
imports.push(
323+
factory.createImportDeclaration(
317324
undefined,
318-
factory.createNamedImports([
319-
factory.createImportSpecifier(
320-
false,
321-
undefined,
322-
factory.createIdentifier("IPostgresInterval")
323-
),
324-
])
325-
),
326-
factory.createStringLiteral("postgres-interval"),
327-
undefined
328-
),
325+
factory.createImportClause(
326+
false,
327+
undefined,
328+
factory.createNamedImports([
329+
factory.createImportSpecifier(
330+
false,
331+
undefined,
332+
factory.createIdentifier("IPostgresInterval")
333+
),
334+
])
335+
),
336+
factory.createStringLiteral("postgres-interval"),
337+
undefined
338+
)
339+
);
340+
}
341+
342+
imports.push(
329343
factory.createInterfaceDeclaration(
330344
undefined,
331345
factory.createIdentifier("Client"),
@@ -363,8 +377,10 @@ export function preamble() {
363377
)
364378
),
365379
]
366-
),
367-
];
380+
)
381+
);
382+
383+
return imports;
368384
}
369385

370386
function funcParamsDecl(iface: string | undefined, params: Parameter[]) {

src/drivers/postgres.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export function columnType(column?: Column): TypeNode {
246246
]);
247247
}
248248

249-
export function preamble() {
249+
export function preamble(queries: unknown) {
250250
return [
251251
factory.createImportDeclaration(
252252
undefined,

0 commit comments

Comments
 (0)