From 0e667619a304c221d3402583064d9bdfa56a15c8 Mon Sep 17 00:00:00 2001 From: msand Date: Thu, 19 Sep 2024 16:26:39 +0200 Subject: [PATCH] Support binary result format --- packages/pg-protocol/src/parser.ts | 10 ++++++---- packages/pg-protocol/src/serializer.ts | 10 +++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/pg-protocol/src/parser.ts b/packages/pg-protocol/src/parser.ts index 5a3b0f6be..1f9cbc735 100644 --- a/packages/pg-protocol/src/parser.ts +++ b/packages/pg-protocol/src/parser.ts @@ -278,13 +278,15 @@ export class Parser { } private parseDataRowMessage(offset: number, length: number, bytes: Buffer) { - this.reader.setBuffer(offset, bytes) - const fieldCount = this.reader.int16() + const reader = this.reader + reader.setBuffer(offset, bytes) + const fieldCount = reader.int16() const fields: any[] = new Array(fieldCount) + const textMode = this.mode === 'text' for (let i = 0; i < fieldCount; i++) { - const len = this.reader.int32() + const len = reader.int32() // a -1 for length means the value of the field is null - fields[i] = len === -1 ? null : this.reader.string(len) + fields[i] = len === -1 ? null : textMode ? reader.string(len) : reader.bytes(len) } return new DataRowMessage(length, fields) } diff --git a/packages/pg-protocol/src/serializer.ts b/packages/pg-protocol/src/serializer.ts index 07e2fe498..771a8855b 100644 --- a/packages/pg-protocol/src/serializer.ts +++ b/packages/pg-protocol/src/serializer.ts @@ -160,7 +160,15 @@ const bind = (config: BindOpts = {}): Buffer => { writer.add(paramWriter.flush()) // format code - writer.addInt16(binary ? ParamType.BINARY : ParamType.STRING) + if (binary) { + const columnFormatCount = 1 + writer.addInt16(columnFormatCount) + writer.addInt16(ParamType.BINARY) + } else { + const columnFormatCount = 0 + writer.addInt16(columnFormatCount) + } + return writer.flush(code.bind) }