diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 928c2feda..c913f9cd9 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -1167,7 +1167,7 @@ public struct DatabaseEvent { /// dbQueue.inDatabase { db in /// let persons = Person.fetchAll(db) /// } -public protocol DatabaseReader : class { +public protocol DatabaseReader { /// This method is an implementation detail: do not use it directly. /// /// A single statement executed in the block argument can be fully executed @@ -1186,3 +1186,18 @@ extension Database : DatabaseReader { return try block(db: self) } } + + +// ========================================================================= +// MARK: - DatabaseWriter + +public protocol DatabaseWriter { + func _write(block: (db: Database) throws -> T) rethrows -> T +} + +extension Database : DatabaseWriter { + /// This method is an implementation detail: do not use it directly. + public func _write(block: (db: Database) throws -> T) rethrows -> T { + return try block(db: self) + } +} diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index a8fef9cf6..c3e19b05d 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -100,7 +100,7 @@ public protocol MutablePersistable : TableMapping { /// that they invoke the performInsert() method. /// /// - throws: A DatabaseError whenever an SQLite error occurs. - mutating func insert(db: Database) throws + mutating func insert(writer: DatabaseWriter) throws /// Executes an UPDATE statement. /// @@ -172,8 +172,8 @@ public extension MutablePersistable { /// Executes an INSERT statement. /// /// The default implementation for insert() invokes performInsert(). - mutating func insert(db: Database) throws { - try performInsert(db) + mutating func insert(writer: DatabaseWriter) throws { + try performInsert(writer) } /// Executes an UPDATE statement. @@ -228,11 +228,13 @@ public extension MutablePersistable { /// that adopt MutablePersistable can invoke performInsert() in their /// implementation of insert(). They should not provide their own /// implementation of performInsert(). - mutating func performInsert(db: Database) throws { - let dataMapper = DataMapper(db, self) - let changes = try dataMapper.insertStatement().execute() - if let rowID = changes.insertedRowID { - didInsertWithRowID(rowID, forColumn: dataMapper.primaryKey.rowIDColumn) + mutating func performInsert(writer: DatabaseWriter) throws { + try writer._write { db in + let dataMapper = DataMapper(db, self) + let changes = try dataMapper.insertStatement().execute() + if let rowID = changes.insertedRowID { + self.didInsertWithRowID(rowID, forColumn: dataMapper.primaryKey.rowIDColumn) + } } } @@ -392,7 +394,7 @@ public protocol Persistable : MutablePersistable { /// that they invoke the performInsert() method. /// /// - throws: A DatabaseError whenever an SQLite error occurs. - func insert(db: Database) throws + func insert(writer: DatabaseWriter) throws /// Executes an INSERT or an UPDATE statement so that `self` is saved in /// the database. @@ -426,8 +428,8 @@ public extension Persistable { /// Executes an INSERT statement. /// /// The default implementation for insert() invokes performInsert(). - func insert(db: Database) throws { - try performInsert(db) + func insert(writer: DatabaseWriter) throws { + try performInsert(writer) } /// Executes an INSERT or an UPDATE statement so that `self` is saved in @@ -448,11 +450,13 @@ public extension Persistable { /// that adopt Persistable can invoke performInsert() in their /// implementation of insert(). They should not provide their own /// implementation of performInsert(). - func performInsert(db: Database) throws { - let dataMapper = DataMapper(db, self) - let changes = try dataMapper.insertStatement().execute() - if let rowID = changes.insertedRowID { - didInsertWithRowID(rowID, forColumn: dataMapper.primaryKey.rowIDColumn) + func performInsert(writer: DatabaseWriter) throws { + try writer._write { db in + let dataMapper = DataMapper(db, self) + let changes = try dataMapper.insertStatement().execute() + if let rowID = changes.insertedRowID { + self.didInsertWithRowID(rowID, forColumn: dataMapper.primaryKey.rowIDColumn) + } } } diff --git a/GRDB/Record/Record.swift b/GRDB/Record/Record.swift index fd58ba397..2c143417d 100644 --- a/GRDB/Record/Record.swift +++ b/GRDB/Record/Record.swift @@ -182,10 +182,10 @@ public class Record : RowConvertible, TableMapping, Persistable { /// before the insertion. /// /// - throws: A DatabaseError whenever an SQLite error occurs. - public func insert(db: Database) throws { + public func insert(writer: DatabaseWriter) throws { // The simplest code would be: // - // try performInsert(db) + // try performInsert(writer) // hasPersistentChangedValues = false // // But this triggers two calls to persistentDictionary, and this is both @@ -195,30 +195,32 @@ public class Record : RowConvertible, TableMapping, Persistable { // So let's provide our custom implementation of insert, which uses the // same persistentDictionary for both insertion, and change tracking. - let dataMapper = DataMapper(db, self) - var persistentDictionary = dataMapper.persistentDictionary - let changes = try dataMapper.insertStatement().execute() - if let rowID = changes.insertedRowID { - let rowIDColumn = dataMapper.primaryKey.rowIDColumn - didInsertWithRowID(rowID, forColumn: rowIDColumn) - - // Update persistentDictionary with inserted id, so that we can - // set hasPersistentChangedValues to false: - if let rowIDColumn = rowIDColumn { - if persistentDictionary[rowIDColumn] != nil { - persistentDictionary[rowIDColumn] = rowID - } else { - let rowIDColumn = rowIDColumn.lowercaseString - for column in persistentDictionary.keys where column.lowercaseString == rowIDColumn { - persistentDictionary[column] = rowID - break + try writer._write { db in + let dataMapper = DataMapper(db, self) + var persistentDictionary = dataMapper.persistentDictionary + let changes = try dataMapper.insertStatement().execute() + if let rowID = changes.insertedRowID { + let rowIDColumn = dataMapper.primaryKey.rowIDColumn + self.didInsertWithRowID(rowID, forColumn: rowIDColumn) + + // Update persistentDictionary with inserted id, so that we can + // set hasPersistentChangedValues to false: + if let rowIDColumn = rowIDColumn { + if persistentDictionary[rowIDColumn] != nil { + persistentDictionary[rowIDColumn] = rowID + } else { + let rowIDColumn = rowIDColumn.lowercaseString + for column in persistentDictionary.keys where column.lowercaseString == rowIDColumn { + persistentDictionary[column] = rowID + break + } } } } + + // Set hasPersistentChangedValues to false + self.referenceRow = Row(persistentDictionary) } - - // Set hasPersistentChangedValues to false - referenceRow = Row(persistentDictionary) } /// Executes an UPDATE statement. diff --git a/Tests/Public/Core/Persistable/MutablePersistableTests.swift b/Tests/Public/Core/Persistable/MutablePersistableTests.swift index 9a3c829ef..cb28b5370 100644 --- a/Tests/Public/Core/Persistable/MutablePersistableTests.swift +++ b/Tests/Public/Core/Persistable/MutablePersistableTests.swift @@ -58,9 +58,9 @@ struct MutablePersistableCustomizedCountry : MutablePersistable { self.rowID = rowID } - mutating func insert(db: Database) throws { + mutating func insert(writer: DatabaseWriter) throws { willInsert() - try performInsert(db) + try performInsert(writer) } func update(db: Database) throws { diff --git a/Tests/Public/Core/Persistable/PersistableTests.swift b/Tests/Public/Core/Persistable/PersistableTests.swift index 340988e27..511f7e5a3 100644 --- a/Tests/Public/Core/Persistable/PersistableTests.swift +++ b/Tests/Public/Core/Persistable/PersistableTests.swift @@ -68,9 +68,9 @@ struct PersistableCustomizedCountry : Persistable { return ["isoCode": isoCode, "name": name] } - func insert(db: Database) throws { + func insert(writer: DatabaseWriter) throws { willInsert() - try performInsert(db) + try performInsert(writer) } func update(db: Database) throws { diff --git a/Tests/Public/Record/PrimaryKeyRowIDTests.swift b/Tests/Public/Record/PrimaryKeyRowIDTests.swift index bc127974f..22375b572 100644 --- a/Tests/Public/Record/PrimaryKeyRowIDTests.swift +++ b/Tests/Public/Record/PrimaryKeyRowIDTests.swift @@ -49,13 +49,13 @@ class Person : Record { ] } - override func insert(db: Database) throws { + override func insert(writer: DatabaseWriter) throws { // This is implicitely tested with the NOT NULL constraint on creationDate if creationDate == nil { creationDate = NSDate() } - try super.insert(db) + try super.insert(writer) } override func didInsertWithRowID(rowID: Int64, forColumn column: String?) { diff --git a/Tests/Public/Record/RecordEditedTests.swift b/Tests/Public/Record/RecordEditedTests.swift index dc12d92dd..4a23bc9c7 100644 --- a/Tests/Public/Record/RecordEditedTests.swift +++ b/Tests/Public/Record/RecordEditedTests.swift @@ -58,13 +58,13 @@ class PersonWithModifiedCaseColumns: Record { ] } - override func insert(db: Database) throws { + override func insert(writer: DatabaseWriter) throws { // This is implicitely tested with the NOT NULL constraint on creationDate if creationDate == nil { creationDate = NSDate() } - try super.insert(db) + try super.insert(writer) } override func didInsertWithRowID(rowID: Int64, forColumn column: String?) { diff --git a/Tests/Public/Record/RecordSubClassTests.swift b/Tests/Public/Record/RecordSubClassTests.swift index 0be7a332b..552eec0d2 100644 --- a/Tests/Public/Record/RecordSubClassTests.swift +++ b/Tests/Public/Record/RecordSubClassTests.swift @@ -32,9 +32,9 @@ class PersonWithOverrides : Person { super.init(row) } - override func insert(db: Database) throws { + override func insert(writer: DatabaseWriter) throws { lastSavingMethod = .Insert - try super.insert(db) + try super.insert(writer) } override func update(db: Database) throws {