mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Introduce DatabaseWriter
This commit is contained in:
1 parent
1ab5b8ed9c
commit
47d1202e57
8 files changed
+70
-49
No files matched your search
@@ -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<T>(block: (db: Database) throws -> T) rethrows -> T
|
||||
}
|
||||
|
||||
extension Database : DatabaseWriter {
|
||||
/// This method is an implementation detail: do not use it directly.
|
||||
public func _write<T>(block: (db: Database) throws -> T) rethrows -> T {
|
||||
return try block(db: self)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
-22
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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?) {
|
||||
|
||||
@@ -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?) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user