Skip to content

Commit

Permalink
Fix get value of last column with same name in result rows (#3063)
Browse files Browse the repository at this point in the history
* Add failing test for result rows with the same column names

* Fix handling of duplicate column names in results to ensure last value is populated

Fixes handling of result rows that have the same column name duplicated in the results to ensure
that the last value is the one returned to the user. This was the old behavior but unintentionally
broken when the pre-built object optimization was added.
  • Loading branch information
sehrope authored Sep 14, 2023
1 parent a84ebb3 commit 106ca8a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/pg/lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class Result {
var field = this.fields[i].name
if (rawValue !== null) {
row[field] = this._parsers[i](rawValue)
} else {
row[field] = null
}
}
return row
Expand Down
21 changes: 21 additions & 0 deletions packages/pg/test/integration/gh-issues/3062-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'
const helper = require('../test-helper')
var assert = require('assert')
const suite = new helper.Suite()

// https://github.com/brianc/node-postgres/issues/3062
suite.testAsync('result fields with the same name should pick the last value', async () => {
const client = new helper.pg.Client()
await client.connect()

const { rows: [shouldBeNullRow] } = await client.query('SELECT NULL AS test, 10 AS test, NULL AS test')
assert.equal(shouldBeNullRow.test, null)

const { rows: [shouldBeTwelveRow] } = await client.query('SELECT NULL AS test, 10 AS test, 12 AS test')
assert.equal(shouldBeTwelveRow.test, 12)

const { rows: [shouldBeAbcRow] } = await client.query(`SELECT NULL AS test, 10 AS test, 12 AS test, 'ABC' AS test`)
assert.equal(shouldBeAbcRow.test, 'ABC')

await client.end()
})

0 comments on commit 106ca8a

Please sign in to comment.