Skip to content

Commit

Permalink
add Decimal support (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored Dec 11, 2019
1 parent eebaa40 commit bb9c164
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/MySQLNIO/MySQLDataConvertible.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import struct Foundation.Decimal

public protocol MySQLDataConvertible {
init?(mysqlData: MySQLData)
var mysqlData: MySQLData? { get }
Expand Down Expand Up @@ -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)
}
}
9 changes: 9 additions & 0 deletions Tests/MySQLNIOTests/NIOMySQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bb9c164

Please # to comment.