From 106ca8a1786dd9a3b6c790b6643f235be69f9106 Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Thu, 14 Sep 2023 15:43:14 -0400 Subject: [PATCH] Fix get value of last column with same name in result rows (#3063) * 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. --- packages/pg/lib/result.js | 2 ++ .../test/integration/gh-issues/3062-tests.js | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 packages/pg/test/integration/gh-issues/3062-tests.js diff --git a/packages/pg/lib/result.js b/packages/pg/lib/result.js index 187c0d016..f9053c7a9 100644 --- a/packages/pg/lib/result.js +++ b/packages/pg/lib/result.js @@ -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 diff --git a/packages/pg/test/integration/gh-issues/3062-tests.js b/packages/pg/test/integration/gh-issues/3062-tests.js new file mode 100644 index 000000000..7666751ed --- /dev/null +++ b/packages/pg/test/integration/gh-issues/3062-tests.js @@ -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() +})