Record

open class Record : RowConvertible, TableMapping, Persistable

Record is a class that wraps a table row, or the result of any query. It is designed to be subclassed.

  • Creates a Record.

    Declaration

    Swift

    public init()
  • Creates a Record from a row.

    The input row may not come straight from the database. When you want to complete your initialization after being fetched, override awakeFromFetch(row:).

    Declaration

    Swift

    required public init(row: Row)
  • Do not call this method directly.

    This method is called in an arbitrary dispatch queue, after a record has been fetched from the database.

    Record subclasses have an opportunity to complete their initialization.

    Important: subclasses must invoke super’s implementation.

    Declaration

    Swift

    open func awakeFromFetch(row: Row)
  • The name of a database table.

    This table name is required by the insert, update, save, delete, and exists methods.

    class Person : Record {
        override class var databaseTableName: String {
            return "persons"
        }
    }
    

    The implementation of the base class Record raises a fatal error.

    Declaration

    Swift

    open class var databaseTableName: String

    Return Value

    The name of a database table.

  • The 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.

    See https://www.sqlite.org/lang_conflict.html

    Declaration

    Swift

    open class var persistenceConflictPolicy: PersistenceConflictPolicy
  • This flag tells whether the hidden rowid column should be fetched with other columns.

    Its default value is false:

    // SELECT * FROM persons
    try Person.fetchAll(db)
    

    When true, the rowid column is fetched:

    // SELECT *, rowid FROM persons
    try Person.fetchAll(db)
    

    Declaration

    Swift

    open class var selectsRowID: Bool
  • 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 Record.databaseTableName()).

    In particular, primary key columns, if any, must be included.

    class Person : Record {
        var id: Int64?
        var name: String?
    
        override var persistentDictionary: [String: DatabaseValueConvertible?] {
            return ["id": id, "name": name]
        }
    }
    

    The implementation of the base class Record returns an empty dictionary.

    Declaration

    Swift

    open var persistentDictionary: [String: DatabaseValueConvertible?]
  • Notifies 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.

    The implementation of the base Record class does nothing.

    class Person : Record {
        var id: Int64?
        var name: String?
    
        func didInsert(with rowID: Int64, for column: String?) {
            id = rowID
        }
    }
    

    Declaration

    Swift

    open func didInsert(with rowID: Int64, for column: String?)

    Parameters

    rowID

    The inserted rowID.

    column

    The name of the eventual INTEGER PRIMARY KEY column.

  • Returns a copy of self, initialized from the values of persistentDictionary.

    Note that the eventual primary key is copied, as well as the hasPersistentChangedValues flag.

    Declaration

    Swift

    open func copy() -> Self

    Return Value

    A copy of self.

  • A boolean that indicates whether the record has changes that have not been saved.

    This flag is purely informative, and does not prevent insert(), update(), and save() from performing their database queries.

    A record is edited if its persistentDictionary has been changed since last database synchronization (fetch, update, insert). Comparison is performed on values: setting a property to the same value does not trigger the edited flag.

    You can rely on the Record base class to compute this flag for you, or you may set it to true or false when you know better. Setting it to false does not prevent it from turning true on subsequent modifications of the record.

    Declaration

    Swift

    public var hasPersistentChangedValues: Bool
  • A dictionary of changes that have not been saved.

    Its keys are column names, and values the old values that have been changed since last fetching or saving of the record.

    Unless the record has actually been fetched or saved, the old values are nil.

    See hasPersistentChangedValues for more information.

    Declaration

    Swift

    public var persistentChangedValues: [String: DatabaseValue?]
  • Executes an INSERT statement.

    On success, this method sets the hasPersistentChangedValues flag to false.

    This method is guaranteed to have inserted a row in the database if it returns without error.

    Records whose primary key is declared as INTEGER PRIMARY KEY have their id automatically set after successful insertion, if it was nil before the insertion.

    Throws

    A DatabaseError whenever an SQLite error occurs.

    Declaration

    Swift

    open func insert(_ db: Database) throws

    Parameters

    db

    A database connection.

  • Executes an UPDATE statement.

    On success, this method sets the hasPersistentChangedValues flag to false.

    This method is guaranteed to have updated a row in the database if it returns without error.

    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 and record could not be updated.

    Declaration

    Swift

    open func update(_ db: Database, columns: Set<String>) throws

    Parameters

    db

    A database connection.

    columns

    The columns to update.

  • Executes an INSERT or an UPDATE statement so that self is saved in the database.

    If the record has a non-nil primary key and a matching row in the database, this method performs an update.

    Otherwise, performs an insert.

    On success, this method sets the hasPersistentChangedValues flag to false.

    This method is guaranteed to have inserted or updated a row in the database if it returns without error.

    Throws

    A DatabaseError whenever an SQLite error occurs, or errors thrown by update().

    Declaration

    Swift

    final public func save(_ db: Database) throws

    Parameters

    db

    A database connection.

  • Executes a DELETE statement.

    On success, this method sets the hasPersistentChangedValues flag to true.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    open func delete(_ db: Database) throws -> Bool

    Parameters

    db

    A database connection.

    Return Value

    Whether a database row was deleted.