-
Notifications
You must be signed in to change notification settings - Fork 280
/
select-query-node.ts
160 lines (147 loc) · 4.07 KB
/
select-query-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
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
import { freeze } from '../util/object-utils.js'
import { FromNode } from './from-node.js'
import { GroupByItemNode } from './group-by-item-node.js'
import { GroupByNode } from './group-by-node.js'
import { HavingNode, HavingNodeChild } from './having-node.js'
import { JoinNode } from './join-node.js'
import { LimitNode } from './limit-node.js'
import { OffsetNode } from './offset-node.js'
import { OperationNode } from './operation-node.js'
import { TableExpressionNode } from './operation-node-utils.js'
import { OrderByItemNode } from './order-by-item-node.js'
import { OrderByNode } from './order-by-node.js'
import { SelectionNode } from './selection-node.js'
import { WhereNode } from './where-node.js'
import { WithNode } from './with-node.js'
export type SelectModifier =
| 'ForUpdate'
| 'ForNoKeyUpdate'
| 'ForShare'
| 'ForKeyShare'
| 'NoWait'
| 'SkipLocked'
export interface SelectQueryNode extends OperationNode {
readonly kind: 'SelectQueryNode'
readonly from: FromNode
readonly selections?: ReadonlyArray<SelectionNode>
readonly distinctOnSelections?: ReadonlyArray<SelectionNode>
readonly isDistinct?: boolean
readonly joins?: ReadonlyArray<JoinNode>
readonly groupBy?: GroupByNode
readonly orderBy?: OrderByNode
readonly where?: WhereNode
readonly modifiers?: ReadonlyArray<SelectModifier>
readonly limit?: LimitNode
readonly offset?: OffsetNode
readonly with?: WithNode
readonly having?: HavingNode
}
/**
* @internal
*/
export const SelectQueryNode = freeze({
is(node: OperationNode): node is SelectQueryNode {
return node.kind === 'SelectQueryNode'
},
create(
fromItems: ReadonlyArray<TableExpressionNode>,
withNode?: WithNode
): SelectQueryNode {
return freeze({
kind: 'SelectQueryNode',
from: FromNode.create(fromItems),
...(withNode && { with: withNode }),
})
},
cloneWithSelections(
select: SelectQueryNode,
selections: ReadonlyArray<SelectionNode>
): SelectQueryNode {
return freeze({
...select,
selections: select.selections
? freeze([...select.selections, ...selections])
: freeze(selections),
})
},
cloneWithDistinctOnSelections(
select: SelectQueryNode,
selections: ReadonlyArray<SelectionNode>
): SelectQueryNode {
return freeze({
...select,
distinctOnSelections: select.distinctOnSelections
? freeze([...select.distinctOnSelections, ...selections])
: freeze(selections),
})
},
cloneWithModifier(
select: SelectQueryNode,
modifier: SelectModifier
): SelectQueryNode {
return freeze({
...select,
modifiers: select.modifiers
? freeze([...select.modifiers, modifier])
: freeze([modifier]),
})
},
cloneWithOrderByItem(
selectNode: SelectQueryNode,
item: OrderByItemNode
): SelectQueryNode {
return freeze({
...selectNode,
orderBy: selectNode.orderBy
? OrderByNode.cloneWithItem(selectNode.orderBy, item)
: OrderByNode.create(item),
})
},
cloneWithGroupByItems(
selectNode: SelectQueryNode,
items: ReadonlyArray<GroupByItemNode>
): SelectQueryNode {
return freeze({
...selectNode,
groupBy: selectNode.groupBy
? GroupByNode.cloneWithItems(selectNode.groupBy, items)
: GroupByNode.create(items),
})
},
cloneWithLimit(
selectNode: SelectQueryNode,
limit: LimitNode
): SelectQueryNode {
return freeze({
...selectNode,
limit,
})
},
cloneWithOffset(
selectNode: SelectQueryNode,
offset: OffsetNode
): SelectQueryNode {
return freeze({
...selectNode,
offset,
})
},
cloneWithHaving(
selectNode: SelectQueryNode,
op: 'And' | 'Or',
filter: HavingNodeChild
): SelectQueryNode {
return freeze({
...selectNode,
having: selectNode.having
? HavingNode.cloneWithFilter(selectNode.having, op, filter)
: HavingNode.create(filter),
})
},
cloneWithDistinct(selectNode: SelectQueryNode): SelectQueryNode {
return freeze({
...selectNode,
isDistinct: true,
})
},
})