Skip to content
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

docs(api/result): clarify that result.rowCount can be null #2967

Merged
merged 1 commit into from
May 1, 2023

Conversation

futile
Copy link
Contributor

@futile futile commented Apr 25, 2023

I stumbled upon this when running a LOCK-command and looking at the result.rowCount-field, which was null.

This is because result.rowCount is initialized to null, but only set to an int-value if the returned command tag consists of more than one word, which is not the case in general.

The postgres docs (https://www.postgresql.org/docs/current/protocol-message-formats.html, ctrl+f for CommandComplete) say:

The command tag. This is usually a single word that identifies which SQL command was completed.

This explains why, for commands such as LOCK, there is no [ROWS]-field to parse.

See the logic in node-postgres here:

this.rowCount = null
this.oid = null
this.rows = []
this.fields = []
this._parsers = undefined
this._types = types
this.RowCtor = null
this.rowAsArray = rowMode === 'array'
if (this.rowAsArray) {
this.parseRow = this._parseRowAsArray
}
}
// adds a command complete message
addCommandComplete(msg) {
var 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]) {
// COMMMAND 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)
}
}
}

Running a command such as LOCK, postgres will return a command tag of simply the form LOCK, and thus result.rowCount will stay null.

This PR clarififes this in the docs.

`result.rowCount` is initialized to `null`, but only set to an `int`-value if the returned command tag consists of more than one word, which is not the case in general.

For example, the `LOCK` command will return a command tag of simply the form `LOCK`, and thus `result.rowCount` will stay `null`.
@brianc
Copy link
Owner

brianc commented May 1, 2023

ohhh nice doc addition! 👏 🙏

@brianc brianc merged commit d63c761 into brianc:master May 1, 2023
2 of 15 checks passed
@futile futile deleted the patch-1 branch May 1, 2023 19:02
thijs pushed a commit to thijs/node-postgres that referenced this pull request Aug 1, 2023
…c#2967)

`result.rowCount` is initialized to `null`, but only set to an `int`-value if the returned command tag consists of more than one word, which is not the case in general.

For example, the `LOCK` command will return a command tag of simply the form `LOCK`, and thus `result.rowCount` will stay `null`.
fdietze added a commit to fdietze/DefinitelyTyped that referenced this pull request Oct 9, 2023
Here is the PR, which explains it and fixed it in the docs: brianc/node-postgres#2967
typescript-bot pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this pull request Nov 6, 2023
Here is the PR, which explains it and fixed it in the docs: brianc/node-postgres#2967
fisehara added a commit to balena-io/pinejs that referenced this pull request Nov 9, 2023
rowCount can be NULL in the case of PG returning from `LOCK`

Refernece:
DefinitelyTyped/DefinitelyTyped#66990
brianc/node-postgres#2967

In pinejs the case is handled and as of now we don't want to populate a breaking change by transparently push the types to callers.

Change-type: patch
Signed-off-by: Harald Fischer <harald@balena.io>
tadzik added a commit to tadzik/matrix-appservice-irc that referenced this pull request Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants