mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Refactor database tracing
- Make it possible to log the database label along with the executed SQL - Make it possible to avoid arguments expansion - Make it possible to profile statements
This commit is contained in:
1 parent
9552a5ce40
commit
30f619ca2c
4 files changed
+238
-76
No files matched your search
@@ -3,6 +3,7 @@ included:
|
||||
|
||||
disabled_rules:
|
||||
- cyclomatic_complexity
|
||||
- duplicate_enum_cases
|
||||
- file_length
|
||||
- force_cast
|
||||
- force_try
|
||||
|
||||
@@ -63,11 +63,6 @@ public struct Configuration {
|
||||
/// The default label is nil.
|
||||
public var label: String? = nil
|
||||
|
||||
/// A function that is called on every statement executed by the database.
|
||||
///
|
||||
/// Default: nil
|
||||
public var trace: TraceFunction?
|
||||
|
||||
/// If false, SQLite from version 3.29.0 will not interpret a double-quoted
|
||||
/// string as a string literal if it does not match any valid identifier.
|
||||
///
|
||||
@@ -219,6 +214,3 @@ public struct Configuration {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A tracing function that takes an SQL string.
|
||||
public typealias TraceFunction = (String) -> Void
|
||||
+226
-68
@@ -153,7 +153,7 @@ public final class Database {
|
||||
// MARK: - Private properties
|
||||
|
||||
private var busyCallback: BusyCallback?
|
||||
|
||||
private var trace: ((TraceEvent) -> Void)?
|
||||
private var functions = Set<DatabaseFunction>()
|
||||
private var collations = Set<DatabaseCollation>()
|
||||
private var _readOnlyDepth = 0 // Modify with beginReadOnly/endReadOnly
|
||||
@@ -200,8 +200,6 @@ public final class Database {
|
||||
|
||||
/// This method must be called after database initialization
|
||||
func setup() throws {
|
||||
// Setup trace first, so that setup queries are traced.
|
||||
setupTrace()
|
||||
setupDoubleQuotedStringLiterals()
|
||||
try setupForeignKeys()
|
||||
setupBusyMode()
|
||||
@@ -222,71 +220,6 @@ public final class Database {
|
||||
configuration.SQLiteConnectionDidOpen?()
|
||||
}
|
||||
|
||||
private func setupTrace() {
|
||||
guard configuration.trace != nil else {
|
||||
return
|
||||
}
|
||||
// sqlite3_trace_v2 and sqlite3_expanded_sql were introduced in SQLite 3.14.0
|
||||
// http://www.sqlite.org/changes.html#version_3_14
|
||||
// It is available from macOS 10.12, tvOS 10.0, watchOS 3.0
|
||||
// https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS)
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS)
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_trace_v2(
|
||||
sqliteConnection,
|
||||
UInt32(SQLITE_TRACE_STMT),
|
||||
{ (mask, dbPointer, stmt, unexpandedSQL) -> Int32 in
|
||||
Database.trace_v2(mask, dbPointer, stmt, unexpandedSQL, sqlite3_expanded_sql)
|
||||
},
|
||||
dbPointer)
|
||||
#elseif os(Linux)
|
||||
setupTrace_v1()
|
||||
#else
|
||||
if #available(OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_trace_v2(
|
||||
sqliteConnection,
|
||||
UInt32(SQLITE_TRACE_STMT),
|
||||
{ (mask, dbPointer, stmt, unexpandedSQL) -> Int32 in
|
||||
Database.trace_v2(mask, dbPointer, stmt, unexpandedSQL, sqlite3_expanded_sql)
|
||||
},
|
||||
dbPointer)
|
||||
} else {
|
||||
setupTrace_v1()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !(GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS))
|
||||
// Precondition: configuration.trace != nil
|
||||
private func setupTrace_v1() {
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_trace(sqliteConnection, { (dbPointer, sql) in
|
||||
guard let sql = sql.map(String.init) else { return }
|
||||
let db = Unmanaged<Database>.fromOpaque(dbPointer!).takeUnretainedValue()
|
||||
db.configuration.trace!(sql)
|
||||
}, dbPointer)
|
||||
}
|
||||
#endif
|
||||
|
||||
// Precondition: configuration.trace != nil
|
||||
private static func trace_v2(
|
||||
_ mask: UInt32,
|
||||
_ dbPointer: UnsafeMutableRawPointer?,
|
||||
_ stmt: UnsafeMutableRawPointer?,
|
||||
_ unexpandedSQL: UnsafeMutableRawPointer?,
|
||||
_ sqlite3_expanded_sql: @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<Int8>?)
|
||||
-> Int32
|
||||
{
|
||||
guard let stmt = stmt else { return SQLITE_OK }
|
||||
guard let expandedSQLCString = sqlite3_expanded_sql(OpaquePointer(stmt)) else { return SQLITE_OK }
|
||||
let sql = String(cString: expandedSQLCString)
|
||||
sqlite3_free(expandedSQLCString)
|
||||
let db = Unmanaged<Database>.fromOpaque(dbPointer!).takeUnretainedValue()
|
||||
db.configuration.trace!(sql)
|
||||
return SQLITE_OK
|
||||
}
|
||||
|
||||
private func setupDoubleQuotedStringLiterals() {
|
||||
if configuration.acceptsDoubleQuotedStringLiterals {
|
||||
enableDoubleQuotedStringLiterals(sqliteConnection)
|
||||
@@ -555,6 +488,125 @@ public final class Database {
|
||||
return try block()
|
||||
}
|
||||
|
||||
// MARK: - Trace
|
||||
|
||||
/// Registers a tracing function.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// // Trace all SQL statements executed by the database
|
||||
/// var configuration = Configuration()
|
||||
/// configuration.prepareDatabase = { db in
|
||||
/// db.trace(options: .statement) { event in
|
||||
/// print("SQL: \(event)")
|
||||
/// }
|
||||
/// }
|
||||
/// let dbQueue = try DatabaseQueue(path: "...", configuration: configuration)
|
||||
///
|
||||
/// Pass an empty options set, or a nil tracing function in order to stop
|
||||
/// database tracing.
|
||||
///
|
||||
/// See https://www.sqlite.org/c3ref/trace_v2.html for more information.
|
||||
///
|
||||
/// - parameter options: The set of desired event kinds.
|
||||
/// - parameter trace: the tracing function.
|
||||
public func trace(options: TracingOptions, _ trace: ((TraceEvent) -> Void)? = nil) {
|
||||
SchedulingWatchdog.preconditionValidQueue(self)
|
||||
self.trace = trace
|
||||
|
||||
if options.isEmpty || trace == nil {
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS)
|
||||
sqlite3_trace_v2(sqliteConnection, 0, nil, nil)
|
||||
#elseif os(Linux)
|
||||
sqlite3_trace(sqliteConnection, nil)
|
||||
#else
|
||||
if #available(OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
|
||||
sqlite3_trace_v2(sqliteConnection, 0, nil, nil)
|
||||
} else {
|
||||
sqlite3_trace(sqliteConnection, nil, nil)
|
||||
}
|
||||
#endif
|
||||
return
|
||||
}
|
||||
|
||||
// sqlite3_trace_v2 and sqlite3_expanded_sql were introduced in SQLite 3.14.0
|
||||
// http://www.sqlite.org/changes.html#version_3_14
|
||||
// It is available from macOS 10.12, tvOS 10.0, watchOS 3.0
|
||||
// https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS)
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS)
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_trace_v2(sqliteConnection, UInt32(bitPattern: options.rawValue), { (mask, dbPointer, p, x) in
|
||||
let db = Unmanaged<Database>.fromOpaque(dbPointer!).takeUnretainedValue()
|
||||
db.trace_v2(CInt(bitPattern: mask), p, x, sqlite3_expanded_sql)
|
||||
return SQLITE_OK
|
||||
}, dbPointer)
|
||||
#elseif os(Linux)
|
||||
setupTrace_v1()
|
||||
#else
|
||||
if #available(OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_trace_v2(sqliteConnection, UInt32(bitPattern: options.rawValue), { (mask, dbPointer, p, x) in
|
||||
let db = Unmanaged<Database>.fromOpaque(dbPointer!).takeUnretainedValue()
|
||||
db.trace_v2(CInt(bitPattern: mask), p, x, sqlite3_expanded_sql)
|
||||
return SQLITE_OK
|
||||
}, dbPointer)
|
||||
} else {
|
||||
setupTrace_v1()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !(GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS))
|
||||
private func setupTrace_v1() {
|
||||
let dbPointer = Unmanaged.passUnretained(self).toOpaque()
|
||||
sqlite3_trace(sqliteConnection, { (dbPointer, sql) in
|
||||
guard let sql = sql.map(String.init(cString:)) else { return }
|
||||
let db = Unmanaged<Database>.fromOpaque(dbPointer!).takeUnretainedValue()
|
||||
db.trace?(Database.TraceEvent.statement(TraceEvent.Statement(impl: .trace_v1(sql))))
|
||||
}, dbPointer)
|
||||
}
|
||||
#endif
|
||||
|
||||
// Precondition: configuration.trace != nil
|
||||
private func trace_v2(
|
||||
_ mask: CInt,
|
||||
_ p: UnsafeMutableRawPointer?,
|
||||
_ x: UnsafeMutableRawPointer?,
|
||||
_ sqlite3_expanded_sql: @escaping @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<Int8>?)
|
||||
{
|
||||
guard let trace = trace else { return }
|
||||
|
||||
switch mask {
|
||||
case SQLITE_TRACE_STMT:
|
||||
if let sqliteStatement = p, let unexpandedSQL = x {
|
||||
let statement = TraceEvent.Statement(impl: .trace_v2(
|
||||
sqliteStatement: OpaquePointer(sqliteStatement),
|
||||
unexpandedSQL: UnsafePointer(unexpandedSQL.assumingMemoryBound(to: CChar.self)),
|
||||
sqlite3_expanded_sql: sqlite3_expanded_sql))
|
||||
trace(TraceEvent.statement(statement))
|
||||
}
|
||||
case SQLITE_TRACE_PROFILE:
|
||||
if let sqliteStatement = p, let durationP = x?.assumingMemoryBound(to: Int64.self) {
|
||||
let statement = TraceEvent.Statement(impl: .trace_v2(
|
||||
sqliteStatement: OpaquePointer(sqliteStatement),
|
||||
unexpandedSQL: nil,
|
||||
sqlite3_expanded_sql: sqlite3_expanded_sql))
|
||||
let duration = TimeInterval(durationP.pointee) / 1.0e9
|
||||
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS)
|
||||
trace(TraceEvent.profile(statement: statement, duration: duration))
|
||||
#elseif os(Linux)
|
||||
#else
|
||||
if #available(OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
|
||||
trace(TraceEvent.profile(statement: statement, duration: duration))
|
||||
}
|
||||
#endif
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Checkpoints
|
||||
|
||||
func checkpoint(_ kind: Database.CheckpointMode) throws {
|
||||
@@ -1308,6 +1360,112 @@ extension Database {
|
||||
/// log function that takes an error message.
|
||||
public typealias LogErrorFunction = (_ resultCode: ResultCode, _ message: String) -> Void
|
||||
|
||||
/// An option for `Database.trace(options:_:)`
|
||||
public struct TracingOptions: OptionSet {
|
||||
public let rawValue: CInt
|
||||
|
||||
public init(rawValue: CInt) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
/// Reports executed statements
|
||||
public static let statement = TracingOptions(rawValue: SQLITE_TRACE_STMT)
|
||||
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS)
|
||||
/// Reports executed statements and the estimated duration that the
|
||||
/// statement took to run.
|
||||
public static let profile = TracingOptions(rawValue: SQLITE_TRACE_PROFILE)
|
||||
#elseif os(Linux)
|
||||
#else
|
||||
/// Reports executed statements and the estimated duration that the
|
||||
/// statement took to run.
|
||||
@available(OSX 10.12, tvOS 10.0, watchOS 3.0, *)
|
||||
public static let profile = TracingOptions(rawValue: SQLITE_TRACE_PROFILE)
|
||||
#endif
|
||||
}
|
||||
|
||||
/// An event reported by `Database.trace(options:_:)`
|
||||
public enum TraceEvent: CustomStringConvertible {
|
||||
|
||||
/// Information about a statement reported by `Database.trace(options:_:)`
|
||||
public struct Statement {
|
||||
enum Impl {
|
||||
case trace_v1(String)
|
||||
case trace_v2(
|
||||
sqliteStatement: SQLiteStatement,
|
||||
unexpandedSQL: UnsafePointer<CChar>?,
|
||||
sqlite3_expanded_sql: @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<Int8>?)
|
||||
}
|
||||
let impl: Impl
|
||||
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS)
|
||||
/// The executed SQL.
|
||||
public var sql: String { _sql }
|
||||
#elseif os(Linux)
|
||||
#else
|
||||
/// The executed SQL.
|
||||
@available(OSX 10.12, tvOS 10.0, watchOS 3.0, *)
|
||||
public var sql: String { _sql }
|
||||
#endif
|
||||
|
||||
var _sql: String {
|
||||
switch impl {
|
||||
case .trace_v1:
|
||||
// Likely a GRDB bug
|
||||
fatalError("Not get statement SQL")
|
||||
|
||||
case let .trace_v2(sqliteStatement, unexpandedSQL, _):
|
||||
if let unexpandedSQL = unexpandedSQL {
|
||||
return String(cString: unexpandedSQL)
|
||||
.trimmingCharacters(in: .sqlStatementSeparators)
|
||||
} else {
|
||||
return String(cString: sqlite3_sql(sqliteStatement))
|
||||
.trimmingCharacters(in: .sqlStatementSeparators)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The executed SQL, with bound parameters expanded.
|
||||
public var expandedSQL: String {
|
||||
switch impl {
|
||||
case let .trace_v1(expandedSQL):
|
||||
return expandedSQL
|
||||
|
||||
case let .trace_v2(sqliteStatement, _, sqlite3_expanded_sql):
|
||||
guard let cString = sqlite3_expanded_sql(sqliteStatement) else {
|
||||
return ""
|
||||
}
|
||||
defer { sqlite3_free(cString) }
|
||||
return String(cString: cString)
|
||||
.trimmingCharacters(in: .sqlStatementSeparators)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An event reported by `TracingOptions.statement`.
|
||||
case statement(Statement)
|
||||
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER || os(iOS)
|
||||
/// An event reported by `TracingOptions.profile`.
|
||||
case profile(statement: Statement, duration: TimeInterval)
|
||||
#elseif os(Linux)
|
||||
#else
|
||||
/// An event reported by `TracingOptions.profile`.
|
||||
@available(OSX 10.12, tvOS 10.0, watchOS 3.0, *)
|
||||
case profile(statement: Statement, duration: TimeInterval)
|
||||
#endif
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case let .statement(statement):
|
||||
return statement.expandedSQL
|
||||
case let .profile(statement: statement, duration: duration):
|
||||
let durationString = String(format: "%.3f", duration)
|
||||
return "\(durationString)s \(statement.expandedSQL)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The end of a transaction: Commit, or Rollback
|
||||
public enum TransactionCompletion {
|
||||
case commit
|
||||
|
||||
@@ -47,6 +47,14 @@ extension AssociationAggregate {
|
||||
{ preconditionFailure() }
|
||||
}
|
||||
|
||||
extension Configuration {
|
||||
@available(*, unavailable, message: "Use Database.trace(options:_:) in Configuration.prepareDatabase instead.")
|
||||
public var trace: TraceFunction? {
|
||||
get { preconditionFailure() }
|
||||
set { preconditionFailure() }
|
||||
}
|
||||
}
|
||||
|
||||
extension DatabaseFunction {
|
||||
@available(*, unavailable, renamed: "callAsFunction(_:)")
|
||||
public func apply(_ arguments: SQLExpressible...) -> SQLExpression
|
||||
@@ -251,6 +259,9 @@ extension TableRecord where Self: FetchableRecord {
|
||||
{ preconditionFailure() }
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
public typealias TraceFunction = (String) -> Void
|
||||
|
||||
extension ValueObservation {
|
||||
@available(*, unavailable, message: "ValueObservation now schedules its values asynchronously on the main queue by default. See ValueObservation.start() for possible configuration")
|
||||
var scheduling: ValueScheduling {
|
||||
|
||||
Reference in new issue
Block a user