You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You create a `Configuration` before opening a database connection:
8
+
9
+
```swift
10
+
var config =Configuration()
11
+
config.readonly=true
12
+
config.maximumReaderCount=2// (DatabasePool only) The default is 5
13
+
14
+
let dbQueue =tryDatabaseQueue( // or DatabasePool
15
+
path: "/path/to/database.sqlite",
16
+
configuration: config)
17
+
```
18
+
19
+
See <doc:DatabaseConnections>.
20
+
21
+
## Frequent Use Cases
22
+
23
+
#### Tracing SQL Statements
24
+
25
+
You can setup a tracing function that prints out all executed SQL requests with ``prepareDatabase(_:)`` and ``Database/trace(options:_:)``:
26
+
27
+
```swift
28
+
var config =Configuration()
29
+
config.prepareDatabase { db in
30
+
db.trace { print("SQL> \($0)") }
31
+
}
32
+
33
+
let dbQueue =tryDatabaseQueue(
34
+
path: "/path/to/database.sqlite",
35
+
configuration: config)
36
+
37
+
// Prints "SQL> SELECT COUNT(*) FROM player"
38
+
let playerCount = dbQueue.read { db in
39
+
try Player.fetchCount(db)
40
+
}
41
+
```
42
+
43
+
#### Public Statement Arguments
44
+
45
+
Debugging is easier when database errors and tracing functions expose the values sent to the database. Since those values may contain sensitive information, verbose logging is disabled by default. You turn it on with ``publicStatementArguments``:
46
+
47
+
```swift
48
+
var config =Configuration()
49
+
#if DEBUG
50
+
// Protect sensitive information by enabling
51
+
// verbose debugging in DEBUG builds only.
52
+
config.publicStatementArguments=true
53
+
#endif
54
+
55
+
let dbQueue =tryDatabaseQueue(
56
+
path: "/path/to/database.sqlite",
57
+
configuration: config)
58
+
59
+
do {
60
+
try dbQueue.write { db in
61
+
user.name=...
62
+
user.location=...
63
+
user.address=...
64
+
user.phoneNumber=...
65
+
try user.save(db)
66
+
}
67
+
} catch {
68
+
// Prints sensitive information in debug builds only
69
+
print(error)
70
+
}
71
+
```
72
+
73
+
> Warning: It is your responsibility to prevent sensitive information from leaking in unexpected locations, so you should not set the `publicStatementArguments` flag in release builds (think about GDPR and other privacy-related rules).
Copy file name to clipboardExpand all lines: README.md
+9-47Lines changed: 9 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -557,7 +557,7 @@ See the [Concurrency] guide for asynchronous database accesses.
557
557
558
558
- When you don't need to modify the database, prefer the `read` method, because several threads can perform reads in parallel.
559
559
560
-
Reads are generally non-blocking, unless the maximum number of concurrent reads has been reached. In this case, a read has to wait for another read to complete. That maximum number can be [configured](#database-configuration).
560
+
Reads are generally non-blocking, unless the maximum number of concurrent reads has been reached. In this case, a read has to wait for another read to complete. That maximum number can be [configured](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/configuration).
561
561
562
562
- Reads are guaranteed an immutable view of the last committed state of the database, regardless of concurrent writes. This kind of isolation is called [snapshot isolation](https://sqlite.org/isolation.html).
563
563
@@ -576,48 +576,6 @@ See [Database Configuration] for DatabasePool options.
576
576
> :bulb: **Tip**: see the [Demo Applications] for sample code.
577
577
578
578
579
-
## Database Configuration
580
-
581
-
```swift
582
-
var config = Configuration()
583
-
config.readonly = true
584
-
config.foreignKeysEnabled = true // Default is already true
585
-
config.label = "MyDatabase" // Useful when your app opens multiple databases
586
-
config.maximumReaderCount = 10 // (DatabasePool only) The default is 5
587
-
588
-
let dbQueue = try DatabaseQueue( // or DatabasePool
589
-
path: "/path/to/database.sqlite",
590
-
configuration: config)
591
-
```
592
-
593
-
In debug builds, you can increase the verbosity of [error descriptions](#databaseerror) and [trace events](#how-do-i-print-a-request-as-sql) if you opt in for public statement arguments:
594
-
595
-
```swift
596
-
#if DEBUG
597
-
// Protect sensitive information by enabling verbose debugging in DEBUG builds only
598
-
config.publicStatementArguments = true
599
-
#endif
600
-
601
-
let dbQueue = try DatabaseQueue(path: ..., configuration: config)
602
-
603
-
do {
604
-
try dbQueue.write { db in
605
-
user.name = ...
606
-
user.location = ...
607
-
user.address = ...
608
-
user.phoneNumber = ...
609
-
try user.save(db)
610
-
}
611
-
} catch {
612
-
// Prints sensitive information in debug builds only
613
-
print(error)
614
-
}
615
-
```
616
-
617
-
> **Warning**: It is your responsibility to prevent sensitive information from leaking in unexpected locations, so you should not set the `publicStatementArguments` flag in release builds (think about GDPR and other privacy-related rules).
618
-
619
-
See [Configuration](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/configuration) for more details and configuration options.
620
-
621
579
622
580
SQLite API
623
581
==========
@@ -6133,7 +6091,7 @@ do {
6133
6091
}
6134
6092
```
6135
6093
6136
-
If you want to see statement arguments in the error description, [make statement arguments public](#database-configuration).
6094
+
If you want to see statement arguments in the error description, [make statement arguments public](https://swiftpackageindex.com/groue/grdb.swift/configuration/publicstatementarguments).
6137
6095
6138
6096
**SQLite uses [results codes](https://www.sqlite.org/rescode.html) to distinguish between various errors**.
6139
6097
@@ -6637,7 +6595,7 @@ try dbQueue.read { db in
6637
6595
}
6638
6596
```
6639
6597
6640
-
If you want to see statement arguments such as `'[email protected]'` in the logged statements, [make statement arguments public](#database-configuration).
6598
+
If you want to see statement arguments such as `'[email protected]'` in the logged statements, [make statement arguments public](https://swiftpackageindex.com/groue/grdb.swift/configuration/publicstatementarguments).
6641
6599
6642
6600
> **Note**: the generated SQL may change between GRDB releases, without notice: don't have your application rely on any specific SQL output.
6643
6601
@@ -6674,7 +6632,7 @@ try dbQueue.read { db in
6674
6632
}
6675
6633
```
6676
6634
6677
-
If you want to see statement arguments such as `'[email protected]'` in the logged statements, [make statement arguments public](#database-configuration).
6635
+
If you want to see statement arguments such as `'[email protected]'` in the logged statements, [make statement arguments public](https://swiftpackageindex.com/groue/grdb.swift/configuration/publicstatementarguments).
6678
6636
6679
6637
### What Are Experimental Features?
6680
6638
@@ -7084,6 +7042,10 @@ This chapter was replaced with [Persistence Callbacks].
7084
7042
7085
7043
This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databaseobservation).
7086
7044
7045
+
#### Database Configuration
7046
+
7047
+
This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/configuration).
7048
+
7087
7049
#### Database Snapshots
7088
7050
7089
7051
This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/concurrency).
@@ -7207,7 +7169,7 @@ This chapter has been superseded by [ValueObservation] and [DatabaseRegionObserv
0 commit comments