-
Notifications
You must be signed in to change notification settings - Fork 280
/
create-view-builder.ts
124 lines (110 loc) · 3.56 KB
/
create-view-builder.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
import { OperationNodeSource } from '../operation-node/operation-node-source.js'
import { CompiledQuery } from '../query-compiler/compiled-query.js'
import { Compilable } from '../util/compilable.js'
import { preventAwait } from '../util/prevent-await.js'
import { QueryExecutor } from '../query-executor/query-executor.js'
import { QueryId } from '../util/query-id.js'
import { freeze } from '../util/object-utils.js'
import { CreateViewNode } from '../operation-node/create-view-node.js'
import { parseColumnName } from '../parser/reference-parser.js'
import { AnySelectQueryBuilder } from '../util/type-utils.js'
import { ImmediateValuePlugin } from '../plugin/immediate-value/immediate-value-plugin.js'
import { RawBuilder } from '../raw-builder/raw-builder.js'
export class CreateViewBuilder implements OperationNodeSource, Compilable {
readonly #props: CreateViewBuilderProps
constructor(props: CreateViewBuilderProps) {
this.#props = freeze(props)
}
/**
* Adds the "temporary" modifier.
*
* Use this to create a temporary view.
*/
temporary(): CreateViewBuilder {
return new CreateViewBuilder({
...this.#props,
node: CreateViewNode.cloneWith(this.#props.node, {
temporary: true,
}),
})
}
materialized(): CreateViewBuilder {
return new CreateViewBuilder({
...this.#props,
node: CreateViewNode.cloneWith(this.#props.node, {
materialized: true,
}),
})
}
/**
* Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
*/
ifNotExists(): CreateViewBuilder {
return new CreateViewBuilder({
...this.#props,
node: CreateViewNode.cloneWith(this.#props.node, {
ifNotExists: true,
}),
})
}
orReplace(): CreateViewBuilder {
return new CreateViewBuilder({
...this.#props,
node: CreateViewNode.cloneWith(this.#props.node, {
orReplace: true,
}),
})
}
columns(columns: string[]): CreateViewBuilder {
return new CreateViewBuilder({
...this.#props,
node: CreateViewNode.cloneWith(this.#props.node, {
columns: columns.map(parseColumnName),
}),
})
}
/**
* Sets the select query or a `values` statement that creates the view.
*
* WARNING!
* Some dialects don't support parameterized queries in DDL statements and therefore
* the query or raw {@link sql } expression passed here is interpolated into a single
* string opening an SQL injection vulnerability. DO NOT pass unchecked user input
* into the query or raw expression passed to this method!
*/
as(query: AnySelectQueryBuilder | RawBuilder<any>): CreateViewBuilder {
const queryNode = query
.withPlugin(new ImmediateValuePlugin())
.toOperationNode()
return new CreateViewBuilder({
...this.#props,
node: CreateViewNode.cloneWith(this.#props.node, {
as: queryNode,
}),
})
}
toOperationNode(): CreateViewNode {
return this.#props.executor.transformQuery(
this.#props.node,
this.#props.queryId
)
}
compile(): CompiledQuery {
return this.#props.executor.compileQuery(
this.toOperationNode(),
this.#props.queryId
)
}
async execute(): Promise<void> {
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId)
}
}
preventAwait(
CreateViewBuilder,
"don't await CreateViewBuilder instances directly. To execute the query you need to call `execute`"
)
export interface CreateViewBuilderProps {
readonly queryId: QueryId
readonly executor: QueryExecutor
readonly node: CreateViewNode
}