3 namespace Wikimedia\Rdbms
;
9 * Result wrapper for grabbing data queried from an IDatabase object
11 * Note that using the Iterator methods in combination with the non-Iterator
12 * DB result iteration functions may cause rows to be skipped or repeated.
14 * By default, this will use the iteration methods of the IDatabase handle if provided.
15 * Subclasses can override methods to make it solely work on the result resource instead.
16 * If no database is provided, and the subclass does not override the DB iteration methods,
17 * then a RuntimeException will be thrown when iteration is attempted.
19 * The result resource field should not be accessed from non-Database related classes.
20 * It is database class specific and is stored here to associate iterators with queries.
24 class ResultWrapper
implements IResultWrapper
{
25 /** @var resource|array|null Optional underlying result handle for subclass usage */
28 /** @var IDatabase|null */
33 /** @var stdClass|bool|null */
34 protected $currentRow;
37 * Create a row iterator from a result resource and an optional Database object
39 * Only Database-related classes should construct ResultWrapper. Other code may
40 * use the FakeResultWrapper subclass for convenience or compatibility shims, however.
42 * @param IDatabase|null $db Optional database handle
43 * @param ResultWrapper|array|resource $result Optional underlying result handle
45 public function __construct( IDatabase
$db = null, $result ) {
47 if ( $result instanceof ResultWrapper
) {
48 $this->result
= $result->result
;
50 $this->result
= $result;
54 public function numRows() {
55 return $this->getDB()->numRows( $this );
58 public function fetchObject() {
59 return $this->getDB()->fetchObject( $this );
62 public function fetchRow() {
63 return $this->getDB()->fetchRow( $this );
66 public function seek( $pos ) {
67 $this->getDB()->dataSeek( $this, $pos );
71 public function free() {
77 if ( $this->numRows() ) {
78 $this->getDB()->dataSeek( $this, 0 );
81 $this->currentRow
= null;
85 if ( $this->currentRow
=== null ) {
86 $this->currentRow
= $this->fetchObject();
89 return $this->currentRow
;
98 $this->currentRow
= $this->fetchObject();
100 return $this->currentRow
;
104 return $this->current() !== false;
109 * @throws RuntimeException
111 private function getDB() {
113 throw new RuntimeException( static::class . ' needs a DB handle for iteration.' );
121 * @deprecated since 1.29
123 class_alias( ResultWrapper
::class, 'ResultWrapper' );