/
Database.swift
2224 lines (1989 loc) · 83 KB
/
Database.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Foundation
/// A raw SQLite connection, suitable for the SQLite C API.
public typealias SQLiteConnection = OpaquePointer
/// A raw SQLite function argument.
typealias SQLiteValue = OpaquePointer
let SQLITE_TRANSIENT = unsafeBitCast(OpaquePointer(bitPattern: -1), to: sqlite3_destructor_type.self)
/// An SQLite connection.
///
/// You don't create `Database` instances directly. Instead, you connect to a
/// database with one of the <doc:DatabaseConnections>, and you use a database
/// access method. For example:
///
/// ```swift
/// let dbQueue = try DatabaseQueue()
///
/// try dbQueue.write { (db: Database) in
/// try Player(name: "Arthur").insert(db)
/// }
/// ```
///
/// `Database` methods that modify, query, or validate the database schema are
/// listed in <doc:DatabaseSchema>.
///
/// ## Topics
///
/// ### Database Information
///
/// - ``changesCount``
/// - ``configuration``
/// - ``debugDescription``
/// - ``description``
/// - ``lastErrorCode``
/// - ``lastErrorMessage``
/// - ``lastInsertedRowID``
/// - ``maximumStatementArgumentCount``
/// - ``sqliteConnection``
/// - ``totalChangesCount``
/// - ``SQLiteConnection``
///
/// ### Database Statements
///
/// - ``allStatements(literal:)``
/// - ``allStatements(sql:arguments:)``
/// - ``cachedStatement(literal:)``
/// - ``cachedStatement(sql:)``
/// - ``execute(literal:)``
/// - ``execute(sql:arguments:)``
/// - ``makeStatement(literal:)``
/// - ``makeStatement(sql:)``
/// - ``SQLStatementCursor``
///
/// ### Database Transactions
///
/// - ``beginTransaction(_:)``
/// - ``commit()``
/// - ``inSavepoint(_:)``
/// - ``inTransaction(_:_:)``
/// - ``isInsideTransaction``
/// - ``readOnly(_:)``
/// - ``rollback()``
/// - ``transactionDate``
/// - ``TransactionCompletion``
/// - ``TransactionKind``
///
/// ### Printing Database Content
///
/// - ``dumpContent(format:to:)``
/// - ``dumpRequest(_:format:to:)``
/// - ``dumpSchema(to:)``
/// - ``dumpSQL(_:format:to:)``
/// - ``dumpTables(_:format:tableHeader:stableOrder:to:)``
/// - ``DumpFormat``
/// - ``DumpTableHeaderOptions``
///
/// ### Database Observation
///
/// - ``add(transactionObserver:extent:)``
/// - ``remove(transactionObserver:)``
/// - ``afterNextTransaction(onCommit:onRollback:)``
/// - ``notifyChanges(in:)``
/// - ``registerAccess(to:)``
///
/// ### Collations
///
/// - ``add(collation:)``
/// - ``reindex(collation:)-171fj``
/// - ``reindex(collation:)-2hxil``
/// - ``remove(collation:)``
/// - ``CollationName``
/// - ``DatabaseCollation``
///
/// ### SQL Functions
///
/// - ``add(function:)``
/// - ``remove(function:)``
/// - ``DatabaseFunction``
///
/// ### Notifications
///
/// - ``resumeNotification``
/// - ``suspendNotification``
///
/// ### Other Database Operations
///
/// - ``add(tokenizer:)``
/// - ``backup(to:pagesPerStep:progress:)``
/// - ``checkpoint(_:on:)``
/// - ``clearSchemaCache()``
/// - ``logError``
/// - ``releaseMemory()``
/// - ``trace(options:_:)``
/// - ``CheckpointMode``
/// - ``DatabaseBackupProgress``
/// - ``StorageClass``
/// - ``TraceEvent``
/// - ``TracingOptions``
public final class Database: CustomStringConvertible, CustomDebugStringConvertible {
// The Database class is not thread-safe. An instance should always be
// used through a SerializedDatabase.
// MARK: - SQLite C API
// TODO: make it non optional, since one can't get a `Database` instance
// after `close()`.
/// The raw SQLite connection, suitable for the SQLite C API.
///
/// The result is nil after the database has been successfully closed with
/// ``DatabaseReader/close()``.
public private(set) var sqliteConnection: SQLiteConnection?
// MARK: - Configuration
/// The error logging function.
///
/// Related SQLite documentation: <https://www.sqlite.org/errlog.html>
public static var logError: LogErrorFunction? = nil {
didSet {
if logError != nil {
_registerErrorLogCallback { (_, code, message) in
guard let logError = Database.logError else { return }
guard let message = message.map(String.init) else { return }
let resultCode = ResultCode(rawValue: code)
logError(resultCode, message)
}
} else {
_registerErrorLogCallback(nil)
}
}
}
/// The database configuration.
public let configuration: Configuration
/// A description of this database connection.
///
/// The returned string is based on the ``Configuration/label``
/// of ``configuration``.
public let description: String
public var debugDescription: String { "<Database: \(description)>" }
// MARK: - Database Information
/// The rowID of the most recently inserted row.
///
/// If no row has ever been inserted using this database connection,
/// the last inserted rowID is zero.
///
/// Related SQLite documentation: <https://www.sqlite.org/c3ref/last_insert_rowid.html>
public var lastInsertedRowID: Int64 {
SchedulingWatchdog.preconditionValidQueue(self)
return sqlite3_last_insert_rowid(sqliteConnection)
}
/// The number of rows modified, inserted or deleted by the most recent
/// successful INSERT, UPDATE or DELETE statement.
///
/// Related SQLite documentation: <https://www.sqlite.org/c3ref/changes.html>
public var changesCount: Int {
SchedulingWatchdog.preconditionValidQueue(self)
return Int(sqlite3_changes(sqliteConnection))
}
/// The total number of rows modified, inserted or deleted by all successful
/// INSERT, UPDATE or DELETE statements since the database connection was
/// opened.
///
/// Related SQLite documentation: <https://www.sqlite.org/c3ref/total_changes.html>
public var totalChangesCount: Int {
SchedulingWatchdog.preconditionValidQueue(self)
return Int(sqlite3_total_changes(sqliteConnection))
}
/// A Boolean value indicating whether the database connection is currently
/// inside a transaction.
///
/// A database is inside a transaction if and only if it is not in the
/// autocommit mode. See <https://sqlite.org/c3ref/get_autocommit.html>.
public var isInsideTransaction: Bool {
// https://sqlite.org/c3ref/get_autocommit.html
//
// > The sqlite3_get_autocommit() interface returns non-zero or zero if
// > the given database connection is or is not in autocommit mode,
// > respectively.
//
// > Autocommit mode is on by default. Autocommit mode is disabled by a
// > BEGIN statement. Autocommit mode is re-enabled by a COMMIT
// > or ROLLBACK.
//
// > If another thread changes the autocommit status of the database
// > connection while this routine is running, then the return value
// > is undefined.
SchedulingWatchdog.preconditionValidQueue(self)
if sqliteConnection == nil { return false } // Support for SerializedDatabase.deinit
return sqlite3_get_autocommit(sqliteConnection) == 0
}
/// The last error code.
public var lastErrorCode: ResultCode { ResultCode(rawValue: sqlite3_errcode(sqliteConnection)) }
/// The last error message.
public var lastErrorMessage: String? { String(cString: sqlite3_errmsg(sqliteConnection)) }
// MARK: - Internal properties
let path: String
/// Support for schema changes performed with ``DatabasePool``: each read
/// access needs to clear the schema cache if the schema has been modified
/// by the writer connection since the previous read. This property is reset
/// to `PRAGMA schema_version` at the beginning of each DatabasePool read.
///
/// DatabasePool writer connection and DatabaseQueue do not perform such
/// automatic schema management: they won't clear their schema cache if an
/// external connection modifies the schema.
///
/// See `clearSchemaCacheIfNeeded()`.
var lastSchemaVersion: Int32?
/// The cache for the available database schemas (main, temp, attached databases).
var schemaCache = SchemaCache()
/// The cache for statements managed by GRDB. It is distinct from
/// `publicStatementCache` so that we do not mess with statement arguments
/// set by the user.
lazy var internalStatementCache = StatementCache(database: self)
/// The cache for statements managed by the user.
lazy var publicStatementCache = StatementCache(database: self)
/// The database authorizer provides information about compiled
/// database statements, and prevents the truncate optimization when
/// row deletions are observed by transaction observers.
lazy var authorizer = StatementAuthorizer(self)
/// The observation broker supports database observation and
/// transaction observers.
///
/// It is nil in read-only connections, because we do not report read-only
/// transactions to transaction observers.
private(set) var observationBroker: DatabaseObservationBroker?
/// The list of compile options used when building SQLite
static func sqliteCompileOptions() throws -> Set<String> {
try DatabaseQueue().inDatabase {
try Set(String.fetchCursor($0, sql: "PRAGMA COMPILE_OPTIONS"))
}
}
/// Whether the database region selected by statement execution is
/// recorded into `selectedRegion` by `track(_:)`.
///
/// To start recording the selected region, use `recordingSelection(_:_:)`.
private(set) var isRecordingSelectedRegion = false
/// The database region selected by statement execution, when
/// `isRecordingSelectedRegion` is true.
var selectedRegion = DatabaseRegion()
/// Support for `checkForAbortedTransaction()`
var isInsideTransactionBlock = false
/// Support for `checkForSuspensionViolation(from:)`
@LockedBox var isSuspended = false
/// Support for `checkForSuspensionViolation(from:)`
/// This cache is never cleared: we assume journal mode never changes.
var journalModeCache: String?
// MARK: - Transaction Date
enum AutocommitState {
case off
case on
}
/// The state of the auto-commit mode, as left by the last
/// executed statement.
///
/// The goal of this property is to detect changes in the auto-commit mode.
/// When you need to know if the database is currently in the auto-commit
/// mode, always prefer ``isInsideTransaction``.
var autocommitState = AutocommitState.on
/// The date of the current transaction, wrapped in a result that is an
/// error if there was an error grabbing this date when the transaction has
/// started.
///
/// Invariant: `transactionDateResult` is nil iff connection is not
/// inside a transaction.
var transactionDateResult: Result<Date, Error>?
/// The date of the current transaction.
///
/// The returned date is constant at any point during a transaction. It is
/// set when the database leaves the
/// [autocommit mode](https://www.sqlite.org/c3ref/get_autocommit.html) with
/// a `BEGIN` statement.
///
/// When the database is not currently in a transaction, a new date is
/// returned on each call.
///
/// See <doc:RecordTimestamps> for an example of usage.
///
/// The transaction date, by default, is the start date of the current
/// transaction. You can override this default behavior by configuring
/// ``Configuration/transactionClock``.
public var transactionDate: Date {
get throws {
SchedulingWatchdog.preconditionValidQueue(self)
// Check invariant: `transactionDateResult` is nil iff connection
// is not inside a transaction.
assert(isInsideTransaction || transactionDateResult == nil)
if let transactionDateResult {
return try transactionDateResult.get()
} else {
return try configuration.transactionClock.now(self)
}
}
}
// MARK: - Private properties
/// Support for ``Configuration/busyMode``.
private var busyCallback: BusyCallback?
/// Support for ``trace(options:_:)``.
private var trace: ((TraceEvent) -> Void)?
/// The registered custom SQL functions.
private var functions = Set<DatabaseFunction>()
/// The registered custom SQL collations.
private var collations = Set<DatabaseCollation>()
/// Support for `beginReadOnly()` and `endReadOnly()`.
private var readOnlyDepth = 0
// MARK: - Initializer
init(
path: String,
description: String,
configuration: Configuration) throws
{
self.sqliteConnection = try Database.openConnection(path: path, flags: configuration.SQLiteOpenFlags)
self.description = description
self.configuration = configuration
self.path = path
// We do not report read-only transactions to transaction observers, so
// don't bother installing the observation broker for read-only connections.
if !configuration.readonly {
observationBroker = DatabaseObservationBroker(self)
}
}
deinit {
assert(sqliteConnection == nil)
}
// MARK: - Database Opening
private static func openConnection(path: String, flags: CInt) throws -> SQLiteConnection {
// See <https://www.sqlite.org/c3ref/open.html>
var sqliteConnection: SQLiteConnection? = nil
let code = sqlite3_open_v2(path, &sqliteConnection, flags, nil)
guard code == SQLITE_OK else {
// https://www.sqlite.org/c3ref/open.html
// > Whether or not an error occurs when it is opened, resources
// > associated with the database connection handle should be
// > released by passing it to sqlite3_close() when it is no
// > longer required.
//
// https://www.sqlite.org/c3ref/close.html
// > Calling sqlite3_close() or sqlite3_close_v2() with a NULL
// > pointer argument is a harmless no-op.
_ = sqlite3_close(sqliteConnection) // ignore result code
throw DatabaseError(resultCode: code)
}
guard let sqliteConnection else {
throw DatabaseError(resultCode: .SQLITE_INTERNAL) // WTF SQLite?
}
return sqliteConnection
}
// MARK: - Database Setup
/// This method must be called after database initialization
func setUp() throws {
setupBusyMode()
setupDoubleQuotedStringLiterals()
try setupForeignKeys()
setupDefaultFunctions()
setupDefaultCollations()
setupAuthorizer()
observationBroker?.installCommitAndRollbackHooks()
try activateExtendedCodes()
#if SQLITE_HAS_CODEC
try validateSQLCipher()
#endif
// Last step before we can start accessing the database.
try configuration.setUp(self)
try validateFormat()
configuration.SQLiteConnectionDidOpen?()
}
/// Performs ``Configuration/JournalModeConfiguration/wal``.
func setUpWALMode() throws {
let journalMode = try String.fetchOne(self, sql: "PRAGMA journal_mode = WAL")
guard journalMode == "wal" else {
throw DatabaseError(message: "could not activate WAL Mode at path: \(path)")
}
// https://www.sqlite.org/pragma.html#pragma_synchronous
// > Many applications choose NORMAL when in WAL mode
try execute(sql: "PRAGMA synchronous = NORMAL")
// Make sure a non-empty wal file exists.
//
// The presence of the wal file avoids an SQLITE_CANTOPEN (14)
// error when the user opens a pool and reads from it.
// See <https://github.com/groue/GRDB.swift/issues/102>.
//
// The non-empty wal file avoids an SQLITE_ERROR (1) error
// when the user opens a pool and creates a wal snapshot
// (which happens when starting a ValueObservation).
// See <https://github.com/groue/GRDB.swift/issues/1383>.
let walPath = path + "-wal"
if try FileManager.default.fileExists(atPath: walPath) == false
|| (URL(fileURLWithPath: walPath).resourceValues(forKeys: [.fileSizeKey]).fileSize ?? 0) == 0
{
try inSavepoint {
try execute(sql: """
CREATE TABLE grdb_issue_102 (id INTEGER PRIMARY KEY);
DROP TABLE grdb_issue_102;
""")
return .commit
}
}
}
private func setupDoubleQuotedStringLiterals() {
if configuration.acceptsDoubleQuotedStringLiterals {
_enableDoubleQuotedStringLiterals(sqliteConnection)
} else {
_disableDoubleQuotedStringLiterals(sqliteConnection)
}
}
private func setupForeignKeys() throws {
// Foreign keys are disabled by default with SQLite3
if configuration.foreignKeysEnabled {
try execute(sql: "PRAGMA foreign_keys = ON")
}
}
private func setupBusyMode() {
let busyMode = configuration.readonly
? configuration.readonlyBusyMode ?? configuration.busyMode
: configuration.busyMode
switch busyMode {
case .immediateError:
break
case .timeout(let duration):
let milliseconds = CInt(duration * 1000)
sqlite3_busy_timeout(sqliteConnection, milliseconds)
case .callback(let callback):
busyCallback = callback
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
sqlite3_busy_handler(
sqliteConnection,
{ (dbPointer, numberOfTries) in
let db = Unmanaged<Database>.fromOpaque(dbPointer!).takeUnretainedValue()
let callback = db.busyCallback!
return callback(Int(numberOfTries)) ? 1 : 0
},
dbPointer)
}
}
private func setupDefaultFunctions() {
add(function: .capitalize)
add(function: .lowercase)
add(function: .uppercase)
add(function: .localizedCapitalize)
add(function: .localizedLowercase)
add(function: .localizedUppercase)
}
private func setupDefaultCollations() {
add(collation: .unicodeCompare)
add(collation: .caseInsensitiveCompare)
add(collation: .localizedCaseInsensitiveCompare)
add(collation: .localizedCompare)
add(collation: .localizedStandardCompare)
}
private func setupAuthorizer() {
// SQLite authorizer is set only once per database connection.
//
// This is because authorizer changes have SQLite invalidate statements,
// with undesired side effects. See:
//
// - DatabaseCursorTests.testIssue583()
// - http://sqlite.1065341.n5.nabble.com/Issue-report-sqlite3-set-authorizer-triggers-error-4-516-SQLITE-ABORT-ROLLBACK-during-statement-itern-td107972.html
authorizer.register()
}
private func activateExtendedCodes() throws {
if (configuration.SQLiteOpenFlags & 0x02000000 /* SQLITE_OPEN_EXRESCODE */) != 0 {
// Nothing to do
return
}
let code = sqlite3_extended_result_codes(sqliteConnection, 1)
guard code == SQLITE_OK else {
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
}
}
#if SQLITE_HAS_CODEC
private func validateSQLCipher() throws {
// https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688
//
// > In order to avoid situations where SQLite might be used
// > improperly at runtime, we strongly recommend that
// > applications institute a runtime test to ensure that the
// > application is actually using SQLCipher on the active
// > connection.
if try String.fetchOne(self, sql: "PRAGMA cipher_version") == nil {
throw DatabaseError(resultCode: .SQLITE_MISUSE, message: """
GRDB is not linked against SQLCipher. \
Check https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688
""")
}
}
#endif
private func validateFormat() throws {
// Users are surprised when they open a picture as a database and
// see no error (https://github.com/groue/GRDB.swift/issues/54).
//
// So let's fail early if file is not a database, or encrypted with
// another passphrase.
try makeStatement(sql: "SELECT * FROM sqlite_master LIMIT 1").makeCursor().next()
}
// MARK: - Database Closing
/// Closes a connection with `sqlite3_close`. This method is intended for
/// the public `close()` function. It may fail.
func close() throws {
SchedulingWatchdog.preconditionValidQueue(self)
guard let sqliteConnection else {
// Already closed
return
}
configuration.SQLiteConnectionWillClose?(sqliteConnection)
// Finalize all cached statements since they would prevent
// immediate connection closing.
internalStatementCache.clear()
publicStatementCache.clear()
// https://www.sqlite.org/c3ref/close.html
// > If the database connection is associated with unfinalized prepared
// > statements or unfinished sqlite3_backup objects then
// > sqlite3_close() will leave the database connection open and
// > return SQLITE_BUSY.
let code = sqlite3_close(sqliteConnection)
guard code == SQLITE_OK else {
// So there remain some unfinalized prepared statement somewhere.
if let log = Self.logError {
if code == SQLITE_BUSY {
// Let the user know about unfinalized statements that did
// prevent the connection from closing properly.
var stmt: SQLiteStatement? = sqlite3_next_stmt(sqliteConnection, nil)
while stmt != nil {
log(ResultCode(rawValue: code), "unfinalized statement: \(String(cString: sqlite3_sql(stmt)))")
stmt = sqlite3_next_stmt(sqliteConnection, stmt)
}
}
}
throw DatabaseError(resultCode: code, message: lastErrorMessage)
}
self.sqliteConnection = nil
configuration.SQLiteConnectionDidClose?()
}
/// Closes a connection with `sqlite3_close_v2`. This method is intended for
/// deallocated connections.
func close_v2() {
SchedulingWatchdog.preconditionValidQueue(self)
guard let sqliteConnection else {
// Already closed
return
}
configuration.SQLiteConnectionWillClose?(sqliteConnection)
// Finalize all cached statements since they would prevent
// immediate connection closing.
internalStatementCache.clear()
publicStatementCache.clear()
// https://www.sqlite.org/c3ref/close.html
// > If sqlite3_close_v2() is called with unfinalized prepared
// > statements and/or unfinished sqlite3_backups, then the database
// > connection becomes an unusable "zombie" which will automatically
// > be deallocated when the last prepared statement is finalized or the
// > last sqlite3_backup is finished.
// >
// > The sqlite3_close_v2() interface is intended for use with host
// > languages that are garbage collected, and where the order in which
// > destructors are called is arbitrary.
let code = sqlite3_close_v2(sqliteConnection)
if code != SQLITE_OK, let log = Self.logError {
// A rare situation where GRDB doesn't fatalError on
// unprocessed errors.
let message = String(cString: sqlite3_errmsg(sqliteConnection))
log(ResultCode(rawValue: code), "could not close database: \(message)")
}
self.sqliteConnection = nil
configuration.SQLiteConnectionDidClose?()
}
// MARK: - Limits
/// The maximum number of arguments accepted by an SQLite statement.
///
/// For example, requests such as the one below must make sure the `ids`
/// array does not contain more than `maximumStatementArgumentCount`
/// elements:
///
/// ```swift
/// // DELETE FROM player WHERE id IN (?, ?, ...)
/// let ids: [Int] = ...
/// try dbQueue.write { db in
/// try Player.deleteAll(db, keys: ids)
/// }
/// ```
///
/// Related SQLite documentation: see `SQLITE_LIMIT_VARIABLE_NUMBER` in
/// <https://www.sqlite.org/limits.html>.
public var maximumStatementArgumentCount: Int {
Int(sqlite3_limit(sqliteConnection, SQLITE_LIMIT_VARIABLE_NUMBER, -1))
}
// MARK: - Functions
/// Adds or redefines a custom SQL function.
///
/// When you want to add a function to all connections created by a
/// ``DatabasePool``, add the function in
/// ``Configuration/prepareDatabase(_:)``:
///
/// ```swift
/// var config = Configuration()
/// config.prepareDatabase { db in
/// // Add the function to both writer and readers connections.
/// db.add(function: ...)
/// }
/// let dbPool = try DatabasePool(path: ..., configuration: config)
/// ```
public func add(function: DatabaseFunction) {
functions.update(with: function)
function.install(in: self)
}
/// Removes a custom SQL function.
public func remove(function: DatabaseFunction) {
functions.remove(function)
function.uninstall(in: self)
}
// MARK: - Collations
/// Adds or redefines a collation.
///
/// When you want to add a collation to all connections created by a
/// ``DatabasePool``, add the collation in
/// ``Configuration/prepareDatabase(_:)``:
///
/// ```swift
/// var config = Configuration()
/// config.prepareDatabase { db in
/// // Add the collation to both writer and readers connections.
/// db.add(collation: ...)
/// }
/// let dbPool = try DatabasePool(path: ..., configuration: config)
/// ```
public func add(collation: DatabaseCollation) {
collations.update(with: collation)
let collationPointer = Unmanaged.passUnretained(collation).toOpaque()
let code = sqlite3_create_collation_v2(
sqliteConnection,
collation.name,
SQLITE_UTF8,
collationPointer,
{ (collationPointer, length1, buffer1, length2, buffer2) in
let collation = Unmanaged<DatabaseCollation>.fromOpaque(collationPointer!).takeUnretainedValue()
return CInt(collation.function(length1, buffer1, length2, buffer2).rawValue)
}, nil)
guard code == SQLITE_OK else {
// Assume a GRDB bug: there is no point throwing any error.
fatalError(DatabaseError(resultCode: code, message: lastErrorMessage))
}
}
/// Removes a collation.
public func remove(collation: DatabaseCollation) {
collations.remove(collation)
sqlite3_create_collation_v2(
sqliteConnection,
collation.name,
SQLITE_UTF8,
nil, nil, nil)
}
// MARK: - Read-Only Access
/// MUST be balanced with `endReadOnly()`.
func beginReadOnly() throws {
if configuration.readonly { return }
if readOnlyDepth == 0 {
try internalCachedStatement(sql: "PRAGMA query_only = 1").execute()
}
readOnlyDepth += 1
}
/// MUST balance `beginReadOnly()`.
func endReadOnly() throws {
if configuration.readonly { return }
readOnlyDepth -= 1
assert(readOnlyDepth >= 0, "unbalanced endReadOnly()")
if readOnlyDepth == 0 {
try internalCachedStatement(sql: "PRAGMA query_only = 0").execute()
}
}
/// Executes read-only database operations, and returns their result
/// after they have finished executing.
///
/// Attempts to write throw a ``DatabaseError`` with
/// resultCode `SQLITE_READONLY`.
///
/// For example:
///
/// ```swift
/// try dbQueue.write do { db in
/// // Write OK
/// try Player(...).insert(db)
///
/// try db.readOnly {
/// // Read OK
/// let players = try Player.fetchAll(db)
///
/// // Throws SQLITE_READONLY
/// try Player(...).insert(db)
/// }
/// }
/// ```
///
/// This method is reentrant.
///
/// - parameter value: A closure that reads from the database.
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or the
/// error thrown by `value`.
public func readOnly<T>(_ value: () throws -> T) throws -> T {
try beginReadOnly()
return try throwingFirstError(
execute: value,
finally: endReadOnly)
}
/// Returns whether database connection is read-only (due to
/// `SQLITE_OPEN_READONLY` or `PRAGMA query_only=1`).
var isReadOnly: Bool {
readOnlyDepth > 0 || configuration.readonly
}
// MARK: - Database Observation
/// Reports the database region to ``ValueObservation``.
///
/// For example:
///
/// ```swift
/// let observation = ValueObservation.tracking { db in
/// // All changes to the 'player' and 'team' tables
/// // will trigger the observation.
/// try db.registerAccess(to: Player.all())
/// try db.registerAccess(to: Team.all())
/// }
/// ```
///
/// See ``ValueObservation/trackingConstantRegion(_:)`` for some examples
/// of region reporting.
///
/// This method has no effect on a ``ValueObservation`` created with an
/// explicit list of tracked regions. In the example below, only the
/// `player` table is tracked:
///
/// ```swift
/// // Observes the 'player' table only
/// let observation = ValueObservation.tracking(region: Player.all()) { db in
/// // Ignored
/// try db.registerAccess(to: Team.all())
/// }
/// ```
public func registerAccess(to region: @autoclosure () -> some DatabaseRegionConvertible) throws {
if isRecordingSelectedRegion {
try selectedRegion.formUnion(region().databaseRegion(self))
}
}
/// Notifies that some changes were performed in the provided
/// database region.
///
/// This method makes it possible to notify undetected changes, such as
/// changes performed by another process, changes performed by
/// direct calls to SQLite C functions, or changes to the
/// database schema.
/// See <doc:GRDB/TransactionObserver#Dealing-with-Undetected-Changes>
/// for a detailed list of undetected database modifications.
///
/// It triggers active transaction observers (``TransactionObserver``).
/// In particular, ``ValueObservation`` that observe the input `region`
/// will fetch and notify a fresh value.
///
/// For example:
///
/// ```swift
/// try dbQueue.write { db in
/// // Notify observers that some changes were performed in the database
/// try db.notifyChanges(in: .fullDatabase)
///
/// // Notify observers that some changes were performed in the player table
/// try db.notifyChanges(in: Player.all())
///
/// // Equivalent alternative
/// try db.notifyChanges(in: Table("player"))
/// }
/// ```
///
/// This method has no effect when called from a read-only
/// database access.
///
/// > Caveat: Individual rowids in the input region are ignored.
/// > Notifying a change to a specific rowid is the same as notifying a
/// > change in the whole table:
/// >
/// > ```swift
/// > try dbQueue.write { db in
/// > // Equivalent
/// > try db.notifyChanges(in: Player.all())
/// > try db.notifyChanges(in: Player.filter(id: 1))
/// > }
/// > ```
public func notifyChanges(in region: some DatabaseRegionConvertible) throws {
// Don't do anything when read-only, because read-only transactions
// are not notified. We don't want to notify transactions observers
// of changes, and have them wait for a commit notification that
// will never come.
if !isReadOnly, let observationBroker {
let eventKinds = try region
.databaseRegion(self)
// Use canonical table names for case insensitivity of the input.
.canonicalTables(self)
.impactfulEventKinds(self)
try observationBroker.notifyChanges(withEventsOfKind: eventKinds)
}
}
/// Extends the `region` argument with the database region selected by all
/// statements executed by the closure, and all regions explicitly tracked
/// with the ``registerAccess(to:)`` method.
///
/// For example:
///
/// ```swift
/// var region = DatabaseRegion()
/// try db.recordingSelection(®ion) {
/// let players = try Player.fetchAll(db)
/// let team = try Team.fetchOne(db, id: 42)
/// try db.registerAccess(to: Table("awards"))
/// }
/// print(region) // awards,player(*),team(*)[42]
/// ```
///
/// This method is used by ``ValueObservation``:
///
/// ```swift
/// let playersObservation = ValueObservation.tracking { db in
/// // Here all fetches are recorded, so that we know what is the
/// // database region that must be observed.
/// try Player.fetchAll(db)
/// }
/// ```
func recordingSelection<T>(_ region: inout DatabaseRegion, _ block: () throws -> T) rethrows -> T {
if region.isFullDatabase {
return try block()
}
let oldFlag = self.isRecordingSelectedRegion
let oldRegion = self.selectedRegion
isRecordingSelectedRegion = true
selectedRegion = DatabaseRegion()
defer {
region.formUnion(selectedRegion)
isRecordingSelectedRegion = oldFlag
if isRecordingSelectedRegion {
selectedRegion = oldRegion.union(selectedRegion)
} else {
selectedRegion = oldRegion
}
}
return try block()
}
// MARK: - Trace
/// Registers a tracing function.
///
/// For example:
///
/// ```swift
/// // Trace all SQL statements executed by the database
/// var config = Configuration()
/// config.prepareDatabase { db in
/// db.trace(options: .statement) { event in
/// print("SQL: \(event)")
/// }
/// }
/// let dbQueue = try DatabaseQueue(path: ..., configuration: config)
/// ```
///
/// Pass an empty options set in order to stop database tracing:
///
/// ```swift
/// // Stop tracing
/// db.trace(options: [])
/// ```
///
/// If you want to see statement arguments in the traced events, you will
/// need to set the ``Configuration/publicStatementArguments`` flag in the
/// database ``configuration``.
///
/// Related SQLite documentation: <https://www.sqlite.org/c3ref/trace_v2.html>
///
/// - parameter options: The set of desired event kinds. Defaults to
/// `.statement`, which notifies all executed database statements.
/// - parameter trace: the tracing function.
public func trace(options: TracingOptions = .statement, _ trace: ((TraceEvent) -> Void)? = nil) {
SchedulingWatchdog.preconditionValidQueue(self)
self.trace = trace
if options.isEmpty || trace == nil {
sqlite3_trace_v2(sqliteConnection, 0, nil, nil)
return
}