Skip to content

Commit e0ed3fb

Browse files
authored
add data type to bind (#63)
* add data type to bind * add char uint8 test * regen linuxmain
1 parent 91ad8e1 commit e0ed3fb

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

Sources/PostgresNIO/Connection/PostgresClient+Query.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private final class PostgresParameterizedQuery: PostgresRequest {
9090
let parse = PostgresMessage.Parse(
9191
statementName: "",
9292
query: self.query,
93-
parameterTypes: []
93+
parameterTypes: self.binds.map { $0.type }
9494
)
9595
let describe = PostgresMessage.Describe(
9696
command: .statement,

Sources/PostgresNIO/Data/PostgresData+String.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ extension PostgresData {
4646
return self.double?.description
4747
case .int2, .int4, .int8:
4848
return self.int?.description
49+
case .bpchar:
50+
return self.character?.description
4951
default:
5052
return nil
5153
}
@@ -56,6 +58,27 @@ extension PostgresData {
5658
return string
5759
}
5860
}
61+
62+
public var character: Character? {
63+
guard var value = self.value else {
64+
return nil
65+
}
66+
67+
switch self.formatCode {
68+
case .binary:
69+
switch self.type {
70+
case .bpchar:
71+
guard let byte = value.readInteger(as: UInt8.self) else {
72+
return nil
73+
}
74+
return Character(UnicodeScalar(byte))
75+
default:
76+
return nil
77+
}
78+
case .text:
79+
return nil
80+
}
81+
}
5982
}
6083

6184
extension PostgresData: ExpressibleByStringLiteral {

Sources/PostgresNIO/Data/PostgresData+UUID.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ extension PostgresData {
1717
return nil
1818
}
1919

20-
return value.readUUID()
20+
switch self.formatCode {
21+
case .binary:
22+
switch self.type {
23+
case .uuid:
24+
return value.readUUID()
25+
case .varchar, .text:
26+
return self.string.flatMap { UUID(uuidString: $0) }
27+
default:
28+
return nil
29+
}
30+
case .text:
31+
return nil
32+
}
2133
}
2234
}
2335

Sources/PostgresNIO/Message/PostgresMessage+Parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension PostgresMessage {
1717
/// Note that this is not an indication of the number of parameters that might appear in the
1818
/// query string, only the number that the frontend wants to prespecify types for.
1919
/// Specifies the object ID of the parameter data type. Placing a zero here is equivalent to leaving the type unspecified.
20-
public var parameterTypes: [PostgresFormatCode]
20+
public var parameterTypes: [PostgresDataType]
2121

2222
/// Serializes this message into a byte buffer.
2323
public func serialize(into buffer: inout ByteBuffer) {

Tests/PostgresNIOTests/NIOPostgresTests.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,32 @@ final class NIOPostgresTests: XCTestCase {
573573
_ = try conn.query(query, [.init(date: date)]).wait()
574574
XCTFail("should have failed")
575575
} catch PostgresError.server(let error) {
576-
XCTAssertEqual(error.fields[.routine], "report_invalid_encoding")
576+
XCTAssertEqual(error.fields[.routine], "transformTypeCast")
577577
}
578578

579579
}
580+
581+
func testBindCharString() throws {
582+
// https://github.com/vapor/postgres-nio/issues/53
583+
let query = """
584+
SELECT $1::char as "char"
585+
"""
586+
let conn = try PostgresConnection.test(on: eventLoop).wait()
587+
defer { try! conn.close().wait() }
588+
let rows = try conn.query(query, [.init(string: "f")]).wait()
589+
XCTAssertEqual(rows[0].column("char")?.string, "f")
590+
}
591+
592+
func testBindCharUInt8() throws {
593+
// https://github.com/vapor/postgres-nio/issues/53
594+
let query = """
595+
SELECT $1::char as "char"
596+
"""
597+
let conn = try PostgresConnection.test(on: eventLoop).wait()
598+
defer { try! conn.close().wait() }
599+
let rows = try conn.query(query, [.init(uint8: 42)]).wait()
600+
XCTAssertEqual(rows[0].column("char")?.string, "*")
601+
}
580602

581603
// MARK: Performance
582604

Tests/PostgresNIOTests/XCTestManifests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ extension NIOPostgresTests {
77
// to regenerate.
88
static let __allTests__NIOPostgresTests = [
99
("testAverageLengthNumeric", testAverageLengthNumeric),
10+
("testBindCharString", testBindCharString),
11+
("testBindCharUInt8", testBindCharUInt8),
1012
("testBindDate", testBindDate),
1113
("testBindInteger", testBindInteger),
1214
("testBoolSerialize", testBoolSerialize),

0 commit comments

Comments
 (0)