Documentation

This commit is contained in:
Gwendal Roué committed 2020-06-01 07:39:43 +02:00
1 parent b47680638d
commit 9540df84cf
2 files changed
+44 -1

No files matched your search

+5
View File
@@ -70,6 +70,11 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
## Next Release
### Documentation Diff
- Updated FAQ: [How do I print a request as SQL?](README.md#how-do-i-print-a-request-as-sql)
- New FAQ: [How do I monitor the duration of database statements execution?](README.md#how-do-i-monitor-the-duration-of-database-statements-execution)
### Fixed
- [#784](https://github.com/groue/GRDB.swift/pull/784): Fix observation of eager-loaded to-many associations
+39 -1
View File
@@ -7277,6 +7277,7 @@ FAQ
- [How do I open a database stored as a resource of my application?](#how-do-i-open-a-database-stored-as-a-resource-of-my-application)
- [How do I close a database connection?](#how-do-i-close-a-database-connection)
- [How do I print a request as SQL?](#how-do-i-print-a-request-as-sql)
- [How do I monitor the duration of database statements execution?](#how-do-i-monitor-the-duration-of-database-statements-execution)
- [Generic parameter 'T' could not be inferred](#generic-parameter-t-could-not-be-inferred)
- [SQLite error 10 "disk I/O error", SQLite error 23 "not authorized"](#sqlite-error-10-disk-io-error-sqlite-error-23-not-authorized)
- [SQLite error 21 "wrong number of statement arguments" with LIKE queries](#sqlite-error-21-wrong-number-of-statement-arguments-with-like-queries)
@@ -7346,7 +7347,7 @@ try dbQueue.read { db in
}
```
Another option is to setup a tracing function that prints out all SQL requests executed by your application. You provide the trace 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
@@ -7362,9 +7363,46 @@ try dbQueue.read { db in
}
```
If you want to hide values such as `'O''Brien'` from the logged statements, adapt the tracing function as below:
```swift
try db.trace { event in
if case let .statement(statement) = event {
// Prints SELECT * FROM player WHERE name = ?
print(statement.sql)
}
}
```
> :point_up: **Note**: the generated SQL may change between GRDB releases, without notice: don't have your application rely on any specific SQL output.
### How do I monitor the duration of database statements execution?
Use the `trace(options:_:)` method, with the `.profile` option:
```swift
var config = Configuration()
config.prepareDatabase = { db in
try db.trace(options: .profile) { event in
// Prints all SQL statements with their duration
print(event)
// Access to detailed profiling information
if case let .profile(statement, duration) = event, duration > 0.5 {
print("Slow query: \(statement.sql)")
}
}
}
let dbQueue = try DatabaseQueue(path: dbPath, configuration: config)
try dbQueue.read { db in
let players = try Player.filter(Column("name") == "O'Brien").fetchAll(db)
// Prints "0.003s SELECT * FROM player WHERE name = 'O''Brien'"
}
```
### Generic parameter 'T' could not be inferred
You may get this error when using the `read` and `write` methods of database queues and pools: