diff --git a/CHANGELOG.md b/CHANGELOG.md
index e0389ba8a..b26043feb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,7 +13,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection:
#### 4.x Releases
-- `4.6.x` Releases - [4.6.0](#460)
+- `4.6.x` Releases - [4.6.0](#460) | [4.6.1](#461)
- `4.5.x` Releases - [4.5.0](#450)
- `4.4.x` Releases - [4.4.0](#440)
- `4.3.x` Releases - [4.3.0](#430)
@@ -64,6 +64,15 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection:
-->
+## 4.6.1
+
+Released November 10, 2019 • [diff](https://github.com/groue/GRDB.swift/compare/v4.6.0...v4.6.1)
+
+**Fixed**
+
+- [#647](https://github.com/groue/GRDB.swift/pull/647): Honor conflict resolution for batch updates
+
+
## 4.6.0
Released November 9, 2019 • [diff](https://github.com/groue/GRDB.swift/compare/v4.5.0...v4.6.0)
diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec
index e4131f4d7..8172ca7be 100644
--- a/GRDB.swift.podspec
+++ b/GRDB.swift.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'GRDB.swift'
- s.version = '4.6.0'
+ s.version = '4.6.1'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'A toolkit for SQLite databases, with a focus on application development.'
diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift
index 1db6cd367..433276135 100644
--- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift
+++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift
@@ -503,11 +503,23 @@ extension QueryInterfaceRequest where T: MutablePersistableRecord {
/// }
///
/// - parameter db: A database connection.
- /// - returns: The number of updated rows
+ /// - parameter conflictResolution: A policy for conflict resolution,
+ /// defaulting to the record's persistenceConflictPolicy.
+ /// - parameter assignments: An array of column assignments.
+ /// - returns: The number of updated rows.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
@discardableResult
- public func updateAll(_ db: Database, _ assignments: [ColumnAssignment]) throws -> Int {
- guard let updateStatement = try SQLQueryGenerator(query).makeUpdateStatement(db, assignments) else {
+ public func updateAll(
+ _ db: Database,
+ onConflict conflictResolution: Database.ConflictResolution? = nil,
+ _ assignments: [ColumnAssignment]) throws -> Int
+ {
+ let conflictResolution = conflictResolution ?? RowDecoder.persistenceConflictPolicy.conflictResolutionForUpdate
+ guard let updateStatement = try SQLQueryGenerator(query).makeUpdateStatement(
+ db,
+ conflictResolution: conflictResolution,
+ assignments: assignments) else
+ {
// database not hit
return 0
}
@@ -525,16 +537,21 @@ extension QueryInterfaceRequest where T: MutablePersistableRecord {
/// }
///
/// - parameter db: A database connection.
- /// - returns: The number of updated rows
+ /// - parameter conflictResolution: A policy for conflict resolution,
+ /// defaulting to the record's persistenceConflictPolicy.
+ /// - parameter assignment: A column assignment.
+ /// - parameter otherAssignments: Eventual other column assignments.
+ /// - returns: The number of updated rows.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
@discardableResult
public func updateAll(
_ db: Database,
+ onConflict conflictResolution: Database.ConflictResolution? = nil,
_ assignment: ColumnAssignment,
_ otherAssignments: ColumnAssignment...)
throws -> Int
{
- return try updateAll(db, [assignment] + otherAssignments)
+ return try updateAll(db, onConflict: conflictResolution, [assignment] + otherAssignments)
}
}
diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift
index dcf3f9957..789f0ab4b 100644
--- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift
+++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift
@@ -170,7 +170,12 @@ struct SQLQueryGenerator {
}
/// Returns nil if assignments is empty
- func makeUpdateStatement(_ db: Database, _ assignments: [ColumnAssignment]) throws -> UpdateStatement? {
+ func makeUpdateStatement(
+ _ db: Database,
+ conflictResolution: Database.ConflictResolution,
+ assignments: [ColumnAssignment])
+ throws -> UpdateStatement?
+ {
if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty {
// Programmer error
fatalError("Can't update query with GROUP BY clause")
@@ -197,7 +202,13 @@ struct SQLQueryGenerator {
var context = SQLGenerationContext.queryGenerationContext(aliases: relation.allAliases)
- var sql = try "UPDATE " + relation.source.sql(db, &context)
+ var sql = "UPDATE "
+
+ if conflictResolution != .abort {
+ sql += "OR \(conflictResolution.rawValue) "
+ }
+
+ sql += try relation.source.sql(db, &context)
let assignmentsSQL = assignments
.map({ assignment in
diff --git a/GRDB/Record/PersistableRecord.swift b/GRDB/Record/PersistableRecord.swift
index 87b07372a..56795f70d 100644
--- a/GRDB/Record/PersistableRecord.swift
+++ b/GRDB/Record/PersistableRecord.swift
@@ -530,11 +530,19 @@ extension MutablePersistableRecord {
/// }
///
/// - parameter db: A database connection.
- /// - returns: The number of updated rows
+ /// - parameter conflictResolution: A policy for conflict resolution,
+ /// defaulting to the record's persistenceConflictPolicy.
+ /// - parameter assignments: An array of column assignments.
+ /// - returns: The number of updated rows.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
@discardableResult
- public static func updateAll(_ db: Database, _ assignments: [ColumnAssignment]) throws -> Int {
- return try all().updateAll(db, assignments)
+ public static func updateAll(
+ _ db: Database,
+ onConflict conflictResolution: Database.ConflictResolution? = nil,
+ _ assignments: [ColumnAssignment])
+ throws -> Int
+ {
+ return try all().updateAll(db, onConflict: conflictResolution, assignments)
}
/// Updates all records; returns the number of updated records.
@@ -547,16 +555,21 @@ extension MutablePersistableRecord {
/// }
///
/// - parameter db: A database connection.
- /// - returns: The number of updated rows
+ /// - parameter conflictResolution: A policy for conflict resolution,
+ /// defaulting to the record's persistenceConflictPolicy.
+ /// - parameter assignment: A column assignment.
+ /// - parameter otherAssignments: Eventual other column assignments.
+ /// - returns: The number of updated rows.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
@discardableResult
public static func updateAll(
_ db: Database,
+ onConflict conflictResolution: Database.ConflictResolution? = nil,
_ assignment: ColumnAssignment,
_ otherAssignments: ColumnAssignment...)
throws -> Int
{
- return try updateAll(db, [assignment] + otherAssignments)
+ return try updateAll(db, onConflict: conflictResolution, [assignment] + otherAssignments)
}
}
diff --git a/Makefile b/Makefile
index 1ed94b091..910985736 100644
--- a/Makefile
+++ b/Makefile
@@ -452,8 +452,8 @@ ifdef JAZZY
--author 'Gwendal Roué' \
--author_url https://github.com/groue \
--github_url https://github.com/groue/GRDB.swift \
- --github-file-prefix https://github.com/groue/GRDB.swift/tree/v4.6.0 \
- --module-version 4.6.0 \
+ --github-file-prefix https://github.com/groue/GRDB.swift/tree/v4.6.1 \
+ --module-version 4.6.1 \
--module GRDB \
--root-url http://groue.github.io/GRDB.swift/docs/4.6/ \
--output Documentation/Reference \
diff --git a/README.md b/README.md
index ff2b6a916..7ca44155d 100644
--- a/README.md
+++ b/README.md
@@ -5,14 +5,14 @@ GRDB 4 [ • [Migrating From GRDB 3 to GRDB 4](Documentation/GRDB3MigrationGuide.md)
+**Latest release**: November 10, 2019 • version 4.6.1 • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 3 to GRDB 4](Documentation/GRDB3MigrationGuide.md)
**Requirements**: iOS 9.0+ / macOS 10.9+ / tvOS 9.0+ / watchOS 2.0+ • Swift 4.2+ / Xcode 10.0+
| Swift version | GRDB version |
| ------------- | ----------------------------------------------------------- |
-| **Swift 5** | **v4.6.0** |
-| **Swift 4.2** | **v4.6.0** |
+| **Swift 5** | **v4.6.1** |
+| **Swift 4.2** | **v4.6.1** |
| Swift 4.1 | [v3.7.0](https://github.com/groue/GRDB.swift/tree/v3.7.0) |
| Swift 4 | [v2.10.0](https://github.com/groue/GRDB.swift/tree/v2.10.0) |
| Swift 3.2 | [v1.3.0](https://github.com/groue/GRDB.swift/tree/v1.3.0) |
@@ -4659,6 +4659,13 @@ As a convenience, you can also use the `+=`, `-=`, `*=`, or `/=` operators:
try Player.updateAll(db, scoreColumn += bonusColumn * 2)
```
+Default [Conflict Resolution] rules apply, and you may also provide a specific one:
+
+```swift
+// UPDATE OR IGNORE player SET ...
+try Player.updateAll(db, onConflict: .ignore, /* assignments... */)
+```
+
> :point_up: **Note** The `updateAll` method is only available for records that adopts the [PersistableRecord] protocol.
diff --git a/Support/Info.plist b/Support/Info.plist
index fa76e3e5b..ee4f7fbd8 100644
--- a/Support/Info.plist
+++ b/Support/Info.plist
@@ -15,7 +15,7 @@
CFBundlePackageType
FMWK
CFBundleShortVersionString
- 4.6.0
+ 4.6.1
CFBundleSignature
????
CFBundleVersion
diff --git a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift
index 650a76563..6488a6be8 100644
--- a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift
+++ b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift
@@ -35,8 +35,7 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase {
}
func testRequestUpdateAll() throws {
- let dbQueue = try makeDatabaseQueue()
- try dbQueue.write { db in
+ try makeDatabaseQueue().write { db in
let assignment = Columns.score <- 0
try Player.updateAll(db, assignment)
@@ -282,4 +281,91 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase {
try XCTAssertEqual(Player.fetchOne(db, key: 1)!.score, 2)
}
}
+
+ func testConflictPolicyAbort() throws {
+ struct AbortPlayer: PersistableRecord {
+ static let databaseTableName = "player"
+ static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .abort)
+ func encode(to container: inout PersistenceContainer) { }
+ }
+ try makeDatabaseQueue().write { db in
+ try AbortPlayer.updateAll(db, Column("score") <- 0)
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE "player" SET "score" = 0
+ """)
+
+ try AbortPlayer.updateAll(db, [Column("score") <- 0])
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE "player" SET "score" = 0
+ """)
+
+ try AbortPlayer.all().updateAll(db, Column("score") <- 0)
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE "player" SET "score" = 0
+ """)
+
+ try AbortPlayer.all().updateAll(db, [Column("score") <- 0])
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE "player" SET "score" = 0
+ """)
+ }
+ }
+
+ func testConflictPolicyIgnore() throws {
+ struct IgnorePlayer: PersistableRecord {
+ static let databaseTableName = "player"
+ static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .ignore)
+ func encode(to container: inout PersistenceContainer) { }
+ }
+ try makeDatabaseQueue().write { db in
+ try IgnorePlayer.updateAll(db, Column("score") <- 0)
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+
+ try IgnorePlayer.updateAll(db, [Column("score") <- 0])
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+
+ try IgnorePlayer.all().updateAll(db, Column("score") <- 0)
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+
+ try IgnorePlayer.all().updateAll(db, [Column("score") <- 0])
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+ }
+ }
+
+ func testConflictPolicyCustom() throws {
+ try makeDatabaseQueue().write { db in
+ try Player.updateAll(db, Column("score") <- 0)
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE "player" SET "score" = 0
+ """)
+
+ try Player.updateAll(db, onConflict: .ignore, Column("score") <- 0)
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+
+ try Player.updateAll(db, onConflict: .ignore, [Column("score") <- 0])
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+
+ try Player.all().updateAll(db, onConflict: .ignore, Column("score") <- 0)
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+
+ try Player.all().updateAll(db, onConflict: .ignore, [Column("score") <- 0])
+ XCTAssertEqual(self.lastSQLQuery, """
+ UPDATE OR IGNORE "player" SET "score" = 0
+ """)
+ }
+ }
}