amphp / postgres

Async Postgres client for PHP based on Amp.
MIT License
97 stars 20 forks source link

Row count reported by driver hidden behind private API #30

Closed Bilge closed 3 years ago

Bilge commented 4 years ago

One of the most useful things to know when performing a query is how many results are in the results set. This information is provided by the driver and exposed by a public method in this library, but by an object that is inaccessible to the consumer.

PgSqlResultSet exposes the method numRows(), but PgSqlResultSet is wrapped by PooledResultSet which is what the consumer actually receives and which does not provided any public access to the underlying PgSqlResultSet. Due to this, the row count is inaccessible to the consumer.

Also, and this is a side issue, even if it were possible to access numRows() via PooledResultSet, the library only declares interfaces, and the method is not present on the generic SQL interface so declared. The consumer would have to know the underlying object's exact type and override the type system to access it.

trowski commented 4 years ago

The row count cannot be known for all queries and this method will be removed from PgSqlResult in the next version. The new Result object will have getRowCount(), but it may return null if the number of rows is unknown.

Bilge commented 4 years ago

If it's a results set, to my knowledge, the row count can always be known. If it is not, then it's an entirely different object: PgSqlCommandResult. In any case, I will hold off making a PR if you are going to address this issue directly. Although, if the new version is not backwards-compatible, I still think it is worth addressing the issue in this major version.

trowski commented 4 years ago

Unbuffered result sets do not provide a total row count, so that's why the method is missing from the pool result set. I potentially could add numRows() to PooledResultSet in this version, but it would sometimes return null if the pooled result set is unbuffered.

See the streams branch for the upcoming version in sql, sql-common, and this repo.

Bilge commented 4 years ago

What determines whether a results set is "buffered"?

trowski commented 4 years ago

The ext-pq driver is capable of returning unbuffered results. Usually if a query is more than a few rows it is not buffered, but sent in chunks instead. PqUnbufferedResultSet does not have a numRows() method because of this.