mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Merge branch 'development'
This commit is contained in:
commit
146151ffd7
9 files changed
+165
-22
No files matched your search
+10
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -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.'
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 \
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>4.6.0</string>
|
||||
<string>4.6.1</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -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
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user