-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteDatabaseQuery.swift
More file actions
199 lines (180 loc) · 6.15 KB
/
SQLiteDatabaseQuery.swift
File metadata and controls
199 lines (180 loc) · 6.15 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
//
// SQLiteDatabaseQuery.swift
// feather-sqlite-database
//
// Created by Tibor Bödecs on 2026. 01. 10.
//
import FeatherDatabase
import SQLiteNIO
/// A SQLite query with SQL text and bound parameters.
///
/// Use this type to construct SQLite queries safely.
public struct SQLiteDatabaseQuery: DatabaseQuery {
/// 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: [SQLiteData]
/// 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: [SQLiteData] = []
) {
self.sql = sql
self.bindings = bindings
}
}
extension SQLiteDatabaseQuery: ExpressibleByStringInterpolation {
/// A string interpolation builder for SQLite 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: [SQLiteData]
/// 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
) {
self.sql.append(contentsOf: literal)
}
@inlinable
/// Append an interpolated optional string value.
///
/// Non-nil values are bound, and nil values emit `NULL`.
/// - Parameter value: The optional string value to interpolate.
/// - Returns: Nothing.
public mutating func appendInterpolation(
_ value: String?
) {
switch value {
case .some(let value):
self.binds.append(.text(value))
self.sql.append(contentsOf: "?")
case .none:
self.sql.append(contentsOf: "NULL")
}
}
@inlinable
/// Append an interpolated integer value.
///
/// The value is bound as a SQLite integer.
/// - Parameter value: The integer value to interpolate.
/// - Returns: Nothing.
public mutating func appendInterpolation(
_ value: Int
) {
self.binds.append(.integer(value))
self.sql.append(contentsOf: "?")
}
@inlinable
/// Append an interpolated floating-point value.
///
/// The value is bound as a SQLite float.
/// - Parameter value: The double value to interpolate.
/// - Returns: Nothing.
public mutating func appendInterpolation(
_ value: Double
) {
self.binds.append(.float(value))
self.sql.append(contentsOf: "?")
}
@inlinable
/// Append an interpolated string value.
///
/// The value is bound as a SQLite text value.
/// - Parameter value: The string value to interpolate.
/// - Returns: Nothing.
public mutating func appendInterpolation(
_ value: String
) {
self.binds.append(.text(value))
self.sql.append(contentsOf: "?")
}
@inlinable
/// Append an interpolated SQLite data value.
///
/// The value is bound directly as SQLite data.
/// - Parameter value: The SQLite data value to interpolate.
/// - Returns: Nothing.
public mutating func appendInterpolation(
_ value: SQLiteData
) {
self.binds.append(value)
self.sql.append(contentsOf: "?")
}
// @inlinable
// public mutating func appendInterpolation(_ value: SQLiteData?) throws {
// switch value {
// case .none:
// self.binds.append(.null)
// case .some(let value):
// self.binds.append(value)
// }
//
// self.sql.append(contentsOf: "?")
// }
@inlinable
/// Append an unescaped SQL fragment.
///
/// Use this only for trusted identifiers or SQL keywords.
/// - Parameter interpolated: The raw SQL fragment to insert.
/// - Returns: Nothing.
public mutating func appendInterpolation(
unescaped interpolated: String
) {
self.sql.append(contentsOf: 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 = []
}
}