wiki.techinc.nl/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php
Tim Starling bc76602493 DBMS-specific ResultWrapper subclasses
Cleanup after the switch of Database::query() to return ResultWrapper
instead of resource.

* Soft-deprecate the IResultWrapper accessors in IDatabase.
* Move relevant DBMS-specific functionality to ResultWrapper subclasses.
  The deprecated methods in IResultWrapper become short and simple.
  ResultWrapper is now abstract (b/c break).
* Move the implementation of fieldName(), numFields() and one of the
  fieldInfo() implementations to the ResultWrapper subclass in order to
  avoid ResultWrapper::unwrap() calls.
* Make Database::doQuery() return a ResultWrapper subclass instead of
  underlying result data, so that the Database parent class does not
  need to be aware of wrapper construction.
* Hard-deprecate ResultWrapper::unwrap(),
  DatabaseMysqlBase::fieldType(), DatabasePostgres::fieldType().
* Fix the inefficient seeking method in SQLite.
* Make FakeResultWrapper extend ResultWrapper with an implementation
  similar to the SQLite one. This is possible because ResultWrapper does
  not depend on IDatabase anymore.
* Resolve fixme in DatabasePostgres: from studying the source,
  neither pg_fetch_object() nor pg_num_rows() can set an error
  retrievable with pg_last_error(). Removed unnecessary warning
  suppression.
* ResultWrapperTest didn't make sense as a unit test anymore, so I
  adapted it as an integration test against the current DBMS.

This change also means that ResultWrapper::key() always gives the
correct offset, even if Iterator methods are not being used.

Bug: T286694
Change-Id: I935835316c0bd7d3d061bd8fde9c9ce99ce756ec
2021-07-21 06:54:26 -07:00

56 lines
1.2 KiB
PHP

<?php
namespace Wikimedia\Rdbms;
use RuntimeException;
use stdClass;
/**
* Overloads the relevant methods of the real ResultWrapper so it
* doesn't go anywhere near an actual database.
*/
class FakeResultWrapper extends ResultWrapper {
/** @var stdClass[]|array[] */
protected $result;
/**
* @param stdClass[]|array[]|FakeResultWrapper $result
*/
public function __construct( $result ) {
if ( $result instanceof self ) {
$this->result = $result->result;
} else {
$this->result = $result;
}
}
protected function doNumRows() {
return count( $this->result );
}
protected function doFetchObject() {
$value = $this->result[$this->currentPos] ?? false;
return is_array( $value ) ? (object)$value : $value;
}
protected function doFetchRow() {
$row = $this->doFetchObject();
return is_object( $row ) ? get_object_vars( $row ) : $row;
}
protected function doSeek( $pos ) {
}
protected function doFree() {
$this->result = null;
}
protected function doGetFieldNames() {
throw new RuntimeException( __METHOD__ . ' is unimplemented' );
}
}
/**
* @deprecated since 1.29
*/
class_alias( FakeResultWrapper::class, 'FakeResultWrapper' );