// MARK: - Insert Callbacks extension MutablePersistableRecord { @inline(__always) @inlinable public mutating func willInsert(_ db: Database) throws { } @inline(__always) @inlinable public func aroundInsert(_ db: Database, insert: () throws -> InsertionSuccess) throws { _ = try insert() } @inline(__always) @inlinable public mutating func didInsert(_ inserted: InsertionSuccess) { } } // MARK: - Insert extension MutablePersistableRecord { /// Executes an `INSERT` statement. /// /// For example: /// /// ```swift /// try dbQueue.write { db in /// var player = Player(name: "Arthur") /// try player.insert(db) /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// error thrown by the persistence callbacks defined by the record type. @inlinable // allow specialization so that empty callbacks are removed public mutating func insert( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil) throws { try willSave(db) var saved: PersistenceSuccess? try aroundSave(db) { let inserted = try insertWithCallbacks(db, onConflict: conflictResolution) saved = PersistenceSuccess(inserted) return saved! } guard let saved else { try persistenceCallbackMisuse("aroundSave") } didSave(saved) } /// Executes an `INSERT` statement, and returns the inserted record. /// /// For example: /// /// ```swift /// let player = Player(name: "Arthur") /// let insertedPlayer = try await dbQueue.write { [player] db in /// try player.inserted(db) /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - returns: The inserted record. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// error thrown by the persistence callbacks defined by the record type. @inlinable // allow specialization so that empty callbacks are removed public func inserted( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil) throws -> Self { var result = self try result.insert(db, onConflict: conflictResolution) return result } } // MARK: - Insert and Fetch extension MutablePersistableRecord { #if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC /// Executes an `INSERT RETURNING` statement, and returns a new record built /// from the inserted row. /// /// This method is equivalent to ``insertAndFetch(_:onConflict:as:)``, /// with `Self` as the `returnedType` argument: /// /// ```swift /// // Equivalent /// let insertedPlayer = try player.insertAndFetch(db) /// let insertedPlayer = try player.insertAndFetch(db, as: Player.self) /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - returns: The inserted record. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// 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 public func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil) throws -> Self where Self: FetchableRecord { var result = self return try result.insertAndFetch(db, onConflict: conflictResolution, as: Self.self) } /// 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: /// /// ```swift /// // 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: MutablePersistableRecord { /// 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 partial player, get a full one /// try dbQueue.write { db in /// var partialPlayer = PartialPlayer(name: "Alice") /// /// // INSERT INTO player (name) VALUES ('Alice') RETURNING * /// 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 /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - parameter returnedType: The type of the returned record. /// - returns: A record of type `returnedType`. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// 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 public mutating func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil, as returnedType: T.Type) throws -> T { let record = self return try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) { if let result = try T.fetchOne($0) { return result } throw record.recordNotFound(db) } } /// 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: /// /// ```swift /// // 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: MutablePersistableRecord { /// static let databaseTableName = "player" /// var name: String /// } /// /// // Insert a partial player, get the inserted score /// try dbQueue.write { db in /// var 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 /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - parameter selection: The returned columns (must not be empty). /// - parameter fetch: A closure that executes its ``Statement`` argument. /// If the conflict policy is `IGNORE`, the statement may return no row. /// - returns: The result of the `fetch` function. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// error thrown by the persistence callbacks defined by the record type. /// - precondition: `selection` is not empty. @inlinable // allow specialization so that empty callbacks are removed public mutating func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil, selection: [any SQLSelectable], fetch: (Statement) throws -> T) throws -> T { GRDBPrecondition(!selection.isEmpty, "Invalid empty selection") try willSave(db) var success: (inserted: InsertionSuccess, returned: T)? try aroundSave(db) { success = try insertAndFetchWithCallbacks( db, onConflict: conflictResolution, selection: selection, fetch: fetch) return PersistenceSuccess(success!.inserted) } guard let success else { try persistenceCallbackMisuse("aroundSave") } didSave(PersistenceSuccess(success.inserted)) return success.returned } /// 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: /// /// ```swift /// // 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: MutablePersistableRecord { /// static let databaseTableName = "player" /// var name: String /// /// enum Columns { /// static let score = Column("score") /// } /// } /// /// // Insert a partial player, get the inserted score /// try dbQueue.write { db in /// var partialPlayer = PartialPlayer(name: "Alice") /// /// // INSERT INTO player (name) VALUES ('Alice') RETURNING score /// let score = try partialPlayer.insertAndFetch(db) { statement in /// try Int.fetchOne(statement) /// } select: { /// [$0.score] /// } /// print(score) // The inserted score /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - parameter fetch: A closure that executes its ``Statement`` argument. /// If the conflict policy is `IGNORE`, the statement may return no row. /// - parameter select: A closure that returns the returned columns /// (must not be empty). /// - returns: The result of the `fetch` function. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// error thrown by the persistence callbacks defined by the record type. /// - precondition: The result of `select` is not empty. @inlinable // allow specialization so that empty callbacks are removed public mutating func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil, fetch: (Statement) throws -> T, select: (DatabaseComponents) throws -> [any SQLSelectable] ) throws -> T where Self: TableRecord { try insertAndFetch(db, onConflict: conflictResolution, selection: select(Self.databaseComponents), fetch: fetch) } #else /// Executes an `INSERT RETURNING` statement, and returns a new record built /// from the inserted row. /// /// This method is equivalent to ``insertAndFetch(_:onConflict:as:)``, /// with `Self` as the `returnedType` argument: /// /// ```swift /// // Equivalent /// let insertedPlayer = try player.insertAndFetch(db) /// let insertedPlayer = try player.insertAndFetch(db, as: Player.self) /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - returns: The inserted record. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// 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 @available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+ public func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil) throws -> Self where Self: FetchableRecord { var result = self return try result.insertAndFetch(db, onConflict: conflictResolution, as: Self.self) } /// 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: /// /// ```swift /// // 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: MutablePersistableRecord { /// 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 partial player, get a full one /// try dbQueue.write { db in /// var partialPlayer = PartialPlayer(name: "Alice") /// /// // INSERT INTO player (name) VALUES ('Alice') RETURNING * /// 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 /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - parameter returnedType: The type of the returned record. /// - returns: A record of type `returnedType`. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// 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 @available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+ public mutating func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil, as returnedType: T.Type) throws -> T { let record = self return try insertAndFetch(db, onConflict: conflictResolution, selection: T.databaseSelection) { if let result = try T.fetchOne($0) { return result } throw record.recordNotFound(db) } } /// 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: /// /// ```swift /// // 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: MutablePersistableRecord { /// static let databaseTableName = "player" /// var name: String /// } /// /// // Insert a partial player, get the inserted score /// try dbQueue.write { db in /// var 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 /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - parameter selection: The returned columns (must not be empty). /// - parameter fetch: A closure that executes its ``Statement`` argument. /// If the conflict policy is `IGNORE`, the statement may return no row. /// - returns: The result of the `fetch` function. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// error thrown by the persistence callbacks defined by the record type. /// - precondition: `selection` is not empty. @inlinable // allow specialization so that empty callbacks are removed @available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+ public mutating func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil, selection: [any SQLSelectable], fetch: (Statement) throws -> T) throws -> T { GRDBPrecondition(!selection.isEmpty, "Invalid empty selection") try willSave(db) var success: (inserted: InsertionSuccess, returned: T)? try aroundSave(db) { success = try insertAndFetchWithCallbacks( db, onConflict: conflictResolution, selection: selection, fetch: fetch) return PersistenceSuccess(success!.inserted) } guard let success else { try persistenceCallbackMisuse("aroundSave") } didSave(PersistenceSuccess(success.inserted)) return success.returned } /// 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: /// /// ```swift /// // 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: MutablePersistableRecord { /// static let databaseTableName = "player" /// var name: String /// /// enum Columns { /// static let score = Column("score") /// } /// } /// /// // Insert a partial player, get the inserted score /// try dbQueue.write { db in /// var partialPlayer = PartialPlayer(name: "Alice") /// /// // INSERT INTO player (name) VALUES ('Alice') RETURNING score /// let score = try partialPlayer.insertAndFetch(db) { statement in /// try Int.fetchOne(statement) /// } select: { /// [$0.score] /// } /// print(score) // The inserted score /// } /// ``` /// /// - parameter db: A database connection. /// - parameter conflictResolution: A policy for conflict resolution. If /// nil, /// is used. /// - parameter fetch: A closure that executes its ``Statement`` argument. /// If the conflict policy is `IGNORE`, the statement may return no row. /// - parameter select: A closure that returns the returned columns /// (must not be empty). /// - returns: The result of the `fetch` function. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or any /// error thrown by the persistence callbacks defined by the record type. /// - precondition: The result of `select` is not empty. @inlinable // allow specialization so that empty callbacks are removed @available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) // SQLite 3.35.0+ public mutating func insertAndFetch( _ db: Database, onConflict conflictResolution: Database.ConflictResolution? = nil, fetch: (Statement) throws -> T, select: (DatabaseComponents) throws -> [any SQLSelectable] ) throws -> T where Self: TableRecord { try insertAndFetch( db, onConflict: conflictResolution, selection: select(Self.databaseComponents), fetch: fetch) } #endif } // MARK: - Internals extension MutablePersistableRecord { /// Executes an `INSERT` statement, and runs insertion callbacks. @inlinable // allow specialization so that empty callbacks are removed mutating func insertWithCallbacks( _ db: Database, onConflict conflictResolution: Database.ConflictResolution?) throws -> InsertionSuccess { let (inserted, _) = try insertAndFetchWithCallbacks(db, onConflict: conflictResolution, selection: []) { // Nothing to fetch try $0.execute() } return inserted } /// Executes an `INSERT` statement, with `RETURNING` clause if `selection` /// is not empty, and runs insertion callbacks. @inlinable // allow specialization so that empty callbacks are removed mutating func insertAndFetchWithCallbacks( _ db: Database, onConflict conflictResolution: Database.ConflictResolution?, selection: [any SQLSelectable], fetch: (Statement) throws -> T) throws -> (InsertionSuccess, T) { try willInsert(db) var success: (inserted: InsertionSuccess, returned: T)? try aroundInsert(db) { success = try insertAndFetchWithoutCallbacks( db, onConflict: conflictResolution, selection: selection, fetch: fetch) return success!.inserted } guard let success else { try persistenceCallbackMisuse("aroundInsert") } didInsert(success.inserted) return success } /// Executes an `INSERT` statement, with `RETURNING` clause if `selection` /// is not empty, and DOES NOT run insertion callbacks. @usableFromInline func insertAndFetchWithoutCallbacks( _ db: Database, onConflict conflictResolution: Database.ConflictResolution?, selection: [any SQLSelectable], fetch: (Statement) throws -> T) throws -> (InsertionSuccess, T) { let conflictResolution = conflictResolution ?? type(of: self) .persistenceConflictPolicy .conflictResolutionForInsert let dao = try DAO(db, self) let statement = try dao.insertStatement( db, onConflict: conflictResolution, returning: selection) let returned = try fetch(statement) let rowIDColumn = dao.primaryKey.rowIDColumn let rowid = db.lastInsertedRowID // Update the persistenceContainer with the inserted rowid. // This allows the Record class to set its `hasDatabaseChanges` property // to false in its `aroundInsert` callback. var persistenceContainer = dao.persistenceContainer if let rowIDColumn { persistenceContainer[rowIDColumn] = rowid } let inserted = InsertionSuccess( rowID: rowid, rowIDColumn: rowIDColumn, persistenceContainer: persistenceContainer) return (inserted, returned) } }