mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Improve RowModel errors so that they help the developer during the design of his RowModel subclass.
This commit is contained in:
1 parent
50ce41bc89
commit
4d5491e0c7
4 files changed
+155
-34
No files matched your search
@@ -19,6 +19,41 @@
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
567AA8A41B4E3F120082C3E4 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */;
|
||||
proxyType = 2;
|
||||
remoteGlobalIDString = DC3773F319C8CBB3004FCF85;
|
||||
remoteInfo = GRDBOSX;
|
||||
};
|
||||
567AA8A61B4E3F120082C3E4 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */;
|
||||
proxyType = 2;
|
||||
remoteGlobalIDString = 56E5D7F91B4D422D00430942;
|
||||
remoteInfo = GRDBOSXTests;
|
||||
};
|
||||
567AA8A81B4E3F120082C3E4 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */;
|
||||
proxyType = 2;
|
||||
remoteGlobalIDString = 56E5D7CA1B4D3FED00430942;
|
||||
remoteInfo = GRDBiOS;
|
||||
};
|
||||
567AA8AA1B4E3F120082C3E4 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */;
|
||||
proxyType = 2;
|
||||
remoteGlobalIDString = 56E5D7D31B4D3FEE00430942;
|
||||
remoteInfo = GRDBiOSTests;
|
||||
};
|
||||
567AA8AC1B4E41CF0082C3E4 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */;
|
||||
proxyType = 1;
|
||||
remoteGlobalIDString = 56E5D7C91B4D3FED00430942;
|
||||
remoteInfo = GRDBiOS;
|
||||
};
|
||||
56B8F46B1B4E22B200C24296 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 56B8F44C1B4E22B200C24296 /* Project object */;
|
||||
@@ -79,6 +114,17 @@
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
567AA89E1B4E3F120082C3E4 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
567AA8A51B4E3F120082C3E4 /* GRDB.framework */,
|
||||
567AA8A71B4E3F120082C3E4 /* GRDBTests.xctest */,
|
||||
567AA8A91B4E3F120082C3E4 /* GRDB.framework */,
|
||||
567AA8AB1B4E3F120082C3E4 /* GRDBTests.xctest */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
56B8F44B1B4E22B200C24296 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -147,6 +193,7 @@
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
567AA8AD1B4E41CF0082C3E4 /* PBXTargetDependency */,
|
||||
);
|
||||
name = GRDBDemoiOS;
|
||||
productName = GRDBDemoiOS;
|
||||
@@ -222,6 +269,12 @@
|
||||
mainGroup = 56B8F44B1B4E22B200C24296;
|
||||
productRefGroup = 56B8F4551B4E22B200C24296 /* Products */;
|
||||
projectDirPath = "";
|
||||
projectReferences = (
|
||||
{
|
||||
ProductGroup = 567AA89E1B4E3F120082C3E4 /* Products */;
|
||||
ProjectRef = 56B8F4871B4E235C00C24296 /* GRDB.xcodeproj */;
|
||||
},
|
||||
);
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
56B8F4531B4E22B200C24296 /* GRDBDemoiOS */,
|
||||
@@ -231,6 +284,37 @@
|
||||
};
|
||||
/* End PBXProject section */
|
||||
|
||||
/* Begin PBXReferenceProxy section */
|
||||
567AA8A51B4E3F120082C3E4 /* GRDB.framework */ = {
|
||||
isa = PBXReferenceProxy;
|
||||
fileType = wrapper.framework;
|
||||
path = GRDB.framework;
|
||||
remoteRef = 567AA8A41B4E3F120082C3E4 /* PBXContainerItemProxy */;
|
||||
sourceTree = BUILT_PRODUCTS_DIR;
|
||||
};
|
||||
567AA8A71B4E3F120082C3E4 /* GRDBTests.xctest */ = {
|
||||
isa = PBXReferenceProxy;
|
||||
fileType = wrapper.cfbundle;
|
||||
path = GRDBTests.xctest;
|
||||
remoteRef = 567AA8A61B4E3F120082C3E4 /* PBXContainerItemProxy */;
|
||||
sourceTree = BUILT_PRODUCTS_DIR;
|
||||
};
|
||||
567AA8A91B4E3F120082C3E4 /* GRDB.framework */ = {
|
||||
isa = PBXReferenceProxy;
|
||||
fileType = wrapper.framework;
|
||||
path = GRDB.framework;
|
||||
remoteRef = 567AA8A81B4E3F120082C3E4 /* PBXContainerItemProxy */;
|
||||
sourceTree = BUILT_PRODUCTS_DIR;
|
||||
};
|
||||
567AA8AB1B4E3F120082C3E4 /* GRDBTests.xctest */ = {
|
||||
isa = PBXReferenceProxy;
|
||||
fileType = wrapper.cfbundle;
|
||||
path = GRDBTests.xctest;
|
||||
remoteRef = 567AA8AA1B4E3F120082C3E4 /* PBXContainerItemProxy */;
|
||||
sourceTree = BUILT_PRODUCTS_DIR;
|
||||
};
|
||||
/* End PBXReferenceProxy section */
|
||||
|
||||
/* Begin PBXResourcesBuildPhase section */
|
||||
56B8F4521B4E22B200C24296 /* Resources */ = {
|
||||
isa = PBXResourcesBuildPhase;
|
||||
@@ -289,6 +373,11 @@
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin PBXTargetDependency section */
|
||||
567AA8AD1B4E41CF0082C3E4 /* PBXTargetDependency */ = {
|
||||
isa = PBXTargetDependency;
|
||||
name = GRDBiOS;
|
||||
targetProxy = 567AA8AC1B4E41CF0082C3E4 /* PBXContainerItemProxy */;
|
||||
};
|
||||
56B8F46C1B4E22B200C24296 /* PBXTargetDependency */ = {
|
||||
isa = PBXTargetDependency;
|
||||
target = 56B8F4531B4E22B200C24296 /* GRDBDemoiOS */;
|
||||
|
||||
+56
-28
@@ -154,7 +154,7 @@ public class RowModel {
|
||||
if let row = try Version(self).fetchOneRow(db) {
|
||||
updateWithRow(row)
|
||||
} else {
|
||||
throw RowModelError.RowModelNotFound
|
||||
throw RowModelError.RowModelNotFound(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,13 +220,18 @@ public class RowModel {
|
||||
/// Returns an optional (rowIDColumn, insertedRowID) if and only if the
|
||||
/// row model should be updated.
|
||||
func insert(db: Database, conflictResolution: ConflictResolution?) throws -> (String, Int64)? {
|
||||
// Check class requirement before instance requirements.
|
||||
// Fail early if databaseTable is nil (not overriden)
|
||||
guard let table = databaseTable else {
|
||||
throw RowModelError.UnspecifiedTable
|
||||
throw RowModelError.UnspecifiedTable(rowModel.dynamicType)
|
||||
}
|
||||
|
||||
// We need something to insert
|
||||
let insertedDic = storedDatabaseDictionary
|
||||
guard insertedDic.count > 0 else {
|
||||
throw RowModelError.InvalidDatabaseDictionary(rowModel)
|
||||
}
|
||||
|
||||
// INSERT INTO table (id, name) VALUES (:id, :name)
|
||||
let insertedDic = storedDatabaseDictionary
|
||||
let columnNames = insertedDic.keys
|
||||
let columnSQL = ",".join(columnNames.map { $0.quotedDatabaseIdentifier })
|
||||
let valuesSQL = ",".join([String](count: columnNames.count, repeatedValue: "?"))
|
||||
@@ -271,14 +276,19 @@ public class RowModel {
|
||||
}
|
||||
|
||||
func update(db: Database, conflictResolution: ConflictResolution? = nil) throws {
|
||||
// Check class requirement before instance requirements.
|
||||
// Fail early if databaseTable is nil (not overriden)
|
||||
guard let table = databaseTable else {
|
||||
throw RowModelError.UnspecifiedTable
|
||||
throw RowModelError.UnspecifiedTable(rowModel.dynamicType)
|
||||
}
|
||||
|
||||
// Fail early if storedDatabaseDictionary is empty (not overriden)
|
||||
guard storedDatabaseDictionary.count > 0 else {
|
||||
throw RowModelError.InvalidDatabaseDictionary(rowModel)
|
||||
}
|
||||
|
||||
// Update requires strongPrimaryKeyDictionary
|
||||
guard let primaryKeyDictionary = strongPrimaryKeyDictionary else {
|
||||
throw RowModelError.InvalidPrimaryKey
|
||||
throw RowModelError.InvalidPrimaryKey(rowModel)
|
||||
}
|
||||
|
||||
// Don't update primary key columns
|
||||
@@ -287,6 +297,11 @@ public class RowModel {
|
||||
updatedDictionary.removeValueForKey(column)
|
||||
}
|
||||
|
||||
// We need something to update
|
||||
guard updatedDictionary.count > 0 else {
|
||||
throw RowModelError.InvalidDatabaseDictionary(rowModel)
|
||||
}
|
||||
|
||||
// "UPDATE table SET name = ? WHERE id = ?"
|
||||
let updateSQL = ",".join(updatedDictionary.keys.map { column in "\(column.quotedDatabaseIdentifier)=?" })
|
||||
let whereSQL = " AND ".join(primaryKeyDictionary.keys.map { column in "\(column.quotedDatabaseIdentifier)=?" })
|
||||
@@ -313,7 +328,7 @@ public class RowModel {
|
||||
|
||||
// Check is some row was actually changed
|
||||
if changedRowCount == 0 {
|
||||
throw RowModelError.RowModelNotFound
|
||||
throw RowModelError.RowModelNotFound(rowModel)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,14 +342,19 @@ public class RowModel {
|
||||
}
|
||||
|
||||
func delete(db: Database) throws {
|
||||
// Check class requirement before instance requirements.
|
||||
// Fail early if databaseTable is nil (not overriden)
|
||||
guard let table = databaseTable else {
|
||||
throw RowModelError.UnspecifiedTable
|
||||
throw RowModelError.UnspecifiedTable(rowModel.dynamicType)
|
||||
}
|
||||
|
||||
// Fail early if storedDatabaseDictionary is empty (not overriden)
|
||||
guard storedDatabaseDictionary.count > 0 else {
|
||||
throw RowModelError.InvalidDatabaseDictionary(rowModel)
|
||||
}
|
||||
|
||||
// Delete requires strongPrimaryKeyDictionary
|
||||
guard let primaryKeyDictionary = strongPrimaryKeyDictionary else {
|
||||
throw RowModelError.InvalidPrimaryKey
|
||||
throw RowModelError.InvalidPrimaryKey(rowModel)
|
||||
}
|
||||
|
||||
// "DELETE FROM table WHERE id = ?"
|
||||
@@ -345,14 +365,19 @@ public class RowModel {
|
||||
}
|
||||
|
||||
func fetchOneRow(db: Database) throws -> Row? {
|
||||
// Check class requirement before instance requirements.
|
||||
// Fail early if databaseTable is nil (not overriden)
|
||||
guard databaseTable != nil else {
|
||||
throw RowModelError.UnspecifiedTable
|
||||
throw RowModelError.UnspecifiedTable(rowModel.dynamicType)
|
||||
}
|
||||
|
||||
// Fail early if storedDatabaseDictionary is empty (not overriden)
|
||||
guard storedDatabaseDictionary.count > 0 else {
|
||||
throw RowModelError.InvalidDatabaseDictionary(rowModel)
|
||||
}
|
||||
|
||||
// fetchOneRow requires strongPrimaryKeyDictionary
|
||||
guard let primaryKeyDictionary = strongPrimaryKeyDictionary else {
|
||||
throw RowModelError.InvalidPrimaryKey
|
||||
throw RowModelError.InvalidPrimaryKey(rowModel)
|
||||
}
|
||||
|
||||
// Fetch
|
||||
@@ -387,28 +412,31 @@ extension RowModel : CustomStringConvertible {
|
||||
|
||||
/// A RowModel-specific error
|
||||
public enum RowModelError: ErrorType {
|
||||
/// RowModel.databaseTable is not defined
|
||||
case UnspecifiedTable
|
||||
/// RowModel.databaseTable returns nil
|
||||
case UnspecifiedTable(RowModel.Type)
|
||||
|
||||
/// RowModel could not be identified: either the databaseTableName class
|
||||
/// method return .None, or the RowModel has nil primary key.
|
||||
case InvalidPrimaryKey
|
||||
/// RowModel.databaseDictionary returns an invalid dictionary.
|
||||
case InvalidDatabaseDictionary(RowModel)
|
||||
|
||||
/// Failed update or reload because the RowModel could not be found in the
|
||||
/// database.
|
||||
case RowModelNotFound
|
||||
/// Primary key does not uniquely identifies a database row.
|
||||
case InvalidPrimaryKey(RowModel)
|
||||
|
||||
/// No matching row could be found in the database.
|
||||
case RowModelNotFound(RowModel)
|
||||
}
|
||||
|
||||
extension RowModelError : CustomStringConvertible {
|
||||
/// A textual representation of `self`.
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .UnspecifiedTable:
|
||||
return "Unspecified Table"
|
||||
case .InvalidPrimaryKey:
|
||||
return "Invalid primary key"
|
||||
case .RowModelNotFound:
|
||||
return "RowModel not found"
|
||||
case .UnspecifiedTable(let type):
|
||||
return "Nil Table returned from \(type).databaseTable"
|
||||
case .InvalidDatabaseDictionary(let rowModel):
|
||||
return "Invalid database dictionary returned from \(rowModel.dynamicType).storedDatabaseDictionary"
|
||||
case .InvalidPrimaryKey(let rowModel):
|
||||
return "Invalid primary key in \(rowModel)"
|
||||
case .RowModelNotFound(let rowModel):
|
||||
return "RowModel not found: \(rowModel)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -749,7 +749,9 @@ Other primary keys (single or multiple columns) are not managed by GRDB: you hav
|
||||
|
||||
RowModel methods can throw [DatabaseError](#error-handling) and also specific errors of type **RowModelError**:
|
||||
|
||||
- **RowModelError.UnspecifiedTable**: thrown by `insert`, `update`, `delete` and `reload` when the databaseTable class method returns nil.
|
||||
- **RowModelError.UnspecifiedTable**: thrown by `insert`, `update`, `delete` and `reload` when the databaseTable class method returns nil (typically when it has not been overriden yet).
|
||||
|
||||
- **RowModelError.InvalidDatabaseDictionary**: thrown by `ìnsert`, `update`, `delete` and `reload` when the storedDatabaseDictionary does not contain enough values (typically when it has not been overriden yet).
|
||||
|
||||
- **RowModelError.InvalidPrimaryKey**: thrown by `update`, `delete` and `reload` when the primary key is nil.
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
- [ ] RowModel support for implicit RowID column (see https://www.sqlite.org/autoinc.html)
|
||||
- [ ] Fetch with a collection of primary keys: db.fetch(RowModel.type, primaryKeys:[...]). Problem: users will often want to sort, so the plain SELECT * FROM persons WHERE id IN (?,?,?) won't be appreciated. Needs more thought.
|
||||
- [ ] Let Bindings eat arrays, or make it easy to generate series of ?,?,?,?
|
||||
- [ ] Pierlo complains that databaseDictionary returns a dictionary of values when updateFromDatabaseRow eats a Row. See if we can do something about that. Experiment with a setDatabaseValueForKey API.
|
||||
- [ ] CocoaPod
|
||||
- [ ] Carthage
|
||||
- [ ] When one calls modelRow.insert(db), and has defined the table, but forgets to override storedDatabaseDictionary, he gets "SQLite error 1 with statement `INSERT INTO "persons" () VALUES ()`: near ")": syntax error" which does not tell him to override storedDatabaseDictionary.
|
||||
- [ ] When one forgets to set the primaryKey in setDatabaseValue(_:column:), he has issues later deleting or updating. It's difficult to know why the delete fails.
|
||||
- [X] Pierlo complains that databaseDictionary returns a dictionary of values when updateFromDatabaseRow eats a Row. See if we can do something about that. Experiment with a setDatabaseValueForKey API.
|
||||
- [X] CocoaPod
|
||||
- [X] Carthage
|
||||
- [X] When one calls modelRow.insert(db), and has defined the table, but forgets to override storedDatabaseDictionary, he gets "SQLite error 1 with statement `INSERT INTO "persons" () VALUES ()`: near ")": syntax error" which does not tell him to override storedDatabaseDictionary.
|
||||
- [ ] Test for the item above
|
||||
- [X] When one forgets to set the primaryKey in setDatabaseValue(_:column:), he has issues later deleting or updating. It's difficult to know why the delete fails.
|
||||
- [ ] Test for the item above
|
||||
- [ ] Beta 3: remove strings behind enum cases: "If an element of an enum with string raw type does not have an explicit raw value, it will default to the text of the enum’s name".
|
||||
- [ ] Beta 3: leading dot is now possible: foo\n.bar\n.baz
|
||||
- [ ] Beta 3: "Errors trapped by a try! expression, or unhandled at the top level of a script or playground, now display the error value in the trap message"
|
||||
Reference in new issue
Block a user