-
Notifications
You must be signed in to change notification settings - Fork 279
/
filter-parser.ts
219 lines (194 loc) · 5.8 KB
/
filter-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
215
216
217
218
219
import { FilterNode } from '../operation-node/filter-node.js'
import {
isBoolean,
isFunction,
isNull,
isString,
} from '../util/object-utils.js'
import {
AnyQueryBuilder,
AnyRawBuilder,
QueryBuilderFactory,
RawBuilderFactory,
} from '../util/type-utils.js'
import { isOperationNodeSource } from '../operation-node/operation-node-source.js'
import { RawNode } from '../operation-node/raw-node.js'
import {
Operator,
OperatorNode,
OPERATORS,
} from '../operation-node/operator-node.js'
import { ParensNode } from '../operation-node/parens-node.js'
import {
parseReferenceExpression,
ReferenceExpression,
} from './reference-parser.js'
import {
parseValueExpressionOrList,
ValueExpressionOrList,
} from './value-parser.js'
import { SelectQueryNode } from '../operation-node/select-query-node.js'
import { JoinBuilder } from '../query-builder/join-builder.js'
import { JoinNode } from '../operation-node/join-node.js'
import { FilterExpressionNode } from '../operation-node/operation-node-utils.js'
import { ValueNode } from '../operation-node/value-node.js'
import { ParseContext } from './parse-context.js'
export type ExistsExpression<DB, TB extends keyof DB> =
| AnyQueryBuilder
| QueryBuilderFactory<DB, TB>
| AnyRawBuilder
| RawBuilderFactory<DB, TB>
export type FilterOperator = Operator | AnyRawBuilder
export function parseWhereFilter(
ctx: ParseContext,
args: any[]
): FilterExpressionNode {
if (args.length === 3) {
return parseThreeArgFilter(ctx, args[0], args[1], args[2])
} else if (args.length === 1 && isFunction(args[0])) {
return parseWhereGroup(ctx, args[0])
} else {
throw new Error(
`invalid arguments passed to a where method ${JSON.stringify(args)}`
)
}
}
export function parseHavingFilter(
ctx: ParseContext,
args: any[]
): FilterExpressionNode {
if (args.length === 3) {
return parseThreeArgFilter(ctx, args[0], args[1], args[2])
} else if (args.length === 1 && isFunction(args[0])) {
return parseHavingGroup(ctx, args[0])
} else {
throw new Error(
`invalid arguments passed to a having method ${JSON.stringify(args)}`
)
}
}
export function parseOnFilter(
ctx: ParseContext,
args: any[]
): FilterExpressionNode {
if (args.length === 3) {
return parseThreeArgFilter(ctx, args[0], args[1], args[2])
} else if (args.length === 1 && isFunction(args[0])) {
return parseOnGroup(ctx, args[0])
} else {
throw new Error(
`invalid arguments passed to an on method ${JSON.stringify(args)}`
)
}
}
export function parseReferenceFilter(
ctx: ParseContext,
lhs: ReferenceExpression<any, any>,
op: FilterOperator,
rhs: ReferenceExpression<any, any>
): FilterNode {
return FilterNode.create(
parseReferenceExpression(ctx, lhs),
parseFilterOperator(op),
parseReferenceExpression(ctx, rhs)
)
}
export function parseExistFilter(
ctx: ParseContext,
arg: ExistsExpression<any, any>
): FilterNode {
return parseExistExpression(ctx, 'exists', arg)
}
export function parseNotExistFilter(
ctx: ParseContext,
arg: ExistsExpression<any, any>
): FilterNode {
return parseExistExpression(ctx, 'not exists', arg)
}
function parseThreeArgFilter(
ctx: ParseContext,
left: ReferenceExpression<any, any>,
op: FilterOperator,
right: ValueExpressionOrList<any, any, any>
): FilterNode {
if ((op === 'is' || op === 'is not') && (isNull(right) || isBoolean(right))) {
return parseIsFilter(ctx, left, op, right)
}
return FilterNode.create(
parseReferenceExpression(ctx, left),
parseFilterOperator(op),
parseValueExpressionOrList(ctx, right)
)
}
function parseIsFilter(
ctx: ParseContext,
left: ReferenceExpression<any, any>,
op: 'is' | 'is not',
right: null | boolean
) {
return FilterNode.create(
parseReferenceExpression(ctx, left),
parseFilterOperator(op),
ValueNode.createImmediate(right)
)
}
function parseFilterOperator(op: FilterOperator): OperatorNode | RawNode {
if (isString(op)) {
if (OPERATORS.includes(op)) {
return OperatorNode.create(op)
}
} else if (isOperationNodeSource(op)) {
return op.toOperationNode()
}
throw new Error(
`invalid comparison operator ${JSON.stringify(
op
)} passed to a filter method`
)
}
function parseWhereGroup(
ctx: ParseContext,
callback: (qb: AnyQueryBuilder) => AnyQueryBuilder
): ParensNode {
const query = callback(ctx.createSelectQueryBuilder([]))
const queryNode = query.toOperationNode() as SelectQueryNode
if (!queryNode.where) {
throw new Error('no `where*` methods called insided a group callback')
}
return ParensNode.create(queryNode.where.where)
}
function parseHavingGroup(
ctx: ParseContext,
callback: (qb: AnyQueryBuilder) => AnyQueryBuilder
): ParensNode {
const query = callback(ctx.createSelectQueryBuilder([]))
const queryNode = query.toOperationNode() as SelectQueryNode
if (!queryNode.having) {
throw new Error('no `having*` methods called insided a group callback')
}
return ParensNode.create(queryNode.having.having)
}
function parseOnGroup(
ctx: ParseContext,
callback: (qb: JoinBuilder<any, any>) => JoinBuilder<any, any>
): ParensNode {
const joinBuilder = callback(ctx.createJoinBuilder('InnerJoin', 'table'))
const joinNode = joinBuilder.toOperationNode() as JoinNode
if (!joinNode.on) {
throw new Error('no `on*` methods called insided a group callback')
}
return ParensNode.create(joinNode.on)
}
function parseExistExpression(
ctx: ParseContext,
type: 'exists' | 'not exists',
arg: ExistsExpression<any, any>
): FilterNode {
const node = isFunction(arg)
? arg(ctx.createExpressionBuilder()).toOperationNode()
: arg.toOperationNode()
if (!SelectQueryNode.is(node) && !RawNode.is(node)) {
throw new Error('invalid where exists arg')
}
return FilterNode.create(undefined, OperatorNode.create(type), node)
}