postgres-nio
postgres-nio copied to clipboard
Prepared queries encoding/decoding error
Describe the bug
Substitution of parameters ($1, $2, ...) to prepared queries fails. E.g. for Int value 10 it fails with error message «server: invalid byte sequence for encoding "UTF8": 0x00 (report_invalid_encoding)». I guess the problem is that format (text or binary) is incorrect. The same for unprepared queries works fine.
Decoding of values fails too. Simple SELECT 10 as prepared query can't decode the result as Int, but decodes it as Data of 4 bytes (0x00, 0x00, 0x00, 0x0A). In this case the problem is in «Int+PostgresCodable.swift:254» converting "\0\0\0\10" string to Int. But if format of the received cells is changed to .binary before decoding then values are successfully decoded.
To Reproduce
That's Xcode tests:
func testSelectPlain() async throws {
let postgres = try await Postgres()
let result = try postgres.connection.query("SELECT 10, 20", logger: postgres.logger).wait()
let rows = result.rows
XCTAssertEqual(rows.count, 1)
let values = try rows.first!.decode((Int, Int).self, context: .default)
XCTAssertEqual(values.0, 10)
XCTAssertEqual(values.1, 20)
}
func testSelectPlainPrepared() async throws {
let postgres = try await Postgres()
let query = try postgres.connection.prepare(query: "SELECT 10, 20").wait()
let rows = try query.execute().wait()
XCTAssertEqual(rows.count, 1)
let values = try rows.first!.decode((Int, Int).self, context: .default)
XCTAssertEqual(values.0, 10)
XCTAssertEqual(values.1, 20)
}
func testSelectPlainPreparedBinary() async throws {
let postgres = try await Postgres()
let query = try postgres.connection.prepare(query: "SELECT 10, 20").wait()
let rows = try query.execute().wait()
XCTAssertEqual(rows.count, 1)
var iterator = rows.first!.makeIterator()
if var cell = iterator.next() {
cell.format = .binary
let value = try cell.decode(Int.self, context: .default)
XCTAssertEqual(value, 10)
}
if var cell = iterator.next() {
cell.format = .binary
let value = try cell.decode(Int.self, context: .default)
XCTAssertEqual(value, 20)
}
}
func testSelectFormatted() async throws {
let postgres = try await Postgres()
let result = try postgres.connection.query("SELECT $1, $2", [ 10, 20 ].map { $0.postgresData! }).wait()
let rows = result.rows
XCTAssertEqual(rows.count, 1)
let values = try rows.first!.decode((Int, Int).self, context: .default)
XCTAssertEqual(values.0, 10)
XCTAssertEqual(values.1, 20)
}
func testSelectFormattedPrepared() async throws {
let postgres = try await Postgres()
let query = try postgres.connection.prepare(query: "SELECT $1, $2").wait()
let rows = try query.execute([ 10, 20 ].map { $0.postgresData! }).wait()
XCTAssertEqual(rows.count, 1)
let values = try rows.first!.decode((Int, Int).self, context: .default)
XCTAssertEqual(values.0, 10)
XCTAssertEqual(values.1, 20)
}
private class Postgres {
let connection: PostgresConnection
let logger = Logger(label: "postgres-logger")
init() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
do {
connection = try await PostgresConnection.connect(
on: eventLoopGroup.next(),
configuration: .init(
connection: .init(
host: "localhost",
port: 5432
),
authentication: .init(
username: "postgres",
database: nil,
password: nil
),
tls: .disable
),
id: 1,
logger: logger
)
self.eventLoopGroup = eventLoopGroup
}
catch {
try? eventLoopGroup.syncShutdownGracefully()
throw error
}
}
deinit {
do {
try connection.close().wait()
try eventLoopGroup.syncShutdownGracefully()
}
catch { print("Failed to stop instance of KvPostgreSQL with error: \(error)") }
}
private let eventLoopGroup: MultiThreadedEventLoopGroup
}
Steps to reproduce the behavior:
- Add package with configuration '.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.11.0")'
- Run tests above.
- See errors in
testSelectPlainPrepared()andtestSelectFormattedPrepared(). - See walk around in
testSelectPlainPreparedBinary().
Expected behavior
Correct encoding and decoding of values for prepared queries.
Environment
- Vapor Framework version: PostgresNIO 1.11.0
- Postgres: PostgreSQL 14.4 on aarch64-apple-darwin20.6.0, compiled by Apple clang version 12.0.5 (clang-1205.0.22.9), 64-bit
- OS version: MacOS 12.4
- Xcode: 13.4.1