3 namespace Wikimedia\Rdbms
;
10 * Result wrapper for grabbing data queried from an IDatabase object
12 * Note that using the Iterator methods in combination with the non-Iterator
13 * DB result iteration functions may cause rows to be skipped or repeated.
15 * By default, this will use the iteration methods of the IDatabase handle if provided.
16 * Subclasses can override methods to make it solely work on the result resource instead.
17 * If no database is provided, and the subclass does not override the DB iteration methods,
18 * then a RuntimeException will be thrown when iteration is attempted.
20 * The result resource field should not be accessed from non-Database related classes.
21 * It is database class specific and is stored here to associate iterators with queries.
25 class ResultWrapper
implements IResultWrapper
{
26 /** @var resource|array|null Optional underlying result handle for subclass usage */
29 /** @var IDatabase|null */
34 /** @var stdClass|null */
35 protected $currentRow = null;
38 * Create a row iterator from a result resource and an optional Database object
40 * Only Database-related classes should construct ResultWrapper. Other code may
41 * use the FakeResultWrapper subclass for convenience or compatibility shims, however.
43 * @param IDatabase|null $db Optional database handle
44 * @param ResultWrapper|array|resource $result Optional underlying result handle
46 public function __construct( IDatabase
$db = null, $result ) {
48 if ( $result instanceof ResultWrapper
) {
49 $this->result
= $result->result
;
51 $this->result
= $result;
55 public function numRows() {
56 return $this->getDB()->numRows( $this );
59 public function fetchObject() {
60 return $this->getDB()->fetchObject( $this );
63 public function fetchRow() {
64 return $this->getDB()->fetchRow( $this );
67 public function seek( $row ) {
68 $this->getDB()->dataSeek( $this, $row );
71 public function free() {
73 $this->db
->freeResult( $this );
81 * @throws RuntimeException
83 private function getDB() {
85 throw new RuntimeException( static::class . ' needs a DB handle for iteration.' );
92 if ( $this->numRows() ) {
93 $this->getDB()->dataSeek( $this, 0 );
96 $this->currentRow
= null;
100 if ( is_null( $this->currentRow
) ) {
104 return $this->currentRow
;
113 $this->currentRow
= $this->fetchObject();
115 return $this->currentRow
;
119 return $this->current() !== false;
123 class_alias( ResultWrapper
::class, 'ResultWrapper' );