Skip to content

Option to use pre-shaped result rows; fixes #3042 #3043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Query extends EventEmitter {
this.isPreparedStatement = false
this._canceledDueToError = false
this._promise = null
this._prebuiltEmptyResultObject = null
}

requiresPreparation() {
Expand Down Expand Up @@ -64,6 +65,7 @@ class Query extends EventEmitter {
}
this._result = new Result(this._rowMode, this.types)
this._results.push(this._result)
this._prebuiltEmptyResultObject = null
}
}

Expand All @@ -83,8 +85,12 @@ class Query extends EventEmitter {
return
}

if (!this._prebuiltEmptyResultObject) {
this._createPrebuiltEmptyResultObject()
}

try {
row = this._result.parseRow(msg.fields)
row = this._result.parseRow(msg.fields, { ... this._prebuiltEmptyResultObject })
} catch (err) {
this._canceledDueToError = err
return
Expand Down Expand Up @@ -236,6 +242,13 @@ class Query extends EventEmitter {
handleCopyData(msg, connection) {
// noop
}
_createPrebuiltEmptyResultObject() {
var row = {};
for (var i = 0; i < this._result.fields.length; i++) {
row[this._result.fields[i].name] = null
}
this._prebuiltEmptyResultObject = row
}
}

module.exports = Query
3 changes: 1 addition & 2 deletions packages/pg/lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class Result {
return row
}

parseRow(rowData) {
var row = {}
parseRow(rowData, row = {}) {
for (var i = 0, len = rowData.length; i < len; i++) {
var rawValue = rowData[i]
var field = this.fields[i].name
Expand Down