MutablePersistable
public protocol MutablePersistable : TableMapping
Types that adopt MutablePersistable can be inserted, updated, and deleted.
This protocol is intented for types that have an INTEGER PRIMARY KEY, and are interested in the inserted RowID: they can mutate themselves upon successful insertion with the didInsert(with:for:) method.
The insert() and save() methods are mutating methods.
-
persistenceConflictPolicy
Default implementationThe policy that handles SQLite conflicts when records are inserted or updated.
This property is optional: its default value uses the ABORT policy for both insertions and updates, and has GRDB generate regular INSERT and UPDATE queries.
If insertions are resolved with .ignore policy, the
didInsert(with:for:)
method is not called upon successful insertion, even if a row was actually inserted without any conflict.Default Implementation
Describes the conflict policy for insertions and updates.
The default value specifies ABORT policy for both insertions and updates, which has GRDB generate regular INSERT and UPDATE queries.
Declaration
Swift
static var persistenceConflictPolicy: PersistenceConflictPolicy
-
Returns the values that should be stored in the database.
Keys of the returned dictionary must match the column names of the target database table (see TableMapping.databaseTableName()).
In particular, primary key columns, if any, must be included.
struct Person : MutablePersistable { var id: Int64? var name: String? var persistentDictionary: [String: DatabaseValueConvertible?] { return ["id": id, "name": name] } }
Declaration
Swift
var persistentDictionary: [String: DatabaseValueConvertible?]
-
didInsert(with:for:)
Default implementationNotifies the record that it was succesfully inserted.
Do not call this method directly: it is called for you, in a protected dispatch queue, with the inserted RowID and the eventual INTEGER PRIMARY KEY column name.
This method is optional: the default implementation does nothing.
struct Person : MutablePersistable { var id: Int64? var name: String? mutating func didInsert(with rowID: Int64, for column: String?) { self.id = rowID } }
Default Implementation
Notifies the record that it was succesfully inserted.
The default implementation does nothing.
Declaration
Swift
mutating func didInsert(with rowID: Int64, for column: String?)
Parameters
rowID
The inserted rowID.
column
The name of the eventual INTEGER PRIMARY KEY column.
-
insert(_:)
Default implementationExecutes an INSERT statement.
This method is guaranteed to have inserted a row in the database if it returns without error.
Upon successful insertion, the didInsert(with:for:) method is called with the inserted RowID and the eventual INTEGER PRIMARY KEY column name.
This method has a default implementation, so your adopting types don’t have to implement it. Yet your types can provide their own implementation of insert(). In their implementation, it is recommended that they invoke the performInsert() method.
Throws
A DatabaseError whenever an SQLite error occurs.Default Implementation
Executes an INSERT statement.
The default implementation for insert() invokes performInsert().
Declaration
Swift
mutating func insert(_ db: Database) throws
Parameters
db
A database connection.
-
update(_:columns:)
Default implementationExecutes an UPDATE statement.
This method is guaranteed to have updated a row in the database if it returns without error.
This method has a default implementation, so your adopting types don’t have to implement it. Yet your types can provide their own implementation of update(). In their implementation, it is recommended that they invoke the performUpdate() method.
Throws
A DatabaseError is thrown whenever an SQLite error occurs. PersistenceError.recordNotFound is thrown if the primary key does not match any row in the database.Default Implementation
Executes an UPDATE statement.
Throws
A DatabaseError is thrown whenever an SQLite error occurs. PersistenceError.recordNotFound is thrown if the primary key does not match any row in the database.Executes an UPDATE statement.
Throws
A DatabaseError is thrown whenever an SQLite error occurs. PersistenceError.recordNotFound is thrown if the primary key does not match any row in the database.Executes an UPDATE statement.
Throws
A DatabaseError is thrown whenever an SQLite error occurs. PersistenceError.recordNotFound is thrown if the primary key does not match any row in the database.Declaration
Swift
func update(_ db: Database, columns: Set<String>) throws
Parameters
db
A database connection.
columns
The columns to update.
-
save(_:)
Default implementationExecutes an INSERT or an UPDATE statement so that
self
is saved in the database.If the receiver has a non-nil primary key and a matching row in the database, this method performs an update.
Otherwise, performs an insert.
This method is guaranteed to have inserted or updated a row in the database if it returns without error.
This method has a default implementation, so your adopting types don’t have to implement it. Yet your types can provide their own implementation of save(). In their implementation, it is recommended that they invoke the performSave() method.
Throws
A DatabaseError whenever an SQLite error occurs, or errors thrown by update().Default Implementation
Executes an INSERT or an UPDATE statement so that
self
is saved in the database.The default implementation for save() invokes performSave().
Declaration
Swift
mutating func save(_ db: Database) throws
Parameters
db
A database connection.
-
delete(_:)
Default implementationExecutes a DELETE statement.
This method has a default implementation, so your adopting types don’t have to implement it. Yet your types can provide their own implementation of delete(). In their implementation, it is recommended that they invoke the performDelete() method.
Throws
A DatabaseError is thrown whenever an SQLite error occurs.Default Implementation
Executes a DELETE statement.
The default implementation for delete() invokes performDelete().
Declaration
Swift
func delete(_ db: Database) throws -> Bool
Parameters
db
A database connection.
Return Value
Whether a database row was deleted.
-
exists(_:)
Default implementationReturns true if and only if the primary key matches a row in the database.
This method has a default implementation, so your adopting types don’t have to implement it. Yet your types can provide their own implementation of exists(). In their implementation, it is recommended that they invoke the performExists() method.
Throws
A DatabaseError is thrown whenever an SQLite error occurs.Default Implementation
Returns true if and only if the primary key matches a row in the database.
The default implementation for exists() invokes performExists().
Declaration
Swift
func exists(_ db: Database) throws -> Bool
Parameters
db
A database connection.
Return Value
Whether the primary key matches a row in the database.
-
update(_:)
Extension methodExecutes an UPDATE statement that updates all table columns.
Throws
A DatabaseError is thrown whenever an SQLite error occurs. PersistenceError.recordNotFound is thrown if the primary key does not match any row in the database.Declaration
Swift
public func update(_ db: Database) throws
Parameters
db
A database connection.
-
performInsert(_:)
Extension methodDon’t invoke this method directly: it is an internal method for types that adopt MutablePersistable.
performInsert() provides the default implementation for insert(). Types that adopt MutablePersistable can invoke performInsert() in their implementation of insert(). They should not provide their own implementation of performInsert().
Declaration
Swift
public mutating func performInsert(_ db: Database) throws
-
performUpdate(_:columns:)
Extension methodDon’t invoke this method directly: it is an internal method for types that adopt MutablePersistable.
performUpdate() provides the default implementation for update(). Types that adopt MutablePersistable can invoke performUpdate() in their implementation of update(). They should not provide their own implementation of performUpdate().
Throws
A DatabaseError is thrown whenever an SQLite error occurs. PersistenceError.recordNotFound is thrown if the primary key does not match any row in the database.Declaration
Swift
public func performUpdate(_ db: Database, columns: Set<String>) throws
Parameters
db
A database connection.
columns
The columns to update.
-
performSave(_:)
Extension methodDon’t invoke this method directly: it is an internal method for types that adopt MutablePersistable.
performSave() provides the default implementation for save(). Types that adopt MutablePersistable can invoke performSave() in their implementation of save(). They should not provide their own implementation of performSave().
This default implementation forwards the job to
update
orinsert
.Declaration
Swift
public mutating func performSave(_ db: Database) throws
-
performDelete(_:)
Extension methodDon’t invoke this method directly: it is an internal method for types that adopt MutablePersistable.
performDelete() provides the default implementation for deelte(). Types that adopt MutablePersistable can invoke performDelete() in their implementation of delete(). They should not provide their own implementation of performDelete().
Declaration
Swift
public func performDelete(_ db: Database) throws -> Bool
-
performExists(_:)
Extension methodDon’t invoke this method directly: it is an internal method for types that adopt MutablePersistable.
performExists() provides the default implementation for exists(). Types that adopt MutablePersistable can invoke performExists() in their implementation of exists(). They should not provide their own implementation of performExists().
Declaration
Swift
public func performExists(_ db: Database) throws -> Bool