diff --git a/CHANGELOG.md b/CHANGELOG.md index 14cd76cef..9198d105e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,13 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: **New** +- Batch updates now accept nil assignments: + + ```swift + // UPDATE player SET score = NULL + try Player.updateAll(db, scoreColumn <- nil) + ``` + - DatabaseMigrator querying methods have been enhanced: ```swift diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index f0cc36f57..1bb0fcd03 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -644,20 +644,32 @@ infix operator <- : ColumnAssignment /// } public struct ColumnAssignment { var column: ColumnExpression - var value: SQLExpressible + var value: SQLExpressible? + + func sql(_ context: inout SQLGenerationContext) -> String { + if let value = value { + return column.expressionSQL(&context, wrappedInParenthesis: false) + + " = " + + value.sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) + } else { + return column.expressionSQL(&context, wrappedInParenthesis: false) + + " = NULL" + } + } } /// Creates an assignment to a value. /// /// Column("valid") <- true /// Column("score") <- 0 +/// Column("score") <- nil /// Column("score") <- Column("score") + Column("bonus") /// /// try dbQueue.write { db in /// // UPDATE player SET score = 0 /// try Player.updateAll(db, Column("score") <- 0) /// } -public func <- (column: ColumnExpression, value: SQLExpressible) -> ColumnAssignment { +public func <- (column: ColumnExpression, value: SQLExpressible?) -> ColumnAssignment { return ColumnAssignment(column: column, value: value) } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 7114af533..bc017284a 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -228,11 +228,7 @@ struct SQLQueryGenerator { sql += try relation.source.sql(db, &context) let assignmentsSQL = assignments - .map({ assignment in - assignment.column.expressionSQL(&context, wrappedInParenthesis: false) + - " = " + - assignment.value.sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) - }) + .map { $0.sql(&context) } .joined(separator: ", ") sql += " SET " + assignmentsSQL @@ -297,11 +293,7 @@ struct SQLQueryGenerator { sql += tableName.quotedDatabaseIdentifier let assignmentsSQL = assignments - .map({ assignment in - assignment.column.expressionSQL(&context, wrappedInParenthesis: false) + - " = " + - assignment.value.sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) - }) + .map { $0.sql(&context) } .joined(separator: ", ") sql += " SET " + assignmentsSQL sql += " WHERE rowid IN (\(selectSQL))" diff --git a/README.md b/README.md index 669793c33..55093ff56 100644 --- a/README.md +++ b/README.md @@ -4699,8 +4699,8 @@ Player.deleteOne(db, key: ["email": "arthur@example.com"]) **Requests can batch update records**. The `updateAll()` method accepts *column assignments* defined with the `<-` operator: ```swift -// UPDATE player SET score = 0, isHealthy = 1 -try Player.updateAll(db, scoreColumn <- 0, isHealthyColumn <- true) +// UPDATE player SET score = 0, isHealthy = 1, bonus = NULL +try Player.updateAll(db, scoreColumn <- 0, isHealthyColumn <- true, bonus <- nil) // UPDATE player SET score = 0 WHERE team = 'red' try Player diff --git a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift index 7c85037e5..b024d590d 100644 --- a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift +++ b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift @@ -106,6 +106,17 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { } } + func testNilAssignment() throws { + try makeDatabaseQueue().write { db in + try Player.createTable(db) + + try Player.updateAll(db, Columns.score <- nil) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE "player" SET "score" = NULL + """) + } + } + func testComplexAssignment() throws { try makeDatabaseQueue().write { db in try Player.createTable(db)