diff --git a/Sources/MySQLNIO/MySQLDataConvertible.swift b/Sources/MySQLNIO/MySQLDataConvertible.swift index 4fbef38..aa2937d 100644 --- a/Sources/MySQLNIO/MySQLDataConvertible.swift +++ b/Sources/MySQLNIO/MySQLDataConvertible.swift @@ -1,3 +1,5 @@ +import struct Foundation.Decimal + public protocol MySQLDataConvertible { init?(mysqlData: MySQLData) var mysqlData: MySQLData? { get } @@ -105,3 +107,16 @@ extension Float: MySQLDataConvertible { return .init(float: self) } } + +extension Decimal: MySQLDataConvertible { + public init?(mysqlData: MySQLData) { + guard let string = mysqlData.string else { + return nil + } + self.init(string: string) + } + + public var mysqlData: MySQLData? { + .init(string: self.description) + } +} diff --git a/Tests/MySQLNIOTests/NIOMySQLTests.swift b/Tests/MySQLNIOTests/NIOMySQLTests.swift index e858af7..6a5aff9 100644 --- a/Tests/MySQLNIOTests/NIOMySQLTests.swift +++ b/Tests/MySQLNIOTests/NIOMySQLTests.swift @@ -277,6 +277,15 @@ final class NIOMySQLTests: XCTestCase { 0xE9, 0x88, 0xE7, 0x02, 0x96, 0xD7, 0x09, 0x9B, 0xC5, 0x70, 0x8A, 0x87, 0x2F, 0x4C, 0xC1, 0x72 ]) } + + func testQuery_decimal() throws { + let conn = try MySQLConnection.test(on: self.eventLoop).wait() + defer { try! conn.close().wait() } + do { + let rows = try conn.query("SELECT '3.1415926' as d").wait() + XCTAssertEqual(rows[0].column("d").flatMap { Decimal(mysqlData: $0) }?.description, "3.1415926") + } + } override func setUp() { self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)