Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add data type to bind #63

Merged
merged 3 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/Connection/PostgresClient+Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private final class PostgresParameterizedQuery: PostgresRequest {
let parse = PostgresMessage.Parse(
statementName: "",
query: self.query,
parameterTypes: []
parameterTypes: self.binds.map { $0.type }
)
let describe = PostgresMessage.Describe(
command: .statement,
Expand Down
23 changes: 23 additions & 0 deletions Sources/PostgresNIO/Data/PostgresData+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ extension PostgresData {
return self.double?.description
case .int2, .int4, .int8:
return self.int?.description
case .bpchar:
return self.character?.description
default:
return nil
}
Expand All @@ -56,6 +58,27 @@ extension PostgresData {
return string
}
}

public var character: Character? {
guard var value = self.value else {
return nil
}

switch self.formatCode {
case .binary:
switch self.type {
case .bpchar:
guard let byte = value.readInteger(as: UInt8.self) else {
return nil
}
return Character(UnicodeScalar(byte))
default:
return nil
}
case .text:
return nil
}
}
}

extension PostgresData: ExpressibleByStringLiteral {
Expand Down
14 changes: 13 additions & 1 deletion Sources/PostgresNIO/Data/PostgresData+UUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ extension PostgresData {
return nil
}

return value.readUUID()
switch self.formatCode {
case .binary:
switch self.type {
case .uuid:
return value.readUUID()
case .varchar, .text:
return self.string.flatMap { UUID(uuidString: $0) }
default:
return nil
}
case .text:
return nil
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/Message/PostgresMessage+Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension PostgresMessage {
/// Note that this is not an indication of the number of parameters that might appear in the
/// query string, only the number that the frontend wants to prespecify types for.
/// Specifies the object ID of the parameter data type. Placing a zero here is equivalent to leaving the type unspecified.
public var parameterTypes: [PostgresFormatCode]
public var parameterTypes: [PostgresDataType]

/// Serializes this message into a byte buffer.
public func serialize(into buffer: inout ByteBuffer) {
Expand Down
24 changes: 23 additions & 1 deletion Tests/PostgresNIOTests/NIOPostgresTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,32 @@ final class NIOPostgresTests: XCTestCase {
_ = try conn.query(query, [.init(date: date)]).wait()
XCTFail("should have failed")
} catch PostgresError.server(let error) {
XCTAssertEqual(error.fields[.routine], "report_invalid_encoding")
XCTAssertEqual(error.fields[.routine], "transformTypeCast")
}

}

func testBindCharString() throws {
// https://github.com/vapor/postgres-nio/issues/53
let query = """
SELECT $1::char as "char"
"""
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query(query, [.init(string: "f")]).wait()
XCTAssertEqual(rows[0].column("char")?.string, "f")
}

func testBindCharUInt8() throws {
// https://github.com/vapor/postgres-nio/issues/53
let query = """
SELECT $1::char as "char"
"""
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query(query, [.init(uint8: 42)]).wait()
XCTAssertEqual(rows[0].column("char")?.string, "*")
}

// MARK: Performance

Expand Down
2 changes: 2 additions & 0 deletions Tests/PostgresNIOTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ extension NIOPostgresTests {
// to regenerate.
static let __allTests__NIOPostgresTests = [
("testAverageLengthNumeric", testAverageLengthNumeric),
("testBindCharString", testBindCharString),
("testBindCharUInt8", testBindCharUInt8),
("testBindDate", testBindDate),
("testBindInteger", testBindInteger),
("testBoolSerialize", testBoolSerialize),
Expand Down