Skip to content

Commit

Permalink
#171: always use extended result codes
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Feb 26, 2017
1 parent 7eb592d commit f0d1f23
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
9 changes: 9 additions & 0 deletions GRDB/Core/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@ public final class Database {
}

do {
// Use extended result codes
do {
let code = sqlite3_extended_result_codes(sqliteConnection!, 1)
guard code == SQLITE_OK else {
throw DatabaseError(code: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection)))
}
}

// Eventual passphrase
#if SQLITE_HAS_CODEC
if let passphrase = configuration.passphrase {
try Database.set(passphrase: passphrase, forConnection: sqliteConnection!)
Expand Down
4 changes: 2 additions & 2 deletions Tests/Public/Core/Database/DatabaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ class DatabaseTests : GRDBTestCase {
}
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!, "FOREIGN KEY constraint failed")
XCTAssertEqual(error.sql!, "COMMIT TRANSACTION")
XCTAssertEqual(error.description, "SQLite error 19 with statement `COMMIT TRANSACTION`: FOREIGN KEY constraint failed")
XCTAssertEqual(error.description, "SQLite error 787 with statement `COMMIT TRANSACTION`: FOREIGN KEY constraint failed")
}

// Make sure we can open another transaction
Expand Down
24 changes: 12 additions & 12 deletions Tests/Public/Core/DatabaseError/DatabaseErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class DatabaseErrorTests: GRDBTestCase {
return .commit
}
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)")
XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
XCTAssertEqual(error.description.lowercased(), "sqlite error 787 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")

XCTAssertEqual(sqlQueries.count, 2)
XCTAssertEqual(sqlQueries[0], "INSERT INTO pets (masterId, name) VALUES (1, 'Bobby')")
Expand Down Expand Up @@ -56,10 +56,10 @@ class DatabaseErrorTests: GRDBTestCase {
}
}
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)")
XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
XCTAssertEqual(error.description.lowercased(), "sqlite error 787 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
}
}
}
Expand All @@ -78,10 +78,10 @@ class DatabaseErrorTests: GRDBTestCase {
try db.execute("INSERT INTO pets (masterId, name) VALUES (?, ?)", arguments: [1, "Bobby"])
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)")
XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
XCTAssertEqual(error.description.lowercased(), "sqlite error 787 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
}
}

Expand All @@ -92,10 +92,10 @@ class DatabaseErrorTests: GRDBTestCase {
try statement.execute(arguments: [1, "Bobby"])
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)")
XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
XCTAssertEqual(error.description.lowercased(), "sqlite error 787 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
}
}

Expand All @@ -107,10 +107,10 @@ class DatabaseErrorTests: GRDBTestCase {
try statement.execute()
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)")
XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
XCTAssertEqual(error.description.lowercased(), "sqlite error 787 with statement `insert into pets (masterid, name) values (?, ?)` arguments [1, \"bobby\"]: foreign key constraint failed")
}
}
}
Expand All @@ -127,10 +127,10 @@ class DatabaseErrorTests: GRDBTestCase {
"INSERT INTO pets (masterId, name) VALUES (1, 'Bobby')")
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (1, 'Bobby')")
XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (1, 'bobby')`: foreign key constraint failed")
XCTAssertEqual(error.description.lowercased(), "sqlite error 787 with statement `insert into pets (masterid, name) values (1, 'bobby')`: foreign key constraint failed")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Public/Core/Persistable/PersistableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ class PersistableTests: GRDBTestCase {
do {
try Citizenship(personID: person.id!, countryIsoCode: "US").insert(db)
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
}
try Citizenship(personID: person.id!, countryIsoCode: "FR").insert(db)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ class TransactionObserverTests: GRDBTestCase {
try Artwork(title: "meh").save(db)
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand All @@ -809,7 +809,7 @@ class TransactionObserverTests: GRDBTestCase {
}
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand All @@ -834,7 +834,7 @@ class TransactionObserverTests: GRDBTestCase {
XCTFail("Expected Error")
} catch let error as DatabaseError {
// Immediate constraint check has failed.
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand All @@ -848,7 +848,7 @@ class TransactionObserverTests: GRDBTestCase {
}
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand Down Expand Up @@ -930,7 +930,7 @@ class TransactionObserverTests: GRDBTestCase {
try Artwork(title: "meh").save(db)
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand All @@ -943,7 +943,7 @@ class TransactionObserverTests: GRDBTestCase {
}
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand All @@ -969,7 +969,7 @@ class TransactionObserverTests: GRDBTestCase {
XCTFail("Expected Error")
} catch let error as DatabaseError {
// Immediate constraint check has failed.
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand All @@ -983,7 +983,7 @@ class TransactionObserverTests: GRDBTestCase {
}
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
#if SQLITE_ENABLE_PREUPDATE_HOOK
XCTAssertEqual(observer.willChangeCount, 0)
#endif
Expand Down
6 changes: 3 additions & 3 deletions Tests/Public/Migrations/DatabaseMigratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ class DatabaseMigratorTests : GRDBTestCase {
// The first migration should be committed.
// The second migration should be rollbacked.

XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!.lowercased(), "foreign key constraint failed") // lowercased: accept multiple SQLite version
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)")
XCTAssertEqual(error.description.lowercased(), "sqlite error 19 with statement `insert into pets (masterid, name) values (?, ?)` arguments [123, \"bobby\"]: foreign key constraint failed")
XCTAssertEqual(error.description.lowercased(), "sqlite error 787 with statement `insert into pets (masterid, name) values (?, ?)` arguments [123, \"bobby\"]: foreign key constraint failed")

let names = try dbQueue.inDatabase { db in
try String.fetchAll(db, "SELECT name FROM persons")
Expand Down Expand Up @@ -172,7 +172,7 @@ class DatabaseMigratorTests : GRDBTestCase {
// Migration 1 and 2 should be committed.
// Migration 3 should not be committed.

XCTAssertEqual(error.code, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.primaryResultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.message!, "FOREIGN KEY constraint failed")
XCTAssertTrue(error.sql == nil)
XCTAssertEqual(error.description, "SQLite error 19: FOREIGN KEY constraint failed")
Expand Down

0 comments on commit f0d1f23

Please sign in to comment.