/
SwiftData.swift
1068 lines (913 loc) · 38.9 KB
/
SwiftData.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* This is a heavily modified version of SwiftData.swift by Ryan Fowler
* This has been enhanced to support custom files, correct binding, versioning,
* and a streaming results via Cursors. The API has also been changed to use NSError, Cursors, and
* to force callers to request a connection before executing commands. Database creation helpers, savepoint
* helpers, image support, and other features have been removed.
*/
// SwiftData.swift
//
// Copyright (c) 2014 Ryan Fowler
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
import UIKit
import Shared
import XCGLogger
private let DatabaseBusyTimeout: Int32 = 3 * 1000
private let log = Logger.syncLogger
/**
* Handle to a SQLite database.
* Each instance holds a single connection that is shared across all queries.
*/
public class SwiftData {
let filename: String
static var EnableWAL = true
static var EnableForeignKeys = true
/// Used to keep track of the corrupted databases we've logged.
static var corruptionLogsWritten = Set<String>()
/// Used for testing.
static var ReuseConnections = true
/// For thread-safe access to the shared connection.
private let sharedConnectionQueue: dispatch_queue_t
/// Shared connection to this database.
private var sharedConnection: ConcreteSQLiteDBConnection?
private var key: String? = nil
private var prevKey: String? = nil
init(filename: String, key: String? = nil, prevKey: String? = nil) {
self.filename = filename
self.sharedConnectionQueue = dispatch_queue_create("SwiftData queue: \(filename)", DISPATCH_QUEUE_SERIAL)
// Ensure that multi-thread mode is enabled by default.
// See https://www.sqlite.org/threadsafe.html
assert(sqlite3_threadsafe() == 2)
self.key = key
self.prevKey = prevKey
}
private func getSharedConnection() -> ConcreteSQLiteDBConnection? {
var connection: ConcreteSQLiteDBConnection?
dispatch_sync(sharedConnectionQueue) {
if self.sharedConnection == nil {
log.debug(">>> Creating shared SQLiteDBConnection for \(self.filename) on thread \(NSThread.currentThread()).")
self.sharedConnection = ConcreteSQLiteDBConnection(filename: self.filename, flags: SwiftData.Flags.ReadWriteCreate.toSQL(), key: self.key, prevKey: self.prevKey)
}
connection = self.sharedConnection
}
return connection
}
/**
* The real meat of all the execute methods. This is used internally to open and
* close a database connection and run a block of code inside it.
*/
func withConnection(flags: SwiftData.Flags, synchronous: Bool=true, cb: (db: SQLiteDBConnection) -> NSError?) -> NSError? {
let conn: ConcreteSQLiteDBConnection?
if SwiftData.ReuseConnections {
conn = getSharedConnection()
} else {
log.debug(">>> Creating non-shared SQLiteDBConnection for \(self.filename) on thread \(NSThread.currentThread()).")
conn = ConcreteSQLiteDBConnection(filename: filename, flags: flags.toSQL(), key: self.key, prevKey: self.prevKey)
}
guard let connection = conn else {
// Run the callback with a fake failed connection.
// Yeah, that means we have an error return value but we still run the callback.
let failed = FailedSQLiteDBConnection()
let queue = self.sharedConnectionQueue
let noConnection = NSError(domain: "mozilla", code: 0, userInfo: [NSLocalizedDescriptionKey: "Could not create a connection"])
if synchronous {
var error: NSError? = nil
dispatch_sync(queue) {
error = cb(db: failed)
}
return error ?? noConnection
}
dispatch_async(queue) {
cb(db: failed)
}
return noConnection
}
if synchronous {
var error: NSError? = nil
dispatch_sync(connection.queue) {
error = cb(db: connection)
}
return error
}
dispatch_async(connection.queue) {
cb(db: connection)
}
return nil
}
func transaction(transactionClosure: (db: SQLiteDBConnection) -> Bool) -> NSError? {
return self.transaction(synchronous: true, transactionClosure: transactionClosure)
}
/**
* Helper for opening a connection, starting a transaction, and then running a block of code inside it.
* The code block can return true if the transaction should be committed. False if we should roll back.
*/
func transaction(synchronous synchronous: Bool=true, transactionClosure: (db: SQLiteDBConnection) -> Bool) -> NSError? {
return withConnection(SwiftData.Flags.ReadWriteCreate, synchronous: synchronous) { db in
if let err = db.executeChange("BEGIN EXCLUSIVE") {
log.warning("BEGIN EXCLUSIVE failed.")
return err
}
if transactionClosure(db: db) {
log.verbose("Op in transaction succeeded. Committing.")
if let err = db.executeChange("COMMIT") {
log.error("COMMIT failed. Rolling back.")
db.executeChange("ROLLBACK")
return err
}
} else {
log.debug("Op in transaction failed. Rolling back.")
if let err = db.executeChange("ROLLBACK") {
return err
}
}
return nil
}
}
// Don't use this unless you know what you're doing. The deinitializer
// should be used to achieve refcounting semantics.
func forceClose() {
self.sharedConnection = nil
}
public enum Flags {
case ReadOnly
case ReadWrite
case ReadWriteCreate
private func toSQL() -> Int32 {
switch self {
case .ReadOnly:
return SQLITE_OPEN_READONLY
case .ReadWrite:
return SQLITE_OPEN_READWRITE
case .ReadWriteCreate:
return SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
}
}
}
}
/**
* Wrapper class for a SQLite statement.
* This class helps manage the statement lifecycle. By holding a reference to the SQL connection, we ensure
* the connection is never deinitialized while the statement is active. This class is responsible for
* finalizing the SQL statement once it goes out of scope.
*/
private class SQLiteDBStatement {
var pointer: COpaquePointer = nil
private let connection: ConcreteSQLiteDBConnection
init(connection: ConcreteSQLiteDBConnection, query: String, args: [AnyObject?]?) throws {
self.connection = connection
let status = sqlite3_prepare_v2(connection.sqliteDB, query, -1, &pointer, nil)
if status != SQLITE_OK {
throw connection.createErr("During: SQL Prepare \(query)", status: Int(status))
}
if let args = args,
let bindError = bind(args) {
throw bindError
}
}
/// Binds arguments to the statement.
private func bind(objects: [AnyObject?]) -> NSError? {
let count = Int(sqlite3_bind_parameter_count(pointer))
if (count < objects.count) {
return connection.createErr("During: Bind", status: 202)
}
if (count > objects.count) {
return connection.createErr("During: Bind", status: 201)
}
for (index, obj) in objects.enumerate() {
var status: Int32 = SQLITE_OK
// Doubles also pass obj as Int, so order is important here.
if obj is Double {
status = sqlite3_bind_double(pointer, Int32(index+1), obj as! Double)
} else if obj is Int {
status = sqlite3_bind_int(pointer, Int32(index+1), Int32(obj as! Int))
} else if obj is Bool {
status = sqlite3_bind_int(pointer, Int32(index+1), (obj as! Bool) ? 1 : 0)
} else if obj is String {
typealias CFunction = @convention(c) (UnsafeMutablePointer<()>) -> Void
let transient = unsafeBitCast(-1, CFunction.self)
status = sqlite3_bind_text(pointer, Int32(index+1), (obj as! String).cStringUsingEncoding(NSUTF8StringEncoding)!, -1, transient)
} else if obj is NSData {
status = sqlite3_bind_blob(pointer, Int32(index+1), (obj as! NSData).bytes, -1, nil)
} else if obj is NSDate {
let timestamp = (obj as! NSDate).timeIntervalSince1970
status = sqlite3_bind_double(pointer, Int32(index+1), timestamp)
} else if obj === nil {
status = sqlite3_bind_null(pointer, Int32(index+1))
}
if status != SQLITE_OK {
return connection.createErr("During: Bind", status: Int(status))
}
}
return nil
}
func close() {
if nil != self.pointer {
sqlite3_finalize(self.pointer)
self.pointer = nil
}
}
deinit {
if nil != self.pointer {
sqlite3_finalize(self.pointer)
}
}
}
protocol SQLiteDBConnection {
var lastInsertedRowID: Int { get }
var numberOfRowsModified: Int { get }
func executeChange(sqlStr: String) -> NSError?
func executeChange(sqlStr: String, withArgs args: [AnyObject?]?) -> NSError?
func executeQuery<T>(sqlStr: String, factory: ((SDRow) -> T)) -> Cursor<T>
func executeQuery<T>(sqlStr: String, factory: ((SDRow) -> T), withArgs args: [AnyObject?]?) -> Cursor<T>
func executeQueryUnsafe<T>(sqlStr: String, factory: ((SDRow) -> T), withArgs args: [AnyObject?]?) -> Cursor<T>
func interrupt()
func checkpoint()
func checkpoint(mode: Int32)
func vacuum() -> NSError?
}
// Represents a failure to open.
class FailedSQLiteDBConnection: SQLiteDBConnection {
private func fail(str: String) -> NSError {
return NSError(domain: "mozilla", code: 0, userInfo: [NSLocalizedDescriptionKey: str])
}
var lastInsertedRowID: Int { return 0 }
var numberOfRowsModified: Int { return 0 }
func executeChange(sqlStr: String) -> NSError? {
return self.fail("Non-open connection; can't execute change.")
}
func executeChange(sqlStr: String, withArgs args: [AnyObject?]?) -> NSError? {
return self.fail("Non-open connection; can't execute change.")
}
func executeQuery<T>(sqlStr: String, factory: ((SDRow) -> T)) -> Cursor<T> {
return self.executeQuery(sqlStr, factory: factory, withArgs: nil)
}
func executeQuery<T>(sqlStr: String, factory: ((SDRow) -> T), withArgs args: [AnyObject?]?) -> Cursor<T> {
return Cursor<T>(err: self.fail("Non-open connection; can't execute query."))
}
func executeQueryUnsafe<T>(sqlStr: String, factory: ((SDRow) -> T), withArgs args: [AnyObject?]?) -> Cursor<T> {
return Cursor<T>(err: self.fail("Non-open connection; can't execute query."))
}
func interrupt() {}
func checkpoint() {}
func checkpoint(mode: Int32) {}
func vacuum() -> NSError? {
return self.fail("Non-open connection; can't vacuum.")
}
}
public class ConcreteSQLiteDBConnection: SQLiteDBConnection {
private var sqliteDB: COpaquePointer = nil
private let filename: String
private let debug_enabled = false
private let queue: dispatch_queue_t
public var version: Int {
get {
return pragma("user_version", factory: IntFactory) ?? 0
}
set {
executeChange("PRAGMA user_version = \(newValue)")
}
}
private func setKey(key: String?) -> NSError? {
sqlite3_key(sqliteDB, key ?? "", Int32((key ?? "").characters.count))
let cursor = executeQuery("SELECT count(*) FROM sqlite_master;", factory: IntFactory, withArgs: nil)
if cursor.status != .Success {
return NSError(domain: "mozilla", code: 0, userInfo: [NSLocalizedDescriptionKey: "Invalid key"])
}
return nil
}
private func reKey(oldKey: String?, newKey: String?) -> NSError? {
sqlite3_key(sqliteDB, oldKey ?? "", Int32((oldKey ?? "").characters.count))
sqlite3_rekey(sqliteDB, newKey ?? "", Int32((newKey ?? "").characters.count))
// Check that the new key actually works
sqlite3_key(sqliteDB, newKey ?? "", Int32((newKey ?? "").characters.count))
let cursor = executeQuery("SELECT count(*) FROM sqlite_master;", factory: IntFactory, withArgs: nil)
if cursor.status != .Success {
return NSError(domain: "mozilla", code: 0, userInfo: [NSLocalizedDescriptionKey: "Rekey failed"])
}
return nil
}
func interrupt() {
log.debug("Interrupt")
sqlite3_interrupt(sqliteDB)
}
private func pragma<T: Equatable>(pragma: String, expected: T?, factory: SDRow -> T, message: String) throws {
let cursorResult = self.pragma(pragma, factory: factory)
if cursorResult != expected {
log.error("\(message): \(cursorResult), \(expected)")
throw NSError(domain: "mozilla", code: 0, userInfo: [NSLocalizedDescriptionKey: "PRAGMA didn't return expected output: \(message)."])
}
}
private func pragma<T>(pragma: String, factory: SDRow -> T) -> T? {
let cursor = executeQueryUnsafe("PRAGMA \(pragma)", factory: factory, withArgs: nil)
defer { cursor.close() }
return cursor[0]
}
private func prepareShared() {
if SwiftData.EnableForeignKeys {
pragma("foreign_keys=ON", factory: IntFactory)
}
// Retry queries before returning locked errors.
sqlite3_busy_timeout(self.sqliteDB, DatabaseBusyTimeout)
}
private func prepareEncrypted(flags: Int32, key: String?, prevKey: String? = nil) throws {
// Setting the key needs to be the first thing done with the database.
if let _ = setKey(key) {
if let err = closeCustomConnection(immediately: true) {
log.error("Couldn't close connection: \(err). Failing to open.")
throw err
}
if let err = openWithFlags(flags) {
throw err
}
if let err = reKey(prevKey, newKey: key) {
log.error("Unable to encrypt database")
throw err
}
}
if SwiftData.EnableWAL {
log.info("Enabling WAL mode.")
try pragma("journal_mode=WAL", expected: "wal",
factory: StringFactory, message: "WAL journal mode set")
}
self.prepareShared()
}
private func prepareCleartext() throws {
// If we just created the DB -- i.e., no tables have been created yet -- then
// we can set the page size right now and save a vacuum.
//
// For where these values come from, see Bug 1213623.
//
// Note that sqlcipher uses cipher_page_size instead, but we don't set that
// because it needs to be set from day one.
let desiredPageSize = 32 * 1024
pragma("page_size=\(desiredPageSize)", factory: IntFactory)
let currentPageSize = pragma("page_size", factory: IntFactory)
// This has to be done without WAL, so we always hop into rollback/delete journal mode.
if currentPageSize != desiredPageSize {
try pragma("journal_mode=DELETE", expected: "delete",
factory: StringFactory, message: "delete journal mode set")
try pragma("page_size=\(desiredPageSize)", expected: nil,
factory: IntFactory, message: "Page size set")
log.info("Vacuuming to alter database page size from \(currentPageSize) to \(desiredPageSize).")
if let err = self.vacuum() {
log.error("Vacuuming failed: \(err).")
} else {
log.debug("Vacuuming succeeded.")
}
}
if SwiftData.EnableWAL {
log.info("Enabling WAL mode.")
let desiredPagesPerJournal = 16
let desiredCheckpointSize = desiredPagesPerJournal * desiredPageSize
let desiredJournalSizeLimit = 3 * desiredCheckpointSize
/*
* With whole-module-optimization enabled in Xcode 7.2 and 7.2.1, the
* compiler seems to eagerly discard these queries if they're simply
* inlined, causing a crash in `pragma`.
*
* Hackily hold on to them.
*/
let journalModeQuery = "journal_mode=WAL"
let autoCheckpointQuery = "wal_autocheckpoint=\(desiredPagesPerJournal)"
let journalSizeQuery = "journal_size_limit=\(desiredJournalSizeLimit)"
try withExtendedLifetime(journalModeQuery, {
try pragma(journalModeQuery, expected: "wal",
factory: StringFactory, message: "WAL journal mode set")
})
try withExtendedLifetime(autoCheckpointQuery, {
try pragma(autoCheckpointQuery, expected: desiredPagesPerJournal,
factory: IntFactory, message: "WAL autocheckpoint set")
})
try withExtendedLifetime(journalSizeQuery, {
try pragma(journalSizeQuery, expected: desiredJournalSizeLimit,
factory: IntFactory, message: "WAL journal size limit set")
})
}
self.prepareShared()
}
init?(filename: String, flags: Int32, key: String? = nil, prevKey: String? = nil) {
log.debug("Opening connection to \(filename).")
self.filename = filename
self.queue = dispatch_queue_create("SQLite connection: \(filename)", DISPATCH_QUEUE_SERIAL)
if let failure = openWithFlags(flags) {
log.warning("Opening connection to \(filename) failed: \(failure).")
return nil
}
if key == nil && prevKey == nil {
do {
try self.prepareCleartext()
} catch {
return nil
}
} else {
do {
try self.prepareEncrypted(flags, key: key, prevKey: prevKey)
} catch {
return nil
}
}
}
deinit {
log.debug("deinit: closing connection on thread \(NSThread.currentThread()).")
dispatch_sync(self.queue) {
self.closeCustomConnection()
}
}
public var lastInsertedRowID: Int {
return Int(sqlite3_last_insert_rowid(sqliteDB))
}
public var numberOfRowsModified: Int {
return Int(sqlite3_changes(sqliteDB))
}
func checkpoint() {
self.checkpoint(SQLITE_CHECKPOINT_FULL)
}
/**
* Blindly attempts a WAL checkpoint on all attached databases.
*/
func checkpoint(mode: Int32) {
guard sqliteDB != nil else {
log.warning("Trying to checkpoint a nil DB!")
return
}
log.debug("Running WAL checkpoint on \(self.filename) on thread \(NSThread.currentThread()).")
sqlite3_wal_checkpoint_v2(sqliteDB, nil, mode, nil, nil)
log.debug("WAL checkpoint done on \(self.filename).")
}
func vacuum() -> NSError? {
return self.executeChange("VACUUM")
}
/// Creates an error from a sqlite status. Will print to the console if debug_enabled is set.
/// Do not call this unless you're going to return this error.
private func createErr(description: String, status: Int) -> NSError {
var msg = SDError.errorMessageFromCode(status)
if (debug_enabled) {
log.debug("SwiftData Error -> \(description)")
log.debug(" -> Code: \(status) - \(msg)")
}
if let errMsg = String.fromCString(sqlite3_errmsg(sqliteDB)) {
msg += " " + errMsg
if (debug_enabled) {
log.debug(" -> Details: \(errMsg)")
}
}
return NSError(domain: "org.mozilla", code: status, userInfo: [NSLocalizedDescriptionKey: msg])
}
/// Open the connection. This is called when the db is created. You should not call it yourself.
private func openWithFlags(flags: Int32) -> NSError? {
let status = sqlite3_open_v2(filename.cStringUsingEncoding(NSUTF8StringEncoding)!, &sqliteDB, flags, nil)
if status != SQLITE_OK {
return createErr("During: Opening Database with Flags", status: Int(status))
}
return nil
}
/// Closes a connection. This is called via deinit. Do not call this yourself.
private func closeCustomConnection(immediately immediately: Bool=false) -> NSError? {
log.debug("Closing custom connection for \(self.filename) on \(NSThread.currentThread()).")
// TODO: add a lock here?
let db = self.sqliteDB
self.sqliteDB = nil
// Don't bother trying to call sqlite3_close multiple times.
guard db != nil else {
log.warning("Connection was nil.")
return nil
}
let status = immediately ? sqlite3_close(db) : sqlite3_close_v2(db)
// Note that if we use sqlite3_close_v2, this will still return SQLITE_OK even if
// there are outstanding prepared statements
if status != SQLITE_OK {
log.error("Got status \(status) while attempting to close.")
return createErr("During: closing database with flags", status: Int(status))
}
log.debug("Closed \(self.filename).")
return nil
}
public func executeChange(sqlStr: String) -> NSError? {
return self.executeChange(sqlStr, withArgs: nil)
}
/// Executes a change on the database.
public func executeChange(sqlStr: String, withArgs args: [AnyObject?]?) -> NSError? {
var error: NSError?
let statement: SQLiteDBStatement?
do {
statement = try SQLiteDBStatement(connection: self, query: sqlStr, args: args)
} catch let error1 as NSError {
error = error1
statement = nil
log.error("SQL error: \(error1.localizedDescription) for SQL \(sqlStr).")
}
// Close, not reset -- this isn't going to be reused.
defer { statement?.close() }
if let error = error {
// Special case: Write additional info to the database log in the case of a database corruption.
if error.code == Int(SQLITE_CORRUPT) {
writeCorruptionInfoForDBNamed(filename, toLogger: Logger.corruptLogger)
}
log.error("SQL error: \(error.localizedDescription) for SQL \(sqlStr).")
return error
}
let status = sqlite3_step(statement!.pointer)
if status != SQLITE_DONE && status != SQLITE_OK {
error = createErr("During: SQL Step \(sqlStr)", status: Int(status))
}
return error
}
func executeQuery<T>(sqlStr: String, factory: ((SDRow) -> T)) -> Cursor<T> {
return self.executeQuery(sqlStr, factory: factory, withArgs: nil)
}
/// Queries the database.
/// Returns a cursor pre-filled with the complete result set.
func executeQuery<T>(sqlStr: String, factory: ((SDRow) -> T), withArgs args: [AnyObject?]?) -> Cursor<T> {
var error: NSError?
let statement: SQLiteDBStatement?
do {
statement = try SQLiteDBStatement(connection: self, query: sqlStr, args: args)
} catch let error1 as NSError {
error = error1
statement = nil
}
// Close, not reset -- this isn't going to be reused, and the FilledSQLiteCursor
// consumes everything.
defer { statement?.close() }
if let error = error {
// Special case: Write additional info to the database log in the case of a database corruption.
if error.code == Int(SQLITE_CORRUPT) {
writeCorruptionInfoForDBNamed(filename, toLogger: Logger.corruptLogger)
}
log.error("SQL error: \(error.localizedDescription).")
return Cursor<T>(err: error)
}
return FilledSQLiteCursor<T>(statement: statement!, factory: factory)
}
func writeCorruptionInfoForDBNamed(dbFilename: String, toLogger logger: XCGLogger) {
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
guard !SwiftData.corruptionLogsWritten.contains(dbFilename) else { return }
logger.error("Corrupt DB detected! DB filename: \(dbFilename)")
let dbFileSize = ("file://\(dbFilename)".asURL)?.allocatedFileSize() ?? 0
logger.error("DB file size: \(dbFileSize) bytes")
logger.error("Integrity check:")
let messages = self.executeQueryUnsafe("PRAGMA integrity_check", factory: StringFactory, withArgs: nil)
defer { messages.close() }
if messages.status == CursorStatus.Success {
for message in messages {
logger.error(message)
}
logger.error("----")
} else {
logger.error("Couldn't run integrity check: \(messages.statusMessage).")
}
// Write call stack.
logger.error("Call stack: ")
for message in NSThread.callStackSymbols() {
logger.error(" >> \(message)")
}
logger.error("----")
// Write open file handles.
let openDescriptors = FSUtils.openFileDescriptors()
logger.error("Open file descriptors: ")
for (k, v) in openDescriptors {
logger.error(" \(k): \(v)")
}
logger.error("----")
SwiftData.corruptionLogsWritten.insert(dbFilename)
}
}
/**
* Queries the database.
* Returns a live cursor that holds the query statement and database connection.
* Instances of this class *must not* leak outside of the connection queue!
*/
func executeQueryUnsafe<T>(sqlStr: String, factory: ((SDRow) -> T), withArgs args: [AnyObject?]?) -> Cursor<T> {
var error: NSError?
let statement: SQLiteDBStatement?
do {
statement = try SQLiteDBStatement(connection: self, query: sqlStr, args: args)
} catch let error1 as NSError {
error = error1
statement = nil
}
if let error = error {
return Cursor(err: error)
}
return LiveSQLiteCursor(statement: statement!, factory: factory)
}
}
/// Helper for queries that return a single integer result.
func IntFactory(row: SDRow) -> Int {
return row[0] as! Int
}
/// Helper for queries that return a single String result.
func StringFactory(row: SDRow) -> String {
return row[0] as! String
}
/// Wrapper around a statement for getting data from a row. This provides accessors for subscript indexing
/// and a generator for iterating over columns.
class SDRow: SequenceType {
// The sqlite statement this row came from.
private let statement: SQLiteDBStatement
// The columns of this database. The indices of these are assumed to match the indices
// of the statement.
private let columnNames: [String]
private init(statement: SQLiteDBStatement, columns: [String]) {
self.statement = statement
self.columnNames = columns
}
// Return the value at this index in the row
private func getValue(index: Int) -> AnyObject? {
let i = Int32(index)
let type = sqlite3_column_type(statement.pointer, i)
var ret: AnyObject? = nil
switch type {
case SQLITE_NULL, SQLITE_INTEGER:
ret = NSNumber(longLong: sqlite3_column_int64(statement.pointer, i))
case SQLITE_TEXT:
let text = UnsafePointer<Int8>(sqlite3_column_text(statement.pointer, i))
ret = String.fromCString(text)
case SQLITE_BLOB:
let blob = sqlite3_column_blob(statement.pointer, i)
if blob != nil {
let size = sqlite3_column_bytes(statement.pointer, i)
ret = NSData(bytes: blob, length: Int(size))
}
case SQLITE_FLOAT:
ret = Double(sqlite3_column_double(statement.pointer, i))
default:
log.warning("SwiftData Warning -> Column: \(index) is of an unrecognized type, returning nil")
}
return ret
}
// Accessor getting column 'key' in the row
subscript(key: Int) -> AnyObject? {
return getValue(key)
}
// Accessor getting a named column in the row. This (currently) depends on
// the columns array passed into this Row to find the correct index.
subscript(key: String) -> AnyObject? {
get {
if let index = columnNames.indexOf(key) {
return getValue(index)
}
return nil
}
}
// Allow iterating through the row. This is currently broken.
func generate() -> AnyGenerator<Any> {
let nextIndex = 0
return AnyGenerator() {
// This crashes the compiler. Yay!
if (nextIndex < self.columnNames.count) {
return nil // self.getValue(nextIndex)
}
return nil
}
}
}
/// Helper for pretty printing SQL (and other custom) error codes.
private struct SDError {
private static func errorMessageFromCode(errorCode: Int) -> String {
switch errorCode {
case -1:
return "No error"
// SQLite error codes and descriptions as per: http://www.sqlite.org/c3ref/c_abort.html
case 0:
return "Successful result"
case 1:
return "SQL error or missing database"
case 2:
return "Internal logic error in SQLite"
case 3:
return "Access permission denied"
case 4:
return "Callback routine requested an abort"
case 5:
return "The database file is busy"
case 6:
return "A table in the database is locked"
case 7:
return "A malloc() failed"
case 8:
return "Attempt to write a readonly database"
case 9:
return "Operation terminated by sqlite3_interrupt()"
case 10:
return "Some kind of disk I/O error occurred"
case 11:
return "The database disk image is malformed"
case 12:
return "Unknown opcode in sqlite3_file_control()"
case 13:
return "Insertion failed because database is full"
case 14:
return "Unable to open the database file"
case 15:
return "Database lock protocol error"
case 16:
return "Database is empty"
case 17:
return "The database schema changed"
case 18:
return "String or BLOB exceeds size limit"
case 19:
return "Abort due to constraint violation"
case 20:
return "Data type mismatch"
case 21:
return "Library used incorrectly"
case 22:
return "Uses OS features not supported on host"
case 23:
return "Authorization denied"
case 24:
return "Auxiliary database format error"
case 25:
return "2nd parameter to sqlite3_bind out of range"
case 26:
return "File opened that is not a database file"
case 27:
return "Notifications from sqlite3_log()"
case 28:
return "Warnings from sqlite3_log()"
case 100:
return "sqlite3_step() has another row ready"
case 101:
return "sqlite3_step() has finished executing"
// Custom SwiftData errors
// Binding errors
case 201:
return "Not enough objects to bind provided"
case 202:
return "Too many objects to bind provided"
// Custom connection errors
case 301:
return "A custom connection is already open"
case 302:
return "Cannot open a custom connection inside a transaction"
case 303:
return "Cannot open a custom connection inside a savepoint"
case 304:
return "A custom connection is not currently open"
case 305:
return "Cannot close a custom connection inside a transaction"
case 306:
return "Cannot close a custom connection inside a savepoint"
// Index and table errors
case 401:
return "At least one column name must be provided"
case 402:
return "Error extracting index names from sqlite_master"
case 403:
return "Error extracting table names from sqlite_master"
// Transaction and savepoint errors
case 501:
return "Cannot begin a transaction within a savepoint"
case 502:
return "Cannot begin a transaction within another transaction"
// Unknown error
default:
return "Unknown error"
}
}
}
/// Provides access to the result set returned by a database query.
/// The entire result set is cached, so this does not retain a reference
/// to the statement or the database connection.
private class FilledSQLiteCursor<T>: ArrayCursor<T> {
private init(statement: SQLiteDBStatement, factory: (SDRow) -> T) {
var status = CursorStatus.Success
var statusMessage = ""
let data = FilledSQLiteCursor.getValues(statement, factory: factory, status: &status, statusMessage: &statusMessage)
super.init(data: data, status: status, statusMessage: statusMessage)
}
/// Return an array with the set of results and release the statement.
private class func getValues(statement: SQLiteDBStatement, factory: (SDRow) -> T, inout status: CursorStatus, inout statusMessage: String) -> [T] {
var rows = [T]()
var count = 0
status = CursorStatus.Success
statusMessage = "Success"
var columns = [String]()
let columnCount = sqlite3_column_count(statement.pointer)
for i in 0..<columnCount {
let columnName = String.fromCString(sqlite3_column_name(statement.pointer, i))!
columns.append(columnName)
}
while true {
let sqlStatus = sqlite3_step(statement.pointer)
if sqlStatus != SQLITE_ROW {
if sqlStatus != SQLITE_DONE {
// NOTE: By setting our status to failure here, we'll report our count as zero,
// regardless of how far we've read at this point.
status = CursorStatus.Failure
statusMessage = SDError.errorMessageFromCode(Int(sqlStatus))
}
break
}
count += 1
let row = SDRow(statement: statement, columns: columns)
let result = factory(row)
rows.append(result)
}
return rows
}
}
/// Wrapper around a statement to help with iterating through the results.
private class LiveSQLiteCursor<T>: Cursor<T> {
private var statement: SQLiteDBStatement!
// Function for generating objects of type T from a row.
private let factory: (SDRow) -> T
// Status of the previous fetch request.
private var sqlStatus: Int32 = 0
// Number of rows in the database
// XXX - When Cursor becomes an interface, this should be a normal property, but right now
// we can't override the Cursor getter for count with a stored property.
private var _count: Int = 0
override var count: Int {
get {
if status != .Success {
return 0
}
return _count
}
}
private var position: Int = -1 {
didSet {
// If we're already there, shortcut out.
if (oldValue == position) {
return
}