Skip to content

Adds support for returning extended error codes #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/SQLite/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public final class Connection {
Int(sqlite3_total_changes(handle))
}

/// Whether or not the database will return extended error codes when errors are handled.
public var usesExtendedErrorCodes: Bool = false {
didSet {
sqlite3_extended_result_codes(handle, usesExtendedErrorCodes ? 1 : 0)
}
}

// MARK: - Execute

/// Executes a batch of SQL statements.
Expand Down
24 changes: 23 additions & 1 deletion Sources/SQLite/Core/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,27 @@ public enum Result: Error {
/// - statement: the statement which produced the error
case error(message: String, code: Int32, statement: Statement?)

/// Represents a SQLite specific [extended error code] (https://sqlite.org/rescode.html#primary_result_codes_versus_extended_result_codes)
///
/// - message: English-language text that describes the error
///
/// - extendedCode: SQLite [extended error code](https://sqlite.org/rescode.html#extended_result_code_list)
///
/// - statement: the statement which produced the error
case extendedError(message: String, extendedCode: Int32, statement: Statement?)

init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) {
guard !Result.successCodes.contains(errorCode) else { return nil }

let message = String(cString: sqlite3_errmsg(connection.handle))
self = .error(message: message, code: errorCode, statement: statement)

guard connection.usesExtendedErrorCodes else {
self = .error(message: message, code: errorCode, statement: statement)
return
}

let extendedErrorCode = sqlite3_extended_errcode(connection.handle)
self = .extendedError(message: message, extendedCode: extendedErrorCode, statement: statement)
}

}
Expand All @@ -40,6 +56,12 @@ extension Result: CustomStringConvertible {
} else {
return "\(message) (code: \(errorCode))"
}
case let .extendedError(message, extendedCode, statement):
if let statement = statement {
return "\(message) (\(statement)) (extended code: \(extendedCode))"
} else {
return "\(message) (extended code: \(extendedCode))"
}
}
}
}
4 changes: 4 additions & 0 deletions Tests/SQLiteTests/Core/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class ConnectionTests: SQLiteTestCase {
XCTAssertEqual(2, db.totalChanges)
}

func test_useExtendedErrorCodes_returnsFalseDefault() throws {
XCTAssertFalse(db.usesExtendedErrorCodes)
}

func test_prepare_preparesAndReturnsStatements() throws {
_ = try db.prepare("SELECT * FROM users WHERE admin = 0")
_ = try db.prepare("SELECT * FROM users WHERE admin = ?", 0)
Expand Down
13 changes: 13 additions & 0 deletions Tests/SQLiteTests/Core/ResultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ class ResultTests: XCTestCase {
XCTAssertEqual("not an error (SELECT 1) (code: 21)",
Result(errorCode: SQLITE_MISUSE, connection: connection, statement: statement)?.description)
}

func test_init_extended_with_other_code_returns_error() {
connection.usesExtendedErrorCodes = true
if case .some(.extendedError(let message, let extendedCode, let statement)) =
Result(errorCode: SQLITE_MISUSE, connection: connection, statement: nil) {
XCTAssertEqual("not an error", message)
XCTAssertEqual(extendedCode, 0)
XCTAssertNil(statement)
XCTAssert(connection === connection)
} else {
XCTFail("no error")
}
}
}
13 changes: 13 additions & 0 deletions Tests/SQLiteTests/Typed/QueryIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ class QueryIntegrationTests: SQLiteTestCase {
}
}

func test_extendedErrorCodes_catchConstraintError() throws {
db.usesExtendedErrorCodes = true
try db.run(users.insert(email <- "[email protected]"))
do {
try db.run(users.insert(email <- "[email protected]"))
XCTFail("expected error")
} catch let Result.extendedError(_, extendedCode, _) where extendedCode == 2_067 {
// SQLITE_CONSTRAINT_UNIQUE expected
} catch let error {
XCTFail("unexpected error: \(error)")
}
}

// https://github.com/stephencelis/SQLite.swift/issues/285
func test_order_by_random() throws {
try insertUsers(["a", "b", "c'"])
Expand Down