mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
CTEs can enter UPDATE and DELETE queries
This commit is contained in:
1 parent
b636c391df
commit
c36539adb3
2 files changed
+67
-27
No files matched your search
@@ -66,29 +66,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
// SQL aliases.
|
||||
let context = SQLGenerationContext(parent: context, aliases: relation.allAliases)
|
||||
|
||||
var sql = ""
|
||||
|
||||
if !ctes.isEmpty {
|
||||
sql += "WITH "
|
||||
if ctes.values.contains(where: \.isRecursive) {
|
||||
sql += "RECURSIVE "
|
||||
}
|
||||
sql += try ctes
|
||||
.map { tableName, cte in
|
||||
var columnsSQL = ""
|
||||
if let columns = cte.columns, !columns.isEmpty {
|
||||
columnsSQL = "("
|
||||
+ columns.map(\.quotedDatabaseIdentifier).joined(separator: ", ")
|
||||
+ ")"
|
||||
}
|
||||
let cteContext = SQLGenerationContext(parent: context)
|
||||
let requestSQL = try cte.request.requestSQL(cteContext, forSingleResult: false)
|
||||
return "\(tableName.quotedDatabaseIdentifier)\(columnsSQL) AS (\(requestSQL))"
|
||||
}
|
||||
.joined(separator: ", ")
|
||||
sql += " "
|
||||
}
|
||||
|
||||
var sql = try commonTableExpressionsPrefix(context)
|
||||
sql += "SELECT"
|
||||
|
||||
if isDistinct {
|
||||
@@ -256,13 +234,14 @@ struct SQLQueryGenerator: Refinable {
|
||||
func makeDeleteStatement(_ db: Database) throws -> UpdateStatement {
|
||||
switch try grouping(db) {
|
||||
case .none:
|
||||
guard relation.joins.isEmpty && ctes.isEmpty else {
|
||||
guard relation.joins.isEmpty else {
|
||||
return try makeTrivialDeleteStatement(db)
|
||||
}
|
||||
|
||||
let context = SQLGenerationContext(db, aliases: relation.allAliases)
|
||||
|
||||
var sql = try "DELETE FROM " + relation.source.sql(context)
|
||||
var sql = try commonTableExpressionsPrefix(context)
|
||||
sql += try "DELETE FROM " + relation.source.sql(context)
|
||||
|
||||
let filters = try relation.filtersPromise.resolve(db)
|
||||
if filters.isEmpty == false {
|
||||
@@ -321,7 +300,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
{
|
||||
switch try grouping(db) {
|
||||
case .none:
|
||||
guard relation.joins.isEmpty && ctes.isEmpty else {
|
||||
guard relation.joins.isEmpty else {
|
||||
return try makeTrivialUpdateStatement(
|
||||
db,
|
||||
conflictResolution: conflictResolution,
|
||||
@@ -336,7 +315,8 @@ struct SQLQueryGenerator: Refinable {
|
||||
|
||||
let context = SQLGenerationContext(db, aliases: relation.allAliases)
|
||||
|
||||
var sql = "UPDATE "
|
||||
var sql = try commonTableExpressionsPrefix(context)
|
||||
sql += "UPDATE "
|
||||
|
||||
if conflictResolution != .abort {
|
||||
sql += "OR \(conflictResolution.rawValue) "
|
||||
@@ -423,6 +403,32 @@ struct SQLQueryGenerator: Refinable {
|
||||
return statement
|
||||
}
|
||||
|
||||
private func commonTableExpressionsPrefix(_ context: SQLGenerationContext) throws -> String {
|
||||
if ctes.isEmpty {
|
||||
return ""
|
||||
}
|
||||
|
||||
var sql = "WITH "
|
||||
if ctes.values.contains(where: \.isRecursive) {
|
||||
sql += "RECURSIVE "
|
||||
}
|
||||
sql += try ctes
|
||||
.map { tableName, cte in
|
||||
var columnsSQL = ""
|
||||
if let columns = cte.columns, !columns.isEmpty {
|
||||
columnsSQL = "("
|
||||
+ columns.map(\.quotedDatabaseIdentifier).joined(separator: ", ")
|
||||
+ ")"
|
||||
}
|
||||
let cteContext = SQLGenerationContext(parent: context)
|
||||
let requestSQL = try cte.request.requestSQL(cteContext, forSingleResult: false)
|
||||
return "\(tableName.quotedDatabaseIdentifier)\(columnsSQL) AS (\(requestSQL))"
|
||||
}
|
||||
.joined(separator: ", ")
|
||||
sql += " "
|
||||
return sql
|
||||
}
|
||||
|
||||
/// Informs about the query grouping
|
||||
private enum GroupingInfo {
|
||||
/// No grouping at all: SELECT ... FROM player
|
||||
|
||||
@@ -545,4 +545,38 @@ class CommonTableExpressionTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testUpdate() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "t") { t in
|
||||
t.column("a")
|
||||
}
|
||||
|
||||
struct T: Encodable, PersistableRecord { }
|
||||
let cte = CommonTableExpression<Void>(named: "cte", sql: "SELECT 1")
|
||||
|
||||
try T.with(cte).updateAll(db, Column("a").set(to: cte.all()))
|
||||
XCTAssertEqual(lastSQLQuery, """
|
||||
WITH "cte" AS (SELECT 1) UPDATE "t" SET "a" = (SELECT * FROM "cte")
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
func testDelete() throws {
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.create(table: "t") { t in
|
||||
t.column("a")
|
||||
}
|
||||
|
||||
struct T: Encodable, PersistableRecord { }
|
||||
let cte = CommonTableExpression<Void>(named: "cte", sql: "SELECT 1")
|
||||
|
||||
try T.with(cte)
|
||||
.filter(Column("a") == cte.all())
|
||||
.deleteAll(db)
|
||||
XCTAssertEqual(lastSQLQuery, """
|
||||
WITH "cte" AS (SELECT 1) DELETE FROM "t" WHERE "a" = (SELECT * FROM "cte")
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user