-
-
Notifications
You must be signed in to change notification settings - Fork 744
/
Copy pathDatabase+Statements.swift
569 lines (523 loc) · 21.3 KB
/
Database+Statements.swift
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Import C SQLite functions
#if GRDBCIPHER
import SQLCipher
#elseif SWIFT_PACKAGE
import GRDBSQLite
#elseif !GRDBCUSTOMSQLITE && !GRDBCIPHER
import SQLite3
#endif
import Foundation
extension Database {
// MARK: - Statements
/// Returns a new prepared statement that can be reused.
///
/// For example:
///
/// ```swift
/// let statement = try db.makeStatement(sql: "SELECT * FROM player WHERE id = ?")
/// let player1 = try Player.fetchOne(statement, arguments: [1])!
/// let player2 = try Player.fetchOne(statement, arguments: [2])!
///
/// let statement = try db.makeStatement(sql: "INSERT INTO player (name) VALUES (?)")
/// try statement.execute(arguments: ["Arthur"])
/// try statement.execute(arguments: ["Barbara"])
/// ```
///
/// - parameter sql: An SQL string.
/// - returns: A prepared statement.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func makeStatement(sql: String) throws -> Statement {
try makeStatement(sql: sql, prepFlags: 0)
}
/// Returns a new prepared statement that can be reused.
///
/// For example:
///
/// ```swift
/// let statement = try db.makeStatement(literal: "SELECT * FROM player WHERE id = ?")
/// let player1 = try Player.fetchOne(statement, arguments: [1])!
/// let player2 = try Player.fetchOne(statement, arguments: [2])!
///
/// let statement = try db.makeStatement(literal: "INSERT INTO player (name) VALUES (?)")
/// try statement.execute(arguments: ["Arthur"])
/// try statement.execute(arguments: ["Barbara"])
/// ```
///
/// In the provided literal, no argument must be set, or all arguments must
/// be set. An error is raised otherwise. For example:
///
/// ```swift
/// // OK
/// try makeStatement(literal: """
/// SELECT COUNT(*) FROM player WHERE score > ?
/// """)
/// try makeStatement(literal: """
/// SELECT COUNT(*) FROM player WHERE score > \(1000)
/// """)
///
/// // NOT OK (first argument is not set, but second is)
/// try makeStatement(literal: """
/// SELECT COUNT(*) FROM player
/// WHERE color = ? AND score > \(1000)
/// """)
/// ```
///
/// - parameter sqlLiteral: An ``SQL`` literal.
/// - returns: A prepared statement.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func makeStatement(literal sqlLiteral: SQL) throws -> Statement {
let (sql, arguments) = try sqlLiteral.build(self)
let statement = try makeStatement(sql: sql)
if arguments.isEmpty == false {
// Throws if arguments do not match
try statement.setArguments(arguments)
}
return statement
}
/// Returns a new prepared statement that can be reused.
///
/// For example:
///
/// let statement = try db.makeStatement(sql: "SELECT COUNT(*) FROM player WHERE score > ?", prepFlags: 0)
/// let moreThanTwentyCount = try Int.fetchOne(statement, arguments: [20])!
/// let moreThanThirtyCount = try Int.fetchOne(statement, arguments: [30])!
///
/// - parameter sql: An SQL string.
/// - parameter prepFlags: Flags for sqlite3_prepare_v3 (available from
/// SQLite 3.20.0, see <http://www.sqlite.org/c3ref/prepare.html>)
/// - returns: A prepared statement.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
func makeStatement(sql: String, prepFlags: CUnsignedInt) throws -> Statement {
let statements = SQLStatementCursor(database: self, sql: sql, arguments: nil, prepFlags: prepFlags)
guard let statement = try statements.next() else {
throw DatabaseError(
resultCode: .SQLITE_ERROR,
message: "empty statement",
sql: sql)
}
do {
guard try statements.next() == nil else {
throw DatabaseError(
resultCode: .SQLITE_MISUSE,
message: """
Multiple statements found. To execute multiple statements, use \
Database.execute(sql:) or Database.allStatements(sql:) instead.
""",
sql: sql)
}
} catch {
// Something while would not compile was found after the first statement.
// Complain about multiple statements anyway.
throw DatabaseError(
resultCode: .SQLITE_MISUSE,
message: """
Multiple statements found. To execute multiple statements, use \
Database.execute(sql:) or Database.allStatements(sql:) instead.
""",
sql: sql)
}
return statement
}
/// Returns a prepared statement that can be reused.
///
/// For example:
///
/// ```swift
/// let statement = try db.cachedStatement(sql: "SELECT * FROM player WHERE id = ?")
/// let player1 = try Player.fetchOne(statement, arguments: [1])!
/// let player2 = try Player.fetchOne(statement, arguments: [2])!
///
/// let statement = try db.cachedStatement(sql: "INSERT INTO player (name) VALUES (?)")
/// try statement.execute(arguments: ["Arthur"])
/// try statement.execute(arguments: ["Barbara"])
/// ```
///
/// The returned statement may have already been used: it may or may not
/// contain values for its eventual arguments.
///
/// - parameter sql: An SQL string.
/// - returns: A prepared statement.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func cachedStatement(sql: String) throws -> Statement {
try publicStatementCache.statement(sql)
}
/// Returns a prepared statement that can be reused.
///
/// For example:
///
/// ```swift
/// let statement = try db.cachedStatement(literal: "SELECT * FROM player WHERE id = ?")
/// let player1 = try Player.fetchOne(statement, arguments: [1])!
/// let player2 = try Player.fetchOne(statement, arguments: [2])!
///
/// let statement = try db.cachedStatement(literal: "INSERT INTO player (name) VALUES (?)")
/// try statement.execute(arguments: ["Arthur"])
/// try statement.execute(arguments: ["Barbara"])
/// ```
///
/// In the provided literal, no argument must be set, or all arguments must
/// be set. An error is raised otherwise. For example:
///
/// ```swift
/// // OK
/// try cachedStatement(literal: """
/// SELECT COUNT(*) FROM player WHERE score > ?
/// """)
/// try cachedStatement(literal: """
/// SELECT COUNT(*) FROM player WHERE score > \(1000)
/// """)
///
/// // NOT OK (first argument is not set, but second is)
/// try cachedStatement(literal: """
/// SELECT COUNT(*) FROM player
/// WHERE color = ? AND score > \(1000)
/// """)
/// ```
///
/// - parameter sqlLiteral: An ``SQL`` literal.
/// - returns: A prepared statement.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func cachedStatement(literal sqlLiteral: SQL) throws -> Statement {
let (sql, arguments) = try sqlLiteral.build(self)
let statement = try cachedStatement(sql: sql)
if arguments.isEmpty == false {
// Throws if arguments do not match
try statement.setArguments(arguments)
}
return statement
}
/// Returns a cached statement that does not conflict with user's cached statements.
func internalCachedStatement(sql: String) throws -> Statement {
try internalStatementCache.statement(sql)
}
/// Returns a cursor of prepared statements.
///
/// For example:
///
/// ```swift
/// let statements = try db.allStatements(sql: """
/// INSERT INTO player (name) VALUES (?);
/// INSERT INTO player (name) VALUES (?);
/// INSERT INTO player (name) VALUES (?);
/// """, arguments: ["Arthur", "Barbara", "O'Brien"])
/// while let statement = try statements.next() {
/// try statement.execute()
/// }
/// ```
///
/// The `arguments` parameter must be nil, or all arguments must be set. The
/// returned cursor will throw an error otherwise. For example:
///
/// ```swift
/// // OK
/// try allStatements(sql: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """)
///
/// try allStatements(sql: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """, arguments: [1000, 1000])
///
/// // NOT OK (first argument is set, but second is not)
/// try allStatements(sql: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """, arguments: [1000])
/// ```
///
/// - parameters:
/// - sql: An SQL string.
/// - arguments: Statement arguments.
/// - returns: A cursor of prepared ``Statement``.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func allStatements(sql: String, arguments: StatementArguments? = nil)
throws -> SQLStatementCursor
{
SQLStatementCursor(database: self, sql: sql, arguments: arguments)
}
/// Returns a cursor of prepared statements.
///
/// ``SQL`` literals allow you to safely embed raw values in your SQL,
/// without any risk of syntax errors or SQL injection:
///
/// ```swift
/// let statements = try db.allStatements(literal: """
/// INSERT INTO player (name) VALUES (\("Arthur"));
/// INSERT INTO player (name) VALUES (\("Barbara"));
/// INSERT INTO player (name) VALUES (\("O'Brien"));
/// """)
/// while let statement = try statements.next() {
/// try statement.execute()
/// }
/// ```
///
/// In the provided literal, no argument must be set, or all arguments must
/// be set. The returned cursor will throw an error otherwise. For example:
///
/// ```swift
/// // OK
/// try allStatements(literal: """
/// SELECT COUNT(*) FROM player WHERE score < ?;
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """)
///
/// try allStatements(literal: """
/// SELECT COUNT(*) FROM player WHERE score < \(1000);
/// SELECT COUNT(*) FROM player WHERE score > \(1000);
/// """)
///
/// // NOT OK (first argument is set, but second is not)
/// try allStatements(literal: """
/// SELECT COUNT(*) FROM player WHERE score < \(1000);
/// SELECT COUNT(*) FROM player WHERE score > ?;
/// """)
/// ```
///
/// - parameter sqlLiteral: An ``SQL`` literal.
/// - returns: A cursor of prepared ``Statement``.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func allStatements(literal sqlLiteral: SQL) throws -> SQLStatementCursor {
let context = SQLGenerationContext(self)
let sql = try sqlLiteral.sql(context)
let arguments = context.arguments.isEmpty
? nil // builds statements without arguments
: context.arguments // force arguments to match
return SQLStatementCursor(database: self, sql: sql, arguments: arguments)
}
/// Executes one or several SQL statements.
///
/// For example:
///
/// ```swift
/// try db.execute(
/// sql: "INSERT INTO player (name) VALUES (:name)",
/// arguments: ["name": "Arthur"])
///
/// try db.execute(sql: """
/// INSERT INTO player (name) VALUES (?);
/// INSERT INTO player (name) VALUES (?);
/// INSERT INTO player (name) VALUES (?);
/// """, arguments: ["Arthur", "Barbara", "O'Brien"])
/// ```
///
/// - parameters:
/// - sql: An SQL string.
/// - arguments: Statement arguments.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func execute(sql: String, arguments: StatementArguments = StatementArguments()) throws {
try execute(literal: SQL(sql: sql, arguments: arguments))
}
/// Executes one or several SQL statements.
///
/// ``SQL`` literals allow you to safely embed raw values in your SQL,
/// without any risk of syntax errors or SQL injection:
///
/// ```swift
/// try db.execute(literal: """
/// INSERT INTO player (name) VALUES (\("Arthur"))
/// """)
///
/// try db.execute(literal: """
/// INSERT INTO player (name) VALUES (\("Arthur"));
/// INSERT INTO player (name) VALUES (\("Barbara"));
/// INSERT INTO player (name) VALUES (\("O'Brien"));
/// """)
/// ```
///
/// - parameter sqlLiteral: An ``SQL`` literal.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
public func execute(literal sqlLiteral: SQL) throws {
let statements = try allStatements(literal: sqlLiteral)
while let statement = try statements.next() {
try statement.execute()
}
}
}
/// A cursor over all statements in an SQL string.
public class SQLStatementCursor {
private let database: Database
private let cString: ContiguousArray<CChar>
private let prepFlags: CUnsignedInt
private let initialArgumentCount: Int?
// Mutated by iteration
private var offset: Int // offset in the C string
private var arguments: StatementArguments? // Nil when arguments are set later
init(database: Database, sql: String, arguments: StatementArguments?, prepFlags: CUnsignedInt = 0) {
self.database = database
self.cString = sql.utf8CString
self.prepFlags = prepFlags
self.initialArgumentCount = arguments?.values.count
self.offset = 0
self.arguments = arguments
}
}
// Explicit non-conformance to Sendable: database cursors must be used from
// a serialized database access dispatch queue.
@available(*, unavailable)
extension SQLStatementCursor: Sendable { }
extension SQLStatementCursor: Cursor {
public func next() throws -> Statement? {
guard offset < cString.count - 1 /* trailing \0 */ else {
// End of C string -> end of cursor.
try checkArgumentsAreEmpty()
return nil
}
return try cString.withUnsafeBufferPointer { buffer in
let baseAddress = buffer.baseAddress! // never nil because the buffer contains the trailing \0.
// Compile next statement
var statementEnd: UnsafePointer<CChar>? = nil
let statement = try Statement(
database: database,
statementStart: baseAddress + offset,
statementEnd: &statementEnd,
prepFlags: prepFlags)
// Advance to next statement
offset = statementEnd! - baseAddress // never nil because statement compilation did not fail.
guard let statement else {
// No statement found -> end of cursor.
try checkArgumentsAreEmpty()
return nil
}
if arguments != nil {
// Extract statement arguments
let bindings = try arguments!.extractBindings(
forStatement: statement,
allowingRemainingValues: true)
// unchecked is OK because we just extracted the correct
// number of arguments
statement.setUncheckedArguments(StatementArguments(bindings))
}
return statement
}
}
/// Check that all arguments were consumed: it is a programmer error to
/// provide arguments that do not match the statements.
private func checkArgumentsAreEmpty() throws {
if let arguments,
let initialArgumentCount,
arguments.values.isEmpty == false
{
throw DatabaseError(
resultCode: .SQLITE_MISUSE,
message: "wrong number of statement arguments: \(initialArgumentCount)")
}
}
}
extension Database {
/// Makes sure statement can be executed, and prepares database observation.
@usableFromInline
func statementWillExecute(_ statement: Statement) throws {
// Aborted transactions prevent statement execution (see the
// documentation of this method for more information).
try checkForAbortedTransaction(sql: statement.sql, arguments: statement.arguments)
// Cancelled database accesses must not execute.
// Suspended databases must not execute statements that create the risk
// of `0xdead10cc` exception (see the documentation of this method for
// more information).
try checkForSuspensionViolation(from: statement)
// Record the database region selected by the statement execution.
try registerAccess(to: statement.databaseRegion)
// Database observation: prepare transaction observers.
observationBroker?.statementWillExecute(statement)
}
/// May throw a cancelled commit error, if a transaction observer cancels
/// an empty transaction.
@usableFromInline
func statementDidExecute(_ statement: Statement) throws {
if statement.invalidatesDatabaseSchemaCache {
clearSchemaCache()
}
checkForAutocommitTransition()
// Database observation: cleanup
try observationBroker?.statementDidExecute(statement)
}
/// Always throws an error
@usableFromInline
func statementDidFail(_ statement: Statement, withResultCode resultCode: CInt) throws -> Never {
// Failed statements can not be reused, because `sqlite3_reset` won't
// be able to restore the statement to its initial state:
// https://www.sqlite.org/c3ref/reset.html
//
// So make sure we clear this statement from the cache.
internalStatementCache.remove(statement)
publicStatementCache.remove(statement)
checkForAutocommitTransition()
// Extract values that may be modified by the user in their
// `TransactionObserver.databaseDidRollback(_:)` implementation
// (see below).
let message = lastErrorMessage
let arguments = statement.arguments
// Database observation: cleanup.
//
// If the statement failure is due to a transaction observer that has
// cancelled a transaction, this calls `TransactionObserver.databaseDidRollback(_:)`,
// and throws the user-provided cancelled commit error.
try observationBroker?.statementDidFail(statement)
switch ResultCode(rawValue: resultCode) {
case .SQLITE_INTERRUPT, .SQLITE_ABORT:
// The only error that a user sees when a Task is cancelled
// is CancellationError.
try suspensionMutex.load().checkCancellation()
default:
break
}
// Throw statement failure
throw DatabaseError(
resultCode: resultCode,
message: message,
sql: statement.sql,
arguments: arguments,
publicStatementArguments: configuration.publicStatementArguments)
}
private func checkForAutocommitTransition() {
if sqlite3_get_autocommit(sqliteConnection) == 0 {
if autocommitState == .on {
// Record transaction date as soon as the connection leaves
// auto-commit mode.
// We grab a result, so that this failure is later reported
// whenever the user calls `Database.transactionDate`.
transactionDateResult = Result { try configuration.transactionClock.now(self) }
}
autocommitState = .off
} else {
if autocommitState == .off {
// Reset transaction date
transactionDateResult = nil
}
autocommitState = .on
}
}
}
/// A thread-unsafe statement cache
struct StatementCache {
unowned let db: Database
private var statements: [String: Statement] = [:]
init(database: Database) {
self.db = database
}
mutating func statement(_ sql: String) throws -> Statement {
if let statement = statements[sql] {
return statement
}
// http://www.sqlite.org/c3ref/c_prepare_persistent.html#sqlitepreparepersistent
// > The SQLITE_PREPARE_PERSISTENT flag is a hint to the query
// > planner that the prepared statement will be retained for a long
// > time and probably reused many times.
//
// This looks like a perfect match for cached statements.
let statement = try db.makeStatement(sql: sql, prepFlags: CUnsignedInt(SQLITE_PREPARE_PERSISTENT))
statements[sql] = statement
return statement
}
mutating func clear() {
statements = [:]
}
mutating func remove(_ statement: Statement) {
statements.removeFirst { $0.value === statement }
}
mutating func removeAll(where shouldBeRemoved: (Statement) -> Bool) {
statements = statements.filter { (_, statement) in !shouldBeRemoved(statement) }
}
}