diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 1ba87cc3b..9290c8961 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -1354,7 +1354,7 @@ 56D496681D813086008276D7 /* RowConvertible+QueryInterfaceRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56300B601C53C42C005A543B /* RowConvertible+QueryInterfaceRequestTests.swift */; }; 56D496691D813086008276D7 /* QueryInterfaceExpressionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56300B671C53D25E005A543B /* QueryInterfaceExpressionsTests.swift */; }; 56D4966A1D813086008276D7 /* TableMapping+QueryInterfaceRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56300B6A1C53D3E8005A543B /* TableMapping+QueryInterfaceRequestTests.swift */; }; - 56D4966B1D81309E008276D7 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; + 56D4966B1D81309E008276D7 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; 56D4966C1D81309E008276D7 /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238261B9C74A90082EB20 /* RecordMinimalPrimaryKeyRowIDTests.swift */; }; 56D4966D1D81309E008276D7 /* RecordMinimalPrimaryKeySingleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238271B9C74A90082EB20 /* RecordMinimalPrimaryKeySingleTests.swift */; }; 56D4966E1D81309E008276D7 /* RecordPrimaryKeyMultipleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238281B9C74A90082EB20 /* RecordPrimaryKeyMultipleTests.swift */; }; @@ -1500,13 +1500,13 @@ 56FEE8001F47253700D930EA /* TableMappingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FEE7FA1F47253700D930EA /* TableMappingTests.swift */; }; 56FEE8011F47253700D930EA /* TableMappingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FEE7FA1F47253700D930EA /* TableMappingTests.swift */; }; 56FEE8021F47253700D930EA /* TableMappingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FEE7FA1F47253700D930EA /* TableMappingTests.swift */; }; - 56FF45411D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; - 56FF45421D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; - 56FF45431D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; - 56FF45441D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; - 56FF45451D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; - 56FF45461D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; - 56FF45471D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */; }; + 56FF45411D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; + 56FF45421D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; + 56FF45431D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; + 56FF45441D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; + 56FF45451D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; + 56FF45461D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; + 56FF45471D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */; }; 56FF45571D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF45551D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift */; }; 56FF45581D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF45551D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift */; }; 56FF45591D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FF45551D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift */; }; @@ -2229,7 +2229,7 @@ 56FC98771D969DEF00E3C842 /* SQLExpression+QueryInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SQLExpression+QueryInterface.swift"; sourceTree = ""; }; 56FDECE11BB32DFD009AD709 /* RowFromStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowFromStatementTests.swift; sourceTree = ""; }; 56FEE7FA1F47253700D930EA /* TableMappingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableMappingTests.swift; sourceTree = ""; }; - 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableMappingDeleteByKeyTests.swift; sourceTree = ""; }; + 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MutablePersistableDeleteTests.swift; sourceTree = ""; }; 56FF45551D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RecordUniqueIndexTests.swift; sourceTree = ""; }; 6340BF7F1E5E3F7900832805 /* RecordPersistenceConflictPolicy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RecordPersistenceConflictPolicy.swift; sourceTree = ""; }; DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDB-Bridging.h"; sourceTree = ""; }; @@ -2474,6 +2474,7 @@ children = ( 5674A71C1F30A8DF0095F066 /* MutablePersistableEncodableTests.swift */, 56B021C81D8C0D3900B239BB /* MutablePersistablePersistenceConflictPolicyTests.swift */, + 56FF453F1D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift */, 563363A91C933FF8000BE133 /* MutablePersistableTests.swift */, 563363AA1C933FF8000BE133 /* PersistableTests.swift */, ); @@ -2535,7 +2536,6 @@ 56176C9C1EACEA8C000F3F2B /* TableMapping */ = { isa = PBXGroup; children = ( - 56FF453F1D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift */, 56FEE7FA1F47253700D930EA /* TableMappingTests.swift */, ); name = TableMapping; @@ -4046,7 +4046,7 @@ files = ( 560FC5611CB00B880014AA8E /* DatabaseValueConversionTests.swift in Sources */, 560FC5B31CB031EA0014AA8E /* DataMemoryTests.swift in Sources */, - 56FF45411D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56FF45411D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */, 560FC5621CB00B880014AA8E /* Record+QueryInterfaceRequestTests.swift in Sources */, 5698ACD81DA925420056AF8C /* RowTestCase.swift in Sources */, 56A8C2411D1918EE0096E9D4 /* FoundationNSUUIDTests.swift in Sources */, @@ -4294,7 +4294,7 @@ files = ( 5671561D1CB16729007DC145 /* DatabaseValueConversionTests.swift in Sources */, 5671561E1CB16729007DC145 /* DataMemoryTests.swift in Sources */, - 56FF45421D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56FF45421D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */, 5671561F1CB16729007DC145 /* Record+QueryInterfaceRequestTests.swift in Sources */, 5698ACD91DA925420056AF8C /* RowTestCase.swift in Sources */, 567156221CB16729007DC145 /* StatementColumnConvertibleFetchTests.swift in Sources */, @@ -4575,7 +4575,7 @@ 56176C711EACCCCA000F3F2B /* FTS5CustomTokenizerTests.swift in Sources */, 5672DE5D1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 56AFCA501CB1AA9900F48B96 /* RecordCopyTests.swift in Sources */, - 56FF45451D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56FF45451D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */, 56AFCA521CB1AA9900F48B96 /* PersistableTests.swift in Sources */, 56AFCA531CB1AA9900F48B96 /* DatabaseValueConversionTests.swift in Sources */, 5657AB531D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */, @@ -4715,7 +4715,7 @@ 56AFCAA91CB1ABC800F48B96 /* RecordCopyTests.swift in Sources */, 56A8C24B1D1918F10096E9D4 /* FoundationNSUUIDTests.swift in Sources */, 56AFCAAB1CB1ABC800F48B96 /* PersistableTests.swift in Sources */, - 56FF45461D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56FF45461D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */, 5698AC9C1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 56176C781EACCCCB000F3F2B /* FTS5PatternTests.swift in Sources */, 5674A70D1F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */, @@ -4888,7 +4888,7 @@ 56300B861C54DC95005A543B /* Record+QueryInterfaceRequestTests.swift in Sources */, 56F26C1C1CEE3F32007969C4 /* RowAdapterTests.swift in Sources */, 56DAA2D61DE99DAB006E10C8 /* DatabaseCursorTests.swift in Sources */, - 56FF45441D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56FF45441D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */, 56A238561B9C74A90082EB20 /* RecordPrimaryKeyNoneTests.swift in Sources */, 562EA8331F17B9EB00FA528C /* CompilationSubClassTests.swift in Sources */, 563363BE1C93FD5E000BE133 /* DatabaseQueueConcurrencyTests.swift in Sources */, @@ -5045,7 +5045,7 @@ 56D496B11D8133BC008276D7 /* DatabaseQueueReadOnlyTests.swift in Sources */, 56D4968C1D81316E008276D7 /* RawRepresentable+DatabaseValueConvertibleTests.swift in Sources */, 56D496981D81317B008276D7 /* DatabaseValueTests.swift in Sources */, - 56D4966B1D81309E008276D7 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56D4966B1D81309E008276D7 /* MutablePersistableDeleteTests.swift in Sources */, 56D4965B1D81304E008276D7 /* FoundationNSDecimalNumberTests.swift in Sources */, 562756431E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */, 56D496BF1D8135D4008276D7 /* TableDefinitionTests.swift in Sources */, @@ -5342,7 +5342,7 @@ files = ( F3BA80D71CFB300A003DC1BA /* DatabaseValueTests.swift in Sources */, F3BA80C81CFB2FD8003DC1BA /* DatabaseQueueTests.swift in Sources */, - 56FF45471D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56FF45471D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */, F3BA810C1CFB3056003DC1BA /* StatementArguments+FoundationTests.swift in Sources */, F3BA80B91CFB2FD1003DC1BA /* DatabasePoolBackupTests.swift in Sources */, F3BA811E1CFB3063003DC1BA /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */, @@ -5632,7 +5632,7 @@ 5674A70C1F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */, 5698AC8C1DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */, F3BA81391CFB3064003DC1BA /* RecordWithColumnNameManglingTests.swift in Sources */, - 56FF45431D2C23BA00F21EF9 /* TableMappingDeleteByKeyTests.swift in Sources */, + 56FF45431D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */, F3BA80F01CFB3017003DC1BA /* RowConvertibleTests.swift in Sources */, 5657AB491D108BA9006283EF /* FoundationNSNullTests.swift in Sources */, F3BA80E21CFB300F003DC1BA /* DatabaseValueConvertibleFetchTests.swift in Sources */, diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 858517d33..181035222 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -235,7 +235,7 @@ extension QueryInterfaceRequest { } } -extension QueryInterfaceRequest { +extension QueryInterfaceRequest where RowDecoder: MutablePersistable { // MARK: Deleting diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 6fa0dab2b..250888c35 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -1,3 +1,15 @@ +extension Database.ConflictResolution { + var invalidatesLastInsertedRowID: Bool { + switch self { + case .abort, .fail, .rollback, .replace: + return false + case .ignore: + // Statement may have succeeded without inserting any row + return true + } + } +} + // MARK: - PersistenceError /// An error thrown by a type that adopts Persistable. @@ -18,7 +30,6 @@ extension PersistenceError : CustomStringConvertible { } } - // MARK: - PersistenceContainer /// Use persistence containers in the `encode(to:)` method of your @@ -124,7 +135,6 @@ extension Row { } } - // MARK: - MutablePersistable /// The MutablePersistable protocol uses this type in order to handle SQLite @@ -203,7 +213,6 @@ public protocol MutablePersistable : TableMapping { /// - column: The name of the eventual INTEGER PRIMARY KEY column. mutating func didInsert(with rowID: Int64, for column: String?) - // MARK: - CRUD /// Executes an INSERT statement. @@ -304,7 +313,6 @@ extension MutablePersistable { public mutating func didInsert(with rowID: Int64, for column: String?) { } - // MARK: - CRUD /// Executes an INSERT statement. @@ -383,7 +391,6 @@ extension MutablePersistable { return try performExists(db) } - // MARK: - CRUD Internals /// Return true if record has a non-null primary key @@ -502,15 +509,97 @@ extension MutablePersistable { } -extension Database.ConflictResolution { - var invalidatesLastInsertedRowID: Bool { - switch self { - case .abort, .fail, .rollback, .replace: - return false - case .ignore: - // Statement may have succeeded without inserting any row - return true +extension MutablePersistable { + + // MARK: - Deleting All + + /// Deletes all records; returns the number of deleted rows. + /// + /// - parameter db: A database connection. + /// - returns: The number of deleted rows + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @discardableResult + public static func deleteAll(_ db: Database) throws -> Int { + return try all().deleteAll(db) + } +} + +extension MutablePersistable { + + // MARK: - Deleting by Single-Column Primary Key + + /// Delete records identified by their primary keys; returns the number of + /// deleted rows. + /// + /// try Person.deleteAll(db, keys: [1, 2, 3]) + /// + /// - parameters: + /// - db: A database connection. + /// - keys: A sequence of primary keys. + /// - returns: The number of deleted rows + @discardableResult + public static func deleteAll(_ db: Database, keys: Sequence) throws -> Int where Sequence.Iterator.Element: DatabaseValueConvertible { + let keys = Array(keys) + if keys.isEmpty { + // Avoid hitting the database + return 0 } + return try filter(db, keys: keys).deleteAll(db) + } + + /// Delete a record, identified by its primary key; returns whether a + /// database row was deleted. + /// + /// try Person.deleteOne(db, key: 123) + /// + /// - parameters: + /// - db: A database connection. + /// - key: A primary key value. + /// - returns: Whether a database row was deleted. + @discardableResult + public static func deleteOne(_ db: Database, key: PrimaryKeyType?) throws -> Bool { + guard let key = key else { + // Avoid hitting the database + return false + } + return try deleteAll(db, keys: [key]) > 0 + } +} + +extension MutablePersistable { + + // MARK: - Deleting by Key + + /// Delete records identified by the provided unique keys (primary key or + /// any key with a unique index on it); returns the number of deleted rows. + /// + /// try Person.deleteAll(db, keys: [["email": "a@example.com"], ["email": "b@example.com"]]) + /// + /// - parameters: + /// - db: A database connection. + /// - keys: An array of key dictionaries. + /// - returns: The number of deleted rows + @discardableResult + public static func deleteAll(_ db: Database, keys: [[String: DatabaseValueConvertible?]]) throws -> Int { + if keys.isEmpty { + // Avoid hitting the database + return 0 + } + return try filter(db, keys: keys).deleteAll(db) + } + + /// Delete a record, identified by a unique key (the primary key or any key + /// with a unique index on it); returns whether a database row was deleted. + /// + /// Person.deleteOne(db, key: ["name": Arthur"]) + /// + /// - parameters: + /// - db: A database connection. + /// - key: A dictionary of values. + /// - returns: Whether a database row was deleted. + @discardableResult + public static func deleteOne(_ db: Database, key: [String: DatabaseValueConvertible?]) throws -> Bool { + return try deleteAll(db, keys: [key]) > 0 } } diff --git a/GRDB/Record/TableMapping.swift b/GRDB/Record/TableMapping.swift index 506b18373..d1ae0c159 100644 --- a/GRDB/Record/TableMapping.swift +++ b/GRDB/Record/TableMapping.swift @@ -130,98 +130,3 @@ extension TableMapping { return filter(predicate) } } - -extension TableMapping { - - // MARK: Deleting All - - /// Deletes all records; returns the number of deleted rows. - /// - /// - parameter db: A database connection. - /// - returns: The number of deleted rows - /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @discardableResult - public static func deleteAll(_ db: Database) throws -> Int { - return try all().deleteAll(db) - } -} - -extension TableMapping { - - // MARK: Deleting by Single-Column Primary Key - - /// Delete records identified by their primary keys; returns the number of - /// deleted rows. - /// - /// try Person.deleteAll(db, keys: [1, 2, 3]) - /// - /// - parameters: - /// - db: A database connection. - /// - keys: A sequence of primary keys. - /// - returns: The number of deleted rows - @discardableResult - public static func deleteAll(_ db: Database, keys: Sequence) throws -> Int where Sequence.Iterator.Element: DatabaseValueConvertible { - let keys = Array(keys) - if keys.isEmpty { - // Avoid hitting the database - return 0 - } - return try filter(db, keys: keys).deleteAll(db) - } - - /// Delete a record, identified by its primary key; returns whether a - /// database row was deleted. - /// - /// try Person.deleteOne(db, key: 123) - /// - /// - parameters: - /// - db: A database connection. - /// - key: A primary key value. - /// - returns: Whether a database row was deleted. - @discardableResult - public static func deleteOne(_ db: Database, key: PrimaryKeyType?) throws -> Bool { - guard let key = key else { - // Avoid hitting the database - return false - } - return try deleteAll(db, keys: [key]) > 0 - } -} - -extension TableMapping { - - // MARK: Deleting by Key - - /// Delete records identified by the provided unique keys (primary key or - /// any key with a unique index on it); returns the number of deleted rows. - /// - /// try Person.deleteAll(db, keys: [["email": "a@example.com"], ["email": "b@example.com"]]) - /// - /// - parameters: - /// - db: A database connection. - /// - keys: An array of key dictionaries. - /// - returns: The number of deleted rows - @discardableResult - public static func deleteAll(_ db: Database, keys: [[String: DatabaseValueConvertible?]]) throws -> Int { - if keys.isEmpty { - // Avoid hitting the database - return 0 - } - return try filter(db, keys: keys).deleteAll(db) - } - - /// Delete a record, identified by a unique key (the primary key or any key - /// with a unique index on it); returns whether a database row was deleted. - /// - /// Person.deleteOne(db, key: ["name": Arthur"]) - /// - /// - parameters: - /// - db: A database connection. - /// - key: A dictionary of values. - /// - returns: Whether a database row was deleted. - @discardableResult - public static func deleteOne(_ db: Database, key: [String: DatabaseValueConvertible?]) throws -> Bool { - return try deleteAll(db, keys: [key]) > 0 - } -} - diff --git a/TODO.md b/TODO.md index 211a3c42d..d06df8c95 100644 --- a/TODO.md +++ b/TODO.md @@ -16,10 +16,11 @@ GRDB 2.0 - [ ] Remove all writing powers from TableMapping: - - [ ] TableMapping.deleteAll(_:) -> move to MutablePersistable - - [ ] TableMapping.deleteAll(_:keys:) -> move to MutablePersistable - - [ ] TableMapping.deleteOne(_:key:) -> move to MutablePersistable - - [ ] QueryInterfaceRequest.deleteAll(_:) -> require MutablePersistable adoption + - [X] TableMapping.deleteAll(_:) -> move to MutablePersistable + - [X] TableMapping.deleteAll(_:keys:) -> move to MutablePersistable + - [X] TableMapping.deleteOne(_:key:) -> move to MutablePersistable + - [X] QueryInterfaceRequest.deleteAll(_:) -> require MutablePersistable adoption + - [ ] Documentation - [X] Let TableMapping define the selected columns, not only if rowID is selected or not. - [X] Have RowConvertible.fetchCursor(_:keys:) return a non-optional cursor diff --git a/Tests/GRDBTests/TableMappingDeleteByKeyTests.swift b/Tests/GRDBTests/MutablePersistableDeleteTests.swift similarity index 83% rename from Tests/GRDBTests/TableMappingDeleteByKeyTests.swift rename to Tests/GRDBTests/MutablePersistableDeleteTests.swift index d3afff6fb..ed275bac2 100644 --- a/Tests/GRDBTests/TableMappingDeleteByKeyTests.swift +++ b/Tests/GRDBTests/MutablePersistableDeleteTests.swift @@ -7,20 +7,23 @@ import XCTest import GRDB #endif -private struct Hacker : TableMapping { +private struct Hacker : MutablePersistable { static let databaseTableName = "hackers" + func encode(to container: inout PersistenceContainer) { preconditionFailure("should not be called") } } -private struct Person : TableMapping { +private struct Person : MutablePersistable { static let databaseTableName = "persons" + func encode(to container: inout PersistenceContainer) { preconditionFailure("should not be called") } } -private struct Citizenship : TableMapping { +private struct Citizenship : MutablePersistable { static let databaseTableName = "citizenships" + func encode(to container: inout PersistenceContainer) { preconditionFailure("should not be called") } } -class TableMappingDeleteByKeyTests: GRDBTestCase { +class MutablePersistableDeleteTests: GRDBTestCase { override func setup(_ dbWriter: DatabaseWriter) throws { try dbWriter.write { db in @@ -136,4 +139,25 @@ class TableMappingDeleteByKeyTests: GRDBTestCase { XCTAssertEqual(try Person.fetchCount(db), 1) } } + + func testRequestDelete() throws { + let dbQueue = try makeDatabaseQueue() + + try dbQueue.inDatabase { db in + try Person.deleteAll(db) + XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\"") + + try Person.filter(Column("name") == "Arthur").deleteAll(db) + XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE (\"name\" = 'Arthur')") + + try Person.filter(sql: "id = 1").deleteAll(db) + XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE id = 1") + + try Person.select(Column("name")).deleteAll(db) + XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\"") + + try Person.order(Column("name")).deleteAll(db) + XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\"") + } + } } diff --git a/Tests/GRDBTests/QueryInterfaceRequestTests.swift b/Tests/GRDBTests/QueryInterfaceRequestTests.swift index 115a0afcc..b2f18fd74 100644 --- a/Tests/GRDBTests/QueryInterfaceRequestTests.swift +++ b/Tests/GRDBTests/QueryInterfaceRequestTests.swift @@ -477,31 +477,4 @@ class QueryInterfaceRequestTests: GRDBTestCase { sql(dbQueue, tableRequest.limit(1, offset: 2).limit(3)), "SELECT * FROM \"readers\" LIMIT 3") } - - - // MARK: - Delete - - func testDelete() throws { - let dbQueue = try makeDatabaseQueue() - - try dbQueue.inDatabase { db in - try tableRequest.deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\"") - - try tableRequest.filter(Col.age == 42).deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\" WHERE (\"age\" = 42)") - - try tableRequest.filter(sql: "id = 1").deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\" WHERE id = 1") - - try tableRequest.select(Col.name).deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\"") - - try tableRequest.distinct().deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\"") - - try tableRequest.order(Col.name).deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\"") - } - } } diff --git a/Tests/GRDBTests/TableMapping+QueryInterfaceRequestTests.swift b/Tests/GRDBTests/TableMapping+QueryInterfaceRequestTests.swift index e4b88f2f2..a3ac07434 100644 --- a/Tests/GRDBTests/TableMapping+QueryInterfaceRequestTests.swift +++ b/Tests/GRDBTests/TableMapping+QueryInterfaceRequestTests.swift @@ -266,28 +266,4 @@ class TableMappingQueryInterfaceRequestTests: GRDBTestCase { sql(dbQueue, Reader.limit(1, offset: 2).limit(3)), "SELECT * FROM \"readers\" LIMIT 3") } - - - // MARK: - Delete - - func testDelete() throws { - let dbQueue = try makeDatabaseQueue() - - try dbQueue.inDatabase { db in - try Reader.deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\"") - - try Reader.filter(Col.age == 42).deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\" WHERE (\"age\" = 42)") - - try Reader.filter(sql: "id = 1").deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\" WHERE id = 1") - - try Reader.select(Col.name).deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\"") - - try Reader.order(Col.name).deleteAll(db) - XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"readers\"") - } - } }