Skip to content

Commit a75f389

Browse files
committed
Build builds
1 parent 5388ba3 commit a75f389

File tree

12 files changed

+107
-53
lines changed

12 files changed

+107
-53
lines changed

cf/src/connection.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
563563

564564
if (needsTypes) {
565565
initial.reserve && (initial = null)
566-
return fetchArrayTypes()
566+
return fetchTypes()
567567
}
568568

569569
initial && !initial.reserve && execute(initial)
@@ -659,7 +659,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
659659
name: transform.column.from
660660
? transform.column.from(x.toString('utf8', start, index - 1))
661661
: x.toString('utf8', start, index - 1),
662-
parser: parsers[type],
662+
parser: parsers[type] || parsers[options.shared.typeOidToName[type]],
663663
table,
664664
number,
665665
type
@@ -767,26 +767,29 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
767767
backend.secret = x.readUInt32BE(9)
768768
}
769769

770-
async function fetchArrayTypes() {
770+
async function fetchTypes() {
771771
needsTypes = false
772772
const types = await new Query([`
773-
select b.oid, b.typarray
774-
from pg_catalog.pg_type a
775-
left join pg_catalog.pg_type b on b.oid = a.typelem
776-
where a.typcategory = 'A'
777-
group by b.oid, b.typarray
778-
order by b.oid
773+
select oid, typname, typarray
774+
from pg_catalog.pg_type
775+
order by oid
779776
`], [], execute)
780-
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
777+
types.forEach(({ oid, typname, typarray }) => {
778+
options.shared.typeNameToOid[typname] = oid
779+
options.shared.typeOidToName[oid] = typname
780+
781+
if (typarray) addArrayType(oid, typarray)
782+
})
781783
}
782784

783785
function addArrayType(oid, typarray) {
784786
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return
785-
const parser = options.parsers[oid]
787+
const name = options.shared.typeOidToName[oid]
788+
const parser = options.parsers[oid] || options.parsers[name]
786789
options.shared.typeArrayMap[oid] = typarray
787790
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
788791
options.parsers[typarray].array = true
789-
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
792+
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid] || options.serializers[name], options, typarray)
790793
}
791794

792795
function tryNext(x, xs) {
@@ -973,7 +976,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
973976

974977
function Parse(str, parameters, types, name = '') {
975978
b().P().str(name + b.N).str(str + b.N).i16(parameters.length)
976-
parameters.forEach((x, i) => b.i32(types[i] || 0))
979+
parameters.forEach((x, i) => {
980+
const type = types[i]
981+
b.i32(options.shared.typeNameToOid[type] || type || 0)
982+
})
977983
return b.end()
978984
}
979985

cf/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ function parseOptions(a, b) {
496496
socket : o.socket,
497497
transform : parseTransform(o.transform || { undefined: undefined }),
498498
parameters : {},
499-
shared : { retries: 0, typeArrayMap: {} },
499+
shared : { retries: 0, typeArrayMap: {}, typeNameToOid: {}, typeOidToName: {} },
500500
...mergeUserTypes(o.types)
501501
}
502502
}

cf/src/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function handleValue(x, parameters, types, options) {
8787
return '$' + (types.push(
8888
x instanceof Parameter
8989
? (parameters.push(x.value), x.array
90-
? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)
90+
? x.array[options.shared.typeNameToOid[x.type] || x.type || inferType(x.value)] || x.type || firstIsString(x.value)
9191
: x.type
9292
)
9393
: (parameters.push(x), inferType(x))

cjs/src/connection.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
561561

562562
if (needsTypes) {
563563
initial.reserve && (initial = null)
564-
return fetchArrayTypes()
564+
return fetchTypes()
565565
}
566566

567567
initial && !initial.reserve && execute(initial)
@@ -657,7 +657,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
657657
name: transform.column.from
658658
? transform.column.from(x.toString('utf8', start, index - 1))
659659
: x.toString('utf8', start, index - 1),
660-
parser: parsers[type],
660+
parser: parsers[type] || parsers[options.shared.typeOidToName[type]],
661661
table,
662662
number,
663663
type
@@ -765,26 +765,29 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
765765
backend.secret = x.readUInt32BE(9)
766766
}
767767

768-
async function fetchArrayTypes() {
768+
async function fetchTypes() {
769769
needsTypes = false
770770
const types = await new Query([`
771-
select b.oid, b.typarray
772-
from pg_catalog.pg_type a
773-
left join pg_catalog.pg_type b on b.oid = a.typelem
774-
where a.typcategory = 'A'
775-
group by b.oid, b.typarray
776-
order by b.oid
771+
select oid, typname, typarray
772+
from pg_catalog.pg_type
773+
order by oid
777774
`], [], execute)
778-
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
775+
types.forEach(({ oid, typname, typarray }) => {
776+
options.shared.typeNameToOid[typname] = oid
777+
options.shared.typeOidToName[oid] = typname
778+
779+
if (typarray) addArrayType(oid, typarray)
780+
})
779781
}
780782

781783
function addArrayType(oid, typarray) {
782784
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return
783-
const parser = options.parsers[oid]
785+
const name = options.shared.typeOidToName[oid]
786+
const parser = options.parsers[oid] || options.parsers[name]
784787
options.shared.typeArrayMap[oid] = typarray
785788
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
786789
options.parsers[typarray].array = true
787-
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
790+
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid] || options.serializers[name], options, typarray)
788791
}
789792

