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

Prepared queries encoding/decoding error #303

Open
sdpopov-keyvariable opened this issue Jul 16, 2022 · 0 comments
Open

Prepared queries encoding/decoding error #303

sdpopov-keyvariable opened this issue Jul 16, 2022 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@sdpopov-keyvariable
Copy link

sdpopov-keyvariable commented Jul 16, 2022

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:

  1. Add package with configuration '.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.11.0")'
  2. Run tests above.
  3. See errors in testSelectPlainPrepared() and testSelectFormattedPrepared().
  4. 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
@sdpopov-keyvariable sdpopov-keyvariable added the bug Something isn't working label Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants