From d9f688698ce0396466710e5349e70bd7900ba975 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 28 May 2019 20:01:17 -0700 Subject: [PATCH] rdbms: clean up and refactor ResultWrapper classes Change-Id: Ib1d71dbbd31e134cf89dca58f7aa77bdddc828c0 --- .../resultwrapper/FakeResultWrapper.php | 61 +++++++++++-------- .../database/resultwrapper/IResultWrapper.php | 4 +- .../database/resultwrapper/ResultWrapper.php | 41 ++++++------- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php b/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php index aeb5d8de60..3709de7de2 100644 --- a/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php +++ b/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php @@ -9,10 +9,10 @@ use stdClass; * doesn't go anywhere near an actual database. */ class FakeResultWrapper extends ResultWrapper { - /** @var stdClass[] $result */ + /** @var stdClass[]|array[] $result */ /** - * @param stdClass[] $rows + * @param stdClass[]|array[] $rows */ function __construct( array $rows ) { parent::__construct( null, $rows ); @@ -22,43 +22,52 @@ class FakeResultWrapper extends ResultWrapper { return count( $this->result ); } - function fetchRow() { - if ( $this->pos < count( $this->result ) ) { - $this->currentRow = $this->result[$this->pos]; - } else { - $this->currentRow = false; - } - $this->pos++; - if ( is_object( $this->currentRow ) ) { - return get_object_vars( $this->currentRow ); - } else { - return $this->currentRow; - } + function fetchObject() { + $current = $this->current(); + + $this->next(); + + return $current; } - function seek( $row ) { - $this->pos = $row; + function fetchRow() { + $row = $this->valid() ? $this->result[$this->pos] : false; + + $this->next(); + + return is_object( $row ) ? (array)$row : $row; } - function free() { + function seek( $pos ) { + $this->pos = $pos; } - function fetchObject() { - $this->fetchRow(); - if ( $this->currentRow ) { - return (object)$this->currentRow; - } else { - return false; - } + function free() { + $this->result = null; } function rewind() { $this->pos = 0; - $this->currentRow = null; + } + + function current() { + $row = $this->valid() ? $this->result[$this->pos] : false; + + return is_array( $row ) ? (object)$row : $row; + } + + function key() { + return $this->pos; } function next() { - return $this->fetchObject(); + $this->pos++; + + return $this->current(); + } + + function valid() { + return array_key_exists( $this->pos, $this->result ); } } diff --git a/includes/libs/rdbms/database/resultwrapper/IResultWrapper.php b/includes/libs/rdbms/database/resultwrapper/IResultWrapper.php index debf8a27c3..616fed9d13 100644 --- a/includes/libs/rdbms/database/resultwrapper/IResultWrapper.php +++ b/includes/libs/rdbms/database/resultwrapper/IResultWrapper.php @@ -52,9 +52,9 @@ interface IResultWrapper extends Iterator { * Change the position of the cursor in a result object. * See mysql_data_seek() * - * @param int $row + * @param int $pos */ - public function seek( $row ); + public function seek( $pos ); /** * Free a result object diff --git a/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php b/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php index a9befc2bae..b7938ad256 100644 --- a/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php +++ b/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php @@ -30,8 +30,8 @@ class ResultWrapper implements IResultWrapper { /** @var int */ protected $pos = 0; - /** @var stdClass|null */ - protected $currentRow = null; + /** @var stdClass|bool|null */ + protected $currentRow; /** * Create a row iterator from a result resource and an optional Database object @@ -63,29 +63,16 @@ class ResultWrapper implements IResultWrapper { return $this->getDB()->fetchRow( $this ); } - public function seek( $row ) { - $this->getDB()->dataSeek( $this, $row ); + public function seek( $pos ) { + $this->getDB()->dataSeek( $this, $pos ); + $this->pos = $pos; } public function free() { - if ( $this->db ) { - $this->db = null; - } + $this->db = null; $this->result = null; } - /** - * @return IDatabase - * @throws RuntimeException - */ - private function getDB() { - if ( !$this->db ) { - throw new RuntimeException( static::class . ' needs a DB handle for iteration.' ); - } - - return $this->db; - } - function rewind() { if ( $this->numRows() ) { $this->getDB()->dataSeek( $this, 0 ); @@ -95,8 +82,8 @@ class ResultWrapper implements IResultWrapper { } function current() { - if ( is_null( $this->currentRow ) ) { - $this->next(); + if ( $this->currentRow === null ) { + $this->currentRow = $this->fetchObject(); } return $this->currentRow; @@ -116,6 +103,18 @@ class ResultWrapper implements IResultWrapper { function valid() { return $this->current() !== false; } + + /** + * @return IDatabase + * @throws RuntimeException + */ + private function getDB() { + if ( !$this->db ) { + throw new RuntimeException( static::class . ' needs a DB handle for iteration.' ); + } + + return $this->db; + } } /** -- 2.20.1