mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
inline setupConnection, re-order method calls to call validateFormat later, convert static->instance methods where possible
This commit is contained in:
1 parent
ebc7a9d8e0
commit
29c1c5481c
1 file changed
+73
-80
+73
-80
@@ -203,101 +203,33 @@ extension Database {
|
||||
}
|
||||
throw DatabaseError(resultCode: .SQLITE_INTERNAL) // WTF SQLite?
|
||||
}
|
||||
|
||||
private static func activateExtendedCodes(_ sqliteConnection: SQLiteConnection) throws {
|
||||
let code = sqlite3_extended_result_codes(sqliteConnection, 1)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
|
||||
#if SQLITE_HAS_CODEC
|
||||
private static func validateSQLCipher(_ sqliteConnection: SQLiteConnection) throws {
|
||||
// https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688
|
||||
//
|
||||
// > In order to avoid situations where SQLite might be used
|
||||
// > improperly at runtime, we strongly recommend that
|
||||
// > applications institute a runtime test to ensure that the
|
||||
// > application is actually using SQLCipher on the active
|
||||
// > connection.
|
||||
var sqliteStatement: SQLiteStatement? = nil
|
||||
let code = sqlite3_prepare_v2(sqliteConnection, "PRAGMA cipher_version", -1, &sqliteStatement, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
defer {
|
||||
sqlite3_finalize(sqliteStatement)
|
||||
}
|
||||
if sqlite3_step(sqliteStatement) != SQLITE_ROW || (sqlite3_column_text(sqliteStatement, 0) == nil) {
|
||||
throw DatabaseError(resultCode: .SQLITE_MISUSE, message: """
|
||||
GRDB is not linked against SQLCipher. \
|
||||
Check https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
private static func set(passphrase: String, forConnection sqliteConnection: SQLiteConnection) throws {
|
||||
let data = passphrase.data(using: .utf8)!
|
||||
#if swift(>=5.0)
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0.baseAddress, Int32($0.count))
|
||||
}
|
||||
#else
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0, Int32(data.count))
|
||||
}
|
||||
#endif
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private static func validateDatabaseFormat(_ sqliteConnection: SQLiteConnection) throws {
|
||||
// Users are surprised when they open a picture as a database and
|
||||
// see no error (https://github.com/groue/GRDB.swift/issues/54).
|
||||
//
|
||||
// So let's fail early if file is not a database, or encrypted with
|
||||
// another passphrase.
|
||||
let code = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Database {
|
||||
|
||||
// MARK: - Database Setup
|
||||
func setupConnection() throws {
|
||||
do {
|
||||
try Database.activateExtendedCodes(sqliteConnection)
|
||||
#if SQLITE_HAS_CODEC
|
||||
try Database.validateSQLCipher(sqliteConnection)
|
||||
if let passphrase = configuration.passphrase {
|
||||
try Database.set(passphrase: passphrase, forConnection: sqliteConnection)
|
||||
}
|
||||
#endif
|
||||
try configuration.prepareDatabase?(self)
|
||||
|
||||
try Database.validateDatabaseFormat(sqliteConnection)
|
||||
} catch {
|
||||
throw error
|
||||
}
|
||||
configuration.SQLiteConnectionDidOpen?()
|
||||
}
|
||||
|
||||
/// This method must be called after database initialization
|
||||
func setup() throws {
|
||||
// Setup trace first, so that setup queries are traced.
|
||||
setupTrace()
|
||||
|
||||
try setupConnection()
|
||||
try setupForeignKeys()
|
||||
setupBusyMode()
|
||||
setupDefaultFunctions()
|
||||
setupDefaultCollations()
|
||||
observationBroker.installCommitAndRollbackHooks()
|
||||
try activateExtendedCodes()
|
||||
|
||||
#if SQLITE_HAS_CODEC
|
||||
try validateSQLCipher()
|
||||
if let passphrase = configuration.passphrase {
|
||||
try setCipherPassphrase(passphrase)
|
||||
}
|
||||
#endif
|
||||
|
||||
try configuration.prepareDatabase?(self)
|
||||
try validateFormat()
|
||||
configuration.SQLiteConnectionDidOpen?()
|
||||
}
|
||||
|
||||
private func setupTrace() {
|
||||
@@ -402,6 +334,67 @@ extension Database {
|
||||
add(collation: .localizedCompare)
|
||||
add(collation: .localizedStandardCompare)
|
||||
}
|
||||
|
||||
private func activateExtendedCodes() throws {
|
||||
let code = sqlite3_extended_result_codes(sqliteConnection, 1)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
|
||||
#if SQLITE_HAS_CODEC
|
||||
private func validateSQLCipher() throws {
|
||||
// https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688
|
||||
//
|
||||
// > In order to avoid situations where SQLite might be used
|
||||
// > improperly at runtime, we strongly recommend that
|
||||
// > applications institute a runtime test to ensure that the
|
||||
// > application is actually using SQLCipher on the active
|
||||
// > connection.
|
||||
var sqliteStatement: SQLiteStatement? = nil
|
||||
let code = sqlite3_prepare_v2(sqliteConnection, "PRAGMA cipher_version", -1, &sqliteStatement, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
defer {
|
||||
sqlite3_finalize(sqliteStatement)
|
||||
}
|
||||
if sqlite3_step(sqliteStatement) != SQLITE_ROW || (sqlite3_column_text(sqliteStatement, 0) == nil) {
|
||||
throw DatabaseError(resultCode: .SQLITE_MISUSE, message: """
|
||||
GRDB is not linked against SQLCipher. \
|
||||
Check https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
private func setCipherPassphrase(_ passphrase: String) throws {
|
||||
let data = passphrase.data(using: .utf8)!
|
||||
#if swift(>=5.0)
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0.baseAddress, Int32($0.count))
|
||||
}
|
||||
#else
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0, Int32(data.count))
|
||||
}
|
||||
#endif
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private func validateFormat() throws {
|
||||
// Users are surprised when they open a picture as a database and
|
||||
// see no error (https://github.com/groue/GRDB.swift/issues/54).
|
||||
//
|
||||
// So let's fail early if file is not a database, or encrypted with
|
||||
// another passphrase.
|
||||
let code = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Database {
|
||||
|
||||
Reference in new issue
Block a user