PersistableRecord

Types that adopt PersistableRecord can be inserted, updated, and deleted.

PersistableRecord has non-mutating variants of MutablePersistableRecord methods.

Insertion Callbacks

  • willInsert(_:) Default implementation

    Persistence callback called before the record is inserted.

    Default implementation does nothing.

    Note

    If you need a mutating variant of this method, adopt the MutablePersistableRecord protocol instead.

    Default Implementation

  • didInsert(_:) Default implementation

    Persistence callback called upon successful insertion.

    The default implementation does nothing.

    You can provide a custom implementation in order to grab the auto-incremented id:

    class Player: PersistableRecord {
        var id: Int64?
        var name: String?
    
        func didInsert(_ inserted: InsertionSuccess) {
            id = inserted.rowID
        }
    }
    

    Note

    If you need a mutating variant of this method, adopt the MutablePersistableRecord protocol instead.

    Default Implementation

Insert

  • insert(_:onConflict:) Extension method

    Executes an INSERT statement.

    Throws

    A DatabaseError whenever an SQLite error occurs.

Insert and Fetch

  • Executes an INSERT ... RETURNING ... statement, and returns a new record built from the inserted row.

    This method helps dealing with default column values and generated columns.

    For example:

    // A table with an auto-incremented primary key and a default value
    try dbQueue.write { db in
        try db.execute(sql: """
            CREATE TABLE player(
              id INTEGER PRIMARY KEY AUTOINCREMENT,
              name TEXT,
              score INTEGER DEFAULT 1000)
            """)
    }
    
    // A player with partial database information
    struct PartialPlayer: PersistableRecord {
        static let databaseTableName = "player"
        var name: String
    }
    
    // A full player, with all database information
    struct Player: TableRecord, FetchableRecord {
        var id: Int64
        var name: String
        var score: Int
    }
    
    // Insert a base player, get a full one
    try dbQueue.write { db in
        let partialPlayer = PartialPlayer(name: "Alice")
    
        // INSERT INTO player (name) VALUES ('Alice') RETURNING *
        if let player = try partialPlayer.insertAndFetch(db, as: FullPlayer.self) {
            print(player.id)    // The inserted id
            print(player.name)  // The inserted name
            print(player.score) // The default score
        }
    }
    

    Throws

    A DatabaseError whenever an SQLite error occurs.
  • Executes an INSERT ... RETURNING ... statement, and returns the selected columns from the inserted row.

    This method helps dealing with default column values and generated columns.

    For example:

    // A table with an auto-incremented primary key and a default value
    try dbQueue.write { db in
        try db.execute(sql: """
            CREATE TABLE player(
              id INTEGER PRIMARY KEY AUTOINCREMENT,
              name TEXT,
              score INTEGER DEFAULT 1000)
            """)
    }
    
    // A player with partial database information
    struct PartialPlayer: PersistableRecord {
        static let databaseTableName = "player"
        var name: String
    }
    
    // Insert a base player, get the inserted score
    try dbQueue.write { db in
        let partialPlayer = PartialPlayer(name: "Alice")
    
        // INSERT INTO player (name) VALUES ('Alice') RETURNING score
        let score = try partialPlayer.insertAndFetch(db, selection: [Column("score")]) { statement in
            try Int.fetchOne(statement)
        }
        print(score) // The inserted score
    }
    

    Throws

    A DatabaseError whenever an SQLite error occurs.

    Precondition

    selection is not empty.

Save

  • save(_:onConflict:) Extension method

    Executes an INSERT or UPDATE statement.

    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.

    Throws

    A DatabaseError whenever an SQLite error occurs.

Save and Fetch

  • Executes an INSERT ... RETURNING ... or UPDATE ... RETURNING ... statement, and returns a new record built from the saved row.

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

    Throws

    A DatabaseError whenever an SQLite error occurs.
  • Executes an INSERT ... RETURNING ... or UPDATE ... RETURNING ... statement, and returns the selected columns from the saved row.

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

    Throws

    A DatabaseError whenever an SQLite error occurs.

    Precondition

    selection is not empty.

Upsert

  • upsert(_:) Extension method

    Executes an INSERT ... ON CONFLICT DO UPDATE statement.

    Throws

    A DatabaseError whenever an SQLite error occurs.
  • upsertAndFetch(_:) Extension method

    Executes an INSERT ... ON CONFLICT DO UPDATE ... RETURNING ... statement, and returns the inserted record.

    This method helps dealing with default column values and generated columns.

    For example:

    let player: Player = ...
    let insertedPlayer = player.upsertAndFetch(db)
    

    Throws

    A DatabaseError whenever an SQLite error occurs.
  • upsertAndFetch(_:as:) Extension method

    Executes an INSERT ... ON CONFLICT DO UPDATE ... RETURNING ... statement, and returns the inserted record.

    This method helps dealing with default column values and generated columns.

    For example:

    let player: Player = ...
    let insertedPlayer = player.upsertAndFetch(db)
    

    Throws

    A DatabaseError whenever an SQLite error occurs.