diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index d7d7663ec..5d59a9e2c 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -1017,10 +1017,19 @@ extension QueryInterfaceRequest { /// } public struct ColumnAssignment { var columnName: String - var value: SQLExpression - func sql(_ context: SQLGenerationContext) throws -> String { - try Column(columnName).sqlExpression.sql(context) + " = " + value.sql(context) + /// If nil, this is a "don't assign" assignment. + var value: SQLExpression? + + init(columnName: String, value: SQLExpression? = nil) { + self.columnName = columnName + self.value = value + } + + /// If nil, there's nothing to assign to. + func sql(_ context: SQLGenerationContext) throws -> String? { + guard let value else { return nil } + return try Column(columnName).sqlExpression.sql(context) + " = " + value.sql(context) } } @@ -1039,6 +1048,11 @@ extension ColumnExpression { public func set(to value: (any SQLExpressible)?) -> ColumnAssignment { ColumnAssignment(columnName: name, value: value?.sqlExpression ?? .null) } + + /// Returns an assignment that does not modify this column. + public var noOverwrite: ColumnAssignment { + ColumnAssignment(columnName: name, value: nil) + } } /// Creates an assignment that adds a value diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 1455793e8..9d393571b 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -288,12 +288,6 @@ struct SQLQueryGenerator: Refinable { selection: selection) } - // Check for empty assignments after all programmer errors have - // been checked. - if assignments.isEmpty { - return nil - } - let context = SQLGenerationContext(db, aliases: relation.allAliases, ctes: relation.ctes) var sql = try commonTableExpressionsPrefix(context) @@ -305,10 +299,13 @@ struct SQLQueryGenerator: Refinable { sql += try relation.source.sql(context) - sql += " SET " - sql += try assignments - .map { try $0.sql(context) } + let updateSQL = try assignments + .compactMap { try $0.sql(context) } .joined(separator: ", ") + if updateSQL.isEmpty { + return nil + } + sql += " SET \(updateSQL)" if let filter = try relation.filterPromise?.resolve(db) { sql += " WHERE " @@ -351,12 +348,6 @@ struct SQLQueryGenerator: Refinable { selection: [any SQLSelectable]) throws -> Statement? { - // Check for empty assignments after all programmer errors have - // been checked. - if assignments.isEmpty { - return nil - } - let tableName = relation.source.tableName let alias = TableAlias(tableName: tableName) let context = SQLGenerationContext(db, aliases: [alias]) @@ -374,10 +365,13 @@ struct SQLQueryGenerator: Refinable { sql += tableName.quotedDatabaseIdentifier // SET column = value... - sql += " SET " - sql += try assignments - .map { try $0.sql(context) } + let updateSQL = try assignments + .compactMap { try $0.sql(context) } .joined(separator: ", ") + if updateSQL.isEmpty { + return nil + } + sql += " SET \(updateSQL)" // WHERE id IN (SELECT id FROM ...) sql += " WHERE "