Skip to content

Commit d2698f4

Browse files
authored
Merge pull request #94 from cuva/master
Adds generic return value to transaction closure
2 parents fc1eeeb + 9fbb46e commit d2698f4

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Sources/MySQL/Connection.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public final class Connection {
4545
isClosed = false
4646
}
4747

48-
public func transaction(_ closure: () throws -> Void) throws {
48+
public func transaction<R>(_ closure: () throws -> R) throws -> R {
4949
// required by transactions, but I don't want to open the old
5050
// MySQL query API to the public as it would be a burden to maintain.
5151
func manual(_ query: String) throws {
@@ -55,16 +55,18 @@ public final class Connection {
5555
}
5656

5757
try manual("START TRANSACTION")
58-
58+
59+
let value: R
5960
do {
60-
try closure()
61+
value = try closure()
6162
} catch {
6263
// rollback changes and then rethrow the error
6364
try manual("ROLLBACK")
6465
throw error
6566
}
6667

6768
try manual("COMMIT")
69+
return value
6870
}
6971

7072
@discardableResult

Tests/MySQLTests/MySQLTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class MySQLTests: XCTestCase {
195195
])
196196

197197
try conn.transaction {
198-
try conn.execute("UPDATE transaction SET name = 'James' where name = 'james'")
198+
_ = try conn.execute("UPDATE transaction SET name = 'James' where name = 'james'")
199199
}
200200

201201
if let name = try conn.execute("SELECT * FROM transaction")["0", "name"]?.string {

0 commit comments

Comments
 (0)