-
Notifications
You must be signed in to change notification settings - Fork 280
/
select-parser.ts
214 lines (199 loc) · 5.73 KB
/
select-parser.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
import { isReadonlyArray, isString } from '../util/object-utils.js'
import {
AliasedSelectQueryBuilder,
SelectQueryBuilder,
} from '../query-builder/select-query-builder.js'
import { SelectionNode } from '../operation-node/selection-node.js'
import {
AnyAliasedColumn,
AnyAliasedColumnWithTable,
AnyColumn,
AnyColumnWithTable,
ExtractColumnType,
ValueType,
} from '../util/type-utils.js'
import { parseAliasedStringReference } from './reference-parser.js'
import {
DynamicReferenceBuilder,
isDynamicReferenceBuilder,
} from '../dynamic/dynamic-reference-builder.js'
import {
AliasedExpressionOrFactory,
parseAliasedExpression,
} from './expression-parser.js'
import { Selectable, SelectType } from '../util/column-type.js'
import { parseTable } from './table-parser.js'
import { AliasedExpression } from '../expression/expression.js'
export type SelectExpression<DB, TB extends keyof DB> =
| AnyAliasedColumnWithTable<DB, TB>
| AnyAliasedColumn<DB, TB>
| AnyColumnWithTable<DB, TB>
| AnyColumn<DB, TB>
| DynamicReferenceBuilder<any>
| AliasedExpressionOrFactory<DB, TB>
export type SelectExpressionOrList<DB, TB extends keyof DB> =
| SelectExpression<DB, TB>
| ReadonlyArray<SelectExpression<DB, TB>>
/**
* Given a selection expression returns a query builder type that
* has the selection.
*/
export type QueryBuilderWithSelection<
DB,
TB extends keyof DB,
O,
SE
> = SelectQueryBuilder<DB, TB, O & Selection<DB, TB, SE>>
/**
* `selectAll` output query builder type.
*/
export type SelectAllQueryBuilder<
DB,
TB extends keyof DB,
O,
S extends keyof DB
> = SelectQueryBuilder<DB, TB, O & AllSelection<DB, S>>
export type Selection<DB, TB extends keyof DB, SE> = {
[A in ExtractAliasFromSelectExpression<SE>]: SelectType<
ExtractTypeFromSelectExpression<DB, TB, SE, A>
>
}
type ExtractAliasFromSelectExpression<SE> = SE extends string
? ExtractAliasFromStringSelectExpression<SE>
: SE extends AliasedExpression<any, infer EA>
? EA
: SE extends (qb: any) => AliasedExpression<any, infer EA>
? EA
: SE extends DynamicReferenceBuilder<infer RA>
? ExtractAliasFromStringSelectExpression<RA>
: never
type ExtractAliasFromStringSelectExpression<SE extends string> =
SE extends `${string}.${string}.${string} as ${infer A}`
? A
: SE extends `${string}.${string} as ${infer A}`
? A
: SE extends `${string} as ${infer A}`
? A
: SE extends `${string}.${string}.${infer C}`
? C
: SE extends `${string}.${infer C}`
? C
: SE
type ExtractTypeFromSelectExpression<
DB,
TB extends keyof DB,
SE,
A extends keyof any
> = SE extends string
? ExtractTypeFromStringSelectExpression<DB, TB, SE, A>
: SE extends AliasedSelectQueryBuilder<any, any, infer O, infer QA>
? QA extends A
? ValueType<O>
: never
: SE extends (qb: any) => AliasedSelectQueryBuilder<any, any, infer O, infer QA>
? QA extends A
? ValueType<O>
: never
: SE extends AliasedExpression<infer O, infer EA>
? EA extends A
? O
: never
: SE extends (qb: any) => AliasedExpression<infer O, infer EA>
? EA extends A
? O
: never
: SE extends DynamicReferenceBuilder<infer RA>
? A extends ExtractAliasFromStringSelectExpression<RA>
? ExtractTypeFromStringSelectExpression<DB, TB, RA, A> | undefined
: never
: never
type ExtractTypeFromStringSelectExpression<
DB,
TB extends keyof DB,
SE extends string,
A extends keyof any
> = SE extends `${infer SC}.${infer T}.${infer C} as ${infer RA}`
? RA extends A
? `${SC}.${T}` extends TB
? C extends keyof DB[`${SC}.${T}`]
? DB[`${SC}.${T}`][C]
: never
: never
: never
: SE extends `${infer T}.${infer C} as ${infer RA}`
? RA extends A
? T extends TB
? C extends keyof DB[T]
? DB[T][C]
: never
: never
: never
: SE extends `${infer C} as ${infer RA}`
? RA extends A
? C extends AnyColumn<DB, TB>
? ExtractColumnType<DB, TB, C>
: never
: never
: SE extends `${infer SC}.${infer T}.${infer C}`
? C extends A
? `${SC}.${T}` extends TB
? C extends keyof DB[`${SC}.${T}`]
? DB[`${SC}.${T}`][C]
: never
: never
: never
: SE extends `${infer T}.${infer C}`
? C extends A
? T extends TB
? C extends keyof DB[T]
? DB[T][C]
: never
: never
: never
: SE extends A
? SE extends AnyColumn<DB, TB>
? ExtractColumnType<DB, TB, SE>
: never
: never
type AllSelection<DB, TB extends keyof DB> = Selectable<{
[C in AnyColumn<DB, TB>]: {
[T in TB]: C extends keyof DB[T] ? DB[T][C] : never
}[TB]
}>
export function parseSelectExpressionOrList(
selection: SelectExpressionOrList<any, any>
): SelectionNode[] {
if (isReadonlyArray(selection)) {
return selection.map((it) => parseSelectExpression(it))
} else {
return [parseSelectExpression(selection)]
}
}
function parseSelectExpression(
selection: SelectExpression<any, any>
): SelectionNode {
if (isString(selection)) {
return SelectionNode.create(parseAliasedStringReference(selection))
} else if (isDynamicReferenceBuilder(selection)) {
return SelectionNode.create(selection.toOperationNode())
} else {
return SelectionNode.create(parseAliasedExpression(selection))
}
}
export function parseSelectAll(table?: string | string[]): SelectionNode[] {
if (!table) {
return [SelectionNode.createSelectAll()]
} else if (Array.isArray(table)) {
return table.map(parseSelectAllArg)
} else {
return [parseSelectAllArg(table)]
}
}
function parseSelectAllArg(table: string): SelectionNode {
if (isString(table)) {
return SelectionNode.createSelectAllFromTable(parseTable(table))
}
throw new Error(
`invalid value selectAll expression: ${JSON.stringify(table)}`
)
}