From 92e1336db05d5cc09113709d1f7952bbdaa78a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 19 May 2016 14:00:45 +0200 Subject: [PATCH] Fix issue #54 --- GRDB/Core/Database.swift | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index b0166fa5c..7b59e8ad4 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -109,22 +109,26 @@ public final class Database { throw DatabaseError(code: code, message: String.fromCString(sqlite3_errmsg(sqliteConnection))) } - #if SQLITE_HAS_CODEC - do { + do { + #if SQLITE_HAS_CODEC if let passphrase = configuration.passphrase { try Database.setPassphrase(passphrase, forConnection: sqliteConnection) } - - // Fail early if key is wrong or missing - let readCode = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil) - guard readCode == SQLITE_OK else { - throw DatabaseError(code: readCode, message: String.fromCString(sqlite3_errmsg(sqliteConnection))) - } - } catch { - closeConnection(sqliteConnection) - throw error + #endif + + // 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 readCode = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil) + guard readCode == SQLITE_OK else { + throw DatabaseError(code: readCode, message: String.fromCString(sqlite3_errmsg(sqliteConnection))) } - #endif + } catch { + closeConnection(sqliteConnection) + throw error + } self.configuration = configuration self.schemaCache = schemaCache