Skip to content

PostgreSQL refactor: handle all data returned by native functions in DB_pgsql::simpleQuery()#46

Merged
schengawegga merged 3 commits intopear:trunkfrom
DanCld:pgsql-simplequery-refactor
Jun 27, 2025
Merged

PostgreSQL refactor: handle all data returned by native functions in DB_pgsql::simpleQuery()#46
schengawegga merged 3 commits intopear:trunkfrom
DanCld:pgsql-simplequery-refactor

Conversation

@DanCld
Copy link
Contributor

@DanCld DanCld commented May 2, 2025

This refactor was inspired by the need to support a native PostgreSQL feature, that is handling data returned by DML queries such as INSERT or UPDATE.

In practical terms, PostgreSQL supports queries like "INSERT INTO ... VALUES ... RETURNING..." that can be used to easily retrieve the last insertion id, although the feature is far more extensive, as per docs: https://www.postgresql.org/docs/current/dml-returning.html

This refactor did away with the strong differentiation between the "read" and "write" code paths, while only relying on the response received from the PostgreSQL backend. This also allows for better error handling regarding errors returned during the actual query execution.

@DanCld DanCld closed this May 2, 2025
@DanCld DanCld deleted the pgsql-simplequery-refactor branch May 2, 2025 15:53
@DanCld DanCld restored the pgsql-simplequery-refactor branch May 2, 2025 16:07
@DanCld DanCld reopened this May 2, 2025
@DanCld DanCld marked this pull request as ready for review May 2, 2025 16:36
// this query has returned data
$this->row[$this->_resultId($result)] = 0; // reset the row counter.
$numrows = $this->numRows($result);
if (is_object($numrows)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there is a problem with this if-statement.
According to the documentation, the function pg_num_rows() returns a '-1' value on error.
In the method numRows() here, it checks for a 'null' response, to identify an error.
I think this is not correct in numRows().
Can you check this, please?
I don´t have any PG instance to check this on my own.
Maybe the failure handling in this method has never worked well...... ;-)

The other changes looks good to me.

pg_num_rows() only returns int values, so the null result comparison never achieved the proper intent.
@DanCld DanCld requested a review from schengawegga June 26, 2025 19:07
@schengawegga schengawegga merged commit 1e6dbe3 into pear:trunk Jun 27, 2025
@schengawegga schengawegga added this to the Version 1.12.3 milestone Nov 10, 2025
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.

2 participants