790793
function tryNext(x, xs) {
@@ -971,7 +974,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
971974

972975
function Parse(str, parameters, types, name = '') {
973976
b().P().str(name + b.N).str(str + b.N).i16(parameters.length)
974-
parameters.forEach((x, i) => b.i32(types[i] || 0))
977+
parameters.forEach((x, i) => {
978+
const type = types[i]
979+
b.i32(options.shared.typeNameToOid[type] || type || 0)
980+
})
975981
return b.end()
976982
}
977983

cjs/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ function parseOptions(a, b) {
495495
socket : o.socket,
496496
transform : parseTransform(o.transform || { undefined: undefined }),
497497
parameters : {},
498-
shared : { retries: 0, typeArrayMap: {} },
498+
shared : { retries: 0, typeArrayMap: {}, typeNameToOid: {}, typeOidToName: {} },
499499
...mergeUserTypes(o.types)
500500
}
501501
}

cjs/src/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports.handleValue = handleValue;function handleValue(x, parameters, typ
8686
return '$' + (types.push(
8787
x instanceof Parameter
8888
? (parameters.push(x.value), x.array
89-
? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)
89+
? x.array[options.shared.typeNameToOid[x.type] || x.type || inferType(x.value)] || x.type || firstIsString(x.value)
9090
: x.type
9191
)
9292
: (parameters.push(x), inferType(x))

cjs/tests/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,24 @@ t('Point type array', async() => {
539539
return [30, (await sql`select x from test`)[0].x[1][1], await sql`drop table test`]
540540
})
541541

542+
t('Point type with named OIDs', async() => {
543+
const sql = postgres({
544+
...options,
545+
types: {
546+
point: {
547+
to: 'point',
548+
from: ['point'],
549+
serialize: ([x, y]) => '(' + x + ',' + y + ')',
550+
parse: (x) => x.slice(1, -1).split(',').map(x => +x)
551+
}
552+
}
553+
})
554+
555+
await sql`create table test (x point)`
556+
await sql`insert into test (x) values (${ sql.types.point([10, 20]) })`
557+
return [20, (await sql`select x from test`)[0].x[1], await sql`drop table test`]
558+
})
559+
542560
t('sql file', async() =>
543561
[1, (await sql.file(rel('select.sql')))[0].x]
544562
)

deno/src/connection.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
564564

565565
if (needsTypes) {
566566
initial.reserve && (initial = null)
567-
return fetchArrayTypes()
567+
return fetchTypes()
568568
}
569569

570570
initial && !initial.reserve && execute(initial)
@@ -660,7 +660,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
660660
name: transform.column.from
661661
? transform.column.from(x.toString('utf8', start, index - 1))
662662
: x.toString('utf8', start, index - 1),
663-
parser: parsers[type],
663+
parser: parsers[type] || parsers[options.shared.typeOidToName[type]],
664664
table,
665665
number,
666666
type
@@ -768,26 +768,29 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
768768
backend.secret = x.readUInt32BE(9)
769769
}
770770

771-
async function fetchArrayTypes() {
771+
async function fetchTypes() {
772772
needsTypes = false
773773
const types = await new Query([`
774-
select b.oid, b.typarray
775-
from pg_catalog.pg_type a
776-
left join pg_catalog.pg_type b on b.oid = a.typelem
777-
where a.typcategory = 'A'
778-
group by b.oid, b.typarray
779-
order by b.oid
774+
select oid, typname, typarray
775+
from pg_catalog.pg_type
776+
order by oid
780777
`], [], execute)
781-
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
778+
types.forEach(({ oid, typname, typarray }) => {
779+
options.shared.typeNameToOid[typname] = oid
780+
options.shared.typeOidToName[oid] = typname
781+
782+
if (typarray) addArrayType(oid, typarray)
783+
})
782784
}
783785

784786
function addArrayType(oid, typarray) {
785787
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return
786-
const parser = options.parsers[oid]
788+
const name = options.shared.typeOidToName[oid]
789+
const parser = options.parsers[oid] || options.parsers[name]
787790
options.shared.typeArrayMap[oid] = typarray
788791
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
789792
options.parsers[typarray].array = true
790-
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
793+
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid] || options.serializers[name], options, typarray)
791794
}
792795

793796
function tryNext(x, xs) {
@@ -974,7 +977,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
974977

975978
function Parse(str, parameters, types, name = '') {
976979
b().P().str(name + b.N).str(str + b.N).i16(parameters.length)
977-
parameters.forEach((x, i) => b.i32(types[i] || 0))
980+
parameters.forEach((x, i) => {
981+
const type = types[i]
982+
b.i32(options.shared.typeNameToOid[type] || type || 0)
983+
})
978984
return b.end()
979985
}
980986

deno/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ function parseOptions(a, b) {
496496
socket : o.socket,
497497
transform : parseTransform(o.transform || { undefined: undefined }),
498498
parameters : {},
499-
shared : { retries: 0, typeArrayMap: {} },
499+
shared : { retries: 0, typeArrayMap: {}, typeNameToOid: {}, typeOidToName: {} },
500500
...mergeUserTypes(o.types)
501501
}
502502
}

deno/src/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function handleValue(x, parameters, types, options) {
8787
return '$' + (types.push(
8888
x instanceof Parameter
8989
? (parameters.push(x.value), x.array
90-
? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)
90+
? x.array[options.shared.typeNameToOid[x.type] || x.type || inferType(x.value)] || x.type || firstIsString(x.value)
9191
: x.type
9292
)
9393
: (parameters.push(x), inferType(x))

0 commit comments

Comments
 (0)