From: Aaron Schulz Date: Sun, 18 Sep 2016 03:21:50 +0000 (-0700) Subject: Various small cleanups to ResultWrapper X-Git-Tag: 1.31.0-rc.0~5483^2 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=38035471dbc93f3b6e2570ca7a88c9ebcb030857;p=lhc%2Fweb%2Fwiklou.git Various small cleanups to ResultWrapper * Make FakeResultWrapper call super to avoid IDEA warnings * Lots of documentation cleanups Change-Id: Ifcd7163890fa8c7718e88c4244cc38371ed624fc --- diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php index 11acde78f6..46b1fc226e 100644 --- a/includes/libs/rdbms/database/DatabaseSqlite.php +++ b/includes/libs/rdbms/database/DatabaseSqlite.php @@ -300,12 +300,12 @@ class DatabaseSqlite extends DatabaseBase { $res = $this->mConn->query( $sql ); if ( $res === false ) { return false; - } else { - $r = $res instanceof ResultWrapper ? $res->result : $res; - $this->mAffectedRows = $r->rowCount(); - $res = new ResultWrapper( $this, $r->fetchAll() ); } + $r = $res instanceof ResultWrapper ? $res->result : $res; + $this->mAffectedRows = $r->rowCount(); + $res = new ResultWrapper( $this, $r->fetchAll() ); + return $res; } @@ -1060,4 +1060,4 @@ class DatabaseSqlite extends DatabaseBase { return 'SQLite ' . (string)$this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION ); } -} // end DatabaseSqlite class +} diff --git a/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php b/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php index 774def8086..1a046cf696 100644 --- a/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php +++ b/includes/libs/rdbms/database/resultwrapper/FakeResultWrapper.php @@ -4,35 +4,19 @@ * doesn't go anywhere near an actual database. */ class FakeResultWrapper extends ResultWrapper { - /** @var array */ - public $result = []; - - /** @var null And it's going to stay that way :D */ - protected $db = null; - - /** @var int */ - protected $pos = 0; - - /** @var array|stdClass|bool */ - protected $currentRow = null; + /** @var $result stdClass[] */ /** - * @param array $array + * @param stdClass[] $rows */ - function __construct( $array ) { - $this->result = $array; + function __construct( array $rows ) { + parent::__construct( null, $rows ); } - /** - * @return int - */ function numRows() { return count( $this->result ); } - /** - * @return array|bool - */ function fetchRow() { if ( $this->pos < count( $this->result ) ) { $this->currentRow = $this->result[$this->pos]; @@ -54,10 +38,6 @@ class FakeResultWrapper extends ResultWrapper { function free() { } - /** - * Callers want to be able to access fields with $this->fieldName - * @return bool|stdClass - */ function fetchObject() { $this->fetchRow(); if ( $this->currentRow ) { @@ -72,9 +52,6 @@ class FakeResultWrapper extends ResultWrapper { $this->currentRow = null; } - /** - * @return bool|stdClass - */ function next() { return $this->fetchObject(); } diff --git a/includes/libs/rdbms/database/resultwrapper/MssqlResultWrapper.php b/includes/libs/rdbms/database/resultwrapper/MssqlResultWrapper.php index cccb8f17d2..768511bf5b 100644 --- a/includes/libs/rdbms/database/resultwrapper/MssqlResultWrapper.php +++ b/includes/libs/rdbms/database/resultwrapper/MssqlResultWrapper.php @@ -1,5 +1,6 @@ db = $database; - + public function __construct( IDatabase $db = null, $result ) { + $this->db = $db; if ( $result instanceof ResultWrapper ) { $this->result = $result->result; } else { @@ -37,8 +50,8 @@ class ResultWrapper implements Iterator { * * @return int */ - function numRows() { - return $this->db->numRows( $this ); + public function numRows() { + return $this->getDB()->numRows( $this ); } /** @@ -49,8 +62,8 @@ class ResultWrapper implements Iterator { * @return stdClass|bool * @throws DBUnexpectedError Thrown if the database returns an error */ - function fetchObject() { - return $this->db->fetchObject( $this ); + public function fetchObject() { + return $this->getDB()->fetchObject( $this ); } /** @@ -60,38 +73,49 @@ class ResultWrapper implements Iterator { * @return array|bool * @throws DBUnexpectedError Thrown if the database returns an error */ - function fetchRow() { - return $this->db->fetchRow( $this ); + public function fetchRow() { + return $this->getDB()->fetchRow( $this ); } /** - * Free a result object + * Change the position of the cursor in a result object. + * See mysql_data_seek() + * + * @param int $row */ - function free() { - $this->db->freeResult( $this ); - unset( $this->result ); - unset( $this->db ); + public function seek( $row ) { + $this->getDB()->dataSeek( $this, $row ); } /** - * Change the position of the cursor in a result object. - * See mysql_data_seek() + * Free a result object * - * @param int $row + * This either saves memory in PHP (buffered queries) or on the server (unbuffered queries). + * In general, queries are not large enough in result sets for this to be worth calling. */ - function seek( $row ) { - $this->db->dataSeek( $this, $row ); + public function free() { + if ( $this->db ) { + $this->db->freeResult( $this ); + $this->db = null; + } + $this->result = null; } - /* - * ======= Iterator functions ======= - * Note that using these in combination with the non-iterator functions - * above may cause rows to be skipped or repeated. + /** + * @return IDatabase + * @throws RuntimeException */ + private function getDB() { + if ( !$this->db ) { + throw new RuntimeException( get_class( $this ) . ' needs a DB handle for iteration.' ); + } + + return $this->db; + } function rewind() { if ( $this->numRows() ) { - $this->db->dataSeek( $this, 0 ); + $this->getDB()->dataSeek( $this, 0 ); } $this->pos = 0; $this->currentRow = null; @@ -125,9 +149,6 @@ class ResultWrapper implements Iterator { return $this->currentRow; } - /** - * @return bool - */ function valid() { return $this->current() !== false; }