rdbms: clean up and refactor ResultWrapper classes

Change-Id: Ib1d71dbbd31e134cf89dca58f7aa77bdddc828c0
This commit is contained in:
Aaron Schulz 2019-05-28 20:01:17 -07:00
parent d9677363ea
commit d9f688698c
3 changed files with 58 additions and 50 deletions

View 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 seek( $pos ) {
$this->pos = $pos;
}
function free() {
}
function fetchObject() {
$this->fetchRow();
if ( $this->currentRow ) {
return (object)$this->currentRow;
} else {
return false;
}
$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 );
}
}

View 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

View 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;
}
}
/**