Change-Id: Ib1d71dbbd31e134cf89dca58f7aa77bdddc828c0
* doesn't go anywhere near an actual database.
*/
class FakeResultWrapper extends ResultWrapper {
* 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 );
*/
function __construct( array $rows ) {
parent::__construct( null, $rows );
return count( $this->result );
}
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 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;
}
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;
- return $this->fetchObject();
+ $this->pos++;
+
+ return $this->current();
+ }
+
+ function valid() {
+ return array_key_exists( $this->pos, $this->result );
* Change the position of the cursor in a result object.
* See mysql_data_seek()
*
* Change the position of the cursor in a result object.
* See mysql_data_seek()
*
- public function seek( $row );
+ public function seek( $pos );
/**
* Free a result object
/**
* Free a result object
/** @var int */
protected $pos = 0;
/** @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
/**
* Create a row iterator from a result resource and an optional Database object
return $this->getDB()->fetchRow( $this );
}
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() {
}
public function free() {
- if ( $this->db ) {
- $this->db = 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 );
function rewind() {
if ( $this->numRows() ) {
$this->getDB()->dataSeek( $this, 0 );
- if ( is_null( $this->currentRow ) ) {
- $this->next();
+ if ( $this->currentRow === null ) {
+ $this->currentRow = $this->fetchObject();
}
return $this->currentRow;
}
return $this->currentRow;
function valid() {
return $this->current() !== false;
}
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;
+ }