mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
QueryInterfaceRequest.deleteAll (for @pakko972)
This commit is contained in:
1 parent
238ebdeed8
commit
e4ae0988a4
4 files changed
+74
No files matched your search
@@ -233,6 +233,19 @@ extension QueryInterfaceRequest {
|
||||
}
|
||||
|
||||
|
||||
extension QueryInterfaceRequest {
|
||||
|
||||
// MARK: Deleting
|
||||
|
||||
/// Returns the number of rows matched by the request.
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
public func deleteAll(db: Database) throws {
|
||||
try query.makeDeleteStatement(db).execute()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension QueryInterfaceRequest {
|
||||
|
||||
// MARK: QueryInterfaceRequest as subquery
|
||||
|
||||
@@ -177,6 +177,35 @@ public struct _SQLSelectQuery {
|
||||
}
|
||||
}
|
||||
|
||||
func makeDeleteStatement(db: Database) -> UpdateStatement {
|
||||
guard groupByExpressions.isEmpty else {
|
||||
fatalError("Can't delete query with GROUP BY expression")
|
||||
}
|
||||
|
||||
guard havingExpression == nil else {
|
||||
fatalError("Can't delete query with GROUP BY expression")
|
||||
}
|
||||
|
||||
guard limit == nil else {
|
||||
fatalError("Can't delete query with limit")
|
||||
}
|
||||
|
||||
var sql = "DELETE"
|
||||
var arguments: StatementArguments? = StatementArguments()
|
||||
|
||||
if let source = source {
|
||||
sql += " FROM " + source.sql(&arguments)
|
||||
}
|
||||
|
||||
if let whereExpression = whereExpression {
|
||||
sql += " WHERE " + whereExpression.sql(&arguments)
|
||||
}
|
||||
|
||||
let statement = try! db.updateStatement(sql)
|
||||
statement.arguments = arguments!
|
||||
return statement
|
||||
}
|
||||
|
||||
// SELECT COUNT(*) FROM (self)
|
||||
private var trivialCountQuery: _SQLSelectQuery {
|
||||
return _SQLSelectQuery(
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
- [ ] Fix slow compilation time of SQLTableBuilderTests.swift
|
||||
- [ ] @pakko972: try Record.filter(...).deleteAll(db)
|
||||
- [X] feature
|
||||
- [ ] documentation
|
||||
- [ ] @hdlj: Extensibility of the Query Interface
|
||||
- [ ] @hdlj: FetchedRecordsController throttling
|
||||
- [ ] @hdlj: Check RAM pressure
|
||||
|
||||
@@ -485,4 +485,33 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
sql(dbQueue, tableRequest.limit(1, offset: 2).limit(3)),
|
||||
"SELECT * FROM \"readers\" LIMIT 3")
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Delete
|
||||
|
||||
func testDelete() {
|
||||
assertNoError {
|
||||
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\"")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user