forked from codemix/ts-sql
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Evaluator.ts
395 lines (365 loc) · 10.1 KB
/
Evaluator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import {
AssignmentExpression,
BinaryExpression,
BooleanLiteral,
DeleteStatement,
FieldSpecifier,
Identifier,
InnerJoinSpecifier,
InsertStatement,
JoinSpecifier,
LogicalExpression,
MemberExpression,
NullLiteral,
NumericLiteral,
SelectStatement,
Statement,
StringLiteral,
TableSpecifier,
UpdateStatement,
} from "./AST";
import { Database, JoinedSchema } from "./Schema";
import {
MatchStringLike,
Merge,
UnionToIntersection,
AssembleEntries,
ToJoinedSchema,
TupleToUnion,
} from "./Utils";
type EvaluateStatement<
DB extends Database,
Node extends Statement
> = Node extends SelectStatement
? EvaluateSelectStatement<DB, Node>
: Node extends InsertStatement
? EvaluateInsertStatement<DB, Node>
: Node extends UpdateStatement
? EvaluateUpdateStatement<DB, Node>
: never;
// TBD
// type EvaluateStatement<
// DB extends Database,
// Node extends Statement
// > = Node extends SelectStatement
// ? EvaluateSelectStatement<DB, Node>
// : Node extends InsertStatement
// ? EvaluateInsertStatement<DB, Node>
// : Node extends UpdateStatement
// ? EvaluateUpdateStatement<DB, Node>
// : Node extends DeleteStatement
// ? EvaluateDeleteStatement<DB, Node>
// : never;
type InsertRow<
Table extends readonly any[],
Assignments extends readonly AssignmentExpression[]
> = [
Merge<
UnionToIntersection<
AllReadonly<AssembleEntries<ApplyAssignments<Assignments>>>
>
>,
...Table
];
type AllReadonly<T> = { readonly [K in keyof T]: T[K] };
type ApplyAssignments<Assignments extends readonly AssignmentExpression[]> = {
[K in keyof Assignments]: Assignments[K] extends AssignmentExpression<
Identifier<infer Key>,
infer Value
>
? [
Key,
NullLiteral extends Value ? null : Exclude<ExtractValue<Value>, null>
]
: never;
};
type ExtractValue<T> = T extends NullLiteral
? null
: T extends BooleanLiteral<infer Value>
? Value
: T extends NumericLiteral<infer Value>
? Value
: T extends StringLiteral<infer Value>
? Value
: never;
type EvaluateDeleteStatement<
DB extends Database,
Node extends DeleteStatement
> = Node extends DeleteStatement<infer TableName, infer Where>
? {
[K in keyof DB]: K extends TableName
? DeleteRows<TableName extends keyof DB ? DB[TableName] : never, Where>
: DB[K];
}
: never;
type DeleteRows<Table, Where> = FilterUndefined<
{
[Index in keyof Table]: EvaluateExpression<Table[Index], Where> extends true
? undefined
: Table[Index];
}
>;
export type EvaluateSelectStatement<
DB extends Database,
Node extends SelectStatement
> = Node extends SelectStatement<infer Fields, infer From, infer Joins>
? From extends TableSpecifier<
Identifier<infer Source>,
Identifier<infer Alias>
>
? Source extends keyof DB["schema"]
? Fields extends [FieldSpecifier<Identifier<"*">, Identifier<"*">>]
? Array<ToJoinedSchema<DB, From, Joins>["public"]>
: Array<ExtractFields<DB, ToJoinedSchema<DB, From, Joins>, Fields>>
: never
: never
: never;
export type EvaluateInsertStatement<
DB extends Database,
Node extends InsertStatement
> = Node extends InsertStatement<
infer TableName,
infer Fields,
infer Values,
infer ReturningFields
>
? ReturningFields extends []
? never
: TableName extends keyof DB["schema"]
? ReturningFields extends [FieldSpecifier<Identifier<"*">, Identifier<"*">>]
? Array<DB["schema"][TableName]>
: Array<
AssembleEntries<
{
[K in keyof ReturningFields]: ReturningFields[K] extends FieldSpecifier<
infer Source,
Identifier<infer Alias>
>
? Source extends Identifier<infer Name>
? Name extends keyof DB["schema"][TableName]
? [
ExtractFieldAlias<ReturningFields[K]>,
DB["schema"][TableName][Name]
]
: never
: never
: never;
}
>
>
: never
: never;
export type EvaluateUpdateStatement<
DB extends Database,
Node extends UpdateStatement
> = Node extends UpdateStatement<
infer TableName,
infer Values,
infer Where,
infer ReturningFields
>
? ReturningFields extends []
? never
: TableName extends keyof DB["schema"]
? ReturningFields extends [FieldSpecifier<Identifier<"*">, Identifier<"*">>]
? Array<DB["schema"][TableName]>
: Array<
AssembleEntries<
{
[K in keyof ReturningFields]: ReturningFields[K] extends FieldSpecifier<
infer Source,
Identifier<infer Alias>
>
? Source extends Identifier<infer Name>
? Name extends keyof DB["schema"][TableName]
? [
ExtractFieldAlias<ReturningFields[K]>,
DB["schema"][TableName][Name]
]
: never
: never
: never;
}
>
>
: never
: never;
export type ExtractFieldAlias<Field> = Field extends FieldSpecifier<
Identifier<any>,
Identifier<infer Alias>
>
? Alias
: never;
export type FilterUndefined<T> = T extends Readonly<[infer Head, ...infer Tail]>
? Head extends undefined
? FilterUndefined<Tail>
: [Head, ...FilterUndefined<Tail>]
: [];
// Get `a.*` type of fields
type _GetSelectAllFields<
Schema extends JoinedSchema<any>,
Fields extends FieldSpecifier<any>[],
Normalized extends Record<string, any>[] = []
> = Fields extends [infer Head, ...infer Tail]
? Tail extends FieldSpecifier<any>[]
? Head extends FieldSpecifier<infer Source, Identifier<infer Alias>>
? Source extends MemberExpression<infer O, infer P>
? P extends "*"
? O extends Schema["from"]["alias"]
? _GetSelectAllFields<
Schema,
Tail,
[...Normalized, Schema["from"]["schema"]]
>
: O extends keyof Schema["joins"]
? _GetSelectAllFields<
Schema,
Tail,
[...Normalized, Schema["joins"]]
>
: never
: _GetSelectAllFields<Schema, Tail, Normalized>
: _GetSelectAllFields<Schema, Tail, Normalized>
: never
: Normalized
: Normalized;
export type ExtractFields<
DB extends Database,
Schema extends JoinedSchema<any>,
Fields extends FieldSpecifier<any>[],
SelectAllFields extends Record<string, any>[] = _GetSelectAllFields<
Schema,
Fields
>
> = Merge<
AssembleEntries<
{
[K in keyof Fields]: Fields[K] extends FieldSpecifier<
infer Source,
Identifier<infer Alias>
>
? Source extends Identifier<infer Name>
? Name extends keyof Schema["public"]
? [ExtractFieldAlias<Fields[K]>, Schema["public"][Name]]
: never
: Source extends MemberExpression<infer O, infer P>
? O extends keyof Schema["joins"]
? P extends keyof Schema["joins"][O]
? [Alias, Schema["joins"][O][P]]
: never
: O extends Schema["from"]["alias"]
? P extends keyof Schema["from"]["schema"]
? [Alias, Schema["from"]["schema"][P]]
: never
: never
: never
: never;
}
> &
UnionToIntersection<TupleToUnion<SelectAllFields>>
>;
type EvaluateExpression<Row, Exp> =
| EvaluateLogicalExpression<Row, Exp>
| EvaluateBinaryExpression<Row, Exp>
| EvaluateMemberExpression<Row, Exp>
| EvaluateIdentifier<Row, Exp>
| EvaluateNullLiteral<Row, Exp>
| EvaluateBooleanLiteral<Row, Exp>
| EvaluateNumericLiteral<Row, Exp>
| EvaluateStringLiteral<Row, Exp>;
type EvaluateBinaryExpression<Row, Exp> = Exp extends BinaryExpression<
infer Left,
infer Op,
infer Right
>
? Op extends "="
? EvaluateBinaryEquals<
EvaluateExpression<Row, Left>,
EvaluateExpression<Row, Right>
>
: Op extends "!="
? EvaluateBinaryNotEquals<
EvaluateExpression<Row, Left>,
EvaluateExpression<Row, Right>
>
: Op extends "LIKE"
? EvaluateBinaryLike<
EvaluateExpression<Row, Left>,
EvaluateExpression<Row, Right>
>
: never
: never;
type EvaluateBinaryEquals<Left, Right> = Left extends Right
? Right extends Left
? true
: false
: false;
type EvaluateBinaryNotEquals<Left, Right> = EvaluateBinaryEquals<
Left,
Right
> extends true
? false
: true;
type EvaluateBinaryLike<Left, Right> = MatchStringLike<Left, Right>;
type EvaluateLogicalExpression<Row, Exp> = Exp extends LogicalExpression<
infer Left,
infer Op,
infer Right
>
? Op extends "AND"
? EvaluateLogicalAND<
EvaluateExpression<Row, Left>,
EvaluateExpression<Row, Right>
>
: Op extends "OR"
? EvaluateLogicalOR<
EvaluateExpression<Row, Left>,
EvaluateExpression<Row, Right>
>
: never
: never;
type EvaluateLogicalAND<Left, Right> = Left extends true
? Right extends true
? true
: false
: false;
type EvaluateLogicalOR<Left, Right> = Left extends true
? true
: Right extends true
? true
: false;
type EvaluateMemberExpression<Row, Exp> = Exp extends MemberExpression<
infer O,
infer P
>
? O extends keyof Row
? EvaluateIdentifier<Row[O], Identifier<P>>
: never
: never;
type EvaluateIdentifier<Row, Exp> = Exp extends Identifier<infer Name>
? Name extends keyof Row
? Row[Name]
: never
: never;
export type EvaluateNullLiteral<Row, Exp> = Exp extends NullLiteral
? null
: never;
export type EvaluateBooleanLiteral<Row, Exp> = Exp extends BooleanLiteral<
infer Value
>
? Value
: never;
export type EvaluateStringLiteral<Row, Exp> = Exp extends StringLiteral<
infer Value
>
? Value
: never;
export type EvaluateNumericLiteral<Row, Exp> = Exp extends NumericLiteral<
infer Value
>
? Value
: never;
export type Evaluate<
DB extends Database,
S extends Statement
> = EvaluateStatement<DB, S>;