Skip to content

Commit

Permalink
implement length encoded (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored Apr 17, 2020
1 parent 819479d commit 2cc8433
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
24 changes: 22 additions & 2 deletions Sources/MySQLNIO/Protocol/MySQLProtocol+COM_STMT_EXECUTE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ extension MySQLProtocol {
case .none: break
case .some(var buffer):
if value.type.encodingLength == nil {
// TODO: make length encoded
packet.payload.writeInteger(numericCast(buffer.readableBytes), endianness: .little, as: UInt8.self)
packet.payload.writeLengthEncoded(buffer.readableBytes)
}
packet.payload.writeBuffer(&buffer)
}
Expand All @@ -86,3 +85,24 @@ extension MySQLProtocol {
}
}
}

extension ByteBuffer {
mutating func writeLengthEncoded(_ integer: Int) {
assert(integer >= 0, "Length must be positive")
switch integer {
case 0..<251:
self.writeInteger(numericCast(integer), as: UInt8.self)
case 251..<1<<16:
self.writeInteger(0xFC, as: UInt8.self)
self.writeInteger(numericCast(integer), endianness: .little, as: UInt16.self)
case 1<<16..<1<<24:
self.writeInteger(0xFD, as: UInt8.self)
self.writeInteger(numericCast(integer & 0xFF), as: UInt8.self)
self.writeInteger(numericCast(integer >> 8 & 0xFF), as: UInt8.self)
self.writeInteger(numericCast(integer >> 16 & 0xFF), as: UInt8.self)
default:
self.writeInteger(0xFE, as: UInt8.self)
self.writeInteger(numericCast(integer), endianness: .little, as: UInt64.self)
}
}
}
24 changes: 24 additions & 0 deletions Tests/MySQLNIOTests/NIOMySQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,30 @@ final class NIOMySQLTests: XCTestCase {
XCTAssert(time.microsecond == UInt32(100000))
XCTAssert(time2.microsecond == UInt32(100000))
}

func testString_lengthEncoded_uint8() throws {
let conn = try MySQLConnection.test(on: self.eventLoop).wait()
defer { try! conn.close().wait() }
let string = String(repeating: "a", count: 128)
let rows = try! conn.query("SELECT ? as s", [MySQLData(string: string)]).wait()
XCTAssertEqual(rows[0].column("s")?.string, string)
}

func testString_lengthEncoded_fc() throws {
let conn = try MySQLConnection.test(on: self.eventLoop).wait()
defer { try! conn.close().wait() }
let string = String(repeating: "a", count: 512)
let rows = try! conn.query("SELECT ? as s", [MySQLData(string: string)]).wait()
XCTAssertEqual(rows[0].column("s")?.string, string)
}

func testString_lengthEncoded_fd() throws {
let conn = try MySQLConnection.test(on: self.eventLoop).wait()
defer { try! conn.close().wait() }
let string = String(repeating: "a", count: 1<<17)
let rows = try! conn.query("SELECT ? as s", [MySQLData(string: string)]).wait()
XCTAssertEqual(rows[0].column("s")?.string, string)
}

func testTypes() throws {
/// support
Expand Down

0 comments on commit 2cc8433

Please sign in to comment.