Skip to content

Commit d89f824

Browse files
committed
Add columns to result - fixes porsager#18
1 parent fc6804a commit d89f824

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

lib/backend.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ function Backend({
107107

108108
value = length === -1
109109
? null
110-
: column.p === undefined
110+
: column.parser === undefined
111111
? x.toString('utf8', index, index += length)
112-
: column.p.array === true
113-
? column.p(x.toString('utf8', index + 1, index += length))
114-
: column.p(x.toString('utf8', index, index += length))
112+
: column.parser.array === true
113+
? column.parser(x.toString('utf8', index + 1, index += length))
114+
: column.parser(x.toString('utf8', index, index += length))
115115

116-
row[column.n] = transform.value ? transform.value(value) : value
116+
row[column.name] = transform.value ? transform.value(value) : value
117117
}
118118

119119
backend.query.stream
120-
? backend.query.stream(transform.row ? transform.row(row) : row, rows++)
120+
? backend.query.stream(transform.row ? transform.row(row) : row, backend.query.result)
121121
: (backend.query.result[rows++] = transform.row ? transform.row(row) : row)
122122
}
123123

@@ -188,7 +188,7 @@ function Backend({
188188
rows = 0
189189

190190
if (backend.query.statement.columns)
191-
return
191+
return backend.query.result.columns = backend.query.statement.columns
192192

193193
const length = x.readInt16BE(5)
194194
let index = 7
@@ -199,14 +199,17 @@ function Backend({
199199
for (let i = 0; i < length; ++i) {
200200
start = index
201201
while (x[index++] !== 0);
202+
const type = x.readInt32BE(index + 6)
202203
backend.query.statement.columns[i] = {
203-
n: transform.column
204+
name: transform.column
204205
? transform.column(x.toString('utf8', start, index - 1))
205206
: x.toString('utf8', start, index - 1),
206-
p: parsers[x.readInt32BE(index + 6)]
207+
parser: parsers[type],
208+
type
207209
}
208210
index += 18
209211
}
212+
backend.query.result.columns = backend.query.statement.columns
210213
}
211214

212215
/* c8 ignore next 3 */

tests/index.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ t('Stream returns empty array', async() => {
785785

786786
t('Cursor works', async() => {
787787
const order = []
788-
await sql`select 1 as x union select 2 as x`.cursor(async (x) => {
788+
await sql`select 1 as x union select 2 as x`.cursor(async(x) => {
789789
order.push(x.x + 'a')
790790
await new Promise(r => setTimeout(r, 100))
791791
order.push(x.x + 'b')
@@ -795,15 +795,15 @@ t('Cursor works', async() => {
795795

796796
t('Cursor custom n works', async() => {
797797
const order = []
798-
await sql`select * from generate_series(1,20)`.cursor(10, async (x) => {
798+
await sql`select * from generate_series(1,20)`.cursor(10, async(x) => {
799799
order.push(x.length)
800800
})
801801
return ['10,10', order.join(',')]
802802
})
803803

804804
t('Cursor cancel works', async() => {
805805
let result
806-
await sql`select * from generate_series(1,10) as x`.cursor(async ({ x }) => {
806+
await sql`select * from generate_series(1,10) as x`.cursor(async({ x }) => {
807807
result = x
808808
return sql.END
809809
})
@@ -812,10 +812,10 @@ t('Cursor cancel works', async() => {
812812

813813
t('Cursor throw works', async() => {
814814
const order = []
815-
await sql`select 1 as x union select 2 as x`.cursor(async (x) => {
815+
await sql`select 1 as x union select 2 as x`.cursor(async(x) => {
816816
order.push(x.x + 'a')
817817
await new Promise(r => setTimeout(r, 100))
818-
throw 'watty'
818+
throw new Error('watty')
819819
}).catch(() => order.push('err'))
820820
return ['1aerr', order.join('')]
821821
})
@@ -965,3 +965,24 @@ t('requests works after single connect_timeout', async() => {
965965
t('Postgres errors are of type PostgresError', async() =>
966966
[true, (await sql`bad keyword`.catch(e => e)) instanceof sql.PostgresError]
967967
)
968+
969+
t('Result has columns spec', async() =>
970+
['x', (await sql`select 1 as x`).columns[0].name]
971+
)
972+
973+
t('Stream has result as second argument', async() => {
974+
let x
975+
await sql`select 1 as x`.stream((_, result) => x = result)
976+
return ['x', x.columns[0].name]
977+
})
978+
979+
t('Result as arrays', async() => {
980+
const sql = postgres({
981+
...options,
982+
transform: {
983+
row: x => Object.values(x)
984+
}
985+
})
986+
987+
return ['1,2', (await sql`select 1 as a, 2 as b`)[0].join(',')]
988+
})

tests/test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ async function test(o, name, fn) {
2424
new Promise((resolve, reject) => fn.timer = setTimeout(() => reject('Timed out'), 500)),
2525
fn()
2626
]))
27-
.then(([expected, got]) => {
27+
.then((x) => {
28+
if (!Array.isArray(x))
29+
throw new Error('Test should return result array')
30+
31+
const [expected, got] = x
2832
if (expected !== got)
2933
throw new Error(expected + ' != ' + util.inspect(got))
3034
tests[line].succeeded = true

0 commit comments

Comments
 (0)