Skip to content

Do not parse command complete info unless accessed #3511

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
76 changes: 54 additions & 22 deletions packages/pg/lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const matchRegexp = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/
// passed as second argument to provided callback
class Result {
constructor(rowMode, types) {
this.command = null
this.rowCount = null
this.oid = null
this._command = undefined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking of esoteric backwards compatibility needs: since the minimum Node version is 16, private class fields are fully supported. Could start using those instead of an underscore prefix for everything, to make future backwards compatibility easier to manage (since there’s no way user code can come to rely on private fields, whether it’s through direct access or stringification).

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could start using those instead of an underscore prefix for everything, to make future backwards compatibility easier to manage (since there’s no way user code can come to rely on private fields, whether it’s through direct access or stringification).

i love this idea. you're so awesome, ty!

this._rowCount = undefined
this._oid = undefined
this._commandCompleteMsg = undefined
this.rows = []
this.fields = []
this._parsers = undefined
Expand All @@ -26,25 +27,10 @@ class Result {

// adds a command complete message
addCommandComplete(msg) {
let match
if (msg.text) {
// pure javascript
match = matchRegexp.exec(msg.text)
} else {
// native bindings
match = matchRegexp.exec(msg.command)
}
if (match) {
this.command = match[1]
if (match[3]) {
// COMMAND OID ROWS
this.oid = parseInt(match[2], 10)
this.rowCount = parseInt(match[3], 10)
} else if (match[2]) {
// COMMAND ROWS
this.rowCount = parseInt(match[2], 10)
}
}
this._commandCompleteMsg = msg
this._command = undefined
this._rowCount = undefined
this._oid = undefined
}

_parseRowAsArray(rowData) {
Expand Down Expand Up @@ -104,6 +90,52 @@ class Result {

this._prebuiltEmptyResultObject = { ...row }
}
get command() {
this._parseCommandCompleteIfNeeded()
return this._command
}

get rowCount() {
this._parseCommandCompleteIfNeeded()
return this._rowCount
}

get oid() {
this._parseCommandCompleteIfNeeded()
return this._oid
}

_parseCommandCompleteIfNeeded() {
if (this._command !== undefined || !this._commandCompleteMsg) {
return
}
let match
const msg = this._commandCompleteMsg
if (msg.text) {
match = matchRegexp.exec(msg.text)
} else {
match = matchRegexp.exec(msg.command)
}
if (match) {
this._command = match[1]
if (match[3]) {
// COMMAND OID ROWS
this._oid = parseInt(match[2], 10)
this._rowCount = parseInt(match[3], 10)
} else if (match[2]) {
// COMMAND ROWS
this._oid = null
this._rowCount = parseInt(match[2], 10)
} else {
this._oid = null
this._rowCount = null
}
} else {
this._command = null
this._oid = null
this._rowCount = null
}
}
}

module.exports = Result