Skip to content

Commit

Permalink
#171: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Mar 2, 2017
1 parent 0ff53f0 commit 1b44683
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 26 deletions.
18 changes: 9 additions & 9 deletions GRDB/Core/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ public final class Database {
var sqliteConnection: SQLiteConnection? = nil
let code = sqlite3_open_v2(path, &sqliteConnection, configuration.SQLiteOpenFlags, nil)
guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection)))
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
}

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

Expand All @@ -338,7 +338,7 @@ public final class Database {
do {
let code = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil)
guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection)))
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
}
}
} catch {
Expand Down Expand Up @@ -690,7 +690,7 @@ extension Database {
var sqliteStatement: SQLiteStatement? = nil
let code = sqlite3_prepare_v2(sqliteConnection, statementStart, -1, &sqliteStatement, &statementEnd)
guard code == SQLITE_OK else {
error = DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage, sql: sql)
error = DatabaseError(resultCode: code, message: lastErrorMessage, sql: sql)
break
}

Expand Down Expand Up @@ -787,7 +787,7 @@ extension Database {

guard code == SQLITE_OK else {
// Assume a GRDB bug: there is no point throwing any error.
fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description)
fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description)
}
}

Expand All @@ -802,7 +802,7 @@ extension Database {
nil, nil, nil, nil, nil)
guard code == SQLITE_OK else {
// Assume a GRDB bug: there is no point throwing any error.
fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description)
fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description)
}
}
}
Expand Down Expand Up @@ -891,7 +891,7 @@ extension Database {
}, nil)
guard code == SQLITE_OK else {
// Assume a GRDB bug: there is no point throwing any error.
fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description)
fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description)
}
}

Expand Down Expand Up @@ -960,7 +960,7 @@ extension Database {
sqlite3_key(sqliteConnection, bytes, Int32(data.count))
}
guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection)))
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
}
}

Expand All @@ -979,7 +979,7 @@ extension Database {
sqlite3_rekey(sqliteConnection, bytes, Int32(data.count))
}
guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: String(cString: sqlite3_errmsg(sqliteConnection)))
throw DatabaseError(resultCode: code, message: lastErrorMessage)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions GRDB/Core/DatabaseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,21 @@ public struct DatabaseError : Error {
/// The SQL query that yielded the error (if relevant).
public let sql: String?

/// Creates a Database Error
public init(resultCode: ResultCode = .SQLITE_ERROR, message: String? = nil, sql: String? = nil, arguments: StatementArguments? = nil) {
self.extendedResultCode = resultCode
self.message = message
self.sql = sql
self.arguments = arguments
}

/// Creates a Database Error with a raw Int32 result code.
///
/// This initializer is not public because library user is not supposed to
/// be exposed to raw result codes.
init(resultCode: Int32, message: String? = nil, sql: String? = nil, arguments: StatementArguments? = nil) {
self.init(resultCode: ResultCode(rawValue: resultCode), message: message, sql: sql, arguments: arguments)
}

// MARK: Not public

Expand Down
2 changes: 1 addition & 1 deletion GRDB/Core/DatabasePool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public final class DatabasePool {
// when kind is not .Passive.
let code = sqlite3_wal_checkpoint_v2(db.sqliteConnection, nil, kind.rawValue, nil, nil)
guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: db.lastErrorMessage, sql: nil)
throw DatabaseError(resultCode: code, message: db.lastErrorMessage, sql: nil)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions GRDB/Core/DatabaseReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ extension DatabaseReader {
case SQLITE_OK:
afterBackupStep?()
case let code:
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: dbDest.lastErrorMessage)
throw DatabaseError(resultCode: code, message: dbDest.lastErrorMessage)
}
}
} catch {
Expand All @@ -170,7 +170,7 @@ extension DatabaseReader {
case SQLITE_OK:
break
case let code:
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: dbDest.lastErrorMessage)
throw DatabaseError(resultCode: code, message: dbDest.lastErrorMessage)
}

dbDest.clearSchemaCache()
Expand Down
16 changes: 8 additions & 8 deletions GRDB/Core/Statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class Statement {
}

guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql)
throw DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql)
}

guard remainingSQL.isEmpty else {
Expand All @@ -86,7 +86,7 @@ public class Statement {
// throwing any error.
let code = sqlite3_reset(sqliteStatement)
guard code == SQLITE_OK else {
fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql).description)
fatalError(DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql).description)
}
}

Expand Down Expand Up @@ -183,7 +183,7 @@ public class Statement {
// It looks like sqlite3_bind_xxx() functions do not access the file system.
// They should thus succeed, unless a GRDB bug: there is no point throwing any error.
guard code == SQLITE_OK else {
fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql).description)
fatalError(DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql).description)
}
}

Expand All @@ -194,7 +194,7 @@ public class Statement {
// no point throwing any error.
let code = sqlite3_clear_bindings(sqliteStatement)
guard code == SQLITE_OK else {
fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: database.lastErrorMessage, sql: sql).description)
fatalError(DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql).description)
}
}

Expand Down Expand Up @@ -325,9 +325,9 @@ public final class DatabaseCursor<Element> : Cursor {
return nil
case SQLITE_ROW:
return try element()
case let errorCode:
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: ResultCode(rawValue: errorCode), message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
}
}
}
Expand Down Expand Up @@ -426,13 +426,13 @@ public final class UpdateStatement : Statement {
database.updateStatementDidExecute(self)
return

case let errorCode:
case let code:
// Failure
//
// Let database rethrow eventual transaction observer error:
try database.updateStatementDidFail(self)

throw DatabaseError(resultCode: ResultCode(rawValue: errorCode), message: database.lastErrorMessage, sql: sql, arguments: self.arguments) // Error uses self.arguments, not the optional arguments parameter.
throw DatabaseError(resultCode: code, message: database.lastErrorMessage, sql: sql, arguments: self.arguments) // Error uses self.arguments, not the optional arguments parameter.
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion GRDB/FTS/FTS5CustomTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
}
guard code == SQLITE_OK else {
// Assume a GRDB bug: there is no point throwing any error.
fatalError(DatabaseError(resultCode: ResultCode(rawValue: code), message: lastErrorMessage).description)
fatalError(DatabaseError(resultCode: code, message: lastErrorMessage).description)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions GRDB/FTS/FTS5Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
return SQLITE_OK
})
if (code != SQLITE_OK) {
throw DatabaseError(resultCode: ResultCode(rawValue: code))
throw DatabaseError(resultCode: code)
}
}
return context.tokens
Expand Down Expand Up @@ -147,13 +147,13 @@
}

guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: "failed fts5_tokenizer.xCreate")
throw DatabaseError(resultCode: code, message: "failed fts5_tokenizer.xCreate")
}

if let tokenizerPointer = tokenizerPointer {
self.tokenizerPointer = tokenizerPointer
} else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: "nil tokenizer")
throw DatabaseError(resultCode: code, message: "nil tokenizer")
}
}

Expand Down Expand Up @@ -205,7 +205,7 @@
xTokenizerPointer)

guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code))
throw DatabaseError(resultCode: code)
}

let contextPointer = contextHandle.pointee
Expand Down
2 changes: 1 addition & 1 deletion GRDB/FTS/FTS5WrapperTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
// Inject token bytes into SQLite
let code = tokenCallback(context, flags.rawValue, pToken, nToken, iStart, iEnd)
guard code == SQLITE_OK else {
throw DatabaseError(resultCode: ResultCode(rawValue: code), message: "token callback failed")
throw DatabaseError(resultCode: code, message: "token callback failed")
}
}
})
Expand Down

0 comments on commit 1b44683

Please sign in to comment.