mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Support for nil assignment
This commit is contained in:
1 parent
84eef52cb0
commit
a0faf6aa68
5 files changed
+36
-14
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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))"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user