-
Notifications
You must be signed in to change notification settings - Fork 280
/
raw-builder.ts
145 lines (121 loc) · 3.95 KB
/
raw-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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { QueryResult } from '../driver/database-connection.js'
import { AliasNode } from '../operation-node/alias-node.js'
import { OperationNode } from '../operation-node/operation-node.js'
import {
isOperationNodeSource,
OperationNodeSource,
} from '../operation-node/operation-node-source.js'
import { RawNode } from '../operation-node/raw-node.js'
import { ValueNode } from '../operation-node/value-node.js'
import { parseStringReference } from '../parser/reference-parser.js'
import { CompiledQuery } from '../query-compiler/compiled-query.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'
export class RawBuilder<O = unknown> implements OperationNodeSource {
readonly #props: RawBuilderProps
constructor(props: RawBuilderProps) {
this.#props = freeze(props)
}
as<A extends string>(alias: A): AliasedRawBuilder<O, A> {
return new AliasedRawBuilder(this, alias)
}
/**
* Change the output type of the raw expression.
*
* This doesn't produce any SQL. This methods simply returns a copy
* of this `RawBuilder` with a new output type.
*/
castTo<T>(): RawBuilder<T> {
return new RawBuilder({
...this.#props,
})
}
toOperationNode(): RawNode {
const parameterPlaceholderRegex = /(\?\??)/g
const sql = this.#props.sql
const parameters = this.#props.parameters ?? []
const sqlFragments: string[] = []
const argNodes: OperationNode[] = []
let paramIdx = 0
let sqlIdx = 0
let match: RegExpExecArray | null = null
while ((match = parameterPlaceholderRegex.exec(sql))) {
const str = match[1]
if (paramIdx >= parameters.length) {
throw new Error(
`value not provided for all placeholders in string ${sql}`
)
}
if (match.index > 0 && sql[match.index - 1] === '\\') {
continue
}
sqlFragments.push(sql.slice(sqlIdx, match.index).replaceAll('\\?', '?'))
argNodes.push(parseRawArg(str, parameters[paramIdx]))
sqlIdx = match.index + str.length
++paramIdx
}
sqlFragments.push(sql.slice(sqlIdx))
const rawNode = RawNode.create(sqlFragments, argNodes)
return this.#props.executor.transformQuery(rawNode, this.#props.queryId)
}
compile(): CompiledQuery {
return this.#props.executor.compileQuery(
this.toOperationNode(),
this.#props.queryId
)
}
async execute(): Promise<QueryResult<O>> {
return this.#props.executor.executeQuery<O>(
this.compile(),
this.#props.queryId
)
}
}
preventAwait(
RawBuilder,
"don't await RawBuilder instances directly. To execute the query you need to call `execute`"
)
/**
* {@link RawBuilder} with an alias. The result of calling {@link RawBuilder.as}.
*/
export class AliasedRawBuilder<O = unknown, A extends string = never>
implements OperationNodeSource
{
readonly #rawBuilder: RawBuilder<O>
readonly #alias: A
/**
* @private
*
* This needs to be here just so that the typings work. Without this
* the generated .d.ts file contains no reference to the type param A
* which causes this type to be equal to AliasedRawBuilder with any A
* as long as O is the same.
*/
protected get alias(): A {
return this.#alias
}
toOperationNode(): AliasNode {
return AliasNode.create(this.#rawBuilder.toOperationNode(), this.#alias)
}
constructor(rawBuilder: RawBuilder<O>, alias: A) {
this.#rawBuilder = rawBuilder
this.#alias = alias
}
}
function parseRawArg(match: string, arg: any): OperationNode {
if (isOperationNodeSource(arg)) {
return arg.toOperationNode()
} else if (match === '??') {
return parseStringReference(arg)
} else {
return ValueNode.create(arg)
}
}
export interface RawBuilderProps {
readonly queryId: QueryId
readonly executor: QueryExecutor
readonly sql: string
readonly parameters?: any
}