mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
[BREAKING] insertAndFetch, saveAndFetch, and updateAndFetch no longer return optionals.
Those optionals were just an annoyance. Now, nil results are replaced with `RecordError.recordNotFound`. Such an error is only expected with the IGNORE conflict policy.
This commit is contained in:
1 parent
3685d6d505
commit
32f41472b6
10 files changed
+233
-147
No files matched your search
@@ -91,7 +91,6 @@ extension MutablePersistableRecord {
|
|||||||
|
|
||||||
extension MutablePersistableRecord {
|
extension MutablePersistableRecord {
|
||||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
||||||
/// from the inserted row.
|
/// from the inserted row.
|
||||||
///
|
///
|
||||||
@@ -108,22 +107,22 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter conflictResolution: A policy for conflict resolution. If
|
/// - parameter conflictResolution: A policy for conflict resolution. If
|
||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - returns: The inserted record, if any. The result can be nil when the
|
/// - returns: The inserted record.
|
||||||
/// conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the insertion failed due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public func insertAndFetch(
|
public func insertAndFetch(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
||||||
throws -> Self?
|
throws -> Self
|
||||||
where Self: FetchableRecord
|
where Self: FetchableRecord
|
||||||
{
|
{
|
||||||
var result = self
|
var result = self
|
||||||
return try result.insertAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
return try result.insertAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
||||||
/// from the inserted row.
|
/// from the inserted row.
|
||||||
///
|
///
|
||||||
@@ -161,11 +160,10 @@ extension MutablePersistableRecord {
|
|||||||
/// var partialPlayer = PartialPlayer(name: "Alice")
|
/// var partialPlayer = PartialPlayer(name: "Alice")
|
||||||
///
|
///
|
||||||
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
||||||
/// if let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self) {
|
/// let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self)
|
||||||
/// print(player.id) // The inserted id
|
/// print(player.id) // The inserted id
|
||||||
/// print(player.name) // The inserted name
|
/// print(player.name) // The inserted name
|
||||||
/// print(player.score) // The default score
|
/// print(player.score) // The default score
|
||||||
/// }
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -174,19 +172,24 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`, if any. The result can be
|
/// - returns: A record of type `returnedType`.
|
||||||
/// nil when the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the insertion failed due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public mutating func insertAndFetch<T: FetchableRecord & TableRecord>(
|
public mutating func insertAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
let record = self
|
||||||
try T.fetchOne($0)
|
return try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
||||||
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw record.recordNotFound(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +269,6 @@ extension MutablePersistableRecord {
|
|||||||
return success.returned
|
return success.returned
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
||||||
/// from the inserted row.
|
/// from the inserted row.
|
||||||
///
|
///
|
||||||
@@ -283,23 +285,23 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter conflictResolution: A policy for conflict resolution. If
|
/// - parameter conflictResolution: A policy for conflict resolution. If
|
||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - returns: The inserted record, if any. The result can be nil when the
|
/// - returns: The inserted record.
|
||||||
/// conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the insertion failed due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public func insertAndFetch(
|
public func insertAndFetch(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
||||||
throws -> Self?
|
throws -> Self
|
||||||
where Self: FetchableRecord
|
where Self: FetchableRecord
|
||||||
{
|
{
|
||||||
var result = self
|
var result = self
|
||||||
return try result.insertAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
return try result.insertAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
||||||
/// from the inserted row.
|
/// from the inserted row.
|
||||||
///
|
///
|
||||||
@@ -337,11 +339,10 @@ extension MutablePersistableRecord {
|
|||||||
/// var partialPlayer = PartialPlayer(name: "Alice")
|
/// var partialPlayer = PartialPlayer(name: "Alice")
|
||||||
///
|
///
|
||||||
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
||||||
/// if let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self) {
|
/// let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self)
|
||||||
/// print(player.id) // The inserted id
|
/// print(player.id) // The inserted id
|
||||||
/// print(player.name) // The inserted name
|
/// print(player.name) // The inserted name
|
||||||
/// print(player.score) // The default score
|
/// print(player.score) // The default score
|
||||||
/// }
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -350,20 +351,25 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`, if any. The result can be
|
/// - returns: A record of type `returnedType`.
|
||||||
/// nil when the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the insertion failed due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public mutating func insertAndFetch<T: FetchableRecord & TableRecord>(
|
public mutating func insertAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
let record = self
|
||||||
try T.fetchOne($0)
|
return try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
||||||
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw record.recordNotFound(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -87,7 +87,6 @@ extension MutablePersistableRecord {
|
|||||||
|
|
||||||
extension MutablePersistableRecord {
|
extension MutablePersistableRecord {
|
||||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
||||||
/// returns a new record built from the saved row.
|
/// returns a new record built from the saved row.
|
||||||
///
|
///
|
||||||
@@ -107,22 +106,22 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter conflictResolution: A policy for conflict resolution. If
|
/// - parameter conflictResolution: A policy for conflict resolution. If
|
||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - returns: The saved record. The result can be nil when the
|
/// - returns: The saved record.
|
||||||
/// conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the database changes fail due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public func saveAndFetch(
|
public func saveAndFetch(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
||||||
throws -> Self?
|
throws -> Self
|
||||||
where Self: FetchableRecord
|
where Self: FetchableRecord
|
||||||
{
|
{
|
||||||
var result = self
|
var result = self
|
||||||
return try result.saveAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
return try result.saveAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
||||||
/// returns a new record built from the saved row.
|
/// returns a new record built from the saved row.
|
||||||
///
|
///
|
||||||
@@ -135,26 +134,31 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`. The result can be nil when
|
/// - returns: A record of type `returnedType`.
|
||||||
/// the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the database changes fail due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public mutating func saveAndFetch<T: FetchableRecord & TableRecord>(
|
public mutating func saveAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try willSave(db)
|
try willSave(db)
|
||||||
|
|
||||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
var success: (saved: PersistenceSuccess, returned: T)?
|
||||||
try aroundSave(db) {
|
try aroundSave(db) {
|
||||||
|
let record = self
|
||||||
success = try updateOrInsertAndFetchWithCallbacks(
|
success = try updateOrInsertAndFetchWithCallbacks(
|
||||||
db, onConflict: conflictResolution,
|
db, onConflict: conflictResolution,
|
||||||
selection: T.databaseSelection,
|
selection: T.databaseSelection,
|
||||||
fetch: {
|
fetch: {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw record.recordNotFound(db)
|
||||||
})
|
})
|
||||||
return success!.saved
|
return success!.saved
|
||||||
}
|
}
|
||||||
@@ -211,7 +215,6 @@ extension MutablePersistableRecord {
|
|||||||
return success.returned
|
return success.returned
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
||||||
/// returns a new record built from the saved row.
|
/// returns a new record built from the saved row.
|
||||||
///
|
///
|
||||||
@@ -231,23 +234,23 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter conflictResolution: A policy for conflict resolution. If
|
/// - parameter conflictResolution: A policy for conflict resolution. If
|
||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - returns: The saved record. The result can be nil when the
|
/// - returns: The saved record.
|
||||||
/// conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the database changes fail due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public func saveAndFetch(
|
public func saveAndFetch(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
||||||
throws -> Self?
|
throws -> Self
|
||||||
where Self: FetchableRecord
|
where Self: FetchableRecord
|
||||||
{
|
{
|
||||||
var result = self
|
var result = self
|
||||||
return try result.saveAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
return try result.saveAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
||||||
/// returns a new record built from the saved row.
|
/// returns a new record built from the saved row.
|
||||||
///
|
///
|
||||||
@@ -260,27 +263,32 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`. The result can be nil when
|
/// - returns: A record of type `returnedType`.
|
||||||
/// the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the database changes fail due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public mutating func saveAndFetch<T: FetchableRecord & TableRecord>(
|
public mutating func saveAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try willSave(db)
|
try willSave(db)
|
||||||
|
|
||||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
var success: (saved: PersistenceSuccess, returned: T)?
|
||||||
try aroundSave(db) {
|
try aroundSave(db) {
|
||||||
|
let record = self
|
||||||
success = try updateOrInsertAndFetchWithCallbacks(
|
success = try updateOrInsertAndFetchWithCallbacks(
|
||||||
db, onConflict: conflictResolution,
|
db, onConflict: conflictResolution,
|
||||||
selection: T.databaseSelection,
|
selection: T.databaseSelection,
|
||||||
fetch: {
|
fetch: {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw record.recordNotFound(db)
|
||||||
})
|
})
|
||||||
return success!.saved
|
return success!.saved
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,7 +218,6 @@ extension MutablePersistableRecord {
|
|||||||
|
|
||||||
extension MutablePersistableRecord {
|
extension MutablePersistableRecord {
|
||||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `UPDATE RETURNING` statement on all columns, and returns a
|
/// Executes an `UPDATE RETURNING` statement on all columns, and returns a
|
||||||
/// new record built from the updated row.
|
/// new record built from the updated row.
|
||||||
///
|
///
|
||||||
@@ -226,23 +225,23 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter conflictResolution: A policy for conflict resolution. If
|
/// - parameter conflictResolution: A policy for conflict resolution. If
|
||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - returns: The updated record. The result can be nil when the
|
/// - returns: The updated record.
|
||||||
/// conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public func updateAndFetch(
|
public func updateAndFetch(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
||||||
throws -> Self?
|
throws -> Self
|
||||||
where Self: FetchableRecord
|
where Self: FetchableRecord
|
||||||
{
|
{
|
||||||
try updateAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
try updateAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `UPDATE RETURNING` statement on all columns, and returns a
|
/// Executes an `UPDATE RETURNING` statement on all columns, and returns a
|
||||||
/// new record built from the updated row.
|
/// new record built from the updated row.
|
||||||
///
|
///
|
||||||
@@ -251,25 +250,28 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`. The result can be nil when
|
/// - returns: A record of type `returnedType`.
|
||||||
/// the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public func updateAndFetch<T: FetchableRecord & TableRecord>(
|
public func updateAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try updateAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
try updateAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw recordNotFound(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Modifies the record according to the provided `modify` closure, and
|
/// Modifies the record according to the provided `modify` closure, and
|
||||||
/// executes an `UPDATE RETURNING` statement that updates the modified
|
/// executes an `UPDATE RETURNING` statement that updates the modified
|
||||||
/// columns, if and only if the record was modified. The method returns a
|
/// columns, if and only if the record was modified. The method returns a
|
||||||
@@ -280,12 +282,13 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter modify: A closure that modifies the record.
|
/// - parameter modify: A closure that modifies the record.
|
||||||
/// - returns: An updated record, or nil if the record has no change, or
|
/// - returns: An updated record, or nil if the record has no change.
|
||||||
/// in case of a failed update due to the `IGNORE` conflict policy.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public mutating func updateChangesAndFetch(
|
public mutating func updateChangesAndFetch(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
@@ -297,7 +300,6 @@ extension MutablePersistableRecord {
|
|||||||
try updateChangesAndFetch(db, onConflict: conflictResolution, as: Self.self, modify: modify)
|
try updateChangesAndFetch(db, onConflict: conflictResolution, as: Self.self, modify: modify)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Modifies the record according to the provided `modify` closure, and
|
/// Modifies the record according to the provided `modify` closure, and
|
||||||
/// executes an `UPDATE RETURNING` statement that updates the modified
|
/// executes an `UPDATE RETURNING` statement that updates the modified
|
||||||
/// columns, if and only if the record was modified. The method returns a
|
/// columns, if and only if the record was modified. The method returns a
|
||||||
@@ -310,12 +312,13 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - parameter modify: A closure that modifies the record.
|
/// - parameter modify: A closure that modifies the record.
|
||||||
/// - returns: A record of type `returnedType`, or nil if the record has
|
/// - returns: A record of type `returnedType`, or nil if the record has
|
||||||
/// no change, or in case of a failed update due to the `IGNORE`
|
/// no change.
|
||||||
/// conflict policy.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public mutating func updateChangesAndFetch<T: FetchableRecord & TableRecord>(
|
public mutating func updateChangesAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
@@ -324,10 +327,16 @@ extension MutablePersistableRecord {
|
|||||||
modify: (inout Self) throws -> Void)
|
modify: (inout Self) throws -> Void)
|
||||||
throws -> T?
|
throws -> T?
|
||||||
{
|
{
|
||||||
try updateChangesAndFetch(
|
let record = self
|
||||||
|
return try updateChangesAndFetch(
|
||||||
db, onConflict: conflictResolution,
|
db, onConflict: conflictResolution,
|
||||||
selection: T.databaseSelection,
|
selection: T.databaseSelection,
|
||||||
fetch: { try T.fetchOne($0) },
|
fetch: {
|
||||||
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw record.recordNotFound(db)
|
||||||
|
},
|
||||||
modify: modify)
|
modify: modify)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -482,7 +491,6 @@ extension MutablePersistableRecord {
|
|||||||
fetch: fetch)
|
fetch: fetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Modifies the record according to the provided `modify` closure, and
|
/// Modifies the record according to the provided `modify` closure, and
|
||||||
/// executes an `UPDATE RETURNING` statement that updates the modified
|
/// executes an `UPDATE RETURNING` statement that updates the modified
|
||||||
/// columns, if and only if the record was modified. The method returns a
|
/// columns, if and only if the record was modified. The method returns a
|
||||||
@@ -495,7 +503,8 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter selection: The returned columns (must not be empty).
|
/// - parameter selection: The returned columns (must not be empty).
|
||||||
/// - parameter fetch: A function that executes it ``Statement`` argument.
|
/// - parameter fetch: A function that executes it ``Statement`` argument.
|
||||||
/// - parameter modify: A closure that modifies the record.
|
/// - parameter modify: A closure that modifies the record.
|
||||||
/// - returns: The result of the `fetch` function.
|
/// - returns: The result of the `fetch` function, or nil if the record
|
||||||
|
/// has no change.
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
@@ -506,7 +515,7 @@ extension MutablePersistableRecord {
|
|||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
selection: [any SQLSelectable],
|
selection: [any SQLSelectable],
|
||||||
fetch: (Statement) throws -> T?,
|
fetch: (Statement) throws -> T,
|
||||||
modify: (inout Self) throws -> Void)
|
modify: (inout Self) throws -> Void)
|
||||||
throws -> T?
|
throws -> T?
|
||||||
{
|
{
|
||||||
@@ -519,7 +528,6 @@ extension MutablePersistableRecord {
|
|||||||
fetch: fetch)
|
fetch: fetch)
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `UPDATE RETURNING` statement on all columns, and returns a
|
/// Executes an `UPDATE RETURNING` statement on all columns, and returns a
|
||||||
/// new record built from the updated row.
|
/// new record built from the updated row.
|
||||||
///
|
///
|
||||||
@@ -527,18 +535,19 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter conflictResolution: A policy for conflict resolution. If
|
/// - parameter conflictResolution: A policy for conflict resolution. If
|
||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - returns: The updated record. The result can be nil when the
|
/// - returns: The updated record.
|
||||||
/// conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public func updateAndFetch(
|
public func updateAndFetch(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
onConflict conflictResolution: Database.ConflictResolution? = nil)
|
||||||
throws -> Self?
|
throws -> Self
|
||||||
where Self: FetchableRecord
|
where Self: FetchableRecord
|
||||||
{
|
{
|
||||||
try updateAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
try updateAndFetch(db, onConflict: conflictResolution, as: Self.self)
|
||||||
@@ -552,26 +561,29 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`. The result can be nil when
|
/// - returns: A record of type `returnedType`.
|
||||||
/// the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public func updateAndFetch<T: FetchableRecord & TableRecord>(
|
public func updateAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try updateAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
try updateAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw recordNotFound(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Modifies the record according to the provided `modify` closure, and
|
/// Modifies the record according to the provided `modify` closure, and
|
||||||
/// executes an `UPDATE RETURNING` statement that updates the modified
|
/// executes an `UPDATE RETURNING` statement that updates the modified
|
||||||
/// columns, if and only if the record was modified. The method returns a
|
/// columns, if and only if the record was modified. The method returns a
|
||||||
@@ -582,12 +594,13 @@ extension MutablePersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter modify: A closure that modifies the record.
|
/// - parameter modify: A closure that modifies the record.
|
||||||
/// - returns: An updated record, or nil if the record has no change, or
|
/// - returns: An updated record, or nil if the record has no change.
|
||||||
/// in case of a failed update due to the `IGNORE` conflict policy.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public mutating func updateChangesAndFetch(
|
public mutating func updateChangesAndFetch(
|
||||||
@@ -600,7 +613,6 @@ extension MutablePersistableRecord {
|
|||||||
try updateChangesAndFetch(db, onConflict: conflictResolution, as: Self.self, modify: modify)
|
try updateChangesAndFetch(db, onConflict: conflictResolution, as: Self.self, modify: modify)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Modifies the record according to the provided `modify` closure, and
|
/// Modifies the record according to the provided `modify` closure, and
|
||||||
/// executes an `UPDATE RETURNING` statement that updates the modified
|
/// executes an `UPDATE RETURNING` statement that updates the modified
|
||||||
/// columns, if and only if the record was modified. The method returns a
|
/// columns, if and only if the record was modified. The method returns a
|
||||||
@@ -613,12 +625,13 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - parameter modify: A closure that modifies the record.
|
/// - parameter modify: A closure that modifies the record.
|
||||||
/// - returns: A record of type `returnedType`, or nil if the record has
|
/// - returns: A record of type `returnedType`, or nil if the record has
|
||||||
/// no change, or in case of a failed update due to the `IGNORE`
|
/// no change.
|
||||||
/// conflict policy.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
/// primary key does not match any row in the database.
|
/// primary key does not match any row in the database.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the update fails due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public mutating func updateChangesAndFetch<T: FetchableRecord & TableRecord>(
|
public mutating func updateChangesAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
@@ -628,10 +641,16 @@ extension MutablePersistableRecord {
|
|||||||
modify: (inout Self) throws -> Void)
|
modify: (inout Self) throws -> Void)
|
||||||
throws -> T?
|
throws -> T?
|
||||||
{
|
{
|
||||||
try updateChangesAndFetch(
|
let record = self
|
||||||
|
return try updateChangesAndFetch(
|
||||||
db, onConflict: conflictResolution,
|
db, onConflict: conflictResolution,
|
||||||
selection: T.databaseSelection,
|
selection: T.databaseSelection,
|
||||||
fetch: { try T.fetchOne($0) },
|
fetch: {
|
||||||
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw record.recordNotFound(db)
|
||||||
|
},
|
||||||
modify: modify)
|
modify: modify)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -789,7 +808,6 @@ extension MutablePersistableRecord {
|
|||||||
fetch: fetch)
|
fetch: fetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Modifies the record according to the provided `modify` closure, and
|
/// Modifies the record according to the provided `modify` closure, and
|
||||||
/// executes an `UPDATE RETURNING` statement that updates the modified
|
/// executes an `UPDATE RETURNING` statement that updates the modified
|
||||||
/// columns, if and only if the record was modified. The method returns a
|
/// columns, if and only if the record was modified. The method returns a
|
||||||
@@ -802,7 +820,8 @@ extension MutablePersistableRecord {
|
|||||||
/// - parameter selection: The returned columns (must not be empty).
|
/// - parameter selection: The returned columns (must not be empty).
|
||||||
/// - parameter fetch: A function that executes it ``Statement`` argument.
|
/// - parameter fetch: A function that executes it ``Statement`` argument.
|
||||||
/// - parameter modify: A closure that modifies the record.
|
/// - parameter modify: A closure that modifies the record.
|
||||||
/// - returns: The result of the `fetch` function.
|
/// - returns: The result of the `fetch` function, or nil if the record
|
||||||
|
/// has no change.
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type,
|
/// error thrown by the persistence callbacks defined by the record type,
|
||||||
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
/// or ``RecordError/recordNotFound(databaseTableName:key:)`` if the
|
||||||
@@ -814,7 +833,7 @@ extension MutablePersistableRecord {
|
|||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
selection: [any SQLSelectable],
|
selection: [any SQLSelectable],
|
||||||
fetch: (Statement) throws -> T?,
|
fetch: (Statement) throws -> T,
|
||||||
modify: (inout Self) throws -> Void)
|
modify: (inout Self) throws -> Void)
|
||||||
throws -> T?
|
throws -> T?
|
||||||
{
|
{
|
||||||
@@ -839,7 +858,7 @@ extension MutablePersistableRecord {
|
|||||||
onConflict conflictResolution: Database.ConflictResolution?,
|
onConflict conflictResolution: Database.ConflictResolution?,
|
||||||
from container: PersistenceContainer,
|
from container: PersistenceContainer,
|
||||||
selection: [any SQLSelectable],
|
selection: [any SQLSelectable],
|
||||||
fetch: (Statement) throws -> T?)
|
fetch: (Statement) throws -> T)
|
||||||
throws -> T?
|
throws -> T?
|
||||||
{
|
{
|
||||||
let changes = try PersistenceContainer(db, self).changesIterator(from: container)
|
let changes = try PersistenceContainer(db, self).changesIterator(from: container)
|
||||||
@@ -861,7 +880,7 @@ extension MutablePersistableRecord {
|
|||||||
onConflict conflictResolution: Database.ConflictResolution?,
|
onConflict conflictResolution: Database.ConflictResolution?,
|
||||||
from container: PersistenceContainer,
|
from container: PersistenceContainer,
|
||||||
selection: [any SQLSelectable],
|
selection: [any SQLSelectable],
|
||||||
fetch: (Statement) throws -> T?)
|
fetch: (Statement) throws -> T)
|
||||||
throws -> T?
|
throws -> T?
|
||||||
{
|
{
|
||||||
let changes = try PersistenceContainer(db, self).changesIterator(from: container)
|
let changes = try PersistenceContainer(db, self).changesIterator(from: container)
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ extension PersistableRecord {
|
|||||||
|
|
||||||
extension PersistableRecord {
|
extension PersistableRecord {
|
||||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
||||||
/// from the inserted row.
|
/// from the inserted row.
|
||||||
///
|
///
|
||||||
@@ -94,11 +93,10 @@ extension PersistableRecord {
|
|||||||
/// let partialPlayer = PartialPlayer(name: "Alice")
|
/// let partialPlayer = PartialPlayer(name: "Alice")
|
||||||
///
|
///
|
||||||
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
||||||
/// if let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self) {
|
/// let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self)
|
||||||
/// print(player.id) // The inserted id
|
/// print(player.id) // The inserted id
|
||||||
/// print(player.name) // The inserted name
|
/// print(player.name) // The inserted name
|
||||||
/// print(player.score) // The default score
|
/// print(player.score) // The default score
|
||||||
/// }
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -107,19 +105,23 @@ extension PersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`, if any. The result can be
|
/// - returns: A record of type `returnedType`.
|
||||||
/// nil when the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the insertion failed due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public func insertAndFetch<T: FetchableRecord & TableRecord>(
|
public func insertAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw recordNotFound(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,7 +201,6 @@ extension PersistableRecord {
|
|||||||
return success.returned
|
return success.returned
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
/// Executes an `INSERT RETURNING` statement, and returns a new record built
|
||||||
/// from the inserted row.
|
/// from the inserted row.
|
||||||
///
|
///
|
||||||
@@ -237,11 +238,10 @@ extension PersistableRecord {
|
|||||||
/// let partialPlayer = PartialPlayer(name: "Alice")
|
/// let partialPlayer = PartialPlayer(name: "Alice")
|
||||||
///
|
///
|
||||||
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
/// // INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
||||||
/// if let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self) {
|
/// let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self)
|
||||||
/// print(player.id) // The inserted id
|
/// print(player.id) // The inserted id
|
||||||
/// print(player.name) // The inserted name
|
/// print(player.name) // The inserted name
|
||||||
/// print(player.score) // The default score
|
/// print(player.score) // The default score
|
||||||
/// }
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -250,20 +250,24 @@ extension PersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`, if any. The result can be
|
/// - returns: A record of type `returnedType`.
|
||||||
/// nil when the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the insertion failed due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public func insertAndFetch<T: FetchableRecord & TableRecord>(
|
public func insertAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw recordNotFound(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ extension PersistableRecord {
|
|||||||
|
|
||||||
extension PersistableRecord {
|
extension PersistableRecord {
|
||||||
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
#if GRDBCUSTOMSQLITE || GRDBCIPHER
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
||||||
/// returns a new record built from the saved row.
|
/// returns a new record built from the saved row.
|
||||||
///
|
///
|
||||||
@@ -52,26 +51,30 @@ extension PersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`. The result can be nil when
|
/// - returns: A record of type `returnedType`.
|
||||||
/// the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the database changes fail due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
public func saveAndFetch<T: FetchableRecord & TableRecord>(
|
public func saveAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try willSave(db)
|
try willSave(db)
|
||||||
|
|
||||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
var success: (saved: PersistenceSuccess, returned: T)?
|
||||||
try aroundSave(db) {
|
try aroundSave(db) {
|
||||||
success = try updateOrInsertAndFetchWithCallbacks(
|
success = try updateOrInsertAndFetchWithCallbacks(
|
||||||
db, onConflict: conflictResolution,
|
db, onConflict: conflictResolution,
|
||||||
selection: T.databaseSelection,
|
selection: T.databaseSelection,
|
||||||
fetch: {
|
fetch: {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw recordNotFound(db)
|
||||||
})
|
})
|
||||||
return success!.saved
|
return success!.saved
|
||||||
}
|
}
|
||||||
@@ -128,7 +131,6 @@ extension PersistableRecord {
|
|||||||
return success.returned
|
return success.returned
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// TODO: GRDB7 make it unable to return an optional
|
|
||||||
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
/// Executes an `INSERT RETURNING` or `UPDATE RETURNING` statement, and
|
||||||
/// returns a new record built from the saved row.
|
/// returns a new record built from the saved row.
|
||||||
///
|
///
|
||||||
@@ -141,27 +143,31 @@ extension PersistableRecord {
|
|||||||
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
/// nil, <doc:/MutablePersistableRecord/persistenceConflictPolicy-1isyv>
|
||||||
/// is used.
|
/// is used.
|
||||||
/// - parameter returnedType: The type of the returned record.
|
/// - parameter returnedType: The type of the returned record.
|
||||||
/// - returns: A record of type `returnedType`. The result can be nil when
|
/// - returns: A record of type `returnedType`.
|
||||||
/// the conflict policy is `IGNORE`.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any
|
||||||
/// error thrown by the persistence callbacks defined by the record type.
|
/// error thrown by the persistence callbacks defined by the record type.
|
||||||
|
/// ``RecordError/recordNotFound(databaseTableName:key:)`` can be
|
||||||
|
/// thrown if the database changes fail due to the IGNORE conflict policy.
|
||||||
@inlinable // allow specialization so that empty callbacks are removed
|
@inlinable // allow specialization so that empty callbacks are removed
|
||||||
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+
|
||||||
public func saveAndFetch<T: FetchableRecord & TableRecord>(
|
public func saveAndFetch<T: FetchableRecord & TableRecord>(
|
||||||
_ db: Database,
|
_ db: Database,
|
||||||
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
onConflict conflictResolution: Database.ConflictResolution? = nil,
|
||||||
as returnedType: T.Type)
|
as returnedType: T.Type)
|
||||||
throws -> T?
|
throws -> T
|
||||||
{
|
{
|
||||||
try willSave(db)
|
try willSave(db)
|
||||||
|
|
||||||
var success: (saved: PersistenceSuccess, returned: T?)?
|
var success: (saved: PersistenceSuccess, returned: T)?
|
||||||
try aroundSave(db) {
|
try aroundSave(db) {
|
||||||
success = try updateOrInsertAndFetchWithCallbacks(
|
success = try updateOrInsertAndFetchWithCallbacks(
|
||||||
db, onConflict: conflictResolution,
|
db, onConflict: conflictResolution,
|
||||||
selection: T.databaseSelection,
|
selection: T.databaseSelection,
|
||||||
fetch: {
|
fetch: {
|
||||||
try T.fetchOne($0)
|
if let result = try T.fetchOne($0) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
throw recordNotFound(db)
|
||||||
})
|
})
|
||||||
return success!.saved
|
return success!.saved
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import Foundation
|
|||||||
/// - ``exists(_:id:)``
|
/// - ``exists(_:id:)``
|
||||||
/// - ``exists(_:key:)-60hf2``
|
/// - ``exists(_:key:)-60hf2``
|
||||||
/// - ``exists(_:key:)-6ha6``
|
/// - ``exists(_:key:)-6ha6``
|
||||||
|
/// - ``recordNotFound(_:)``
|
||||||
///
|
///
|
||||||
/// ### Throwing Record Not Found Errors
|
/// ### Throwing Record Not Found Errors
|
||||||
///
|
///
|
||||||
@@ -697,6 +698,15 @@ extension TableRecord {
|
|||||||
public enum RecordError: Error {
|
public enum RecordError: Error {
|
||||||
/// A record does not exist in the database.
|
/// A record does not exist in the database.
|
||||||
///
|
///
|
||||||
|
/// This error can be thrown from methods that update, such as
|
||||||
|
/// ``MutablePersistableRecord/update(_:onConflict:)``. In this case,
|
||||||
|
/// the error means that the database was not changed.
|
||||||
|
///
|
||||||
|
/// It can also be thrown from methods that inserts or update with a
|
||||||
|
/// `RETURNING` clause, and the `IGNORE` conflict policy. In this case,
|
||||||
|
/// the error notifies that a conflict has prevented the change from
|
||||||
|
/// being applied.
|
||||||
|
///
|
||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - databaseTableName: The table of the missing record.
|
/// - databaseTableName: The table of the missing record.
|
||||||
/// - key: The key of the missing record (column and values).
|
/// - key: The key of the missing record (column and values).
|
||||||
@@ -740,6 +750,30 @@ extension TableRecord {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension TableRecord where Self: EncodableRecord {
|
||||||
|
/// Returns an error that tells that the record does not exist in
|
||||||
|
/// the database.
|
||||||
|
///
|
||||||
|
/// - returns: ``RecordError/recordNotFound(databaseTableName:key:)``, or
|
||||||
|
/// any error that prevented the `RecordError` from being constructed.
|
||||||
|
public func recordNotFound(_ db: Database) -> any Error {
|
||||||
|
do {
|
||||||
|
let databaseTableName = type(of: self).databaseTableName
|
||||||
|
let primaryKey = try db.primaryKey(databaseTableName)
|
||||||
|
|
||||||
|
let container = try PersistenceContainer(db, self)
|
||||||
|
let key = Dictionary(uniqueKeysWithValues: primaryKey.columns.map {
|
||||||
|
($0, container[caseInsensitive: $0]?.databaseValue ?? .null)
|
||||||
|
})
|
||||||
|
return RecordError.recordNotFound(
|
||||||
|
databaseTableName: databaseTableName,
|
||||||
|
key: key)
|
||||||
|
} catch {
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
|
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
|
||||||
extension TableRecord where Self: Identifiable, ID: DatabaseValueConvertible {
|
extension TableRecord where Self: Identifiable, ID: DatabaseValueConvertible {
|
||||||
/// Returns an error for a record that does not exist in the database.
|
/// Returns an error for a record that does not exist in the database.
|
||||||
|
|||||||
@@ -2310,11 +2310,10 @@ try dbQueue.write { db in
|
|||||||
let partialPlayer = PartialPlayer(name: "Alice")
|
let partialPlayer = PartialPlayer(name: "Alice")
|
||||||
|
|
||||||
// INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
// INSERT INTO player (name) VALUES ('Alice') RETURNING *
|
||||||
if let player = try partialPlayer.insertAndFetch(db, as: Player.self) {
|
let player = try partialPlayer.insertAndFetch(db, as: Player.self)
|
||||||
print(player.id) // The inserted id
|
print(player.id) // The inserted id
|
||||||
print(player.name) // The inserted name
|
print(player.name) // The inserted name
|
||||||
print(player.score) // The default score
|
print(player.score) // The default score
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -3088,15 +3087,16 @@ struct Player : MutablePersistableRecord {
|
|||||||
try player.insert(db)
|
try player.insert(db)
|
||||||
```
|
```
|
||||||
|
|
||||||
> **Note**: If you specify the `ignore` policy for inserts, the [`didInsert` callback](#persistence-callbacks) will be called with some random id in case of failed insert. You can detect failed insertions with `insertAndFetch`:
|
> **Note**: If you specify the `ignore` policy for inserts, the [`didInsert` callback](#persistence-callbacks) will be called with some random id in case of failed insert. You can detect failed insertions with `insertAndFetch`:
|
||||||
>
|
>
|
||||||
> ```swift
|
> ```swift
|
||||||
> // How to detect failed `INSERT OR IGNORE`:
|
> // How to detect failed `INSERT OR IGNORE`:
|
||||||
> // INSERT OR IGNORE INTO player ... RETURNING *
|
> // INSERT OR IGNORE INTO player ... RETURNING *
|
||||||
> if let insertedPlayer = try player.insertAndFetch(db) {
|
> do {
|
||||||
|
> let insertedPlayer = try player.insertAndFetch(db) {
|
||||||
> // Succesful insertion
|
> // Succesful insertion
|
||||||
> } else {
|
> catch RecordError.recordNotFound {
|
||||||
> // Ignored failure
|
> // Failed insertion due to IGNORE policy
|
||||||
> }
|
> }
|
||||||
> ```
|
> ```
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -85,6 +85,11 @@
|
|||||||
- [ ] Check https://sqlite.org/sqlar.html
|
- [ ] Check https://sqlite.org/sqlar.html
|
||||||
- [ ] More schema alterations
|
- [ ] More schema alterations
|
||||||
- [ ] Database.clearSchemaCache() is fine, but what about dbPool readers? Can we invalidate the cache for a whole pool?
|
- [ ] Database.clearSchemaCache() is fine, but what about dbPool readers? Can we invalidate the cache for a whole pool?
|
||||||
|
- [ ] GRDB7/concurrency
|
||||||
|
- migrations
|
||||||
|
- AsyncValueObservation
|
||||||
|
- [ ] GRDB7 JSONB https://hachyderm.io/@jason_abbott/111908186742242971#.
|
||||||
|
- [ ] GRDB7: stop exporting CSQLite https://github.com/groue/GRDB.swift/issues/642#issuecomment-1937347696
|
||||||
|
|
||||||
|
|
||||||
## Unsure if necessary
|
## Unsure if necessary
|
||||||
|
|||||||
@@ -1261,7 +1261,7 @@ extension MutablePersistableRecordTests {
|
|||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
try dbQueue.inDatabase { db in
|
try dbQueue.inDatabase { db in
|
||||||
let player = FullPlayer(id: nil, name: "Arthur", score: 1000)
|
let player = FullPlayer(id: nil, name: "Arthur", score: 1000)
|
||||||
let insertedPlayer = try XCTUnwrap(player.insertAndFetch(db))
|
let insertedPlayer = try player.insertAndFetch(db)
|
||||||
XCTAssertEqual(insertedPlayer.id, 1)
|
XCTAssertEqual(insertedPlayer.id, 1)
|
||||||
XCTAssertEqual(insertedPlayer.name, "Arthur")
|
XCTAssertEqual(insertedPlayer.name, "Arthur")
|
||||||
XCTAssertEqual(insertedPlayer.score, 1000)
|
XCTAssertEqual(insertedPlayer.score, 1000)
|
||||||
@@ -1284,7 +1284,7 @@ extension MutablePersistableRecordTests {
|
|||||||
do {
|
do {
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
var partialPlayer = PartialPlayer(name: "Arthur")
|
var partialPlayer = PartialPlayer(name: "Arthur")
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.insertAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.insertAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
INSERT INTO "player" ("id", "name") VALUES (NULL,'Arthur') RETURNING *
|
INSERT INTO "player" ("id", "name") VALUES (NULL,'Arthur') RETURNING *
|
||||||
@@ -1423,7 +1423,7 @@ extension MutablePersistableRecordTests {
|
|||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
try dbQueue.inDatabase { db in
|
try dbQueue.inDatabase { db in
|
||||||
let player = FullPlayer(id: nil, name: "Arthur", score: 1000)
|
let player = FullPlayer(id: nil, name: "Arthur", score: 1000)
|
||||||
let savedPlayer = try XCTUnwrap(player.saveAndFetch(db))
|
let savedPlayer = try player.saveAndFetch(db)
|
||||||
XCTAssertEqual(savedPlayer.id, 1)
|
XCTAssertEqual(savedPlayer.id, 1)
|
||||||
XCTAssertEqual(savedPlayer.name, "Arthur")
|
XCTAssertEqual(savedPlayer.name, "Arthur")
|
||||||
XCTAssertEqual(savedPlayer.score, 1000)
|
XCTAssertEqual(savedPlayer.score, 1000)
|
||||||
@@ -1446,7 +1446,7 @@ extension MutablePersistableRecordTests {
|
|||||||
do {
|
do {
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
var partialPlayer = PartialPlayer(name: "Arthur")
|
var partialPlayer = PartialPlayer(name: "Arthur")
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.saveAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.saveAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.allSatisfy { !$0.contains("UPDATE") })
|
XCTAssert(sqlQueries.allSatisfy { !$0.contains("UPDATE") })
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
@@ -1483,7 +1483,7 @@ extension MutablePersistableRecordTests {
|
|||||||
var partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
var partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
||||||
try partialPlayer.delete(db)
|
try partialPlayer.delete(db)
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.saveAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.saveAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
UPDATE "player" SET "name"='Arthur' WHERE "id"=1 RETURNING *
|
UPDATE "player" SET "name"='Arthur' WHERE "id"=1 RETURNING *
|
||||||
@@ -1521,7 +1521,7 @@ extension MutablePersistableRecordTests {
|
|||||||
do {
|
do {
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
var partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
var partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.saveAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.saveAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.allSatisfy { !$0.contains("INSERT") })
|
XCTAssert(sqlQueries.allSatisfy { !$0.contains("INSERT") })
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
@@ -1709,7 +1709,7 @@ extension MutablePersistableRecordTests {
|
|||||||
player.name = "Barbara"
|
player.name = "Barbara"
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let updatedPlayer = try XCTUnwrap(player.updateAndFetch(db))
|
let updatedPlayer = try player.updateAndFetch(db)
|
||||||
XCTAssertEqual(updatedPlayer.id, 1)
|
XCTAssertEqual(updatedPlayer.id, 1)
|
||||||
XCTAssertEqual(updatedPlayer.name, "Barbara")
|
XCTAssertEqual(updatedPlayer.name, "Barbara")
|
||||||
XCTAssertEqual(updatedPlayer.score, 1000)
|
XCTAssertEqual(updatedPlayer.score, 1000)
|
||||||
@@ -1760,7 +1760,7 @@ extension MutablePersistableRecordTests {
|
|||||||
player.name = "Barbara"
|
player.name = "Barbara"
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let updatedPlayer = try XCTUnwrap(player.updateAndFetch(db, as: PartialPlayer.self))
|
let updatedPlayer = try player.updateAndFetch(db, as: PartialPlayer.self)
|
||||||
XCTAssertEqual(updatedPlayer.id, 1)
|
XCTAssertEqual(updatedPlayer.id, 1)
|
||||||
XCTAssertEqual(updatedPlayer.name, "Barbara")
|
XCTAssertEqual(updatedPlayer.name, "Barbara")
|
||||||
}
|
}
|
||||||
@@ -2041,11 +2041,15 @@ extension MutablePersistableRecordTests {
|
|||||||
try player.insert(db)
|
try player.insert(db)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let updatedRow = try player.updateChangesAndFetch(
|
// Update with no change
|
||||||
|
let update = try player.updateChangesAndFetch(
|
||||||
db, selection: [AllColumns()],
|
db, selection: [AllColumns()],
|
||||||
fetch: { statement in try Row.fetchOne(statement) },
|
fetch: { statement in
|
||||||
|
XCTFail("Should not be called")
|
||||||
|
return "ignored"
|
||||||
|
},
|
||||||
modify: { $0.name = "Barbara" })
|
modify: { $0.name = "Barbara" })
|
||||||
XCTAssertNil(updatedRow)
|
XCTAssertNil(update)
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|||||||
@@ -1342,7 +1342,7 @@ extension PersistableRecordTests {
|
|||||||
do {
|
do {
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
let partialPlayer = PartialPlayer(name: "Arthur")
|
let partialPlayer = PartialPlayer(name: "Arthur")
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.insertAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.insertAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
INSERT INTO "player" ("id", "name") VALUES (NULL,'Arthur') RETURNING *
|
INSERT INTO "player" ("id", "name") VALUES (NULL,'Arthur') RETURNING *
|
||||||
@@ -1444,7 +1444,7 @@ extension PersistableRecordTests {
|
|||||||
do {
|
do {
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
let partialPlayer = PartialPlayer(name: "Arthur")
|
let partialPlayer = PartialPlayer(name: "Arthur")
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.saveAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.saveAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.allSatisfy { !$0.contains("UPDATE") })
|
XCTAssert(sqlQueries.allSatisfy { !$0.contains("UPDATE") })
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
@@ -1480,7 +1480,7 @@ extension PersistableRecordTests {
|
|||||||
let partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
let partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
||||||
try partialPlayer.delete(db)
|
try partialPlayer.delete(db)
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.saveAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.saveAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
UPDATE "player" SET "name"='Arthur' WHERE "id"=1 RETURNING *
|
UPDATE "player" SET "name"='Arthur' WHERE "id"=1 RETURNING *
|
||||||
@@ -1517,7 +1517,7 @@ extension PersistableRecordTests {
|
|||||||
do {
|
do {
|
||||||
sqlQueries.removeAll()
|
sqlQueries.removeAll()
|
||||||
let partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
let partialPlayer = PartialPlayer(id: 1, name: "Arthur")
|
||||||
let fullPlayer = try XCTUnwrap(partialPlayer.saveAndFetch(db, as: FullPlayer.self))
|
let fullPlayer = try partialPlayer.saveAndFetch(db, as: FullPlayer.self)
|
||||||
|
|
||||||
XCTAssert(sqlQueries.allSatisfy { !$0.contains("INSERT") })
|
XCTAssert(sqlQueries.allSatisfy { !$0.contains("INSERT") })
|
||||||
XCTAssert(sqlQueries.contains("""
|
XCTAssert(sqlQueries.contains("""
|
||||||
|
|||||||
Reference in new issue
Block a user