-
Notifications
You must be signed in to change notification settings - Fork 280
/
aggregate-function-node.ts
87 lines (79 loc) · 2.17 KB
/
aggregate-function-node.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
import { freeze } from '../util/object-utils.js'
import { OperationNode } from './operation-node.js'
import { OverNode } from './over-node.js'
import { SimpleReferenceExpressionNode } from './simple-reference-expression-node.js'
import { WhereNode } from './where-node.js'
type AggregateFunction = 'avg' | 'count' | 'max' | 'min' | 'sum'
export interface AggregateFunctionNode extends OperationNode {
readonly kind: 'AggregateFunctionNode'
readonly func: AggregateFunction
readonly column: SimpleReferenceExpressionNode
readonly distinct?: boolean
readonly filter?: WhereNode
readonly over?: OverNode
}
/**
* @internal
*/
export const AggregateFunctionNode = freeze({
is(node: OperationNode): node is AggregateFunctionNode {
return node.kind === 'AggregateFunctionNode'
},
create(
aggregateFunction: AggregateFunction,
column: SimpleReferenceExpressionNode
): AggregateFunctionNode {
return freeze({
kind: 'AggregateFunctionNode',
func: aggregateFunction,
column,
})
},
cloneWithDistinct(
aggregateFunctionNode: AggregateFunctionNode
): AggregateFunctionNode {
return freeze({
...aggregateFunctionNode,
distinct: true,
})
},
cloneWithFilter(
aggregateFunctionNode: AggregateFunctionNode,
filter: OperationNode
): AggregateFunctionNode {
return freeze({
...aggregateFunctionNode,
filter: aggregateFunctionNode.filter
? WhereNode.cloneWithOperation(
aggregateFunctionNode.filter,
'And',
filter
)
: WhereNode.create(filter),
})
},
cloneWithOrFilter(
aggregateFunctionNode: AggregateFunctionNode,
filter: OperationNode
): AggregateFunctionNode {
return freeze({
...aggregateFunctionNode,
filter: aggregateFunctionNode.filter
? WhereNode.cloneWithOperation(
aggregateFunctionNode.filter,
'Or',
filter
)
: WhereNode.create(filter),
})
},
cloneWithOver(
aggregateFunctionNode: AggregateFunctionNode,
over?: OverNode
): AggregateFunctionNode {
return freeze({
...aggregateFunctionNode,
over,
})
},
})