Fix uses of throwingFirstError(execute:finally:)

This commit is contained in:
Gwendal Roué committed 2020-01-06 08:12:49 +01:00
1 parent c60a945610
commit fd7783c39d
4 files changed
+19 -38

No files matched your search

+3 -7
View File
@@ -569,14 +569,10 @@ public final class Database {
/// Grants read-only access, starting SQLite 3.8.0
func readOnly<T>(_ block: () throws -> T) throws -> T {
try beginReadOnly()
return try throwingFirstError(
execute: {
try beginReadOnly()
return try block()
},
finally: {
try endReadOnly()
})
execute: block,
finally: endReadOnly)
}
// MARK: - Authorizer
+4 -5
View File
@@ -285,15 +285,14 @@ extension DatabaseWriter {
// https://discuss.zetetic.net/t/using-the-sqlite-online-backup-api/2631
// So we'll drop all database objects one after the other.
try writeWithoutTransaction { db in
// Prevent foreign keys from messing with drop table statements
let foreignKeysEnabled = try Bool.fetchOne(db, sql: "PRAGMA foreign_keys")!
if foreignKeysEnabled {
try db.execute(sql: "PRAGMA foreign_keys = OFF")
}
try throwingFirstError(
execute: {
// Prevent foreign keys from messing with drop table statements
if foreignKeysEnabled {
try db.execute(sql: "PRAGMA foreign_keys = OFF")
}
// Remove all database objects, one after the other
try db.inTransaction {
let sql = "SELECT type, name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'"
+7 -7
View File
@@ -46,15 +46,15 @@ struct Migration {
}
private func runWithDisabledForeignKeys(_ db: Database) throws {
// Support for database alterations described at
// https://www.sqlite.org/lang_altertable.html#otheralter
//
// > 1. If foreign key constraints are enabled, disable them using
// > PRAGMA foreign_keys=OFF.
try db.execute(sql: "PRAGMA foreign_keys = OFF")
try throwingFirstError(
execute: {
// Support for database alterations described at
// https://www.sqlite.org/lang_altertable.html#otheralter
//
// > 1. If foreign key constraints are enabled, disable them using
// > PRAGMA foreign_keys=OFF.
try db.execute(sql: "PRAGMA foreign_keys = OFF")
// > 2. Start a transaction.
try db.inTransaction(.immediate) {
try migrate(db)
+5 -19
View File
@@ -126,27 +126,13 @@ extension Character {
/// Makes sure the `finally` function is executed even if `execute` throws, and
/// rethrows the eventual first thrown error.
///
/// For example:
/// Usage:
///
/// // Does not throw
/// try setup()
/// try throwingFirstError(
/// execute: { },
/// finally: { })
///
/// // throws A
/// try throwingFirstError(
/// execute: { throw A },
/// finally: { })
///
/// // throws B
/// try throwingFirstError(
/// execute: { },
/// finally: { throw B })
///
/// // throws A
/// try throwingFirstError(
/// execute: { throw A },
/// finally: { throw B })
/// execute: work,
/// finally: cleanup)
@inline(__always)
func throwingFirstError<T>(execute: () throws -> T, finally: () throws -> Void) throws -> T {
do {
let result = try execute()