diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index aab69a7f2..47aa724cd 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -124,11 +124,14 @@ public struct Configuration { /// let player = try Player.filter(Column("email") == email).fetchOne(db) /// /// For debugging purpose, you can set this flag to true, and get more - /// precise database reports. It is not recommended to do so in - /// release builds: + /// precise database reports. It is your responsibility to prevent sensitive + /// information from leaking in unexpected locations, so you should not set + /// this flag in release builds (think about GDPR and other + /// privacy-related rules): /// /// var config = Configuration() /// #if DEBUG + /// // Protect sensitive information by enabling verbose debugging in DEBUG builds only /// config.publicStatementArguments = true /// #endif /// @@ -143,22 +146,6 @@ public struct Configuration { /// } catch { /// print(error) /// } - /// - /// Regardless of this flag, you can explicitly ask to see the - /// statement arguments with the `expandedDescription` property of database - /// errors and trace events: - /// - /// // The expanded descriptions of trace events and errors always - /// // contain the sensitive information: - /// db.trace { event in - /// print(event.expandedDescription) - /// } - /// do { - /// let email = "..." - /// let player = try Player.filter(Column("email") == email).fetchOne(db) - /// } catch let error as DatabaseError { - /// print(error.expandedDescription) - /// } public var publicStatementArguments = false // MARK: - Managing SQLite Connections diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 5e7e41d8b..8aa5deee6 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -1664,7 +1664,7 @@ extension Database { /// /// For example: /// - /// UPDATE player SET score = ? WHERE id = ? + /// SELECT * FROM player WHERE email = ? public var sql: String { _sql } #elseif os(Linux) #else @@ -1672,7 +1672,7 @@ extension Database { /// /// For example: /// - /// UPDATE player SET score = ? WHERE id = ? + /// SELECT * FROM player WHERE email = ? @available(OSX 10.12, tvOS 10.0, watchOS 3.0, *) public var sql: String { _sql } #endif @@ -1680,8 +1680,8 @@ extension Database { var _sql: String { switch impl { case .trace_v1: - // Likely a GRDB bug - fatalError("Not get statement SQL") + // Likely a GRDB bug: this api is not supposed to be available + fatalError("Unavailable statement SQL") case let .trace_v2(sqliteStatement, unexpandedSQL, _): if let unexpandedSQL = unexpandedSQL { @@ -1698,7 +1698,11 @@ extension Database { /// /// For example: /// - /// UPDATE player SET score = 1000 WHERE id = 1 + /// SELECT * FROM player WHERE email = 'arthur@example.com' + /// + /// - warning: It is your responsibility to prevent sensitive + /// information from leaking in unexpected locations, so use this + /// property with care. public var expandedSQL: String { switch impl { case let .trace_v1(expandedSQL): @@ -1725,8 +1729,8 @@ extension Database { /// /// For example: /// - /// SELECT * FROM player WHERE id = ? - /// 0.1s SELECT * FROM player WHERE id = ? + /// SELECT * FROM player WHERE email = ? + /// 0.1s SELECT * FROM player WHERE email = ? /// /// The format of the event description may change between GRDB releases, /// without notice: don't have your application rely on any specific format. @@ -1752,11 +1756,15 @@ extension Database { /// /// For example: /// - /// SELECT * FROM player WHERE id = 1 - /// 0.1s SELECT * FROM player WHERE id = 1 + /// SELECT * FROM player WHERE email = 'arthur@example.com' + /// 0.1s SELECT * FROM player WHERE email = 'arthur@example.com' /// /// The format of the event description may change between GRDB releases, /// without notice: don't have your application rely on any specific format. + /// + /// - warning: It is your responsibility to prevent sensitive + /// information from leaking in unexpected locations, so use this + /// property with care. public var expandedDescription: String { switch self { case let .statement(statement): diff --git a/GRDB/Core/DatabaseError.swift b/GRDB/Core/DatabaseError.swift index f1fff6199..c0ff3a9c3 100644 --- a/GRDB/Core/DatabaseError.swift +++ b/GRDB/Core/DatabaseError.swift @@ -460,8 +460,8 @@ extension DatabaseError { /// /// For example: /// - /// SQLite error 19: FOREIGN KEY constraint failed - while executing - /// `INSERT INTO pets (masterId, name) VALUES (?, ?)` + /// SQLite error 19: NOT NULL constraint failed: player.score + /// - while executing `UPDATE player SET score = ? WHERE email = ? /// /// The format of the error description may change between GRDB releases, /// without notice: don't have your application rely on any specific format. @@ -483,12 +483,16 @@ extension DatabaseError { /// /// For example: /// - /// SQLite error 19: FOREIGN KEY constraint failed - while executing - /// `INSERT INTO pets (masterId, name) VALUES (?, ?)` - /// with arguments [1, "Bobby"] + /// SQLite error 19: NOT NULL constraint failed: player.score + /// - while executing `UPDATE player SET score = ? WHERE email = ? + /// with arguments [nil, "arthur@example.com"] /// /// The format of the error description may change between GRDB releases, /// without notice: don't have your application rely on any specific format. + /// + /// - warning: It is your responsibility to prevent sensitive + /// information from leaking in unexpected locations, so use this + /// property with care. public var expandedDescription: String { var description = "SQLite error \(resultCode.rawValue)" if let message = message { diff --git a/README.md b/README.md index 49c1b75dd..feafb538d 100644 --- a/README.md +++ b/README.md @@ -7842,13 +7842,12 @@ You can compile the request into a prepared statement: try dbQueue.read { db in let request = Player.filter(Column("email") == "arthur@example.com") let statement = try request.makePreparedRequest(db).statement - print(statement) // SQL: SELECT * FROM player WHERE email = ?, Arguments: ["O'Brien"] + print(statement) // SELECT * FROM player WHERE email = ? + print(statement.arguments) // ["arthur@example.com"] } ``` -> :warning: **Warning**: The description of prepared statements contains statement arguments. It is your responsibility to prevent sensitive information from leaking in unexpected locations: most apps should prefer tracing, described below. - -A tracing function prints out the executed SQL requests. For example, provide a tracing function when you connect to the database: +Another option is to setup a tracing function that prints out the executed SQL requests. For example, provide a tracing function when you connect to the database: ```swift // Prints all SQL statements @@ -7859,26 +7858,12 @@ config.prepareDatabase { db in let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) try dbQueue.read { db in + // Prints "SELECT * FROM player WHERE email = ?" let players = try Player.filter(Column("email") == "arthur@example.com").fetchAll(db) - // Prints "SELECT * FROM player WHERE email = ?" } ``` -If you want to see statement arguments such as `'arthur@example.com'` in the logged statements, [make statement arguments public](#database-configuration), or adapt the tracing function as below (but mind that it is your responsibility to prevent sensitive information from leaking in unexpected locations): - -```swift -db.trace { event in - #if DEBUG - // Protect sensitive information by enabling verbose output in DEBUG builds only - // Prints "SELECT * FROM player WHERE email = 'arthur@example.com'" - print(event.expandedDescription) - #else - // In release builds, never print statement arguments. - // Prints "SELECT * FROM player WHERE email = ?" - print(event) - #endif -} -``` +If you want to see statement arguments such as `'arthur@example.com'` in the logged statements, [make statement arguments public](#database-configuration). > :point_up: **Note**: the generated SQL may change between GRDB releases, without notice: don't have your application rely on any specific SQL output. @@ -7915,29 +7900,7 @@ try dbQueue.read { db in } ``` -If you want to see statement arguments such as `'arthur@example.com'` in the logged statements, [make statement arguments public](#database-configuration), or adapt the tracing function as below (but mind that it is your responsibility to prevent sensitive information from leaking in unexpected locations): - -```swift -db.trace(options: .profile) { event in - #if DEBUG - // Protect sensitive information by enabling verbose output in DEBUG builds only - // Prints "0.003s SELECT * FROM player WHERE email = 'arthur@example.com'" - print(event.expandedDescription) - if case let .profile(statement, duration) = event, duration > 0.5 { - // Prints "Slow query: SELECT * FROM player WHERE email = 'arthur@example.com'" - print("Slow query: \(statement.expandedSQL)") - } - #else - // In release builds, never print statement arguments. - // Prints "0.003s SELECT * FROM player WHERE email = ?" - print(event) - if case let .profile(statement, duration) = event, duration > 0.5 { - // Prints "Slow query: SELECT * FROM player WHERE email = ?" - print("Slow query: \(statement.sql)") - } - #endif -} -``` +If you want to see statement arguments such as `'arthur@example.com'` in the logged statements, [make statement arguments public](#database-configuration). ### What Are Experimental Features?