-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseQuery.swift
More file actions
238 lines (216 loc) · 7.28 KB
/
DatabaseQuery.swift
File metadata and controls
238 lines (216 loc) · 7.28 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// DatabaseQuery.swift
// feather-database
//
// Created by Tibor Bödecs on 2026. 02. 07..
//
/// A database query with SQL text and bound parameters.
///
/// Use this type to construct database queries safely.
public struct DatabaseQuery: Sendable, Equatable, Hashable, Codable {
/// The SQL text to execute.
///
/// This is the raw SQL string for the query.
public var sql: String
/// The bound parameters for the SQL text.
///
/// These values are passed alongside `sql`.
public var bindings: [DatabaseQueryBindings]
/// Create a query from raw SQL and bindings.
///
/// Prefer string interpolation initializers when possible to bind values.
/// - Parameters:
/// - sql: The raw SQL string to execute.
/// - bindings: The bound parameters for the SQL.
public init(
unsafeSQL sql: String,
bindings: [DatabaseQueryBindings] = []
) {
self.sql = sql
self.bindings = bindings
}
}
extension DatabaseQuery: ExpressibleByStringInterpolation {
/// A string interpolation builder for database queries.
///
/// Use interpolation to bind values safely into SQL text.
public struct StringInterpolation: StringInterpolationProtocol, Sendable {
/// The string literal type used by the interpolation.
///
/// This matches the standard `String` literal type.
public typealias StringLiteralType = String
@usableFromInline
var sql: String
@usableFromInline
var binds: [DatabaseQueryBindings]
/// Create a new interpolation buffer.
///
/// Use the provided capacities to preallocate storage.
/// - Parameters:
/// - literalCapacity: The expected literal character count.
/// - interpolationCount: The expected number of interpolations.
public init(
literalCapacity: Int,
interpolationCount: Int
) {
self.sql = ""
self.sql.reserveCapacity(literalCapacity)
self.binds = []
self.binds.reserveCapacity(interpolationCount)
}
/// Append a literal string segment.
///
/// This adds raw SQL text to the builder.
/// - Parameter literal: The literal string segment.
public mutating func appendLiteral(
_ literal: String
) {
sql.append(contentsOf: literal)
}
/// Append an interpolated optional string value.
///
/// Non-nil values are bound, and nil values emit `NULL`.
/// - Parameter value: The optional string value to interpolate.
@inlinable
public mutating func appendInterpolation(
_ value: String?
) {
if let value {
binds.append(
.init(
index: binds.count,
binding: .string(value)
)
)
sql.append(contentsOf: "{{\(binds.count)}}")
}
else {
sql.append(contentsOf: "NULL")
}
}
/// Append an interpolated integer value.
///
/// The value is bound as an integer.
/// - Parameter value: The integer value to interpolate.
@inlinable
public mutating func appendInterpolation(
_ value: Int
) {
binds.append(
.init(
index: binds.count,
binding: .int(value)
)
)
sql.append(contentsOf: "{{\(binds.count)}}")
}
/// Append an interpolated floating-point value.
///
/// The value is bound as a floating-point value.
/// - Parameter value: The double value to interpolate.
@inlinable
public mutating func appendInterpolation(
_ value: Double
) {
binds.append(
.init(
index: binds.count,
binding: .double(value)
)
)
sql.append(contentsOf: "{{\(binds.count)}}")
}
/// Append an interpolated boolean value.
///
/// The value is bound as a boolean value.
/// - Parameter value: The boolean value to interpolate.
@inlinable
public mutating func appendInterpolation(
_ value: Bool
) {
binds.append(
.init(
index: binds.count,
binding: .bool(value)
)
)
sql.append(contentsOf: "{{\(binds.count)}}")
}
/// Append an interpolated string value.
///
/// The value is bound as a text value.
/// - Parameter value: The string value to interpolate.
@inlinable
public mutating func appendInterpolation(
_ value: String
) {
binds.append(
.init(
index: binds.count,
binding: .string(value)
)
)
sql.append(contentsOf: "{{\(binds.count)}}")
}
/// Append an unescaped SQL fragment.
///
/// Use this only for trusted identifiers or SQL keywords.
/// - Parameter interpolated: The raw SQL fragment to insert.
@inlinable
public mutating func appendInterpolation(
unescaped interpolated: String
) {
self.sql.append(contentsOf: interpolated)
}
/// Append an unescaped SQL fragment.
///
/// Use this only for trusted identifiers or SQL keywords.
/// - Parameter interpolated: The raw SQL fragment to insert.
@inlinable
public mutating func appendInterpolation(
unescaped interpolated: Int
) {
self.sql.append(contentsOf: String(interpolated))
}
/// Append an unescaped SQL fragment.
///
/// Use this only for trusted identifiers or SQL keywords.
/// - Parameter interpolated: The raw SQL fragment to insert.
@inlinable
public mutating func appendInterpolation(
unescaped interpolated: Double
) {
self.sql.append(contentsOf: String(interpolated))
}
/// Append an unescaped SQL fragment.
///
/// Use this only for trusted identifiers or SQL keywords.
/// - Parameter interpolated: The raw SQL fragment to insert.
@inlinable
public mutating func appendInterpolation(
unescaped interpolated: Bool
) {
self.sql.append(contentsOf: String(interpolated))
}
}
/// Create a query from a string interpolation builder.
///
/// This initializer is used by Swift string interpolation.
/// - Parameter stringInterpolation: The interpolation builder.
public init(
stringInterpolation: StringInterpolation
) {
self.sql = stringInterpolation.sql
self.bindings = stringInterpolation.binds
}
/// Create a query from a string literal.
///
/// This initializer does not add any bindings.
/// - Parameter value: The literal SQL string.
public init(
stringLiteral value: String
) {
self.sql = value
self.bindings = []
}
}