Skip to content

Commit 7749e3e

Browse files
committed
Make lastError optional
Return it only when the result code is an error code. Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent 3203dfd commit 7749e3e

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

SQLite Tests/DatabaseTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,27 @@ class DatabaseTests: SQLiteTestCase {
396396
XCTAssertEqual(1, db.scalar("SELECT ? = ? COLLATE \"NO DIACRITIC\"", "cafe", "café") as! Int64)
397397
}
398398

399+
func test_lastError_withOK_returnsNil() {
400+
XCTAssertNil(db.lastError)
401+
}
402+
403+
func test_lastError_withRow_returnsNil() {
404+
insertUsers("alice", "betty")
405+
db.prepare("SELECT * FROM users").step()
406+
407+
XCTAssertNil(db.lastError)
408+
}
409+
410+
func test_lastError_withDone_returnsNil() {
411+
db.prepare("SELECT * FROM users").step()
412+
413+
XCTAssertNil(db.lastError)
414+
}
415+
416+
func test_lastError_withError_returnsError() {
417+
insertUsers("alice", "alice")
418+
419+
XCTAssertNotNil(db.lastError)
420+
}
421+
399422
}

SQLite/Database.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,12 +604,15 @@ public final class Database {
604604
// MARK: - Error Handling
605605

606606
/// Returns the last error produced on this connection.
607-
public var lastError: String {
607+
public var lastError: String? {
608+
if successCodes.contains(sqlite3_errcode(handle)) {
609+
return nil
610+
}
608611
return String.fromCString(sqlite3_errmsg(handle))!
609612
}
610613

611614
internal func try(block: () -> Int32) {
612-
perform { if block() != SQLITE_OK { assertionFailure("\(self.lastError)") } }
615+
perform { if block() != SQLITE_OK { assertionFailure("\(self.lastError!)") } }
613616
}
614617

615618
// MARK: - Threading
@@ -644,6 +647,8 @@ extension Database.Location: Printable {
644647

645648
}
646649

650+
internal let successCodes: Set<Int32> = [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]
651+
647652
internal func quote(#literal: String) -> String {
648653
return quote(literal, "'")
649654
}

SQLite/Statement.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public final class Statement {
180180

181181
/// :returns: Whether or not a statement has produced an error.
182182
public var failed: Bool {
183-
return !(status == SQLITE_OK || status == SQLITE_ROW || status == SQLITE_DONE)
183+
return reason != nil
184184
}
185185

186186
/// :returns: The reason for an error.
@@ -192,9 +192,9 @@ public final class Statement {
192192
if failed { return }
193193
database.perform {
194194
self.status = block()
195-
if self.failed {
196-
self.reason = String.fromCString(sqlite3_errmsg(self.database.handle))
197-
assert(self.status == SQLITE_CONSTRAINT || self.status == SQLITE_INTERRUPT, "\(self.reason!)")
195+
if let error = self.database.lastError {
196+
self.reason = error
197+
assert(self.status == SQLITE_CONSTRAINT || self.status == SQLITE_INTERRUPT, "\(error)")
198198
}
199199
}
200200
}

0 commit comments

Comments
 (0)