Skip to content

Commit 0f76488

Browse files
author
Kyle Jessup
committed
added delegate func for empty insert stats. test
1 parent c96be8e commit 0f76488

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let package = Package(
3131
.library(name: "PerfectMySQL", targets: ["PerfectMySQL"])
3232
],
3333
dependencies: [
34-
.package(url: "https://github.com/PerfectlySoft/Perfect-CRUD.git", from: "1.2.0"),
34+
.package(url: "https://github.com/PerfectlySoft/Perfect-CRUD.git", from: "1.2.2"),
3535
.package(url: "https://github.com/PerfectlySoft/\(clientPackage).git", from: "2.0.0"),
3636
],
3737
targets: [

Sources/PerfectMySQL/MySQLCRUD.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ class MySQLGenDelegate: SQLGenDelegate {
187187
database = db
188188
}
189189

190+
func getEmptyInsertSnippet() -> String {
191+
return "() VALUES ()"
192+
}
193+
190194
func getBinding(for expr: Expression) throws -> String {
191195
bindings.append(("?", expr))
192196
return "?"

Tests/PerfectMySQLTests/PerfectMySQLTests.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,32 @@ class PerfectMySQLTests: XCTestCase {
17391739
}
17401740
}
17411741

1742+
func testEmptyInsert() {
1743+
do {
1744+
let db = try getTestDB()
1745+
struct ReturningItem: Codable, Equatable {
1746+
let id: Int?
1747+
var def: Int?
1748+
init(id: Int, def: Int? = nil) {
1749+
self.id = id
1750+
self.def = def
1751+
}
1752+
}
1753+
try db.sql("DROP TABLE IF EXISTS \(ReturningItem.CRUDTableName)")
1754+
try db.sql("CREATE TABLE \(ReturningItem.CRUDTableName) (id INT PRIMARY KEY AUTO_INCREMENT, def INT DEFAULT 42)")
1755+
let table = db.table(ReturningItem.self)
1756+
1757+
let id = try table
1758+
.insert(ReturningItem(id: 0, def: 0),
1759+
ignoreKeys: \ReturningItem.id, \ReturningItem.def)
1760+
.lastInsertId()
1761+
XCTAssertEqual(id, 1)
1762+
1763+
} catch {
1764+
XCTFail("\(error)")
1765+
}
1766+
}
1767+
17421768
static var allTests = [
17431769
("testConnect", testConnect),
17441770
("testListDbs1", testListDbs1),
@@ -1788,7 +1814,8 @@ class PerfectMySQLTests: XCTestCase {
17881814
("testAllPrimTypes2", testAllPrimTypes2),
17891815
("testBespokeSQL", testBespokeSQL),
17901816
("testURL", testURL),
1791-
("testLastInsertId", testLastInsertId)
1817+
("testLastInsertId", testLastInsertId),
1818+
("testEmptyInsert", testEmptyInsert)
17921819
]
17931820
}
17941821

0 commit comments

Comments
 (0)