Skip to content

Commit e5e1b40

Browse files
author
Kyle Jessup
committed
Fixed URL handling
1 parent 4ce635f commit e5e1b40

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

Sources/PerfectMySQL/MySQLCRUD.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ class MySQLCRUDRowReader<K : CodingKey>: KeyedDecodingContainerProtocol {
133133
throw CRUDDecoderError("Invalid Date string \(String(describing: val)).")
134134
}
135135
return date as! T
136+
case .url:
137+
guard let str = val as? String, let url = URL(string: str) else {
138+
throw CRUDDecoderError("Invalid URL string \(String(describing: val)).")
139+
}
140+
return url as! T
136141
case .codable:
137142
guard let data = (val as? String)?.data(using: .utf8) else {
138143
throw CRUDDecoderError("Unsupported type: \(type) for key: \(key.stringValue)")
@@ -314,6 +319,8 @@ class MySQLGenDelegate: SQLGenDelegate {
314319
typeName = "varchar(36)"
315320
case .date:
316321
typeName = "datetime"
322+
case .url:
323+
typeName = "longtext"
317324
case .codable:
318325
typeName = "json"
319326
}
@@ -416,6 +423,8 @@ class MySQLStmtExeDelegate: SQLExeDelegate {
416423
statement.bindParam(b ? 1 : 0)
417424
case .date(let d):
418425
statement.bindParam(d.mysqlFormatted())
426+
case .url(let u):
427+
statement.bindParam(u.absoluteString)
419428
case .uuid(let u):
420429
statement.bindParam(u.uuidString)
421430
case .null:

Tests/PerfectMySQLTests/PerfectMySQLTests.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import XCTest
2222
import PerfectCRUD
2323

2424
let testDBRowCount = 5
25+
#if os(macOS)
2526
let testHost = "127.0.0.1"
27+
#else
28+
let testHost = "host.docker.internal"
29+
#endif
2630
let testUser = "root"
2731
let testPassword = ""
2832
let testDB = "test"
@@ -290,7 +294,7 @@ class PerfectMySQLTests: XCTestCase {
290294
XCTAssert(prepRes, stmt1.errorMessage())
291295
XCTAssert(stmt1.paramCount() == 29)
292296

293-
stmt1.bindParam("varchar '22' string 👻")
297+
stmt1.bindParam("varchar ’22’ string 👻")
294298
stmt1.bindParam(1)
295299
stmt1.bindParam("text string")
296300
stmt1.bindParam("2015-10-21")
@@ -341,7 +345,7 @@ class PerfectMySQLTests: XCTestCase {
341345
let ok = results.forEachRow {
342346
e in
343347

344-
XCTAssertEqual(e[0] as? String, "varchar '22' string 👻")
348+
XCTAssertEqual(e[0] as? String, "varchar ’22’ string 👻")
345349
XCTAssertEqual(e[1] as? Int8, 1)
346350
XCTAssertEqual(e[2] as? String, "text string")
347351
XCTAssertEqual(e[3] as? String, "2015-10-21")
@@ -1016,13 +1020,13 @@ class PerfectMySQLTests: XCTestCase {
10161020
do {
10171021
let db = try getTestDB()
10181022
let t1 = db.table(TestTable1.self)
1019-
let newOne = TestTable1(id: 2000, name: "New ' One", integer: 40)
1023+
let newOne = TestTable1(id: 2000, name: "New ` One", integer: 40)
10201024
try t1.insert(newOne)
10211025
let j1 = t1.where(\TestTable1.id == newOne.id)
10221026
let j2 = try j1.select().map {$0}
10231027
XCTAssertEqual(try j1.count(), 1)
10241028
XCTAssertEqual(j2[0].id, 2000)
1025-
XCTAssertEqual(j2[0].name, "New ' One")
1029+
XCTAssertEqual(j2[0].name, "New ` One")
10261030
} catch {
10271031
XCTFail("\(error)")
10281032
}
@@ -1689,6 +1693,27 @@ class PerfectMySQLTests: XCTestCase {
16891693
}
16901694
}
16911695

1696+
func testURL() {
1697+
do {
1698+
let db = try getTestDB()
1699+
struct TableWithURL: Codable {
1700+
let id: Int
1701+
let url: URL
1702+
}
1703+
try db.create(TableWithURL.self)
1704+
let t1 = db.table(TableWithURL.self)
1705+
let newOne = TableWithURL(id: 2000, url: URL(string: "http://localhost/")!)
1706+
try t1.insert(newOne)
1707+
let j1 = t1.where(\TableWithURL.id == newOne.id)
1708+
let j2 = try j1.select().map {$0}
1709+
XCTAssertEqual(try j1.count(), 1)
1710+
XCTAssertEqual(j2[0].id, 2000)
1711+
XCTAssertEqual(j2[0].url.absoluteString, "http://localhost/")
1712+
} catch {
1713+
XCTFail("\(error)")
1714+
}
1715+
}
1716+
16921717
static var allTests = [
16931718
("testConnect", testConnect),
16941719
("testListDbs1", testListDbs1),
@@ -1736,7 +1761,8 @@ class PerfectMySQLTests: XCTestCase {
17361761
("testBadDecoding", testBadDecoding),
17371762
("testAllPrimTypes1", testAllPrimTypes1),
17381763
("testAllPrimTypes2", testAllPrimTypes2),
1739-
("testBespokeSQL", testBespokeSQL)
1764+
("testBespokeSQL", testBespokeSQL),
1765+
("testURL", testURL)
17401766
]
17411767
}
17421768

0 commit comments

Comments
 (0)