sql-typechecker 0.0.7 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,291 +0,0 @@
1
- import { unaryOp } from "./typecheck";
2
-
3
- const builtinunaryoperatorsFromSyntax: unaryOp[] = [
4
- // {
5
- // name: { name: "IS NULL" },
6
- // operand: { kind: "nullable", typevar: { kind: "anyscalar" } },
7
- // result: { kind: "scalar", name: { name: "boolean" } },
8
- // description: "is NULL check",
9
- // },
10
- ];
11
-
12
- const builtinunaryoperatorsFromSchema: unaryOp[] = [
13
- {
14
- name: { name: "!" },
15
- operand: { kind: "scalar", name: { name: "bigint" } },
16
- result: { kind: "scalar", name: { name: "numeric" } },
17
- description: "factorial",
18
- },
19
- {
20
- name: { name: "!!" },
21
- operand: { kind: "scalar", name: { name: "bigint" } },
22
- result: { kind: "scalar", name: { name: "numeric" } },
23
- description: "deprecated, use ! instead",
24
- },
25
- {
26
- name: { name: "!!" },
27
- operand: { kind: "scalar", name: { name: "tsquery" } },
28
- result: { kind: "scalar", name: { name: "tsquery" } },
29
- description: "NOT tsquery",
30
- },
31
- {
32
- name: { name: "#" },
33
- operand: { kind: "scalar", name: { name: "path" } },
34
- result: { kind: "scalar", name: { name: "integer" } },
35
- description: "number of points",
36
- },
37
- {
38
- name: { name: "#" },
39
- operand: { kind: "scalar", name: { name: "polygon" } },
40
- result: { kind: "scalar", name: { name: "integer" } },
41
- description: "number of points",
42
- },
43
-
44
- {
45
- name: { name: "+" },
46
- operand: { kind: "scalar", name: { name: "bigint" } },
47
- result: { kind: "scalar", name: { name: "bigint" } },
48
- description: "unary plus",
49
- },
50
- {
51
- name: { name: "+" },
52
- operand: { kind: "scalar", name: { name: "double precision" } },
53
- result: { kind: "scalar", name: { name: "double precision" } },
54
- description: "unary plus",
55
- },
56
- {
57
- name: { name: "+" },
58
- operand: { kind: "scalar", name: { name: "integer" } },
59
- result: { kind: "scalar", name: { name: "integer" } },
60
- description: "unary plus",
61
- },
62
- {
63
- name: { name: "+" },
64
- operand: { kind: "scalar", name: { name: "numeric" } },
65
- result: { kind: "scalar", name: { name: "numeric" } },
66
- description: "unary plus",
67
- },
68
- {
69
- name: { name: "+" },
70
- operand: { kind: "scalar", name: { name: "real" } },
71
- result: { kind: "scalar", name: { name: "real" } },
72
- description: "unary plus",
73
- },
74
- {
75
- name: { name: "+" },
76
- operand: { kind: "scalar", name: { name: "smallint" } },
77
- result: { kind: "scalar", name: { name: "smallint" } },
78
- description: "unary plus",
79
- },
80
-
81
- {
82
- name: { name: "-" },
83
- operand: { kind: "scalar", name: { name: "bigint" } },
84
- result: { kind: "scalar", name: { name: "bigint" } },
85
- description: "negate",
86
- },
87
- {
88
- name: { name: "-" },
89
- operand: { kind: "scalar", name: { name: "double precision" } },
90
- result: { kind: "scalar", name: { name: "double precision" } },
91
- description: "negate",
92
- },
93
- {
94
- name: { name: "-" },
95
- operand: { kind: "scalar", name: { name: "integer" } },
96
- result: { kind: "scalar", name: { name: "integer" } },
97
- description: "negate",
98
- },
99
- {
100
- name: { name: "-" },
101
- operand: { kind: "scalar", name: { name: "interval" } },
102
- result: { kind: "scalar", name: { name: "interval" } },
103
- description: "negate",
104
- },
105
- {
106
- name: { name: "-" },
107
- operand: { kind: "scalar", name: { name: "numeric" } },
108
- result: { kind: "scalar", name: { name: "numeric" } },
109
- description: "negate",
110
- },
111
- {
112
- name: { name: "-" },
113
- operand: { kind: "scalar", name: { name: "real" } },
114
- result: { kind: "scalar", name: { name: "real" } },
115
- description: "negate",
116
- },
117
- {
118
- name: { name: "-" },
119
- operand: { kind: "scalar", name: { name: "smallint" } },
120
- result: { kind: "scalar", name: { name: "smallint" } },
121
- description: "negate",
122
- },
123
-
124
- {
125
- name: { name: "?-" },
126
- operand: { kind: "scalar", name: { name: "line" } },
127
- result: { kind: "scalar", name: { name: "boolean" } },
128
- description: "horizontal",
129
- },
130
- {
131
- name: { name: "?-" },
132
- operand: { kind: "scalar", name: { name: "lseg" } },
133
- result: { kind: "scalar", name: { name: "boolean" } },
134
- description: "horizontal",
135
- },
136
-
137
- {
138
- name: { name: "?|" },
139
- operand: { kind: "scalar", name: { name: "line" } },
140
- result: { kind: "scalar", name: { name: "boolean" } },
141
- description: "vertical",
142
- },
143
- {
144
- name: { name: "?|" },
145
- operand: { kind: "scalar", name: { name: "lseg" } },
146
- result: { kind: "scalar", name: { name: "boolean" } },
147
- description: "vertical",
148
- },
149
-
150
- {
151
- name: { name: "@" },
152
- operand: { kind: "scalar", name: { name: "bigint" } },
153
- result: { kind: "scalar", name: { name: "bigint" } },
154
- description: "absolute value",
155
- },
156
- {
157
- name: { name: "@" },
158
- operand: { kind: "scalar", name: { name: "double precision" } },
159
- result: { kind: "scalar", name: { name: "double precision" } },
160
- description: "absolute value",
161
- },
162
- {
163
- name: { name: "@" },
164
- operand: { kind: "scalar", name: { name: "integer" } },
165
- result: { kind: "scalar", name: { name: "integer" } },
166
- description: "absolute value",
167
- },
168
- {
169
- name: { name: "@" },
170
- operand: { kind: "scalar", name: { name: "numeric" } },
171
- result: { kind: "scalar", name: { name: "numeric" } },
172
- description: "absolute value",
173
- },
174
- {
175
- name: { name: "@" },
176
- operand: { kind: "scalar", name: { name: "real" } },
177
- result: { kind: "scalar", name: { name: "real" } },
178
- description: "absolute value",
179
- },
180
- {
181
- name: { name: "@" },
182
- operand: { kind: "scalar", name: { name: "smallint" } },
183
- result: { kind: "scalar", name: { name: "smallint" } },
184
- description: "absolute value",
185
- },
186
- {
187
- name: { name: "@-@" },
188
- operand: { kind: "scalar", name: { name: "lseg" } },
189
- result: { kind: "scalar", name: { name: "double precision" } },
190
- description: "distance between endpoints",
191
- },
192
- {
193
- name: { name: "@-@" },
194
- operand: { kind: "scalar", name: { name: "path" } },
195
- result: { kind: "scalar", name: { name: "double precision" } },
196
- description: "sum of path segment lengths",
197
- },
198
-
199
- {
200
- name: { name: "@@" },
201
- operand: { kind: "scalar", name: { name: "box" } },
202
- result: { kind: "scalar", name: { name: "point" } },
203
- description: "center of",
204
- },
205
- {
206
- name: { name: "@@" },
207
- operand: { kind: "scalar", name: { name: "circle" } },
208
- result: { kind: "scalar", name: { name: "point" } },
209
- description: "center of",
210
- },
211
- {
212
- name: { name: "@@" },
213
- operand: { kind: "scalar", name: { name: "lseg" } },
214
- result: { kind: "scalar", name: { name: "point" } },
215
- description: "center of",
216
- },
217
- {
218
- name: { name: "@@" },
219
- operand: { kind: "scalar", name: { name: "path" } },
220
- result: { kind: "scalar", name: { name: "point" } },
221
- description: "center of",
222
- },
223
- {
224
- name: { name: "@@" },
225
- operand: { kind: "scalar", name: { name: "polygon" } },
226
- result: { kind: "scalar", name: { name: "point" } },
227
- description: "center of",
228
- },
229
-
230
- {
231
- name: { name: "|" },
232
- operand: { kind: "scalar", name: { name: "tinterval" } },
233
- result: { kind: "scalar", name: { name: "abstime" } },
234
- description: "start of interval",
235
- },
236
-
237
- {
238
- name: { name: "|/" },
239
- operand: { kind: "scalar", name: { name: "double precision" } },
240
- result: { kind: "scalar", name: { name: "double precision" } },
241
- description: "square root",
242
- },
243
-
244
- {
245
- name: { name: "||/" },
246
- operand: { kind: "scalar", name: { name: "double precision" } },
247
- result: { kind: "scalar", name: { name: "double precision" } },
248
- description: "cube root",
249
- },
250
-
251
- {
252
- name: { name: "~" },
253
- operand: { kind: "scalar", name: { name: "bigint" } },
254
- result: { kind: "scalar", name: { name: "bigint" } },
255
- description: "bitwise not",
256
- },
257
- {
258
- name: { name: "~" },
259
- operand: { kind: "scalar", name: { name: "bit" } },
260
- result: { kind: "scalar", name: { name: "bit" } },
261
- description: "bitwise not",
262
- },
263
- {
264
- name: { name: "~" },
265
- operand: { kind: "scalar", name: { name: "inet" } },
266
- result: { kind: "scalar", name: { name: "inet" } },
267
- description: "bitwise not",
268
- },
269
- {
270
- name: { name: "~" },
271
- operand: { kind: "scalar", name: { name: "integer" } },
272
- result: { kind: "scalar", name: { name: "integer" } },
273
- description: "bitwise not",
274
- },
275
- {
276
- name: { name: "~" },
277
- operand: { kind: "scalar", name: { name: "macaddr" } },
278
- result: { kind: "scalar", name: { name: "macaddr" } },
279
- description: "bitwise not",
280
- },
281
- {
282
- name: { name: "~" },
283
- operand: { kind: "scalar", name: { name: "smallint" } },
284
- result: { kind: "scalar", name: { name: "smallint" } },
285
- description: "bitwise not",
286
- },
287
- ];
288
-
289
- export const builtinUnaryOperators = builtinunaryoperatorsFromSyntax.concat(
290
- builtinunaryoperatorsFromSchema
291
- );
package/src/cli.ts DELETED
@@ -1,144 +0,0 @@
1
- import * as fs from "fs/promises";
2
- import * as path from "path";
3
- import { CreateFunctionStatement, parse, Statement } from "pgsql-ast-parser";
4
- import * as prettier from "prettier";
5
- import {
6
- functionToTypescript,
7
- genCrudOperations,
8
- genDomain,
9
- getImports,
10
- } from "./codegen";
11
- import { doCreateFunction, parseSetupScripts } from "./typecheck";
12
-
13
- go();
14
-
15
- async function findSqlFilesInDir(dir: string): Promise<string[]> {
16
- const inThisDir = await fs.readdir(dir);
17
- const res: string[] = [];
18
- for (let p of inThisDir) {
19
- const fullP = path.join(dir, p);
20
- if (fullP.endsWith(".sql")) {
21
- res.push(fullP);
22
- } else {
23
- const stat = await fs.stat(fullP);
24
- if (stat.isDirectory()) {
25
- const inSubFolder = await findSqlFilesInDir(fullP);
26
- res.push(...inSubFolder);
27
- } else {
28
- // not a sql file, not a directory
29
- }
30
- }
31
- }
32
- return res;
33
- }
34
-
35
- function isCreateFunctionStatement(
36
- st: Statement
37
- ): st is CreateFunctionStatement {
38
- return st.type === "create function";
39
- }
40
-
41
- async function go() {
42
- const outArgs = findInArgs({ argv: process.argv, flags: ["-o", "--out"] });
43
- const outArg = outArgs[0];
44
- if (!outArg) {
45
- throw new Error("Please provide -o/--out parameter");
46
- }
47
-
48
- const dirs = findInArgs({ argv: process.argv, flags: ["-d", "--dir"] });
49
- const files = findInArgs({ argv: process.argv, flags: ["-f", "--file"] });
50
-
51
- const allSqlFiles = (
52
- await Promise.all(
53
- dirs.map((dir) => findSqlFilesInDir(path.resolve(process.cwd(), dir)))
54
- )
55
- )
56
- .flat()
57
- .concat(files);
58
-
59
- if (allSqlFiles.length === 0) {
60
- throw new Error(
61
- "Please provide at least one SQL file with flags -f/--file or -d/--dir"
62
- );
63
- }
64
-
65
- const allStatements: Statement[] = [];
66
- for (let sqlFile of allSqlFiles) {
67
- console.log("Processing file ${sqlFile}");
68
- const fileContents = await fs.readFile(sqlFile, "utf-8");
69
- const statements: Statement[] = parse(fileContents, {
70
- locationTracking: true,
71
- });
72
- allStatements.push(...statements);
73
- }
74
-
75
- console.log(`Processing ${allStatements.length} statements`);
76
-
77
- const g = parseSetupScripts(allStatements);
78
-
79
- // console.log("Global:\n", JSON.stringify(g, null, 2), "\n");
80
-
81
- const createFunctionStatements = allStatements.filter(
82
- isCreateFunctionStatement
83
- );
84
-
85
- await fs.mkdir(path.resolve(process.cwd(), outArg), { recursive: true });
86
- const functionsOutFile = await prepOutFile(
87
- path.join(path.resolve(process.cwd(), outArg), "functions.ts")
88
- );
89
-
90
- for (let dom of g.domains) {
91
- await fs.appendFile(functionsOutFile, genDomain(dom) + "\n", "utf-8");
92
- }
93
- await fs.appendFile(functionsOutFile, `\n`, "utf-8");
94
-
95
- for (let st of createFunctionStatements) {
96
- try {
97
- const res = doCreateFunction(g, { decls: [], froms: [] }, st);
98
- const writing = prettier.format(functionToTypescript(res), {
99
- parser: "typescript",
100
- });
101
- // console.log(`Writing: ${writing}`);
102
- await fs.appendFile(functionsOutFile, writing, "utf-8");
103
- } catch (err) {
104
- console.error(err instanceof Error ? err.message : JSON.stringify(err));
105
- return;
106
- }
107
- }
108
-
109
- const crudDir = path.join(path.resolve(process.cwd(), outArg), "crud");
110
- await fs.mkdir(crudDir, { recursive: true });
111
-
112
- for (let table of g.tables) {
113
- const tableOutFile = await prepOutFile(
114
- path.join(crudDir, table.name.name + ".ts")
115
- );
116
- const text = genCrudOperations(table);
117
- await fs.appendFile(
118
- tableOutFile,
119
- // text,
120
- prettier.format(text, { parser: "typescript" }),
121
- "utf-8"
122
- );
123
- }
124
- }
125
-
126
- async function prepOutFile(path: string): Promise<string> {
127
- // const stat = await fs.stat(path);
128
- // if (!stat.isFile)
129
- // await fs.truncate(path);
130
- await fs.writeFile(path, getImports() + "\n", "utf-8");
131
- return path;
132
- }
133
-
134
- function findInArgs(opts: { argv: string[]; flags: string[] }): string[] {
135
- let i = 0;
136
- let res = [];
137
- for (let arg of opts.argv) {
138
- if (opts.flags.includes(arg) && opts.argv[i + 1]) {
139
- res.push(opts.argv[i + 1]);
140
- }
141
- i = i + 1;
142
- }
143
- return res;
144
- }