Skip to content

Commit 599d0e7

Browse files
authored
expose table oid and column number (porsager#409)
* expose table oid and att num from description * add types * rename properties * add tests
1 parent bfd6a23 commit 599d0e7

File tree

8 files changed

+54
-3
lines changed

8 files changed

+54
-3
lines changed

cjs/src/connection.js

+4
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
615615
for (let i = 0; i < length; ++i) {
616616
start = index
617617
while (x[index++] !== 0);
618+
const table = x.readUInt32BE(index)
619+
const number = x.readUInt16BE(index + 4)
618620
const type = x.readUInt32BE(index + 6)
619621
query.statement.columns[i] = {
620622
name: transform.column.from
621623
? transform.column.from(x.toString('utf8', start, index - 1))
622624
: x.toString('utf8', start, index - 1),
623625
parser: parsers[type],
626+
table,
627+
number,
624628
type
625629
}
626630
index += 18

cjs/tests/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,18 @@ t('Describe a statement', async() => {
18911891
]
18921892
})
18931893

1894+
t('Include table oid and column number in column details', async() => {
1895+
await sql`create table tester (name text, age int)`
1896+
const r = await sql`select name, age from tester where name like $1 and age > $2`.describe();
1897+
const [{ oid }] = await sql`select oid from pg_class where relname = 'tester'`;
1898+
1899+
return [
1900+
`table:${oid},number:1|table:${oid},number:2`,
1901+
`${ r.columns.map(c => `table:${c.table},number:${c.number}`).join('|') }`,
1902+
await sql`drop table tester`
1903+
]
1904+
})
1905+
18941906
t('Describe a statement without parameters', async() => {
18951907
await sql`create table tester (name text, age int)`
18961908
const r = await sql`select name, age from tester`.describe()
@@ -2182,4 +2194,4 @@ t('Insert array with undefined transform', async() => {
21822194
(await sql`select x from test`)[0].x[0],
21832195
await sql`drop table test`
21842196
]
2185-
})
2197+
})

deno/src/connection.js

+4
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
619619
for (let i = 0; i < length; ++i) {
620620
start = index
621621
while (x[index++] !== 0);
622+
const table = x.readUInt32BE(index)
623+
const number = x.readUInt16BE(index + 4)
622624
const type = x.readUInt32BE(index + 6)
623625
query.statement.columns[i] = {
624626
name: transform.column.from
625627
? transform.column.from(x.toString('utf8', start, index - 1))
626628
: x.toString('utf8', start, index - 1),
627629
parser: parsers[type],
630+
table,
631+
number,
628632
type
629633
}
630634
index += 18

deno/tests/index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,18 @@ t('Describe a statement', async() => {
18931893
]
18941894
})
18951895

1896+
t('Include table oid and column number in column details', async() => {
1897+
await sql`create table tester (name text, age int)`
1898+
const r = await sql`select name, age from tester where name like $1 and age > $2`.describe();
1899+
const [{ oid }] = await sql`select oid from pg_class where relname = 'tester'`;
1900+
1901+
return [
1902+
`table:${oid},number:1|table:${oid},number:2`,
1903+
`${ r.columns.map(c => `table:${c.table},number:${c.number}`).join('|') }`,
1904+
await sql`drop table tester`
1905+
]
1906+
})
1907+
18961908
t('Describe a statement without parameters', async() => {
18971909
await sql`create table tester (name text, age int)`
18981910
const r = await sql`select name, age from tester`.describe()
@@ -2185,5 +2197,4 @@ t('Insert array with undefined transform', async() => {
21852197
await sql`drop table test`
21862198
]
21872199
})
2188-
21892200
;window.addEventListener("unload", () => Deno.exit(process.exitCode))

deno/types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@ declare namespace postgres {
505505
interface Column<T extends string> {
506506
name: T;
507507
type: number;
508+
table: number;
509+
number: number;
508510
parser?(raw: string): unknown;
509511
}
510512

src/connection.js

+4
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
615615
for (let i = 0; i < length; ++i) {
616616
start = index
617617
while (x[index++] !== 0);
618+
const table = x.readUInt32BE(index)
619+
const number = x.readUInt16BE(index + 4)
618620
const type = x.readUInt32BE(index + 6)
619621
query.statement.columns[i] = {
620622
name: transform.column.from
621623
? transform.column.from(x.toString('utf8', start, index - 1))
622624
: x.toString('utf8', start, index - 1),
623625
parser: parsers[type],
626+
table,
627+
number,
624628
type
625629
}
626630
index += 18

tests/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,18 @@ t('Describe a statement', async() => {
18911891
]
18921892
})
18931893

1894+
t('Include table oid and column number in column details', async() => {
1895+
await sql`create table tester (name text, age int)`
1896+
const r = await sql`select name, age from tester where name like $1 and age > $2`.describe();
1897+
const [{ oid }] = await sql`select oid from pg_class where relname = 'tester'`;
1898+
1899+
return [
1900+
`table:${oid},number:1|table:${oid},number:2`,
1901+
`${ r.columns.map(c => `table:${c.table},number:${c.number}`).join('|') }`,
1902+
await sql`drop table tester`
1903+
]
1904+
})
1905+
18941906
t('Describe a statement without parameters', async() => {
18951907
await sql`create table tester (name text, age int)`
18961908
const r = await sql`select name, age from tester`.describe()
@@ -2182,4 +2194,4 @@ t('Insert array with undefined transform', async() => {
21822194
(await sql`select x from test`)[0].x[0],
21832195
await sql`drop table test`
21842196
]
2185-
})
2197+
})

types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ declare namespace postgres {
497497
interface Column<T extends string> {
498498
name: T;
499499
type: number;
500+
table: number;
501+
number: number;
500502
parser?(raw: string): unknown;
501503
}
502504

0 commit comments

Comments
 (0)