Skip to content

Commit ef40726

Browse files
committed
add types only option
1 parent 395a0ba commit ef40726

File tree

1 file changed

+56
-51
lines changed

1 file changed

+56
-51
lines changed

src/app.ts

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ writeOutput(result);
4343
interface Options {
4444
runtime?: string;
4545
driver?: string;
46-
mysql2?: Mysql2Options
46+
mysql2?: Mysql2Options,
47+
types_only?: boolean
4748
}
4849

4950
interface Driver {
@@ -111,7 +112,6 @@ function codegen(input: GenerateRequest): GenerateResponse {
111112
// TODO: Verify options, parse them from protobuf honestly
112113

113114
const querymap = new Map<string, Query[]>();
114-
115115
for (const query of input.queries) {
116116
if (!querymap.has(query.filename)) {
117117
querymap.set(query.filename, []);
@@ -121,8 +121,11 @@ function codegen(input: GenerateRequest): GenerateResponse {
121121
}
122122

123123
for (const [filename, queries] of querymap.entries()) {
124-
const nodes = driver.preamble(queries);
125-
124+
let nodes: Node[]
125+
if (!options.types_only) {
126+
nodes = driver.preamble(queries);
127+
}
128+
nodes = [];
126129
for (const query of queries) {
127130
const colmap = new Map<string, number>();
128131
for (let column of query.columns) {
@@ -138,14 +141,15 @@ function codegen(input: GenerateRequest): GenerateResponse {
138141

139142
const lowerName = query.name[0].toLowerCase() + query.name.slice(1);
140143
const textName = `${lowerName}Query`;
141-
142-
nodes.push(
143-
queryDecl(
144-
textName,
145-
`-- name: ${query.name} ${query.cmd}
146-
${query.text}`
147-
)
148-
);
144+
if (!options.types_only) {
145+
nodes.push(
146+
queryDecl(
147+
textName,
148+
`-- name: ${query.name} ${query.cmd}
149+
${query.text}`
150+
)
151+
);
152+
}
149153

150154
let argIface = undefined;
151155
let returnIface = undefined;
@@ -157,45 +161,46 @@ ${query.text}`
157161
returnIface = `${query.name}Row`;
158162
nodes.push(rowDecl(returnIface, driver, query.columns));
159163
}
160-
161-
switch (query.cmd) {
162-
case ":exec": {
163-
nodes.push(
164-
driver.execDecl(lowerName, textName, argIface, query.params)
165-
);
166-
break;
167-
}
168-
case ":execlastid": {
169-
nodes.push(
170-
driver.execlastidDecl(lowerName, textName, argIface, query.params)
171-
);
172-
break;
173-
}
174-
case ":one": {
175-
nodes.push(
176-
driver.oneDecl(
177-
lowerName,
178-
textName,
179-
argIface,
180-
returnIface ?? "void",
181-
query.params,
182-
query.columns
183-
)
184-
);
185-
break;
186-
}
187-
case ":many": {
188-
nodes.push(
189-
driver.manyDecl(
190-
lowerName,
191-
textName,
192-
argIface,
193-
returnIface ?? "void",
194-
query.params,
195-
query.columns
196-
)
197-
);
198-
break;
164+
if (!options.types_only) {
165+
switch (query.cmd) {
166+
case ":exec": {
167+
nodes.push(
168+
driver.execDecl(lowerName, textName, argIface, query.params)
169+
);
170+
break;
171+
}
172+
case ":execlastid": {
173+
nodes.push(
174+
driver.execlastidDecl(lowerName, textName, argIface, query.params)
175+
);
176+
break;
177+
}
178+
case ":one": {
179+
nodes.push(
180+
driver.oneDecl(
181+
lowerName,
182+
textName,
183+
argIface,
184+
returnIface ?? "void",
185+
query.params,
186+
query.columns
187+
)
188+
);
189+
break;
190+
}
191+
case ":many": {
192+
nodes.push(
193+
driver.manyDecl(
194+
lowerName,
195+
textName,
196+
argIface,
197+
returnIface ?? "void",
198+
query.params,
199+
query.columns
200+
)
201+
);
202+
break;
203+
}
199204
}
200205
}
201206
if (nodes) {

0 commit comments

Comments
 (0)