mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Configuration.logSQL is a built-in query tracer, which logs SQL statements and bindings.
This commit is contained in:
1 parent
33d53e4f97
commit
4e001c880a
6 files changed
+60
-8
No files matched your search
@@ -67,7 +67,7 @@ class GRDBTestCase: XCTestCase {
|
||||
|
||||
self.databasePath = "/tmp/GRDB.sqlite"
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(databasePath) } catch { }
|
||||
let configuration = Configuration(trace: { NSLog("%@", $0) })
|
||||
let configuration = Configuration(trace: Configuration.logSQL)
|
||||
self.dbQueue = try! DatabaseQueue(path: databasePath, configuration: configuration)
|
||||
}
|
||||
|
||||
|
||||
+43
-1
@@ -211,6 +211,22 @@ public struct Bindings {
|
||||
}
|
||||
return dictionary
|
||||
}
|
||||
|
||||
var description: String {
|
||||
return "[" + ", ".join(array.map { value in
|
||||
if var string = value as? String {
|
||||
string = string.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
|
||||
string = string.stringByReplacingOccurrencesOfString("\n", withString: "\\n")
|
||||
string = string.stringByReplacingOccurrencesOfString("\r", withString: "\\r")
|
||||
string = string.stringByReplacingOccurrencesOfString("\t", withString: "\\t")
|
||||
string = string.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
|
||||
return "\"\(string)\""
|
||||
} else if let value = value {
|
||||
return "\(value)"
|
||||
} else {
|
||||
return "nil"
|
||||
}}) + "]"
|
||||
}
|
||||
}
|
||||
|
||||
// Support for dictionary-based bindings
|
||||
@@ -227,6 +243,22 @@ public struct Bindings {
|
||||
func dictionary( defaultColumnNames defaultColumnNames: [String]?) -> [String : SQLiteValueConvertible?] {
|
||||
return dictionary
|
||||
}
|
||||
|
||||
var description: String {
|
||||
return "[" + ", ".join(dictionary.map { (key, value) in
|
||||
if var string = value as? String {
|
||||
string = string.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
|
||||
string = string.stringByReplacingOccurrencesOfString("\n", withString: "\\n")
|
||||
string = string.stringByReplacingOccurrencesOfString("\r", withString: "\\r")
|
||||
string = string.stringByReplacingOccurrencesOfString("\t", withString: "\\t")
|
||||
string = string.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
|
||||
return "\(key): \"\(string)\""
|
||||
} else if let value = value {
|
||||
return "\(key): \(value)"
|
||||
} else {
|
||||
return "\(key): nil"
|
||||
}}) + "]"
|
||||
}
|
||||
}
|
||||
|
||||
// IMPLEMENTATION NOTE:
|
||||
@@ -285,7 +317,7 @@ public struct Bindings {
|
||||
|
||||
|
||||
// The protocol for Bindings underlying implementation
|
||||
protocol BindingsImpl {
|
||||
protocol BindingsImpl : CustomStringConvertible {
|
||||
func bindInStatement(statement: Statement)
|
||||
func dictionary(defaultColumnNames defaultColumnNames: [String]?) -> [String: SQLiteValueConvertible?]
|
||||
}
|
||||
@@ -321,3 +353,13 @@ extension Bindings : DictionaryLiteralConvertible {
|
||||
self.init(dictionary)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CustomStringConvertible
|
||||
|
||||
extension Bindings : CustomStringConvertible {
|
||||
/// A String description of self
|
||||
public var description: String {
|
||||
return impl.description
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,17 @@
|
||||
Configuration are arguments to the DatabaseQueue initializers.
|
||||
*/
|
||||
public struct Configuration {
|
||||
|
||||
/// A tracing function that logs SQL statements
|
||||
public static func logSQL(sql: String, bindings: Bindings?) {
|
||||
NSLog("GRDB: %@", sql)
|
||||
if let bindings = bindings {
|
||||
NSLog("GRDB: bindings %@", bindings.description)
|
||||
}
|
||||
}
|
||||
|
||||
/// A tracing function.
|
||||
public typealias TraceFunction = (String) -> Void
|
||||
public typealias TraceFunction = (sql: String, bindings: Bindings?) -> Void
|
||||
|
||||
/// If true, the database has support for foreign keys.
|
||||
public var foreignKeysEnabled: Bool
|
||||
|
||||
@@ -85,7 +85,7 @@ extension SelectStatement {
|
||||
}
|
||||
|
||||
if let appliedTrace = trace {
|
||||
appliedTrace(self.sql)
|
||||
appliedTrace(sql: self.sql, bindings: self.bindings)
|
||||
trace = nil
|
||||
}
|
||||
let code = sqlite3_step(self.sqliteStatement)
|
||||
|
||||
@@ -31,7 +31,7 @@ public final class UpdateStatement : Statement {
|
||||
}
|
||||
reset()
|
||||
if let trace = database.configuration.trace {
|
||||
trace(sql)
|
||||
trace(sql: sql, bindings: self.bindings)
|
||||
}
|
||||
let code = sqlite3_step(sqliteStatement)
|
||||
switch code {
|
||||
|
||||
@@ -63,9 +63,10 @@ Configure databases:
|
||||
|
||||
```swift
|
||||
let configuration = Configuration(
|
||||
foreignKeysEnabled: true, // default true, because, come on
|
||||
readonly: false, // default false
|
||||
trace: { print($0) } // default nil
|
||||
foreignKeysEnabled: true, // Default true, because, come on
|
||||
readonly: false, // Default false
|
||||
trace: Configuration.logSQL // An optional trace function.
|
||||
// Configuration.logSQL logs all SQL statements.
|
||||
)
|
||||
let dbQueue = try DatabaseQueue(
|
||||
path: "/tmp/GRDB.sqlite",
|
||||
|
||||
Reference in new issue
Block a user