Skip to content
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
4 changes: 4 additions & 0 deletions packages/pg-protocol/src/outbound-serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('serializer', () => {
.addCString('')
.addInt16(0)
.addInt16(0)
.addInt16(1)
.addInt16(0)
.join(true, 'B')
assert.deepEqual(actual, expectedBuffer)
Expand Down Expand Up @@ -123,6 +124,7 @@ describe('serializer', () => {
.addInt32(-1)
.addInt32(4)
.add(Buffer.from('zing'))
.addInt16(1)
.addInt16(0)
.join(true, 'B')
assert.deepEqual(actual, expectedBuffer)
Expand All @@ -149,6 +151,7 @@ describe('serializer', () => {
.addInt32(-1)
.addInt32(-1)
.addInt32(-1)
.addInt16(1)
.addInt16(0)
.join(true, 'B')
assert.deepEqual(actual, expectedBuffer)
Expand Down Expand Up @@ -176,6 +179,7 @@ describe('serializer', () => {
.addInt32(-1)
.addInt32(4)
.add(Buffer.from('zing', 'utf-8'))
.addInt16(1)
.addInt16(0)
.join(true, 'B')
assert.deepEqual(actual, expectedBuffer)
Expand Down
2 changes: 2 additions & 0 deletions packages/pg-protocol/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ const bind = (config: BindOpts = {}): Buffer => {
writer.addInt16(len)
writer.add(paramWriter.flush())

// all results use the same format code
writer.addInt16(1)
// format code
writer.addInt16(binary ? ParamType.BINARY : ParamType.STRING)
return writer.flush(code.bind)
Expand Down
3 changes: 2 additions & 1 deletion packages/pg/lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class Result {
const rawValue = rowData[i]
const field = this.fields[i].name
if (rawValue !== null) {
row[field] = this._parsers[i](rawValue)
const v = this.fields[i].format === 'binary' ? Buffer.from(rawValue) : rawValue
row[field] = this._parsers[i](v)
} else {
row[field] = null
}
Expand Down
25 changes: 25 additions & 0 deletions packages/pg/test/integration/gh-issues/3487-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const helper = require('../test-helper')
const assert = require('assert')

const suite = new helper.Suite()

suite.testAsync('allows you to switch between format modes for arrays', async () => {
const client = new helper.pg.Client()
await client.connect()

const r1 = await client.query({
text: 'SELECT CAST($1 AS INT[]) as a',
values: [[1, 2, 8]],
binary: false,
})
assert.deepEqual([1, 2, 8], r1.rows[0].a)

const r2 = await client.query({
text: 'SELECT CAST($1 AS INT[]) as a',
values: [[4, 5, 6]],
binary: true,
})
assert.deepEqual([4, 5, 6], r2.rows[0].a)

await client.end()
})