mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Replace persistentDictionary with encode(to:)
This commit is contained in:
1 parent
35d34ab524
commit
a913aba018
38 files changed
+517
-722
No files matched your search
@@ -8,6 +8,34 @@ Release Notes
|
||||
- `DatabaseCoder` has been removed.
|
||||
- `QueryInterfaceRequest.exists` has been removed.
|
||||
- `QueryInterfaceRequest.contains` has been removed.
|
||||
- `MutablePersistable`, the protocol that defines persistence methods, has been changed:
|
||||
|
||||
```diff
|
||||
protocol MutablePersistable : TableMapping {
|
||||
- var persistentDictionary: [String: DatabaseValueConvertible?] { get }
|
||||
+ func encode(to container: inout PersistenceContainer)
|
||||
}
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```diff
|
||||
struct Player : MutablePersistable {
|
||||
let name: String
|
||||
let score: Int
|
||||
|
||||
- var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
- return [
|
||||
- "name": name,
|
||||
- "score": score,
|
||||
- ]
|
||||
- }
|
||||
+ func encode(to container: inout PersistenceContainer) {
|
||||
+ container["name"] = name
|
||||
+ container["score"] = score
|
||||
+ }
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## 0.110.0
|
||||
|
||||
@@ -24,11 +24,10 @@ class Person: Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String : DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"name": name,
|
||||
"score": score]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["score"] = score
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -117,8 +117,9 @@ Well, you will be able to use UIColor like all other [value types](../../../#val
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": name, "color": color]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["name"] = name
|
||||
container["color"] = color
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -815,7 +815,7 @@ extension FetchedRecordsController where Record: MutablePersistable {
|
||||
/// - returns: The index path of *record* in the fetched records, or nil
|
||||
/// if record could not be found.
|
||||
public func indexPath(for record: Record) -> IndexPath? {
|
||||
let item = Item<Record>(row: Row(record.persistentDictionary))
|
||||
let item = Item<Record>(row: Row(record))
|
||||
guard let fetchedItems = fetchedItems, let index = fetchedItems.index(where: { itemsAreIdentical($0, item) }) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
+136
-47
@@ -18,19 +18,110 @@ extension PersistenceError : CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
private func databaseValue(for column: String, inDictionary dictionary: [String: DatabaseValueConvertible?]) -> DatabaseValue {
|
||||
if let value = dictionary[column] {
|
||||
return value?.databaseValue ?? .null
|
||||
|
||||
// MARK: - PersistenceContainer
|
||||
|
||||
/// Use persistence containers in the `encode(to:)` method of your
|
||||
/// persistable records:
|
||||
///
|
||||
/// struct Person : MutablePersistable {
|
||||
/// var id: Int64?
|
||||
/// var name: String?
|
||||
///
|
||||
/// func encode(to container: inout PersistenceContainer) {
|
||||
/// container["id"] = id
|
||||
/// container["name"] = name
|
||||
/// }
|
||||
/// }
|
||||
public struct PersistenceContainer {
|
||||
// fileprivate for Row(_:PersistenceContainer)
|
||||
fileprivate var storage: [String: DatabaseValueConvertible?]
|
||||
|
||||
/// Accesses the value associated with the given column.
|
||||
public subscript(_ column: String) -> DatabaseValueConvertible? {
|
||||
get { return storage[column] ?? nil }
|
||||
set { storage.updateValue(newValue, forKey: column) }
|
||||
}
|
||||
let column = column.lowercased()
|
||||
for (key, value) in dictionary where key.lowercased() == column {
|
||||
return value?.databaseValue ?? .null
|
||||
|
||||
/// Accesses the value associated with the given column.
|
||||
public subscript(_ column: Column) -> DatabaseValueConvertible? {
|
||||
get { return self[column.name] }
|
||||
set { self[column.name] = newValue }
|
||||
}
|
||||
|
||||
init() {
|
||||
storage = [:]
|
||||
}
|
||||
|
||||
/// Convenience initializer from a record
|
||||
///
|
||||
/// // Sweet
|
||||
/// let container = PersistenceContainer(record)
|
||||
///
|
||||
/// // Meh
|
||||
/// var container = PersistenceContainer()
|
||||
/// record.encode(to: container)
|
||||
init(_ record: MutablePersistable) {
|
||||
storage = [:]
|
||||
record.encode(to: &self)
|
||||
}
|
||||
|
||||
/// Columns stored in the container, ordered like values.
|
||||
var columns: [String] {
|
||||
return Array(storage.keys)
|
||||
}
|
||||
|
||||
/// Values stored in the container, ordered like columns.
|
||||
var values: [DatabaseValueConvertible?] {
|
||||
return Array(storage.values)
|
||||
}
|
||||
|
||||
/// Accesses the value associated with the given column, in a
|
||||
/// case-insensitive fashion.
|
||||
subscript(caseInsensitive column: String) -> DatabaseValueConvertible? {
|
||||
get {
|
||||
if let value = storage[column] {
|
||||
return value
|
||||
}
|
||||
let lowercaseColumn = column.lowercased()
|
||||
for (key, value) in storage where key.lowercased() == lowercaseColumn {
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
set {
|
||||
if storage[column] != nil {
|
||||
storage[column] = newValue
|
||||
return
|
||||
}
|
||||
let lowercaseColumn = column.lowercased()
|
||||
for key in storage.keys where key.lowercased() == lowercaseColumn {
|
||||
storage[key] = newValue
|
||||
return
|
||||
}
|
||||
|
||||
storage[column] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
var isEmpty: Bool {
|
||||
return storage.isEmpty
|
||||
}
|
||||
|
||||
/// An iterator over the (column, value) pairs
|
||||
func makeIterator() -> DictionaryIterator<String, DatabaseValueConvertible?> {
|
||||
return storage.makeIterator()
|
||||
}
|
||||
return .null
|
||||
}
|
||||
|
||||
private func databaseValues(for columns: [String], inDictionary dictionary: [String: DatabaseValueConvertible?]) -> [DatabaseValue] {
|
||||
return columns.map { databaseValue(for: $0, inDictionary: dictionary) }
|
||||
extension Row {
|
||||
convenience init(_ record: MutablePersistable) {
|
||||
self.init(PersistenceContainer(record))
|
||||
}
|
||||
|
||||
convenience init(_ container: PersistenceContainer) {
|
||||
self.init(container.storage)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,12 +148,6 @@ public struct PersistenceConflictPolicy {
|
||||
}
|
||||
|
||||
/// Types that adopt MutablePersistable can be inserted, updated, and deleted.
|
||||
///
|
||||
/// This protocol is intented for types that have an INTEGER PRIMARY KEY, and
|
||||
/// are interested in the inserted RowID: they can mutate themselves upon
|
||||
/// successful insertion with the didInsert(with:for:) method.
|
||||
///
|
||||
/// The insert() and save() methods are mutating methods.
|
||||
public protocol MutablePersistable : TableMapping {
|
||||
/// The policy that handles SQLite conflicts when records are inserted
|
||||
/// or updated.
|
||||
@@ -78,22 +163,23 @@ public protocol MutablePersistable : TableMapping {
|
||||
/// See https://www.sqlite.org/lang_conflict.html
|
||||
static var persistenceConflictPolicy: PersistenceConflictPolicy { get }
|
||||
|
||||
/// Returns the values that should be stored in the database.
|
||||
/// Defines the values persisted in the database.
|
||||
///
|
||||
/// Keys of the returned dictionary must match the column names of the
|
||||
/// target database table (see TableMapping.databaseTableName()).
|
||||
/// Store in the *container* argument all values that should be stored in
|
||||
/// the columns of the database table (see databaseTableName()).
|
||||
///
|
||||
/// In particular, primary key columns, if any, must be included.
|
||||
/// Primary key columns, if any, must be included.
|
||||
///
|
||||
/// struct Person : MutablePersistable {
|
||||
/// var id: Int64?
|
||||
/// var name: String?
|
||||
///
|
||||
/// var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
/// return ["id": id, "name": name]
|
||||
/// func encode(to container: inout PersistenceContainer) {
|
||||
/// container["id"] = id
|
||||
/// container["name"] = name
|
||||
/// }
|
||||
/// }
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] { get }
|
||||
func encode(to container: inout PersistenceContainer)
|
||||
|
||||
/// Notifies the record that it was succesfully inserted.
|
||||
///
|
||||
@@ -300,17 +386,15 @@ extension MutablePersistable {
|
||||
|
||||
// MARK: - CRUD Internals
|
||||
|
||||
/// Return true if record has a non-null primary key
|
||||
fileprivate func canUpdate(_ db: Database) throws -> Bool {
|
||||
// Fail early if database table does not exist.
|
||||
let databaseTableName = type(of: self).databaseTableName
|
||||
|
||||
// Update according to explicit primary key, or implicit rowid.
|
||||
let primaryKey = try db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.hiddenRowID
|
||||
|
||||
// We need a primary key value in the persistentDictionary
|
||||
let persistentDictionary = self.persistentDictionary
|
||||
for column in primaryKey.columns where !databaseValue(for: column, inDictionary: persistentDictionary).isNull {
|
||||
return true
|
||||
let container = PersistenceContainer(self)
|
||||
for column in primaryKey.columns {
|
||||
if let value = container[caseInsensitive: column], !value.databaseValue.isNull {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -571,7 +655,6 @@ extension Persistable {
|
||||
|
||||
}
|
||||
|
||||
|
||||
// MARK: - DAO
|
||||
|
||||
/// DAO takes care of Persistable CRUD
|
||||
@@ -583,10 +666,10 @@ final class DAO {
|
||||
/// The record
|
||||
let record: MutablePersistable
|
||||
|
||||
/// DAO keeps a copy the record's persistentDictionary, so that this
|
||||
/// DAO keeps a copy the record's persistenceContainer, so that this
|
||||
/// dictionary is built once whatever the database operation. It is
|
||||
/// guaranteed to have at least one (key, value) pair.
|
||||
let persistentDictionary: [String: DatabaseValueConvertible?]
|
||||
let persistenceContainer: PersistenceContainer
|
||||
|
||||
/// The table name
|
||||
let databaseTableName: String
|
||||
@@ -595,17 +678,15 @@ final class DAO {
|
||||
let primaryKey: PrimaryKeyInfo
|
||||
|
||||
init(_ db: Database, _ record: MutablePersistable) throws {
|
||||
// Fail early if database table does not exist.
|
||||
let databaseTableName = type(of: record).databaseTableName
|
||||
let primaryKey = try db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.hiddenRowID
|
||||
let persistenceContainer = PersistenceContainer(record)
|
||||
|
||||
// Fail early if persistentDictionary is empty
|
||||
let persistentDictionary = record.persistentDictionary
|
||||
GRDBPrecondition(persistentDictionary.count > 0, "\(type(of: record)).persistentDictionary: invalid empty dictionary")
|
||||
GRDBPrecondition(!persistenceContainer.isEmpty, "\(type(of: record)): invalid empty persistence container")
|
||||
|
||||
self.db = db
|
||||
self.record = record
|
||||
self.persistentDictionary = persistentDictionary
|
||||
self.persistenceContainer = persistenceContainer
|
||||
self.databaseTableName = databaseTableName
|
||||
self.primaryKey = primaryKey
|
||||
}
|
||||
@@ -614,9 +695,9 @@ final class DAO {
|
||||
let query = InsertQuery(
|
||||
onConflict: onConflict,
|
||||
tableName: databaseTableName,
|
||||
insertedColumns: Array(persistentDictionary.keys))
|
||||
insertedColumns: persistenceContainer.columns)
|
||||
let statement = try db.updateStatement(query.sql, fromCache: .grdb)
|
||||
statement.unsafeSetArguments(StatementArguments(persistentDictionary.values))
|
||||
statement.unsafeSetArguments(StatementArguments(persistenceContainer.values))
|
||||
return statement
|
||||
}
|
||||
|
||||
@@ -624,16 +705,18 @@ final class DAO {
|
||||
func updateStatement(columns: Set<String>, onConflict: Database.ConflictResolution) throws -> UpdateStatement? {
|
||||
// Fail early if primary key does not resolve to a database row.
|
||||
let primaryKeyColumns = primaryKey.columns
|
||||
let primaryKeyValues = databaseValues(for: primaryKeyColumns, inDictionary: persistentDictionary)
|
||||
let primaryKeyValues = primaryKeyColumns.map {
|
||||
persistenceContainer[caseInsensitive: $0]?.databaseValue ?? .null
|
||||
}
|
||||
guard primaryKeyValues.contains(where: { !$0.isNull }) else { return nil }
|
||||
|
||||
let lowercasePersistentColumns = Set(persistentDictionary.keys.map { $0.lowercased() })
|
||||
let lowercasePersistentColumns = Set(persistenceContainer.columns.map { $0.lowercased() })
|
||||
let lowercasePrimaryKeyColumns = Set(primaryKeyColumns.map { $0.lowercased() })
|
||||
var updatedColumns: [String] = []
|
||||
for column in columns {
|
||||
let lowercaseColumn = column.lowercased()
|
||||
// Make sure the requested column is present in persistentDictionary
|
||||
GRDBPrecondition(lowercasePersistentColumns.contains(lowercaseColumn), "column \(column) can't be updated because it is missing from persistentDictionary")
|
||||
// Make sure the requested column is present in persistenceContainer
|
||||
GRDBPrecondition(lowercasePersistentColumns.contains(lowercaseColumn), "column \(column) can't be updated because it is missing from persistence container")
|
||||
// Don't update primary key columns
|
||||
guard !lowercasePrimaryKeyColumns.contains(lowercaseColumn) else { continue }
|
||||
updatedColumns.append(column)
|
||||
@@ -650,7 +733,9 @@ final class DAO {
|
||||
// including tables made of a single primary key column.
|
||||
updatedColumns = primaryKeyColumns
|
||||
}
|
||||
let updatedValues = databaseValues(for: updatedColumns, inDictionary: persistentDictionary)
|
||||
let updatedValues = updatedColumns.map {
|
||||
persistenceContainer[caseInsensitive: $0]?.databaseValue ?? .null
|
||||
}
|
||||
|
||||
let query = UpdateQuery(
|
||||
onConflict: onConflict,
|
||||
@@ -666,7 +751,9 @@ final class DAO {
|
||||
func deleteStatement() throws -> UpdateStatement? {
|
||||
// Fail early if primary key does not resolve to a database row.
|
||||
let primaryKeyColumns = primaryKey.columns
|
||||
let primaryKeyValues = databaseValues(for: primaryKeyColumns, inDictionary: persistentDictionary)
|
||||
let primaryKeyValues = primaryKeyColumns.map {
|
||||
persistenceContainer[caseInsensitive: $0]?.databaseValue ?? .null
|
||||
}
|
||||
guard primaryKeyValues.contains(where: { !$0.isNull }) else { return nil }
|
||||
|
||||
let query = DeleteQuery(
|
||||
@@ -681,7 +768,9 @@ final class DAO {
|
||||
func existsStatement() throws -> SelectStatement? {
|
||||
// Fail early if primary key does not resolve to a database row.
|
||||
let primaryKeyColumns = primaryKey.columns
|
||||
let primaryKeyValues = databaseValues(for: primaryKeyColumns, inDictionary: persistentDictionary)
|
||||
let primaryKeyValues = primaryKeyColumns.map {
|
||||
persistenceContainer[caseInsensitive: $0]?.databaseValue ?? .null
|
||||
}
|
||||
guard primaryKeyValues.contains(where: { !$0.isNull }) else { return nil }
|
||||
|
||||
let query = ExistsQuery(
|
||||
|
||||
+35
-51
@@ -77,25 +77,26 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
}
|
||||
|
||||
|
||||
/// The values that should be stored in the database.
|
||||
/// Defines the values persisted in the database.
|
||||
///
|
||||
/// Keys of the returned dictionary must match the column names of the
|
||||
/// target database table (see Record.databaseTableName()).
|
||||
/// Store in the *container* argument all values that should be stored in
|
||||
/// the columns of the database table (see Record.databaseTableName()).
|
||||
///
|
||||
/// In particular, primary key columns, if any, must be included.
|
||||
/// 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]
|
||||
/// override func encode(to container: inout PersistenceContainer) {
|
||||
/// container["id"] = id
|
||||
/// container["name"] = name
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// The implementation of the base class Record returns an empty dictionary.
|
||||
open var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [:]
|
||||
/// The implementation of the base class Record does not store any value in
|
||||
/// the container.
|
||||
open func encode(to container: inout PersistenceContainer) {
|
||||
}
|
||||
|
||||
/// Notifies the record that it was succesfully inserted.
|
||||
@@ -124,15 +125,22 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
|
||||
// MARK: - Copy
|
||||
|
||||
/// Returns a copy of `self`, initialized from the values of
|
||||
/// persistentDictionary.
|
||||
/// Returns a copy of `self`, initialized from all values encoded in the
|
||||
/// `encode(to:)` method.
|
||||
///
|
||||
/// Note that the eventual primary key is copied, as well as the
|
||||
/// hasPersistentChangedValues flag.
|
||||
/// The eventual primary key is copied, as well as the
|
||||
/// `hasPersistentChangedValues` flag.
|
||||
///
|
||||
/// - returns: A copy of self.
|
||||
open func copy() -> Self {
|
||||
let copy = type(of: self).init(row: Row(persistentDictionary))
|
||||
let row: Row
|
||||
#if swift(>=3.1)
|
||||
row = Row(self)
|
||||
#else
|
||||
// workaround weird Swift 3.0 glitch
|
||||
row = Row(self as! MutablePersistable)
|
||||
#endif
|
||||
let copy = type(of: self).init(row: row)
|
||||
copy.referenceRow = referenceRow
|
||||
return copy
|
||||
}
|
||||
@@ -146,10 +154,11 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
/// 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.
|
||||
/// A record is *edited* if has been changed since last database
|
||||
/// synchronization (fetch, update, insert). Comparison
|
||||
/// is performed between *values* (values stored in the `encode(to:)`
|
||||
/// method, and values loaded from the database). Property setters do not
|
||||
/// trigger this 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
|
||||
@@ -157,7 +166,7 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
/// of the record.
|
||||
public var hasPersistentChangedValues: Bool {
|
||||
get { return makePersistentChangedValuesIterator().next() != nil }
|
||||
set { referenceRow = newValue ? nil : Row(persistentDictionary) }
|
||||
set { referenceRow = newValue ? nil : Row(self) }
|
||||
}
|
||||
|
||||
/// A dictionary of changes that have not been saved.
|
||||
@@ -182,7 +191,7 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
// persistentChangedValues properties.
|
||||
private func makePersistentChangedValuesIterator() -> AnyIterator<(column: String, old: DatabaseValue?)> {
|
||||
let oldRow = referenceRow
|
||||
var newValueIterator = persistentDictionary.makeIterator()
|
||||
var newValueIterator = PersistenceContainer(self).makeIterator()
|
||||
return AnyIterator {
|
||||
// Loop until we find a change, or exhaust columns:
|
||||
while let (column, newValue) = newValueIterator.next() {
|
||||
@@ -220,21 +229,9 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
/// - parameter db: A database connection.
|
||||
/// - throws: A DatabaseError whenever an SQLite error occurs.
|
||||
open func insert(_ db: Database) throws {
|
||||
// The simplest code would be:
|
||||
//
|
||||
// try performInsert(db)
|
||||
// hasPersistentChangedValues = false
|
||||
//
|
||||
// But this triggers two calls to persistentDictionary, and this is both
|
||||
// costly, and ambiguous. Costly because persistentDictionary is slow.
|
||||
// Ambiguous because persistentDictionary may return a different value.
|
||||
//
|
||||
// So let's provide our custom implementation of insert, which uses the
|
||||
// same persistentDictionary for both insertion, and change tracking.
|
||||
|
||||
let conflictResolutionForInsert = type(of: self).persistenceConflictPolicy.conflictResolutionForInsert
|
||||
let dao = try DAO(db, self)
|
||||
var persistentDictionary = dao.persistentDictionary
|
||||
var persistenceContainer = dao.persistenceContainer
|
||||
try dao.insertStatement(onConflict: conflictResolutionForInsert).execute()
|
||||
|
||||
if !conflictResolutionForInsert.invalidatesLastInsertedRowID {
|
||||
@@ -242,23 +239,15 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
let rowIDColumn = dao.primaryKey.rowIDColumn
|
||||
didInsert(with: rowID, for: rowIDColumn)
|
||||
|
||||
// Update persistentDictionary with inserted id, so that we can
|
||||
// Update persistenceContainer 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.lowercased()
|
||||
for column in persistentDictionary.keys where column.lowercased() == rowIDColumn {
|
||||
persistentDictionary[column] = rowID
|
||||
break
|
||||
}
|
||||
}
|
||||
persistenceContainer[caseInsensitive: rowIDColumn] = rowID
|
||||
}
|
||||
}
|
||||
|
||||
// Set hasPersistentChangedValues to false
|
||||
referenceRow = Row(persistentDictionary)
|
||||
referenceRow = Row(persistenceContainer)
|
||||
}
|
||||
|
||||
/// Executes an UPDATE statement.
|
||||
@@ -280,12 +269,7 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
// try performUpdate(db, columns: columns)
|
||||
// hasPersistentChangedValues = false
|
||||
//
|
||||
// But this triggers two calls to persistentDictionary, and this is both
|
||||
// costly, and ambiguous. Costly because persistentDictionary is slow.
|
||||
// Ambiguous because persistentDictionary may return a different value.
|
||||
//
|
||||
// So let's provide our custom implementation of insert, which uses the
|
||||
// same persistentDictionary for both update, and change tracking.
|
||||
// But this would trigger two calls to `encode(to:)`.
|
||||
let dao = try DAO(db, self)
|
||||
guard let statement = try dao.updateStatement(columns: columns, onConflict: type(of: self).persistenceConflictPolicy.conflictResolutionForUpdate) else {
|
||||
// Nil primary key
|
||||
@@ -297,7 +281,7 @@ open class Record : RowConvertible, TableMapping, Persistable {
|
||||
}
|
||||
|
||||
// Set hasPersistentChangedValues to false
|
||||
referenceRow = Row(dao.persistentDictionary)
|
||||
referenceRow = Row(dao.persistenceContainer)
|
||||
}
|
||||
|
||||
/// Executes an INSERT or an UPDATE statement so that `self` is saved in
|
||||
|
||||
@@ -54,8 +54,9 @@ class Person : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "name": name]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -68,8 +68,10 @@ class Person : Record {
|
||||
|
||||
//: 3. The dictionary of values that are stored in the database:
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "firstName": firstName, "lastName": lastName]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["firstName"] = firstName
|
||||
container["lastName"] = lastName
|
||||
}
|
||||
|
||||
//: 4. When relevant, update the person's id after a database row has been inserted:
|
||||
|
||||
@@ -83,14 +83,12 @@ extension PointOfInterest : TableMapping {
|
||||
|
||||
// Adopt MutablePersistable
|
||||
extension PointOfInterest : MutablePersistable {
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"title": title,
|
||||
"favorite": favorite,
|
||||
"latitude": coordinate.latitude,
|
||||
"longitude": coordinate.longitude
|
||||
]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["title"] = title
|
||||
container["favorite"] = favorite
|
||||
container["latitude"] = coordinate.latitude
|
||||
container["longitude"] = coordinate.longitude
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -976,8 +976,9 @@ class Link : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["url": url, "verified": isVerified]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["url"] = url
|
||||
container["verified"] = isVerified
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -1865,6 +1866,25 @@ extension PointOfInterest : RowConvertible {
|
||||
}
|
||||
```
|
||||
|
||||
Rows also accept keys of type `Column`:
|
||||
|
||||
```swift
|
||||
extension PointOfInterest : RowConvertible {
|
||||
static let idColumn = Column("id")
|
||||
static let titleColumn = Column("title")
|
||||
static let latitudeColumn = Column("latitude")
|
||||
static let longitudeColumn = Column("longitude")
|
||||
|
||||
init(row: Row) {
|
||||
id = row.value(PointOfInterest.idColumn)
|
||||
title = row.value(PointOfInterest.titleColumn)
|
||||
coordinate = CLLocationCoordinate2DMake(
|
||||
row.value(PointOfInterest.latitudeColumn),
|
||||
row.value(PointOfInterest.longitudeColumn))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
See [column values](#column-values) for more information about the `row.value()` method.
|
||||
|
||||
> :point_up: **Note**: for performance reasons, the same row argument to `init(row:)` is reused during the iteration of a fetch query. If you want to keep the row for later use, make sure to store a copy: `self.row = row.copy()`.
|
||||
@@ -1971,8 +1991,8 @@ protocol MutablePersistable : TableMapping {
|
||||
/// The name of the database table (from TableMapping)
|
||||
static var databaseTableName: String { get }
|
||||
|
||||
/// The values persisted in the database
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] { get }
|
||||
/// Defines the values persisted in the database
|
||||
func encode(to container: inout PersistenceContainer)
|
||||
|
||||
/// Optional method that lets your adopting type store its rowID upon
|
||||
/// successful insertion. Don't call it directly: it is called for you.
|
||||
@@ -1995,7 +2015,7 @@ Yes, two protocols instead of one. Both grant exactly the same advantages. Here
|
||||
|
||||
- Otherwise, stick with `Persistable`. Particularly if your type is a class.
|
||||
|
||||
The `persistentDictionary` property returns a dictionary whose keys are column names, and values any DatabaseValueConvertible value (Bool, Int, String, Date, Swift enums, etc.) See [Values](#values) for more information.
|
||||
The `encode(to:)` method defines which [values](#values) are stored in database columns.
|
||||
|
||||
The optional `didInsert` method lets the adopting type store its rowID after successful insertion. If your table has an INTEGER PRIMARY KEY column, you are likely to define this method. Otherwise, you can safely ignore it. It is called from a protected dispatch queue, and serialized with all database updates.
|
||||
|
||||
@@ -2005,12 +2025,11 @@ The optional `didInsert` method lets the adopting type store its rowID after suc
|
||||
extension PointOfInterest : MutablePersistable {
|
||||
|
||||
/// The values persisted in the database
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"title": title,
|
||||
"latitude": coordinate.latitude,
|
||||
"longitude": coordinate.longitude]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["title"] = title
|
||||
container["latitude"] = coordinate.latitude
|
||||
container["longitude"] = coordinate.longitude
|
||||
}
|
||||
|
||||
// Update id upon successful insertion:
|
||||
@@ -2028,21 +2047,24 @@ try paris.insert(db)
|
||||
paris.id // some value
|
||||
```
|
||||
|
||||
When your record has many columns, the Swift compiler may take a long time compiling the `persistentDictionary` property:
|
||||
Persistence containers also accept keys of type `Column`:
|
||||
|
||||
```swift
|
||||
// May be long to compile
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"title": title,
|
||||
// many other columns
|
||||
]
|
||||
extension PointOfInterest : MutablePersistable {
|
||||
static let idColumn = Column("id")
|
||||
static let titleColumn = Column("title")
|
||||
static let latitudeColumn = Column("latitude")
|
||||
static let longitudeColumn = Column("longitude")
|
||||
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container[PointOfInterest.idColumn] = id
|
||||
container[PointOfInterest.titleColumn] = title
|
||||
container[PointOfInterest.latitudeColumn] = coordinate.latitude
|
||||
container[PointOfInterest.longitudeColumn] = coordinate.longitude
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
If this happens, check the ["Compilation takes a long time" FAQ](#compilation-takes-a-long-time).
|
||||
|
||||
|
||||
### Persistence Methods
|
||||
|
||||
@@ -2230,12 +2252,11 @@ class PointOfInterest : Record {
|
||||
}
|
||||
|
||||
/// The values persisted in the database
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"title": title,
|
||||
"latitude": coordinate.latitude,
|
||||
"longitude": coordinate.longitude]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["title"] = title
|
||||
container["latitude"] = coordinate.latitude
|
||||
container["longitude"] = coordinate.longitude
|
||||
}
|
||||
|
||||
/// Update record ID after a successful insertion
|
||||
@@ -2370,18 +2391,16 @@ When SQLite won't let you provide an explicit primary key (as in [full-text](#fu
|
||||
event.id // some value
|
||||
```
|
||||
|
||||
3. Include the rowid in your `persistentDictionary`, and keep it in the `didInsert(with:for:)` method (both from the [Persistable and MutablePersistable](#persistable-protocol) protocols):
|
||||
3. Encode the rowid in `encode(to:)`, and keep it in the `didInsert(with:for:)` method (both from the [Persistable and MutablePersistable](#persistable-protocol) protocols):
|
||||
|
||||
```swift
|
||||
struct Event : MutablePersistable {
|
||||
var id: Int64?
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"rowid": id,
|
||||
"message": message,
|
||||
"date": date,
|
||||
]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container[.rowID] = id
|
||||
container["message"] = message
|
||||
container["date"] = date
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -5474,8 +5493,10 @@ class Person : Record {
|
||||
super.init()
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "name": name, "email": email] // Dictionary
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id // String
|
||||
container["name"] = name // String
|
||||
container["email"] = email // String
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -5598,44 +5619,6 @@ let string = try dbQueue.inDatabase { db in
|
||||
```
|
||||
|
||||
|
||||
### Compilation takes a long time
|
||||
|
||||
When your [record type](#records) is very slow to compile, it is usually because its `persistentDictionary` property builds a long dictionary literal:
|
||||
|
||||
```swift
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
// Long dictionary literals are slow to compile
|
||||
return [
|
||||
"a": a,
|
||||
"b": b,
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
That's annoying, but the Swift compiler finds it difficult to compile such a dictionary. We can only hope that compiler improves over time.
|
||||
|
||||
To speed up compilation, build your dictionary step by step:
|
||||
|
||||
```swift
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
var dict: [String: DatabaseValueConvertible?] = [:]
|
||||
dict.updateValue(a, forKey: "a")
|
||||
dict.updateValue(b, forKey: "b")
|
||||
...
|
||||
return dict
|
||||
}
|
||||
```
|
||||
|
||||
> :point_up: **Note**: it is important that you use the `updateValue` method, and not the subscript setter:
|
||||
>
|
||||
> ```swift
|
||||
> // GOOD
|
||||
> dict.updateValue(a, forKey: "a")
|
||||
> // BAD: when the value is nil, this erases the key instead of setting it to nil.
|
||||
> dict["a"] = a
|
||||
> ```
|
||||
|
||||
|
||||
### SQLite error 10 "disk I/O error", SQLite error 23 "not authorized"
|
||||
|
||||
Those errors may be the sign that SQLite can't access the database due to [data protection](#data-protection).
|
||||
|
||||
@@ -27,7 +27,7 @@ v1.0 checklit
|
||||
It's not worth the effort to introduce more concrete request types (AdaptedRequest, AdaptedTypedRequest, BoundRequest)
|
||||
- [X] Question the naming of the `request.bound(to:T.self)` method. Prefer... `request..asRequest(of: T.self)`
|
||||
- [ ] Make GRDB less stringly-typed. When a user defines a static list of columns for a record type, those columns should be easier to use:
|
||||
- [ ] Persistable.persistentDictionary requires string keys, doesn't accept columns
|
||||
- [X] Persistable.persistentDictionary requires string keys, doesn't accept columns
|
||||
- [ ] If possible, improve on `Person.filter(Person.email != nil)` or `Person.filter(Person.Column.email != nil)` (see [#186](https://github.com/groue/GRDB.swift/issues/186))
|
||||
- [ ] Check [SE-0166](https://github.com/apple/swift-evolution/blob/master/proposals/0166-swift-archival-serialization.md)
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@ private class RecordWithNilPrimaryKey : Record {
|
||||
return "records"
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": nil]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ private class RecordForTableWithoutPrimaryKey : Record {
|
||||
return "records"
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": "foo"]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["name"] = "foo"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ private class RecordForTableWithMultipleColumnsPrimaryKey : Record {
|
||||
return "records"
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": "foo"]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["name"] = "foo"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ private class RecordWithRowIDPrimaryKeyNotExposedInPersistentDictionary : Record
|
||||
return "records"
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": "foo"]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["name"] = "foo"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,10 @@ class Person: Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String : DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"name": name,
|
||||
"score": score]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["score"] = score
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -27,13 +27,11 @@ extension Book : MutablePersistable {
|
||||
static let databaseTableName = "books"
|
||||
static let selectsRowID = true
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
Column.rowID.name: id,
|
||||
"title": title,
|
||||
"author": author,
|
||||
"body": body,
|
||||
]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container[.rowID] = id
|
||||
container["title"] = title
|
||||
container["author"] = author
|
||||
container["body"] = body
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -27,13 +27,11 @@ extension Book : MutablePersistable {
|
||||
static let databaseTableName = "books"
|
||||
static let selectsRowID = true
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
Column.rowID.name: id,
|
||||
"title": title,
|
||||
"author": author,
|
||||
"body": body,
|
||||
]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container[.rowID] = id
|
||||
container["title"] = title
|
||||
container["author"] = author
|
||||
container["body"] = body
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -28,13 +28,11 @@ extension Book : MutablePersistable {
|
||||
static let databaseTableName = "books"
|
||||
static let selectsRowID = true
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
Column.rowID.name: id,
|
||||
"title": title,
|
||||
"author": author,
|
||||
"body": body,
|
||||
]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container[.rowID] = id
|
||||
container["title"] = title
|
||||
container["author"] = author
|
||||
container["body"] = body
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -74,8 +74,10 @@ private class Person : Record {
|
||||
return "persons"
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String : DatabaseValueConvertible?] {
|
||||
return ["id": id, "name": name, "email": email]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["email"] = email
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -117,6 +117,17 @@ class GRDBTestCase: XCTestCase {
|
||||
XCTAssertTrue(sqlQueries.contains(sql), "Did not execute \(sql)")
|
||||
}
|
||||
|
||||
func assert(_ record: MutablePersistable, isEncodedIn row: Row) {
|
||||
let recordContent = AnySequence({ PersistenceContainer(record).makeIterator() })
|
||||
for (column, value) in recordContent {
|
||||
if let dbValue: DatabaseValue = row.value(named: column) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(column) in fetched row")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sql(_ databaseReader: DatabaseReader, _ request: Request) -> String {
|
||||
return try! databaseReader.unsafeRead { db in
|
||||
_ = try Row.fetchOne(db, request)
|
||||
|
||||
@@ -12,8 +12,8 @@ private struct DefaultPolicy: MutablePersistable {
|
||||
|
||||
static let databaseTableName = "records"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -27,8 +27,8 @@ private struct MixedPolicy: MutablePersistable {
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .fail, update: .rollback)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -43,8 +43,9 @@ private struct ReplacePolicy: MutablePersistable {
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .replace, update: .replace)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "email": email]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["email"] = email
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -58,8 +59,8 @@ private struct IgnorePolicy: MutablePersistable {
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .ignore, update: .ignore)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -73,8 +74,8 @@ private struct FailPolicy: MutablePersistable {
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .fail, update: .fail)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -88,8 +89,8 @@ private struct AbortPolicy: MutablePersistable {
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .abort)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -103,8 +104,8 @@ private struct RollbackPolicy: MutablePersistable {
|
||||
static let databaseTableName = "records"
|
||||
static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .rollback, update: .rollback)
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -14,8 +14,11 @@ private struct MutablePersistablePerson : MutablePersistable {
|
||||
|
||||
static let databaseTableName = "persons"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["iD": id, "NAme": name, "aGe": age] // various cases
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
// mangle cases
|
||||
container["iD"] = id
|
||||
container["NAme"] = name
|
||||
container["aGe"] = age
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -30,8 +33,9 @@ private struct MutablePersistableCountry : MutablePersistable {
|
||||
|
||||
static let databaseTableName = "countries"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["isoCode": isoCode, "name": name]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["isoCode"] = isoCode
|
||||
container["name"] = name
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -51,8 +55,9 @@ private struct MutablePersistableCustomizedCountry : MutablePersistable {
|
||||
|
||||
static let databaseTableName = "countries"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["isoCode": isoCode, "name": name]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["isoCode"] = isoCode
|
||||
container["name"] = name
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -13,8 +13,9 @@ private struct PersistablePerson : Persistable {
|
||||
|
||||
static let databaseTableName = "persons"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": name, "age": age]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +32,11 @@ private class PersistablePersonClass : Persistable {
|
||||
|
||||
static let databaseTableName = "persons"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["ID": id, "naME": name, "Age": age] // various cases
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
// mangle case
|
||||
container["ID"] = id
|
||||
container["naME"] = name
|
||||
container["Age"] = age
|
||||
}
|
||||
|
||||
func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -46,8 +50,9 @@ private struct PersistableCountry : Persistable {
|
||||
|
||||
static let databaseTableName = "countries"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["isoCode": isoCode, "name": name]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["isoCode"] = isoCode
|
||||
container["name"] = name
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,8 +67,9 @@ private struct PersistableCustomizedCountry : Persistable {
|
||||
|
||||
static let databaseTableName = "countries"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["isoCode": isoCode, "name": name]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["isoCode"] = isoCode
|
||||
container["name"] = name
|
||||
}
|
||||
|
||||
func insert(_ db: Database) throws {
|
||||
@@ -98,8 +104,9 @@ private struct Citizenship : Persistable {
|
||||
|
||||
static let databaseTableName = "citizenships"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["countryIsoCode": countryIsoCode, "personID": personID]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["countryIsoCode"] = countryIsoCode
|
||||
container["personID"] = personID
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,8 +30,10 @@ private class Reader : Record {
|
||||
return "readers"
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "name": name, "age": age]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -31,13 +31,11 @@ private class Person : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"name": name,
|
||||
"age": age,
|
||||
"creationDate": creationDate,
|
||||
]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
container["creationDate"] = creationDate
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,13 +45,11 @@ private class Person : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"name": name,
|
||||
"age": age,
|
||||
"creationDate": creationDate,
|
||||
]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
container["creationDate"] = creationDate
|
||||
}
|
||||
|
||||
override func insert(_ db: Database) throws {
|
||||
@@ -69,7 +67,7 @@ private class Person : Record {
|
||||
}
|
||||
|
||||
private class IntegerPropertyOnRealAffinityColumn : Record {
|
||||
var value: Int!
|
||||
var value: Int?
|
||||
|
||||
init(value: Int?) {
|
||||
self.value = value
|
||||
@@ -83,8 +81,8 @@ private class IntegerPropertyOnRealAffinityColumn : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["value": value]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["value"] = value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,13 +114,11 @@ private class PersonWithModifiedCaseColumns: Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"ID": id,
|
||||
"NAME": name,
|
||||
"AGE": age,
|
||||
"CREATIONDATE": creationDate,
|
||||
]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["ID"] = id
|
||||
container["NAME"] = name
|
||||
container["AGE"] = age
|
||||
container["CREATIONDATE"] = creationDate
|
||||
}
|
||||
|
||||
override func insert(_ db: Database) throws {
|
||||
@@ -165,8 +161,8 @@ class RecordEditedTests: GRDBTestCase {
|
||||
|
||||
func testRecordIsNotEditedAfterFullFetch() throws {
|
||||
// Fetch a record from a row that contains all the columns in
|
||||
// persistentDictionary: An update statement, which only saves the
|
||||
// columns in persistentDictionary would perform no change. So the
|
||||
// persistence container: An update statement, which only saves the
|
||||
// columns in persistence container would perform no change. So the
|
||||
// record is not edited.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -186,34 +182,15 @@ class RecordEditedTests: GRDBTestCase {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE t (value REAL)")
|
||||
try db.execute("INSERT INTO t (value) VALUES (1)")
|
||||
|
||||
// Load a double...
|
||||
let row1 = try Row.fetchOne(db, "SELECT * FROM t")!
|
||||
switch (row1.value(named: "value") as DatabaseValue).storage {
|
||||
case .double(let double):
|
||||
XCTAssertEqual(double, 1.0)
|
||||
default:
|
||||
XCTFail("Unexpected DatabaseValue")
|
||||
}
|
||||
|
||||
// Compare to an Int
|
||||
let record = try IntegerPropertyOnRealAffinityColumn.fetchOne(db, "SELECT * FROM t")!
|
||||
let row2 = Row(record.persistentDictionary)
|
||||
switch (row2.value(named: "value") as DatabaseValue).storage {
|
||||
case .int64(let int64):
|
||||
XCTAssertEqual(int64, 1)
|
||||
default:
|
||||
XCTFail("Unexpected DatabaseValue")
|
||||
}
|
||||
|
||||
XCTAssertFalse(record.hasPersistentChangedValues)
|
||||
}
|
||||
}
|
||||
|
||||
func testRecordIsNotEditedAfterWiderThanFullFetch() throws {
|
||||
// Fetch a record from a row that contains all the columns in
|
||||
// persistentDictionary, plus extra ones: An update statement,
|
||||
// which only saves the columns in persistentDictionary would
|
||||
// persistence container, plus extra ones: An update statement,
|
||||
// which only saves the columns in persistence container would
|
||||
// perform no change. So the record is not edited.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -230,8 +207,8 @@ class RecordEditedTests: GRDBTestCase {
|
||||
|
||||
func testRecordIsEditedAfterPartialFetch() throws {
|
||||
// Fetch a record from a row that does not contain all the columns in
|
||||
// persistentDictionary: An update statement saves the columns in
|
||||
// persistentDictionary, so it may perform unpredictable change.
|
||||
// persistence container: An update statement saves the columns in
|
||||
// persistence container, so it may perform unpredictable change.
|
||||
// So the record is edited.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -262,7 +239,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testRecordIsEditedAfterValueChange() throws {
|
||||
// Any change in a value exposed in persistentDictionary yields a
|
||||
// Any change in a value exposed in persistence container yields a
|
||||
// record that is edited.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -492,8 +469,8 @@ class RecordEditedTests: GRDBTestCase {
|
||||
|
||||
func testChangesAfterFullFetch() throws {
|
||||
// Fetch a record from a row that contains all the columns in
|
||||
// persistentDictionary: An update statement, which only saves the
|
||||
// columns in persistentDictionary would perform no change. So the
|
||||
// persistence container: An update statement, which only saves the
|
||||
// columns in persistence container would perform no change. So the
|
||||
// record is not edited.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -529,8 +506,8 @@ class RecordEditedTests: GRDBTestCase {
|
||||
|
||||
func testChangesAfterPartialFetch() throws {
|
||||
// Fetch a record from a row that does not contain all the columns in
|
||||
// persistentDictionary: An update statement saves the columns in
|
||||
// persistentDictionary, so it may perform unpredictable change.
|
||||
// persistence container: An update statement saves the columns in
|
||||
// persistence container, so it may perform unpredictable change.
|
||||
// So the record is edited.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -589,7 +566,7 @@ class RecordEditedTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testChangesAfterValueChange() throws {
|
||||
// Any change in a value exposed in persistentDictionary yields a
|
||||
// Any change in a value exposed in persistence container yields a
|
||||
// record that is edited.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
@@ -33,8 +33,8 @@ class MinimalRowID : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -62,13 +62,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,13 +74,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,13 +101,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,13 +130,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
try record.update(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,13 +161,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,13 +173,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,13 +185,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,13 +198,7 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@ class MinimalSingle: Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["UUID": UUID]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["UUID"] = UUID
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,13 +71,7 @@ class RecordMinimalPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalSingles WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,13 +100,7 @@ class RecordMinimalPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalSingles WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,13 +130,7 @@ class RecordMinimalPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.update(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalSingles WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,13 +175,7 @@ class RecordMinimalPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalSingles WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,13 +188,7 @@ class RecordMinimalPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalSingles WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,13 +202,7 @@ class RecordMinimalPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM minimalSingles WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,13 +49,11 @@ private class Person : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
Column.rowID.name: id,
|
||||
"name": name,
|
||||
"age": age,
|
||||
"creationDate": creationDate,
|
||||
]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container[.rowID] = id
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
container["creationDate"] = creationDate
|
||||
}
|
||||
|
||||
override func insert(_ db: Database) throws {
|
||||
@@ -92,13 +90,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,13 +103,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
return .rollback
|
||||
}
|
||||
// This is debatable, actually.
|
||||
@@ -131,13 +117,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,13 +155,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,13 +197,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
try record.update(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,13 +228,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,13 +239,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,18 +249,12 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Person(name: "Arthur", age: 41)
|
||||
try record.insert(db)
|
||||
try record.save(db) // Test that useless update succeeds. It is a proof that save() has performed an UPDATE statement, and not an INSERT statement: INSERT would have throw a database error for duplicated key.
|
||||
try record.save(db) // Test that useless update succeeds. It is a proof that save() has performed an UPDATE statement, and not an INSERT statement: INSERT would have thrown a database error for duplicated key.
|
||||
record.age = record.age! + 1
|
||||
try record.save(db) // Actual update
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,13 +267,7 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT *, rowid FROM persons WHERE rowid = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,11 +43,10 @@ private class Citizenship : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"personName": personName,
|
||||
"countryName": countryName,
|
||||
"native": native]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["personName"] = personName
|
||||
container["countryName"] = countryName
|
||||
container["native"] = native
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,13 +83,7 @@ class RecordPrimaryKeyMultipleTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM citizenships WHERE personName = ? AND countryName = ?", arguments: [record.personName, record.countryName])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,13 +110,7 @@ class RecordPrimaryKeyMultipleTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM citizenships WHERE personName = ? AND countryName = ?", arguments: [record.personName, record.countryName])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,13 +152,7 @@ class RecordPrimaryKeyMultipleTests: GRDBTestCase {
|
||||
try record.update(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM citizenships WHERE personName = ? AND countryName = ?", arguments: [record.personName, record.countryName])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,13 +195,7 @@ class RecordPrimaryKeyMultipleTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM citizenships WHERE personName = ? AND countryName = ?", arguments: [record.personName, record.countryName])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,13 +209,7 @@ class RecordPrimaryKeyMultipleTests: GRDBTestCase {
|
||||
try record.save(db) // Actual update
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM citizenships WHERE personName = ? AND countryName = ?", arguments: [record.personName, record.countryName])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,13 +222,7 @@ class RecordPrimaryKeyMultipleTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM citizenships WHERE personName = ? AND countryName = ?", arguments: [record.personName, record.countryName])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,8 +40,9 @@ private class Item : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": name, "email": email]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["name"] = name
|
||||
container["email"] = email
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -46,13 +46,11 @@ private class Person : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"name": name,
|
||||
"age": age,
|
||||
"creationDate": creationDate,
|
||||
]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
container["creationDate"] = creationDate
|
||||
}
|
||||
|
||||
override func insert(_ db: Database) throws {
|
||||
@@ -89,13 +87,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,13 +100,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
return .rollback
|
||||
}
|
||||
// This is debatable, actually.
|
||||
@@ -128,13 +114,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,13 +152,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,13 +194,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
try record.update(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,13 +225,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
XCTAssertTrue(record.id != nil)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,13 +236,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,13 +250,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
try record.save(db) // Actual update
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,13 +263,7 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,8 +38,9 @@ class Pet : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["UUID": UUID, "name": name]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["UUID"] = UUID
|
||||
container["name"] = name
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,13 +76,7 @@ class RecordPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM pets WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,13 +103,7 @@ class RecordPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM pets WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,13 +145,7 @@ class RecordPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.update(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM pets WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,13 +188,7 @@ class RecordPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM pets WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,13 +202,7 @@ class RecordPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.save(db) // Actual update
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM pets WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,13 +215,7 @@ class RecordPrimaryKeySingleTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM pets WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,8 +37,9 @@ class Email : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["email": email, "label": label]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["email"] = email
|
||||
container["label"] = label
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,13 +77,7 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,13 +92,7 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,13 +106,7 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,13 +136,7 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
try record.update(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,13 +181,7 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,13 +194,7 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,13 +208,7 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
try record.save(db)
|
||||
|
||||
let row = try Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.persistentDictionary {
|
||||
if let dbValue: DatabaseValue = row.value(named: key) {
|
||||
XCTAssertEqual(dbValue, value?.databaseValue ?? .null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
assert(record, isEncodedIn: row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,13 +45,11 @@ private class Person : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [
|
||||
"id": id,
|
||||
"name": name,
|
||||
"age": age,
|
||||
"creationDate": creationDate,
|
||||
]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
container["creationDate"] = creationDate
|
||||
}
|
||||
|
||||
override func insert(_ db: Database) throws {
|
||||
|
||||
@@ -37,10 +37,11 @@ class BadlyMangledStuff : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
// User won't peek fancy column names because he will notice that the
|
||||
// generated INSERT query needs actual column names.
|
||||
return ["id": id, "name": name]
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -24,8 +24,10 @@ extension Reader : RowConvertible {
|
||||
extension Reader : MutablePersistable {
|
||||
static let databaseTableName = "readers"
|
||||
|
||||
var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "name": name, "age": age]
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
container["age"] = age
|
||||
}
|
||||
|
||||
mutating func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -117,8 +117,9 @@ private class Artist : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "name": name]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["name"] = name
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
@@ -160,8 +161,10 @@ private class Artwork : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "artistId": artistId, "title": title]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["id"] = id
|
||||
container["artistId"] = artistId
|
||||
container["title"] = title
|
||||
}
|
||||
|
||||
override func didInsert(with rowID: Int64, for column: String?) {
|
||||
|
||||
@@ -222,7 +222,16 @@ class Item : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["i0": i0, "i1": i1, "i2": i2, "i3": i3, "i4": i4, "i5": i5, "i6": i6, "i7": i7, "i8": i8, "i9": i9]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["i0"] = i0
|
||||
container["i1"] = i1
|
||||
container["i2"] = i2
|
||||
container["i3"] = i3
|
||||
container["i4"] = i4
|
||||
container["i5"] = i5
|
||||
container["i6"] = i6
|
||||
container["i7"] = i7
|
||||
container["i8"] = i8
|
||||
container["i9"] = i9
|
||||
}
|
||||
}
|
||||
@@ -64,8 +64,17 @@ class Item : Record {
|
||||
super.init(row: row)
|
||||
}
|
||||
|
||||
override var persistentDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["i0": i0, "i1": i1, "i2": i2, "i3": i3, "i4": i4, "i5": i5, "i6": i6, "i7": i7, "i8": i8, "i9": i9]
|
||||
override func encode(to container: inout PersistenceContainer) {
|
||||
container["i0"] = i0
|
||||
container["i1"] = i1
|
||||
container["i2"] = i2
|
||||
container["i3"] = i3
|
||||
container["i4"] = i4
|
||||
container["i5"] = i5
|
||||
container["i6"] = i6
|
||||
container["i7"] = i7
|
||||
container["i8"] = i8
|
||||
container["i9"] = i9
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user