mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Merge branch 'dev/GRDB6-singleton-record' into development
This commit is contained in:
commit
91e5f838d3
16 files changed
+702
-183
No files matched your search
+3
-1
@@ -101,13 +101,15 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
|
||||
|
||||
- **Fix**: [#1268](https://github.com/groue/GRDB.swift/pull/1268) by [@groue](https://github.com/groue): Fix SQL generation of CHECK constraints
|
||||
- **Breaking**: The `Column` type is no longer `Equatable`
|
||||
- **Documentation Update**: [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md) describes in detail how to update records that customize their persistence methods.
|
||||
- **Documentation Update**: The [Single-Row Tables](Documentation/SingleRowTables.md) guide was updated for the new persistence callbacks.
|
||||
|
||||
## 6.0.0-beta.2
|
||||
|
||||
Released August 23, 2022 • [diff](https://github.com/groue/GRDB.swift/compare/v6.0.0-beta...v6.0.0-beta.2)
|
||||
|
||||
- **New**: Extended UPSERT apis with the ability to define the conflict target, and the assignments performed in case of conflicts.
|
||||
- **Documentation**: A new [Upsert](README.md#upsert) chapter describes upserts in detail.
|
||||
- **Documentation Update**: A new [Upsert](README.md#upsert) chapter describes upserts in detail.
|
||||
|
||||
## 6.0.0-beta
|
||||
|
||||
|
||||
@@ -172,9 +172,32 @@ The record protocols have been refactored. We tried to keep the amount of modifi
|
||||
|
||||
- **PersistableRecord types now customize persistence methods with "persistence callbacks"**.
|
||||
|
||||
It is no longer possible to override persistence methods such as `insert` or `update`. Customizing the persistence methods is now possible with callbacks such as `willSave`, `willInsert`, or `didDelete` (see [Persistence Callbacks](../README.md#persistence-callbacks) for the full list of callbacks).
|
||||
It is no longer possible to override persistence methods such as `insert` or `update`. Customizing the persistence methods is now possible with callbacks such as `willSave`, `willInsert`, or `didDelete` (see [persistence callbacks] for the full list of callbacks).
|
||||
|
||||
For example, let's consider a record that performs some validation before insertion and updates. In GRDB 5, this would look like:
|
||||
**You have to remove the methods below from your own code base**:
|
||||
|
||||
```swift
|
||||
// GRDB 6: remove those methods from your code
|
||||
func insert(_ db: Database) throws
|
||||
func didInsert(with rowID: Int64, for column: String?)
|
||||
func update(_ db: Database, columns: Set<String>) throws
|
||||
func save(_ db: Database) throws
|
||||
func delete(_ db: Database) throws -> Bool
|
||||
func exists(_ db: Database) throws -> Bool
|
||||
```
|
||||
|
||||
- `insert(_:)`: customization is now made with [persistence callbacks].
|
||||
- `didInsert(with:for:)`: this method was renamed `didInsert(_:)` (see previous bullet point).
|
||||
- `update(_:columns:)`: customization is now made with [persistence callbacks].
|
||||
- `save(_:)`: customization is now made with [persistence callbacks].
|
||||
- `delete(_:)`: customization is now made with [persistence callbacks].
|
||||
- `exists(_:)`: this method is no longer customizable.
|
||||
|
||||
To help you update your applications with persistence callbacks, let's look at two examples.
|
||||
|
||||
First, check the updated [Single-Row Tables](SingleRowTables.md) guide, if your application defines a "singleton record".
|
||||
|
||||
Next, let's consider a record that performs some validation before insertion and updates. In GRDB 5, this would look like:
|
||||
|
||||
```swift
|
||||
// GRDB 5
|
||||
@@ -351,3 +374,5 @@ The record protocols have been refactored. We tried to keep the amount of modifi
|
||||
print("Succesful commit")
|
||||
}
|
||||
```
|
||||
|
||||
[persistence callbacks]: ../README.md#persistence-callbacks
|
||||
@@ -116,16 +116,14 @@ We make our record able to access the database:
|
||||
extension AppConfiguration: FetchableRecord, PersistableRecord {
|
||||
```
|
||||
|
||||
We have seen in the [The Single-Row Table] chapter that by default, updates throw an error if the database table is empty. To avoid this error, we instruct GRDB to perform an insert in case of a failed update (see [Persistence Methods] for more information):
|
||||
We have seen in the [The Single-Row Table] chapter that by default, updates throw an error if the database table is empty. To avoid this error, we instruct GRDB to insert the missing default configuration before attempting to update (see [Persistence Callbacks] for more information about the `willSave` method):
|
||||
|
||||
```swift
|
||||
// Customize the default PersistableRecord behavior
|
||||
func update(_ db: Database, columns: Set<String>) throws {
|
||||
do {
|
||||
try performUpdate(db, columns: columns)
|
||||
} catch PersistenceError.recordNotFound {
|
||||
// No row was updated: perform an insert
|
||||
try performInsert(db)
|
||||
func willUpdate(_ db: Database, columns: Set<String>) throws {
|
||||
// Insert the default configuration if it does not exist yet.
|
||||
if try !exists(db) {
|
||||
try AppConfiguration.default.insert(db)
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -165,12 +163,13 @@ try dbQueue.write { db in
|
||||
try config.update(db)
|
||||
try config.save(db)
|
||||
try config.insert(db)
|
||||
try config.upsert(db)
|
||||
}
|
||||
```
|
||||
|
||||
The three `update`, `save` and `insert` methods can be used interchangeably: all three make sure the configuration is stored in the database.
|
||||
The four `update`, `save`, `insert` and `upsert` methods can be used interchangeably. They all make sure the configuration is stored in the database.
|
||||
|
||||
The `updateChanges` method only updates the values changed by its closure argument (and performs an insert if the database table is empty).
|
||||
The `updateChanges` method only updates the values changed by its closure argument (and performs an initial insert of default configuration if the database table is empty).
|
||||
|
||||
See [Persistence Methods] for more information.
|
||||
|
||||
@@ -215,12 +214,10 @@ extension AppConfiguration {
|
||||
// Database Access
|
||||
extension AppConfiguration: FetchableRecord, PersistableRecord {
|
||||
// Customize the default PersistableRecord behavior
|
||||
func update(_ db: Database, columns: Set<String>) throws {
|
||||
do {
|
||||
try performUpdate(db, columns: columns)
|
||||
} catch PersistenceError.recordNotFound {
|
||||
// No row was updated: perform an insert
|
||||
try performInsert(db)
|
||||
func willUpdate(_ db: Database, columns: Set<String>) throws {
|
||||
// Insert the default configuration if it does not exist yet.
|
||||
if try !exists(db) {
|
||||
try AppConfiguration.default.insert(db)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,4 +236,5 @@ extension AppConfiguration: FetchableRecord, PersistableRecord {
|
||||
[The Single-Row Record]: #the-single-row-record
|
||||
[Wrap-Up]: #wrap-up
|
||||
[DRY]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
|
||||
[Persistence Callbacks]: ../README.md#persistence-callbacks
|
||||
[Persistence Methods]: ../README.md#persistence-methods
|
||||
@@ -100,6 +100,9 @@
|
||||
5616AAF1207CD45E00AC3664 /* RequestProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616AAF0207CD45E00AC3664 /* RequestProtocols.swift */; };
|
||||
5616AAF2207CD45E00AC3664 /* RequestProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616AAF0207CD45E00AC3664 /* RequestProtocols.swift */; };
|
||||
5616AAF3207CD45E00AC3664 /* RequestProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616AAF0207CD45E00AC3664 /* RequestProtocols.swift */; };
|
||||
5616B4FB28B5F5220052017E /* SingletonRecordTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616B4FA28B5F5220052017E /* SingletonRecordTest.swift */; };
|
||||
5616B4FC28B5F5220052017E /* SingletonRecordTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616B4FA28B5F5220052017E /* SingletonRecordTest.swift */; };
|
||||
5616B4FD28B5F5220052017E /* SingletonRecordTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616B4FA28B5F5220052017E /* SingletonRecordTest.swift */; };
|
||||
5617294E223533F40006E219 /* EncodableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56172947223533F40006E219 /* EncodableRecord.swift */; };
|
||||
5617294F223533F40006E219 /* EncodableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56172947223533F40006E219 /* EncodableRecord.swift */; };
|
||||
56172950223533F40006E219 /* EncodableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56172947223533F40006E219 /* EncodableRecord.swift */; };
|
||||
@@ -1366,6 +1369,7 @@
|
||||
5615B287222B17BF00061C1C /* AssociationHasOneThroughDecodableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasOneThroughDecodableRecordTests.swift; sourceTree = "<group>"; };
|
||||
561667001D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationNSDecimalNumberTests.swift; sourceTree = "<group>"; };
|
||||
5616AAF0207CD45E00AC3664 /* RequestProtocols.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestProtocols.swift; sourceTree = "<group>"; };
|
||||
5616B4FA28B5F5220052017E /* SingletonRecordTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingletonRecordTest.swift; sourceTree = "<group>"; };
|
||||
56172947223533F40006E219 /* EncodableRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EncodableRecord.swift; sourceTree = "<group>"; };
|
||||
561CFA7123735015000C8BAA /* TableRecordUpdateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableRecordUpdateTests.swift; sourceTree = "<group>"; };
|
||||
561CFA912376E546000C8BAA /* AssociationHasManyThroughOrderingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyThroughOrderingTests.swift; sourceTree = "<group>"; };
|
||||
@@ -2387,8 +2391,9 @@
|
||||
56A238251B9C74A90082EB20 /* Record */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5674A7251F30A8EF0095F066 /* FetchableRecord */,
|
||||
564E73DE203D50B9000C443C /* JoinSupportTests.swift */,
|
||||
5616B4FA28B5F5220052017E /* SingletonRecordTest.swift */,
|
||||
5674A7251F30A8EF0095F066 /* FetchableRecord */,
|
||||
560B3FA41C19DFF800C58EC7 /* PersistableRecord */,
|
||||
56176C9E1EACEDF9000F3F2B /* Record */,
|
||||
56176C9C1EACEA8C000F3F2B /* TableRecord */,
|
||||
@@ -3434,6 +3439,7 @@
|
||||
5674A72D1F30A9090095F066 /* FetchableRecordDecodableTests.swift in Sources */,
|
||||
56419C5F24A51999004967E1 /* Inverted.swift in Sources */,
|
||||
560432A4228F1668009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */,
|
||||
5616B4FC28B5F5220052017E /* SingletonRecordTest.swift in Sources */,
|
||||
566A84412041914000E50BFD /* MutablePersistableRecordChangesTests.swift in Sources */,
|
||||
56176C701EACCCC9000F3F2B /* FTS5WrapperTokenizerTests.swift in Sources */,
|
||||
56FEE7FF1F47253700D930EA /* TableRecordTests.swift in Sources */,
|
||||
@@ -3669,6 +3675,7 @@
|
||||
563B06CA2185D2E500B38F35 /* ValueObservationFetchTests.swift in Sources */,
|
||||
56D496541D812F5B008276D7 /* SQLExpressionLiteralTests.swift in Sources */,
|
||||
56D496961D81317B008276D7 /* PersistableRecordTests.swift in Sources */,
|
||||
5616B4FB28B5F5220052017E /* SingletonRecordTest.swift in Sources */,
|
||||
56419C5724A51998004967E1 /* Inverted.swift in Sources */,
|
||||
560432A3228F1668009D3FE2 /* AssociationPrefetchingObservationTests.swift in Sources */,
|
||||
5698AC9E1DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */,
|
||||
@@ -4052,6 +4059,7 @@
|
||||
AAA4DD73230F262000C74B15 /* FetchableRecordDecodableTests.swift in Sources */,
|
||||
56419C6724A5199B004967E1 /* Inverted.swift in Sources */,
|
||||
AAA4DD74230F262000C74B15 /* AssociationPrefetchingObservationTests.swift in Sources */,
|
||||
5616B4FD28B5F5220052017E /* SingletonRecordTest.swift in Sources */,
|
||||
AAA4DD75230F262000C74B15 /* MutablePersistableRecordChangesTests.swift in Sources */,
|
||||
AAA4DD76230F262000C74B15 /* FTS5WrapperTokenizerTests.swift in Sources */,
|
||||
AAA4DD77230F262000C74B15 /* TableRecordTests.swift in Sources */,
|
||||
|
||||
@@ -29,12 +29,15 @@ extension MutablePersistableRecord {
|
||||
public func delete(_ db: Database) throws -> Bool {
|
||||
try willDelete(db)
|
||||
|
||||
var deleted = false
|
||||
var deleted: Bool?
|
||||
try aroundDelete(db) {
|
||||
deleted = try deleteWithoutCallbacks(db)
|
||||
return deleted
|
||||
return deleted!
|
||||
}
|
||||
|
||||
guard let deleted else {
|
||||
try persistenceCallbackMisuse("aroundDelete")
|
||||
}
|
||||
didDelete(deleted: deleted)
|
||||
return deleted
|
||||
}
|
||||
|
||||
@@ -32,13 +32,16 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
let inserted = try insertWithCallbacks(db, onConflict: conflictResolution)
|
||||
saved = PersistenceSuccess(inserted)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
|
||||
@@ -215,18 +218,20 @@ extension MutablePersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try insertAndFetchWithCallbacks(
|
||||
success = try insertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#else
|
||||
/// Executes an `INSERT ... RETURNING ...` statement, and returns the
|
||||
@@ -374,18 +379,20 @@ extension MutablePersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try insertAndFetchWithCallbacks(
|
||||
success = try insertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -419,18 +426,20 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willInsert(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundInsert(db) {
|
||||
(inserted, returned) = try insertAndFetchWithoutCallbacks(
|
||||
success = try insertAndFetchWithoutCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return inserted
|
||||
return success!.inserted
|
||||
}
|
||||
|
||||
didInsert(inserted)
|
||||
return (inserted, returned)
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundInsert")
|
||||
}
|
||||
didInsert(success.inserted)
|
||||
return success
|
||||
}
|
||||
|
||||
/// Executes an `INSERT` statement, with `RETURNING` clause if `selection`
|
||||
|
||||
@@ -37,12 +37,15 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
saved = try updateOrInsertWithCallbacks(db, onConflict: conflictResolution)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
|
||||
@@ -131,20 +134,22 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T?
|
||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: T.databaseSelection,
|
||||
fetch: {
|
||||
try T.fetchOne($0)
|
||||
})
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
|
||||
/// Executes an `INSERT ... RETURNING ...` or `UPDATE ... RETURNING ...`
|
||||
@@ -173,18 +178,20 @@ extension MutablePersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T!
|
||||
var success: (saved: PersistenceSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
#else
|
||||
/// Executes an `INSERT ... RETURNING ...` or `UPDATE ... RETURNING ...`
|
||||
@@ -242,20 +249,22 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T?
|
||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: T.databaseSelection,
|
||||
fetch: {
|
||||
try T.fetchOne($0)
|
||||
})
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
|
||||
/// Executes an `INSERT ... RETURNING ...` or `UPDATE ... RETURNING ...`
|
||||
@@ -285,18 +294,20 @@ extension MutablePersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T!
|
||||
var success: (saved: PersistenceSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -36,12 +36,15 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var updated: PersistenceSuccess!
|
||||
var updated: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
updated = try updateWithCallbacks(db, onConflict: conflictResolution, columns: Set(columns))
|
||||
return updated
|
||||
return updated!
|
||||
}
|
||||
|
||||
guard let updated else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(updated)
|
||||
}
|
||||
|
||||
@@ -292,19 +295,21 @@ extension MutablePersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var updated: PersistenceSuccess!
|
||||
var returned: T!
|
||||
var success: (updated: PersistenceSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(updated, returned) = try updateAndFetchWithCallbacks(
|
||||
success = try updateAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
columns: Set(columns),
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return updated
|
||||
return success!.updated
|
||||
}
|
||||
|
||||
didSave(updated)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.updated)
|
||||
return success.returned
|
||||
}
|
||||
|
||||
/// Executes an `UPDATE ... RETURNING ...` statement on the provided
|
||||
@@ -565,19 +570,21 @@ extension MutablePersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var updated: PersistenceSuccess!
|
||||
var returned: T!
|
||||
var success: (updated: PersistenceSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(updated, returned) = try updateAndFetchWithCallbacks(
|
||||
success = try updateAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
columns: Set(columns),
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return updated
|
||||
return success!.updated
|
||||
}
|
||||
|
||||
didSave(updated)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.updated)
|
||||
return success.returned
|
||||
}
|
||||
|
||||
/// Executes an `UPDATE ... RETURNING ...` statement on the provided
|
||||
@@ -777,19 +784,21 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willUpdate(db, columns: columns)
|
||||
|
||||
var updated: PersistenceSuccess!
|
||||
var returned: T!
|
||||
var success: (updated: PersistenceSuccess, returned: T)?
|
||||
try aroundUpdate(db, columns: columns) {
|
||||
(updated, returned) = try updateAndFetchWithoutCallbacks(
|
||||
success = try updateAndFetchWithoutCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
columns: columns,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return updated
|
||||
return success!.updated
|
||||
}
|
||||
|
||||
didUpdate(updated)
|
||||
return (updated, returned)
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundUpdate")
|
||||
}
|
||||
didUpdate(success.updated)
|
||||
return success
|
||||
}
|
||||
|
||||
/// Executes an `UPDATE` statement, with `RETURNING` clause if `selection`
|
||||
|
||||
@@ -26,13 +26,16 @@ extension MutablePersistableRecord {
|
||||
public mutating func upsert(_ db: Database) throws {
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
let inserted = try upsertWithCallbacks(db)
|
||||
saved = PersistenceSuccess(inserted)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
|
||||
@@ -144,19 +147,21 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try upsertAndFetchWithCallbacks(
|
||||
success = try upsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictTarget,
|
||||
doUpdate: assignments,
|
||||
selection: T.databaseSelection,
|
||||
decode: { try T(row: $0) })
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#else
|
||||
/// Executes an `INSERT ... ON CONFLICT DO UPDATE` statement.
|
||||
@@ -184,13 +189,16 @@ extension MutablePersistableRecord {
|
||||
public mutating func upsert(_ db: Database) throws {
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
let inserted = try upsertWithCallbacks(db)
|
||||
saved = PersistenceSuccess(inserted)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
|
||||
@@ -304,19 +312,21 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try upsertAndFetchWithCallbacks(
|
||||
success = try upsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictTarget,
|
||||
doUpdate: assignments,
|
||||
selection: T.databaseSelection,
|
||||
decode: { try T(row: $0) })
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -347,19 +357,21 @@ extension MutablePersistableRecord {
|
||||
{
|
||||
try willInsert(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundInsert(db) {
|
||||
(inserted, returned) = try upsertAndFetchWithoutCallbacks(
|
||||
success = try upsertAndFetchWithoutCallbacks(
|
||||
db, onConflict: conflictTarget,
|
||||
doUpdate: assignments,
|
||||
selection: selection,
|
||||
decode: decode)
|
||||
return inserted
|
||||
return success!.inserted
|
||||
}
|
||||
|
||||
didInsert(inserted)
|
||||
return (inserted, returned)
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundInsert")
|
||||
}
|
||||
didInsert(success.inserted)
|
||||
return success
|
||||
}
|
||||
|
||||
/// Executes an `INSERT ... RETURNING` statement, and DOES NOT run
|
||||
|
||||
@@ -22,7 +22,8 @@ public protocol MutablePersistableRecord: EncodableRecord, TableRecord {
|
||||
/// Persistence callback called around the record insertion.
|
||||
///
|
||||
/// If you provide a custom implementation of this method, you must call
|
||||
/// the `insert` parameter at some point in your implementation.
|
||||
/// the `insert` parameter at some point in your implementation, and you
|
||||
/// must rethrow its eventual error.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
@@ -71,7 +72,8 @@ public protocol MutablePersistableRecord: EncodableRecord, TableRecord {
|
||||
/// Persistence callback called around the record update.
|
||||
///
|
||||
/// If you provide a custom implementation of this method, you must call
|
||||
/// the `update` parameter at some point in your implementation.
|
||||
/// the `update` parameter at some point in your implementation, and you
|
||||
/// must rethrow its eventual error.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
@@ -109,7 +111,8 @@ public protocol MutablePersistableRecord: EncodableRecord, TableRecord {
|
||||
/// Persistence callback called around the record update or insertion.
|
||||
///
|
||||
/// If you provide a custom implementation of this method, you must call
|
||||
/// the `save` parameter at some point in your implementation.
|
||||
/// the `save` parameter at some point in your implementation, and you
|
||||
/// must rethrow its eventual error.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
@@ -145,7 +148,8 @@ public protocol MutablePersistableRecord: EncodableRecord, TableRecord {
|
||||
/// Persistence callback called around the destruction of the record.
|
||||
///
|
||||
/// If you provide a custom implementation of this method, you must call
|
||||
/// the `delete` parameter at some point in your implementation.
|
||||
/// the `delete` parameter at some point in your implementation, and you
|
||||
/// must rethrow its eventual error.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
@@ -174,6 +178,21 @@ extension MutablePersistableRecord {
|
||||
public static var persistenceConflictPolicy: PersistenceConflictPolicy {
|
||||
PersistenceConflictPolicy(insert: .abort, update: .abort)
|
||||
}
|
||||
|
||||
/// Call for programmer errors from the `aroundXxx` callbacks.
|
||||
@usableFromInline
|
||||
func persistenceCallbackMisuse(_ callbackName: String) throws -> Never {
|
||||
let message = """
|
||||
Incorrect implementation of the `\(Self.self).\(callbackName)` persistence callback: \
|
||||
the action function was not called, or its error was not rethrown.
|
||||
"""
|
||||
// This is a programmer error, but we must not crash, because it can
|
||||
// only be detected in case of database errors, which happen
|
||||
// infrequently. That's why we gently throw SQLITE_MISUSE.
|
||||
throw DatabaseError(
|
||||
resultCode: .SQLITE_MISUSE,
|
||||
message: message)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Existence Check
|
||||
|
||||
@@ -22,13 +22,16 @@ extension PersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
let inserted = try insertWithCallbacks(db, onConflict: conflictResolution)
|
||||
saved = PersistenceSuccess(inserted)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
}
|
||||
@@ -152,18 +155,20 @@ extension PersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try insertAndFetchWithCallbacks(
|
||||
success = try insertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#else
|
||||
/// Executes an `INSERT ... RETURNING ...` statement, and returns a new
|
||||
@@ -283,18 +288,20 @@ extension PersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try insertAndFetchWithCallbacks(
|
||||
success = try insertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -328,17 +335,19 @@ extension PersistableRecord {
|
||||
{
|
||||
try willInsert(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundInsert(db) {
|
||||
(inserted, returned) = try insertAndFetchWithoutCallbacks(
|
||||
success = try insertAndFetchWithoutCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return inserted
|
||||
return success!.inserted
|
||||
}
|
||||
|
||||
didInsert(inserted)
|
||||
return (inserted, returned)
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundInsert")
|
||||
}
|
||||
didInsert(success.inserted)
|
||||
return success
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,15 @@ extension PersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
saved = try updateOrInsertWithCallbacks(db, onConflict: conflictResolution)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
}
|
||||
@@ -55,20 +58,22 @@ extension PersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T?
|
||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: T.databaseSelection,
|
||||
fetch: {
|
||||
try T.fetchOne($0)
|
||||
})
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
|
||||
/// Executes an `INSERT ... RETURNING ...` or `UPDATE ... RETURNING ...`
|
||||
@@ -97,18 +102,20 @@ extension PersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T!
|
||||
var success: (saved: PersistenceSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
#else
|
||||
/// Executes an `INSERT ... RETURNING ...` or `UPDATE ... RETURNING ...`
|
||||
@@ -134,20 +141,22 @@ extension PersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T?
|
||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: T.databaseSelection,
|
||||
fetch: {
|
||||
try T.fetchOne($0)
|
||||
})
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
|
||||
/// Executes an `INSERT ... RETURNING ...` or `UPDATE ... RETURNING ...`
|
||||
@@ -177,18 +186,20 @@ extension PersistableRecord {
|
||||
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var returned: T!
|
||||
var success: (saved: PersistenceSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(saved, returned) = try updateOrInsertAndFetchWithCallbacks(
|
||||
success = try updateOrInsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictResolution,
|
||||
selection: selection,
|
||||
fetch: fetch)
|
||||
return saved
|
||||
return success!.saved
|
||||
}
|
||||
|
||||
didSave(saved)
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(success.saved)
|
||||
return success.returned
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -26,13 +26,16 @@ extension PersistableRecord {
|
||||
public func upsert(_ db: Database) throws {
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
let inserted = try upsertWithCallbacks(db)
|
||||
saved = PersistenceSuccess(inserted)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
|
||||
@@ -144,19 +147,21 @@ extension PersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try upsertAndFetchWithCallbacks(
|
||||
success = try upsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictTarget,
|
||||
doUpdate: assignments,
|
||||
selection: T.databaseSelection,
|
||||
decode: { try T(row: $0) })
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#else
|
||||
/// Executes an `INSERT ... ON CONFLICT DO UPDATE` statement.
|
||||
@@ -184,13 +189,16 @@ extension PersistableRecord {
|
||||
public func upsert(_ db: Database) throws {
|
||||
try willSave(db)
|
||||
|
||||
var saved: PersistenceSuccess!
|
||||
var saved: PersistenceSuccess?
|
||||
try aroundSave(db) {
|
||||
let inserted = try upsertWithCallbacks(db)
|
||||
saved = PersistenceSuccess(inserted)
|
||||
return saved
|
||||
return saved!
|
||||
}
|
||||
|
||||
guard let saved else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(saved)
|
||||
}
|
||||
|
||||
@@ -304,19 +312,21 @@ extension PersistableRecord {
|
||||
{
|
||||
try willSave(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundSave(db) {
|
||||
(inserted, returned) = try upsertAndFetchWithCallbacks(
|
||||
success = try upsertAndFetchWithCallbacks(
|
||||
db, onConflict: conflictTarget,
|
||||
doUpdate: assignments,
|
||||
selection: T.databaseSelection,
|
||||
decode: { try T(row: $0) })
|
||||
return PersistenceSuccess(inserted)
|
||||
return PersistenceSuccess(success!.inserted)
|
||||
}
|
||||
|
||||
didSave(PersistenceSuccess(inserted))
|
||||
return returned
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundSave")
|
||||
}
|
||||
didSave(PersistenceSuccess(success.inserted))
|
||||
return success.returned
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -335,7 +345,7 @@ extension PersistableRecord {
|
||||
decode: { _ in /* Nothing to decode */ })
|
||||
return inserted
|
||||
}
|
||||
|
||||
|
||||
@inlinable // allow specialization so that empty callbacks are removed
|
||||
func upsertAndFetchWithCallbacks<T>(
|
||||
_ db: Database,
|
||||
@@ -347,18 +357,20 @@ extension PersistableRecord {
|
||||
{
|
||||
try willInsert(db)
|
||||
|
||||
var inserted: InsertionSuccess!
|
||||
var returned: T!
|
||||
var success: (inserted: InsertionSuccess, returned: T)?
|
||||
try aroundInsert(db) {
|
||||
(inserted, returned) = try upsertAndFetchWithoutCallbacks(
|
||||
success = try upsertAndFetchWithoutCallbacks(
|
||||
db, onConflict: conflictTarget,
|
||||
doUpdate: assignments,
|
||||
selection: selection,
|
||||
decode: decode)
|
||||
return inserted
|
||||
return success!.inserted
|
||||
}
|
||||
|
||||
didInsert(inserted)
|
||||
return (inserted, returned)
|
||||
guard let success else {
|
||||
try persistenceCallbackMisuse("aroundInsert")
|
||||
}
|
||||
didInsert(success.inserted)
|
||||
return success
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,8 @@
|
||||
5615B286222B17BA00061C1C /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5615B284222B17B900061C1C /* AssociationHasOneThroughDecodableRecordTests.swift */; };
|
||||
561667041D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 561667001D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift */; };
|
||||
561667081D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 561667001D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift */; };
|
||||
5616B50028B5F5490052017E /* SingletonRecordTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616B4FE28B5F5490052017E /* SingletonRecordTest.swift */; };
|
||||
5616B50128B5F5490052017E /* SingletonRecordTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5616B4FE28B5F5490052017E /* SingletonRecordTest.swift */; };
|
||||
561729552235340E0006E219 /* EncodableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 561729532235340E0006E219 /* EncodableRecord.swift */; };
|
||||
561729562235340E0006E219 /* EncodableRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 561729532235340E0006E219 /* EncodableRecord.swift */; };
|
||||
56176C7E1EACCD2F000F3F2B /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; };
|
||||
@@ -839,6 +841,7 @@
|
||||
5615B277222B109500061C1C /* AssociationHasOneThroughFetchableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasOneThroughFetchableRecordTests.swift; sourceTree = "<group>"; };
|
||||
5615B284222B17B900061C1C /* AssociationHasOneThroughDecodableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasOneThroughDecodableRecordTests.swift; sourceTree = "<group>"; };
|
||||
561667001D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationNSDecimalNumberTests.swift; sourceTree = "<group>"; };
|
||||
5616B4FE28B5F5490052017E /* SingletonRecordTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingletonRecordTest.swift; sourceTree = "<group>"; };
|
||||
561729532235340E0006E219 /* EncodableRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EncodableRecord.swift; sourceTree = "<group>"; };
|
||||
561CFA7B2373503D000C8BAA /* TableRecordUpdateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableRecordUpdateTests.swift; sourceTree = "<group>"; };
|
||||
561CFA9F2376EF4F000C8BAA /* AssociationHasManyOrderingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationHasManyOrderingTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1811,8 +1814,9 @@
|
||||
56A238251B9C74A90082EB20 /* Record */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5674A7251F30A8EF0095F066 /* FetchableRecord */,
|
||||
564E73E7203DA278000C443C /* JoinSupportTests.swift */,
|
||||
5616B4FE28B5F5490052017E /* SingletonRecordTest.swift */,
|
||||
5674A7251F30A8EF0095F066 /* FetchableRecord */,
|
||||
560B3FA41C19DFF800C58EC7 /* PersistableRecord */,
|
||||
56176C9E1EACEDF9000F3F2B /* Record */,
|
||||
56176C9C1EACEA8C000F3F2B /* TableRecord */,
|
||||
@@ -2686,6 +2690,7 @@
|
||||
569BBA2C228DE53200478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */,
|
||||
56F34FC024B094D2007513FC /* SQLExpressionIsConstantTests.swift in Sources */,
|
||||
5670329C212B5462007D270F /* DatabaseUUIDEncodingStrategyTests.swift in Sources */,
|
||||
5616B50128B5F5490052017E /* SingletonRecordTest.swift in Sources */,
|
||||
56F34FC724B0A0C9007513FC /* SQLIdentifyingColumnsTests.swift in Sources */,
|
||||
F3BA810B1CFB3056003DC1BA /* Row+FoundationTests.swift in Sources */,
|
||||
5698AC901DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */,
|
||||
@@ -3068,6 +3073,7 @@
|
||||
569BBA2B228DE53200478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */,
|
||||
56F34FBF24B094D2007513FC /* SQLExpressionIsConstantTests.swift in Sources */,
|
||||
5670329B212B5462007D270F /* DatabaseUUIDEncodingStrategyTests.swift in Sources */,
|
||||
5616B50028B5F5490052017E /* SingletonRecordTest.swift in Sources */,
|
||||
56F34FC624B0A0C9007513FC /* SQLIdentifyingColumnsTests.swift in Sources */,
|
||||
F3BA81131CFB305B003DC1BA /* DatabaseMigratorTests.swift in Sources */,
|
||||
F3BA81381CFB3064003DC1BA /* RecordSubClassTests.swift in Sources */,
|
||||
|
||||
@@ -2421,3 +2421,153 @@ extension MutablePersistableRecordTests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Callback Misuse
|
||||
|
||||
extension MutablePersistableRecordTests {
|
||||
func test_aroundSave_misuse_by_not_calling_the_action() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundSave(_ db: Database, save: () throws -> PersistenceSuccess) throws {
|
||||
// It is a programmer error to not call the `save` argument
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: "CREATE TABLE badRecord(a)")
|
||||
try BadRecord().update(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
|
||||
func test_aroundSave_misuse_by_not_rethrowing_the_action_error() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundSave(_ db: Database, save: () throws -> PersistenceSuccess) throws {
|
||||
// It is a programmer error to not rethrow the error of the `save` argument
|
||||
_ = try? save()
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: "CREATE TABLE badRecord(a)")
|
||||
// Fails because we can't update anything
|
||||
try BadRecord().update(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
|
||||
func test_aroundUpdate_misuse_by_not_calling_the_action() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundUpdate(_ db: Database, columns: Set<String>, update: () throws -> PersistenceSuccess) throws {
|
||||
// It is a programmer error to not call the `update` argument
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: "CREATE TABLE badRecord(a)")
|
||||
try BadRecord().update(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
|
||||
func test_aroundUpdate_misuse_by_not_rethrowing_the_action_error() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundUpdate(_ db: Database, columns: Set<String>, update: () throws -> PersistenceSuccess) throws {
|
||||
// It is a programmer error to not rethrow the error of the `update` argument
|
||||
_ = try? update()
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: "CREATE TABLE badRecord(a)")
|
||||
// Fails because we can't update anything
|
||||
try BadRecord().update(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
|
||||
func test_aroundInsert_misuse_by_not_calling_the_action() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundInsert(_ db: Database, insert: () throws -> InsertionSuccess) throws {
|
||||
// It is a programmer error to not call the `insert` argument
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: "CREATE TABLE badRecord(a)")
|
||||
var record = BadRecord()
|
||||
try record.insert(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
|
||||
func test_aroundInsert_misuse_by_not_rethrowing_the_action_error() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundInsert(_ db: Database, insert: () throws -> InsertionSuccess) throws {
|
||||
// It is a programmer error to not rethrow the error of the `insert` argument
|
||||
_ = try? insert()
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE badRecord(id INTEGER PRIMARY KEY);
|
||||
INSERT INTO badRecord (id) VALUES (1);
|
||||
""")
|
||||
var record = BadRecord()
|
||||
// Fails because we insert a conflict
|
||||
try record.insert(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
|
||||
func test_aroundDelete_misuse_by_not_calling_the_action() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundDelete(_ db: Database, delete: () throws -> Bool) throws {
|
||||
// It is a programmer error to not call the `delete` argument
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: "CREATE TABLE badRecord(a)")
|
||||
try BadRecord().delete(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
|
||||
func test_aroundDelete_misuse_by_not_rethrowing_the_action_error() throws {
|
||||
struct BadRecord: MutablePersistableRecord, Encodable {
|
||||
let id = 1
|
||||
func aroundDelete(_ db: Database, delete: () throws -> Bool) throws {
|
||||
// It is a programmer error to not rethrow the error of the `delete` argument
|
||||
_ = try? delete()
|
||||
}
|
||||
}
|
||||
do {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE badRecord(id INTEGER PRIMARY KEY);
|
||||
CREATE TABLE child(id INTEGER PRIMARY KEY REFERENCES badRecord(id) ON DELETE RESTRICT);
|
||||
INSERT INTO badRecord (id) VALUES (1);
|
||||
INSERT INTO child (id) VALUES (1);
|
||||
""")
|
||||
// Fails because this deletion violates a constraint
|
||||
try BadRecord().delete(db)
|
||||
XCTFail("Expected SQLITE_MISUSE error")
|
||||
}
|
||||
} catch DatabaseError.SQLITE_MISUSE { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
private struct AppConfiguration: Codable {
|
||||
// Support for the single row guarantee
|
||||
private var id = 1
|
||||
|
||||
// The configuration properties
|
||||
var text: String
|
||||
// ... other properties
|
||||
|
||||
init(text: String) {
|
||||
self.text = text
|
||||
}
|
||||
}
|
||||
|
||||
extension AppConfiguration {
|
||||
/// The default configuration
|
||||
static let `default` = AppConfiguration(text: "default")
|
||||
}
|
||||
|
||||
// Database Access
|
||||
extension AppConfiguration: FetchableRecord, PersistableRecord {
|
||||
// Customize the default PersistableRecord behavior
|
||||
func willUpdate(_ db: Database, columns: Set<String>) throws {
|
||||
// Insert the default configuration if it does not exist yet.
|
||||
if try !exists(db) {
|
||||
try AppConfiguration.default.insert(db)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the persisted configuration, or the default one if the
|
||||
/// database table is empty.
|
||||
static func fetch(_ db: Database) throws -> AppConfiguration {
|
||||
try fetchOne(db) ?? .default
|
||||
}
|
||||
}
|
||||
|
||||
class SingletonRecordTest: GRDBTestCase {
|
||||
private func createEmptyAppConfigurationTable(_ db: Database) throws {
|
||||
// Table creation
|
||||
try db.create(table: "appConfiguration") { t in
|
||||
// Single row guarantee
|
||||
t.column("id", .integer).primaryKey(onConflict: .replace).check { $0 == 1 }
|
||||
|
||||
// The configuration columns
|
||||
t.column("text", .text).notNull()
|
||||
// ... other columns
|
||||
}
|
||||
}
|
||||
|
||||
func test_fetch_from_empty_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
// When
|
||||
let config = try AppConfiguration.fetch(db)
|
||||
// Then
|
||||
XCTAssertEqual(config.text, "default")
|
||||
}
|
||||
}
|
||||
|
||||
func test_fetch_from_populated_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
try AppConfiguration(text: "initial").insert(db)
|
||||
// When
|
||||
let config = try AppConfiguration.fetch(db)
|
||||
// Then
|
||||
XCTAssertEqual(config.text, "initial")
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_in_empty_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").insert(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_in_populated_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
try AppConfiguration(text: "initial").insert(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").insert(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_update_in_empty_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").update(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_update_in_populated_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
try AppConfiguration(text: "initial").insert(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").update(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_update_changes_in_empty_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
// When
|
||||
var config = try AppConfiguration.fetch(db)
|
||||
try config.updateChanges(db) {
|
||||
$0.text = "test"
|
||||
}
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_update_changes_in_populated_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
try AppConfiguration(text: "initial").insert(db)
|
||||
// When
|
||||
var config = try AppConfiguration.fetch(db)
|
||||
try config.updateChanges(db) {
|
||||
$0.text = "test"
|
||||
}
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_save_in_empty_database() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").save(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_save_in_populated() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
try AppConfiguration(text: "initial").insert(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").save(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_upsert_in_empty_database() throws {
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
||||
guard sqlite3_libversion_number() >= 3035000 else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#else
|
||||
guard #available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#endif
|
||||
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").upsert(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
|
||||
func test_upsert_in_populated_database() throws {
|
||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
||||
guard sqlite3_libversion_number() >= 3035000 else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#else
|
||||
guard #available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#endif
|
||||
|
||||
try makeDatabaseQueue().write { db in
|
||||
// Given
|
||||
try createEmptyAppConfigurationTable(db)
|
||||
try AppConfiguration(text: "initial").insert(db)
|
||||
// When
|
||||
try AppConfiguration(text: "test").upsert(db)
|
||||
// Then
|
||||
try XCTAssertEqual(AppConfiguration.fetch(db).text, "test")
|
||||
let row = try XCTUnwrap(Row.fetchOne(db, sql: "SELECT * FROM appConfiguration"))
|
||||
XCTAssertEqual(row, ["id": 1, "text": "test"])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user