Skip to content
Open
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
10 changes: 3 additions & 7 deletions packages/pg/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ function arrayString(val) {
if (i > 0) {
result += ','
}
let item = val[i]
const item = val[i]
if (item == null) {
result += 'NULL'
} else if (Array.isArray(item)) {
result += arrayString(item)
} else if (ArrayBuffer.isView(item)) {
if (!(item instanceof Buffer)) {
item = Buffer.from(item.buffer, item.byteOffset, item.byteLength)
}
result += '\\\\x' + item.toString('hex')
} else {
result += escapeElement(prepareValue(item))
const prepared = prepareValue(item)
result += prepared instanceof Buffer ? '\\\\x' + prepared.toString('hex') : escapeElement(prepared)
}
}
result += '}'
Expand Down
45 changes: 45 additions & 0 deletions packages/pg/test/integration/client/custom-binary-array-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'
const helper = require('./test-helper')
const assert = require('assert')
const suite = new helper.Suite()

suite.test('custom binary array elements round-trip through bytea arrays', async function () {
const client = new helper.pg.Client(helper.config)
await client.connect()
try {
const bytes = Uint8Array.from({ length: 256 }, (_, i) => i)
const cases = [
[Buffer.from(bytes), Buffer.from(bytes)],
[Buffer.alloc(0), Buffer.alloc(0)],
[bytes.subarray(127, 130), Buffer.from([127, 128, 129])],
[new DataView(bytes.buffer, 254, 2), Buffer.from([254, 255])],
]
for (const [value, expected] of cases) {
let calls = 0
const customType = {
toPostgres: function () {
calls++
return value
},
}
const result = await client.query('SELECT $1::bytea[] AS value', [[customType, value, null]])
assert.deepStrictEqual(result.rows[0].value, [expected, expected, null])
assert.strictEqual(calls, 1)
}
} finally {
await client.end()
}
})

suite.test('chained custom binary elements round-trip through nested bytea arrays', async function () {
const client = new helper.pg.Client(helper.config)
await client.connect()
try {
const bytes = Buffer.from([0, 128, 255])
const customType = { toPostgres: () => ({ toPostgres: () => bytes }) }
const result = await client.query('SELECT $1::bytea[] AS value', [[[customType], [bytes]]])
assert.deepStrictEqual(result.rows[0].value, [[bytes], [bytes]])
} finally {
await client.end()
}
})
33 changes: 33 additions & 0 deletions packages/pg/test/unit/utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,39 @@ test('prepareValue: Uint8Array array prepared properly', function () {
assert.strictEqual(out, '{\\\\xdead,\\\\xbeef}')
})

test('prepareValue: direct and custom binary array elements prepared properly', function () {
const cases = [
[Buffer.from([0, 128, 255]), '0080ff'],
[Buffer.alloc(0), ''],
[new Uint8Array([1, 128, 255, 2]).subarray(1, 3), '80ff'],
[new DataView(Uint8Array.from([1, 128, 255, 2]).buffer, 1, 2), '80ff'],
]

for (const [value, hex] of cases) {
let calls = 0
const customType = {
toPostgres: function () {
calls++
return value
},
}
assert.strictEqual(utils.prepareValue([value, customType]), `{\\\\x${hex},\\\\x${hex}}`)
assert.strictEqual(calls, 1)
}
})

test('prepareValue: chained custom binary elements in nested arrays prepared properly', function () {
const buffer = Buffer.from([0, 128, 255])
const customType = { toPostgres: () => ({ toPostgres: () => buffer }) }
assert.strictEqual(
utils.prepareValue([
[customType, null],
[buffer, undefined],
]),
'{{\\\\x0080ff,NULL},{\\\\x0080ff,NULL}}'
)
})

test('prepareValue: objects with complex toPostgres prepared properly', function () {
const customType = {
toPostgres: function () {
Expand Down
Loading