diff --git a/Package.swift b/Package.swift index a1eea1e8..6efa6e19 100644 --- a/Package.swift +++ b/Package.swift @@ -7,12 +7,12 @@ let package = Package( .Package(url: "https://github.com/qutheory/cmysql.git", majorVersion: 0, minor: 2), // Data structure for converting between multiple representations - .Package(url: "https://github.com/qutheory/node.git", majorVersion: 0, minor: 2), + .Package(url: "https://github.com/qutheory/node.git", majorVersion: 0, minor: 3), // Core extensions, type-aliases, and functions that facilitate common tasks .Package(url: "https://github.com/qutheory/core.git", majorVersion: 0, minor: 3), // JSON parsing and serialization for storing arrays and objects in MySQL - .Package(url: "https://github.com/qutheory/json.git", majorVersion: 0, minor: 2) + .Package(url: "https://github.com/qutheory/json.git", majorVersion: 0, minor: 3) ] ) diff --git a/Sources/MySQL/Bind+Node.swift b/Sources/MySQL/Bind+Node.swift index 7e99f9b9..0fcaf7a5 100644 --- a/Sources/MySQL/Bind+Node.swift +++ b/Sources/MySQL/Bind+Node.swift @@ -84,6 +84,15 @@ extension Bind { case MYSQL_TYPE_DOUBLE: let double = unwrap(buffer, Double.self) return .number(.double(double)) + case MYSQL_TYPE_DATE: + let time = unwrap(buffer, MYSQL_TIME.self) + return .string("\(time.year.pad(4))-\(time.month.pad(2))-\(time.day.pad(2))") + case MYSQL_TYPE_DATETIME: + let time = unwrap(buffer, MYSQL_TIME.self) + return .string("\(time.year.pad(4))-\(time.month.pad(2))-\(time.day.pad(2)) \(time.hour.pad(2)):\(time.minute.pad(2)):\(time.second.pad(2))") + case MYSQL_TYPE_TIME: + let time = unwrap(buffer, MYSQL_TIME.self) + return .string("\(time.hour.pad(2)):\(time.minute.pad(2)):\(time.second.pad(2))") default: print("[MySQL] Unsupported type: \(variant).") return .null @@ -92,6 +101,22 @@ extension Bind { } } +extension UInt32 { + func pad(_ n: Int) -> String { + var string = description + + if string.characters.count >= n { + return string + } + + for _ in 0..<(n - string.characters.count) { + string = "0" + string + } + + return string + } +} + extension Sequence where Iterator.Element == UInt8 { var string: String { var utf = UTF8() diff --git a/Sources/MySQL/Bind.swift b/Sources/MySQL/Bind.swift index 9fc17cd7..ba0a8180 100644 --- a/Sources/MySQL/Bind.swift +++ b/Sources/MySQL/Bind.swift @@ -198,7 +198,7 @@ extension Node { case .array(let array): var bytes: Bytes = [] do { - bytes = try JSON(array).makeBytes() + bytes = try JSON(node: array).makeBytes() } catch { print("[MySQL] Could not convert array to JSON.") } @@ -208,7 +208,7 @@ extension Node { case .object(let object): var bytes: Bytes = [] do { - bytes = try JSON(object).makeBytes() + bytes = try JSON(node: object).makeBytes() } catch { print("[MySQL] Could not convert object to JSON.") } diff --git a/Tests/MySQL/MySQLTests.swift b/Tests/MySQL/MySQLTests.swift index 617ed572..6df95426 100644 --- a/Tests/MySQL/MySQLTests.swift +++ b/Tests/MySQL/MySQLTests.swift @@ -119,7 +119,7 @@ class MySQLTests: XCTestCase { try mysql.execute("DROP TABLE IF EXISTS json") try mysql.execute("CREATE TABLE json (i INT, b VARCHAR(64), j JSON)") - let json = try JSON([ + let json = try JSON(node: [ "string": "hello, world", "int": 42 ]) @@ -144,6 +144,30 @@ class MySQLTests: XCTestCase { } } + func testTimestamps() { + do { + + try mysql.execute("DROP TABLE IF EXISTS times") + try mysql.execute("CREATE TABLE times (i INT, d DATE, t TIME)") + + + try mysql.execute("INSERT INTO times VALUES (?, ?, ?)", [ + 1, + "2050-05-12", + "13:42" + ]) + + + if let result = try mysql.execute("SELECT * FROM times").first { + print(result) + } else { + XCTFail("No results") + } + } catch { + XCTFail("Testing tables failed: \(error)") + } + } + func testError() { do { try mysql.execute("error")