mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
SQLiteError tests
This commit is contained in:
1 parent
8b2cf74ff3
commit
b39eae5f0b
7 files changed
+116
-42
No files matched your search
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// SQLiteErrorTests.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Gwendal Roué on 06/07/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
@testable import GRDB
|
||||
|
||||
class SQLiteErrorTests: GRDBTestCase {
|
||||
|
||||
func testSQLiteErrorThrownByUpdateStatementContainSQL() {
|
||||
dbQueue.inDatabase { db in
|
||||
do {
|
||||
try db.updateStatement("BLAH")
|
||||
XCTFail()
|
||||
} catch let error as SQLiteError {
|
||||
XCTAssertEqual(error.code, 1)
|
||||
XCTAssertEqual(error.message!, "near \"BLAH\": syntax error")
|
||||
XCTAssertEqual(error.sql!, "BLAH")
|
||||
XCTAssertEqual(error.description, "SQLite error 1 with statement `BLAH`: near \"BLAH\": syntax error")
|
||||
} catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSQLiteErrorThrownByUpdateStatementContainSQLAndBindings() {
|
||||
dbQueue.inDatabase { db in
|
||||
do {
|
||||
try db.execute("CREATE TABLE persons (id INTEGER PRIMARY KEY)")
|
||||
try db.execute("CREATE TABLE pets (masterId INTEGER NOT NULL REFERENCES persons(id), name TEXT)")
|
||||
try db.execute("INSERT INTO pets (masterId, name) VALUES (?, ?)", bindings: [1, "Bobby"])
|
||||
XCTFail()
|
||||
} catch let error as SQLiteError {
|
||||
XCTAssertEqual(error.code, Int(SQLITE_CONSTRAINT))
|
||||
XCTAssertEqual(error.message!, "FOREIGN KEY constraint failed")
|
||||
XCTAssertEqual(error.sql!, "INSERT INTO pets (masterId, name) VALUES (?, ?)")
|
||||
XCTAssertEqual(error.description, "SQLite error 19 with statement `INSERT INTO pets (masterId, name) VALUES (?, ?)` bindings [1, \"Bobby\"]: FOREIGN KEY constraint failed")
|
||||
} catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSQLiteErrorThrownBySelectStatementContainSQL() {
|
||||
dbQueue.inDatabase { db in
|
||||
do {
|
||||
let _ = try SelectStatement(database: db, sql: "SELECT * FROM blah", bindings: nil, unsafe: false)
|
||||
XCTFail()
|
||||
} catch let error as SQLiteError {
|
||||
XCTAssertEqual(error.code, 1)
|
||||
XCTAssertEqual(error.message!, "no such table: blah")
|
||||
XCTAssertEqual(error.sql!, "SELECT * FROM blah")
|
||||
XCTAssertEqual(error.description, "SQLite error 1 with statement `SELECT * FROM blah`: no such table: blah")
|
||||
} catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@
|
||||
5610C20E1B4852D600F8B4EC /* RowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5610C20D1B4852D600F8B4EC /* RowTests.swift */; };
|
||||
5623E08A1B494A8A00B20B7F /* RawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E0891B494A8A00B20B7F /* RawRepresentableTests.swift */; };
|
||||
5623E08C1B49970E00B20B7F /* RowModelDescriptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E08B1B49970E00B20B7F /* RowModelDescriptionTests.swift */; };
|
||||
5623E08F1B4AFA8800B20B7F /* SQLiteErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E08E1B4AFA8800B20B7F /* SQLiteErrorTests.swift */; };
|
||||
5623E0911B4AFACC00B20B7F /* GRDBTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */; };
|
||||
565E191E1B42B2B600D32814 /* Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565E191D1B42B2B600D32814 /* Database.swift */; };
|
||||
5699AE571B42C34A00C35172 /* Statement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699AE561B42C34A00C35172 /* Statement.swift */; };
|
||||
5699AE591B42CD7800C35172 /* Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699AE581B42CD7800C35172 /* Row.swift */; };
|
||||
@@ -32,7 +34,6 @@
|
||||
56CC3C8F1B43A5A700A9A534 /* DatabaseQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */; };
|
||||
56CC3C931B43BF0100A9A534 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C921B43BF0100A9A534 /* Configuration.swift */; };
|
||||
56CC3C951B43C0E600A9A534 /* DatabaseMigrator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C941B43C0E600A9A534 /* DatabaseMigrator.swift */; };
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C981B445DDB00A9A534 /* GRDBTestCase.swift */; };
|
||||
56CC3C9B1B4464AE00A9A534 /* RowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */; };
|
||||
56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */; };
|
||||
56CC3CA51B48289200A9A534 /* RowModelFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */; };
|
||||
@@ -61,6 +62,8 @@
|
||||
5610C20D1B4852D600F8B4EC /* RowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowTests.swift; sourceTree = "<group>"; };
|
||||
5623E0891B494A8A00B20B7F /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = "<group>"; };
|
||||
5623E08B1B49970E00B20B7F /* RowModelDescriptionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelDescriptionTests.swift; sourceTree = "<group>"; };
|
||||
5623E08E1B4AFA8800B20B7F /* SQLiteErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteErrorTests.swift; sourceTree = "<group>"; };
|
||||
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
|
||||
565E191D1B42B2B600D32814 /* Database.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Database.swift; sourceTree = "<group>"; };
|
||||
5699AE561B42C34A00C35172 /* Statement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Statement.swift; sourceTree = "<group>"; };
|
||||
5699AE581B42CD7800C35172 /* Row.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Row.swift; sourceTree = "<group>"; };
|
||||
@@ -81,7 +84,6 @@
|
||||
56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueue.swift; sourceTree = "<group>"; };
|
||||
56CC3C921B43BF0100A9A534 /* Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
|
||||
56CC3C941B43C0E600A9A534 /* DatabaseMigrator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseMigrator.swift; sourceTree = "<group>"; };
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
|
||||
56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = "<group>"; };
|
||||
56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteValueConvertibleTests.swift; sourceTree = "<group>"; };
|
||||
56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelFetchTests.swift; sourceTree = "<group>"; };
|
||||
@@ -129,6 +131,14 @@
|
||||
path = Row;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
5623E08D1B4AFA4200B20B7F /* SQLiteError */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5623E08E1B4AFA8800B20B7F /* SQLiteErrorTests.swift */,
|
||||
);
|
||||
path = SQLiteError;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
56C9E64C1B468EB200DDC541 /* Database */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -184,13 +194,14 @@
|
||||
DC10500F19C904DD00D8CA30 /* GRDB Tests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */,
|
||||
56C9E64C1B468EB200DDC541 /* Database */,
|
||||
56C9E64E1B468EB200DDC541 /* DatabaseMigrator */,
|
||||
56C9E6501B468EB200DDC541 /* SQLiteValueConvertible */,
|
||||
5610C20C1B4852C200F8B4EC /* Row */,
|
||||
56C9E6521B468EB200DDC541 /* RowModel */,
|
||||
56C9E6501B468EB200DDC541 /* SQLiteValueConvertible */,
|
||||
5623E08D1B4AFA4200B20B7F /* SQLiteError */,
|
||||
56C9E6591B468EB200DDC541 /* Statement */,
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTestCase.swift */,
|
||||
DC37740319C8CBB3004FCF85 /* Supporting Files */,
|
||||
);
|
||||
path = "GRDB Tests";
|
||||
@@ -399,6 +410,8 @@
|
||||
56C9E65B1B468EB200DDC541 /* DatabaseTests.swift in Sources */,
|
||||
56C9E65E1B468EB200DDC541 /* PrimaryKeyMultipleTests.swift in Sources */,
|
||||
56C9E6641B468EB200DDC541 /* UpdateStatementTests.swift in Sources */,
|
||||
5623E0911B4AFACC00B20B7F /* GRDBTestCase.swift in Sources */,
|
||||
5623E08F1B4AFA8800B20B7F /* SQLiteErrorTests.swift in Sources */,
|
||||
56C9E6621B468EB200DDC541 /* RowModelSubClassTests.swift in Sources */,
|
||||
56C9E6611B468EB200DDC541 /* PrimaryKeyRowIDTests.swift in Sources */,
|
||||
56C9E65F1B468EB200DDC541 /* PrimaryKeyNoneTests.swift in Sources */,
|
||||
@@ -407,7 +420,6 @@
|
||||
560DB2901B4AD49300B14958 /* RowModelInitializersTests.swift in Sources */,
|
||||
56CC3CA51B48289200A9A534 /* RowModelFetchTests.swift in Sources */,
|
||||
56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */,
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTestCase.swift in Sources */,
|
||||
56C9E6601B468EB200DDC541 /* PrimaryKeySingleTests.swift in Sources */,
|
||||
5623E08C1B49970E00B20B7F /* RowModelDescriptionTests.swift in Sources */,
|
||||
);
|
||||
|
||||
+6
-9
@@ -152,12 +152,17 @@ public final class Database {
|
||||
/// The SQLite connection handle
|
||||
let sqliteConnection = SQLiteConnection()
|
||||
|
||||
/// The last error message
|
||||
var lastErrorMessage: String? { return String.fromCString(sqlite3_errmsg(sqliteConnection)) }
|
||||
|
||||
init(path: String, configuration: Configuration) throws {
|
||||
self.configuration = configuration
|
||||
|
||||
// See https://www.sqlite.org/c3ref/open.html
|
||||
let code = sqlite3_open_v2(path, &sqliteConnection, configuration.sqliteOpenFlags, nil)
|
||||
try throwUnlessSQLITE_OK(code, sqliteConnection: sqliteConnection)
|
||||
if code != SQLITE_OK {
|
||||
throw SQLiteError(code: code, message: String.fromCString(sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
|
||||
if configuration.foreignKeysEnabled {
|
||||
try execute("PRAGMA foreign_keys = ON")
|
||||
@@ -263,14 +268,6 @@ func verboseFailOnError<Result>(@noescape block: (Void) throws -> Result) -> Res
|
||||
}
|
||||
|
||||
|
||||
/// Throws if code is not SQLITE_OK.
|
||||
func throwUnlessSQLITE_OK(code: Int32, sqliteConnection: SQLiteConnection, sql: String? = nil) throws {
|
||||
if code != SQLITE_OK {
|
||||
throw SQLiteError(code: code, sqliteConnection: sqliteConnection, sql: sql)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Feching Rows
|
||||
|
||||
/**
|
||||
|
||||
+18
-16
@@ -47,21 +47,15 @@ public struct SQLiteError : ErrorType {
|
||||
|
||||
// MARK: Not public
|
||||
|
||||
init(code: Int32, message: String? = nil, sql: String? = nil) {
|
||||
/// The bindings that yielded the error (if relevant).
|
||||
/// Not public because the Bindings class has no public method.
|
||||
let bindings: Bindings?
|
||||
|
||||
init(code: Int32, message: String? = nil, sql: String? = nil, bindings: Bindings? = nil) {
|
||||
self.code = Int(code)
|
||||
self.message = message
|
||||
self.sql = sql
|
||||
}
|
||||
|
||||
init(code: Int32, sqliteConnection: SQLiteConnection, sql: String? = nil) {
|
||||
let message: String?
|
||||
let cString = sqlite3_errmsg(sqliteConnection)
|
||||
if cString == nil {
|
||||
message = nil
|
||||
} else {
|
||||
message = String.fromCString(cString)
|
||||
}
|
||||
self.init(code: code, message: message, sql: sql)
|
||||
self.bindings = bindings
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,15 +65,23 @@ extension SQLiteError: CustomStringConvertible {
|
||||
// How to write this with a switch?
|
||||
if let sql = sql {
|
||||
if let message = message {
|
||||
fatalError("SQLite error \(code) with statement `\(sql)`: \(message)")
|
||||
if let bindings = bindings {
|
||||
return "SQLite error \(code) with statement `\(sql)` bindings \(bindings): \(message)"
|
||||
} else {
|
||||
return "SQLite error \(code) with statement `\(sql)`: \(message)"
|
||||
}
|
||||
} else {
|
||||
fatalError("SQLite error \(code) with statement `\(sql)`")
|
||||
if let bindings = bindings {
|
||||
return "SQLite error \(code) with statement `\(sql)` bindings \(bindings)"
|
||||
} else {
|
||||
return "SQLite error \(code) with statement `\(sql)`"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if let message = message {
|
||||
fatalError("SQLite error \(code): \(message)")
|
||||
return "SQLite error \(code): \(message)"
|
||||
} else {
|
||||
fatalError("SQLite error \(code)")
|
||||
return "SQLite error \(code)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ extension SelectStatement {
|
||||
return Row(statement: self, unsafe: self.unsafe)
|
||||
default:
|
||||
verboseFailOnError { () -> Void in
|
||||
throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql)
|
||||
throw SQLiteError(code: code, message: self.database.lastErrorMessage, sql: self.sql, bindings: self.bindings)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -56,7 +56,9 @@ public class Statement {
|
||||
self.database = database
|
||||
self.databaseQueueID = dispatch_get_specific(DatabaseQueue.databaseQueueIDKey)
|
||||
let code = sqlite3_prepare_v2(database.sqliteConnection, sql, -1, &sqliteStatement, nil)
|
||||
try throwUnlessSQLITE_OK(code, sqliteConnection: database.sqliteConnection, sql: sql)
|
||||
if code != SQLITE_OK {
|
||||
throw SQLiteError(code: code, message: database.lastErrorMessage, sql: sql)
|
||||
}
|
||||
|
||||
// Set bingins. Duplicate the didSet property observer since it is not
|
||||
// called during initialization.
|
||||
@@ -93,7 +95,7 @@ public class Statement {
|
||||
|
||||
if code != SQLITE_OK {
|
||||
verboseFailOnError { () -> Void in
|
||||
throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql)
|
||||
throw SQLiteError(code: code, message: database.lastErrorMessage, sql: sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,7 +115,7 @@ public class Statement {
|
||||
let code = sqlite3_reset(sqliteStatement)
|
||||
if code != SQLITE_OK {
|
||||
verboseFailOnError { () -> Void in
|
||||
throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql)
|
||||
throw SQLiteError(code: code, message: database.lastErrorMessage, sql: sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,7 +125,7 @@ public class Statement {
|
||||
let code = sqlite3_clear_bindings(sqliteStatement)
|
||||
if code != SQLITE_OK {
|
||||
verboseFailOnError { () -> Void in
|
||||
throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql)
|
||||
throw SQLiteError(code: code, message: database.lastErrorMessage, sql: sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,22 +24,20 @@
|
||||
|
||||
public final class UpdateStatement : Statement {
|
||||
|
||||
// TODO: document this reset
|
||||
public func execute(bindings bindings: Bindings? = nil) throws {
|
||||
if let bindings = bindings {
|
||||
self.bindings = bindings
|
||||
}
|
||||
|
||||
reset()
|
||||
|
||||
if let trace = database.configuration.trace {
|
||||
trace(sql: sql, bindings: self.bindings)
|
||||
}
|
||||
|
||||
let code = sqlite3_step(sqliteStatement)
|
||||
switch code {
|
||||
case SQLITE_DONE:
|
||||
// the statement has finished executing successfully
|
||||
break
|
||||
default:
|
||||
try throwUnlessSQLITE_OK(code, sqliteConnection: database.sqliteConnection, sql: sql)
|
||||
if code != SQLITE_DONE {
|
||||
throw SQLiteError(code: code, message: database.lastErrorMessage, sql: sql, bindings: self.bindings)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user