rdbms: clean up and refactor ResultWrapper classes
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 29 May 2019 03:01:17 +0000 (20:01 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 29 May 2019 03:01:17 +0000 (20:01 -0700)
Change-Id: Ib1d71dbbd31e134cf89dca58f7aa77bdddc828c0

includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php
includes/libs/rdbms/database/resultwrapper/IResultWrapper.php
includes/libs/rdbms/database/resultwrapper/ResultWrapper.php

index aeb5d8d..3709de7 100644 (file)
@@ -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 );
        }
 }
 
index debf8a2..616fed9 100644 (file)
@@ -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
index a9befc2..b7938ad 100644 (file)
@@ -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;
+       }
 }
 
 /**