3 use Wikimedia\Rdbms\IResultWrapper
;
6 * Result wrapper for grabbing data queried from an IDatabase object
8 * Note that using the Iterator methods in combination with the non-Iterator
9 * DB result iteration functions may cause rows to be skipped or repeated.
11 * By default, this will use the iteration methods of the IDatabase handle if provided.
12 * Subclasses can override methods to make it solely work on the result resource instead.
13 * If no database is provided, and the subclass does not override the DB iteration methods,
14 * then a RuntimeException will be thrown when iteration is attempted.
16 * The result resource field should not be accessed from non-Database related classes.
17 * It is database class specific and is stored here to associate iterators with queries.
21 class ResultWrapper
implements IResultWrapper
{
22 /** @var resource|array|null Optional underlying result handle for subclass usage */
25 /** @var IDatabase|null */
30 /** @var stdClass|null */
31 protected $currentRow = null;
34 * Create a row iterator from a result resource and an optional Database object
36 * Only Database-related classes should construct ResultWrapper. Other code may
37 * use the FakeResultWrapper subclass for convenience or compatibility shims, however.
39 * @param IDatabase|null $db Optional database handle
40 * @param ResultWrapper|array|resource $result Optional underlying result handle
42 public function __construct( IDatabase
$db = null, $result ) {
44 if ( $result instanceof ResultWrapper
) {
45 $this->result
= $result->result
;
47 $this->result
= $result;
51 public function numRows() {
52 return $this->getDB()->numRows( $this );
55 public function fetchObject() {
56 return $this->getDB()->fetchObject( $this );
59 public function fetchRow() {
60 return $this->getDB()->fetchRow( $this );
63 public function seek( $row ) {
64 $this->getDB()->dataSeek( $this, $row );
67 public function free() {
69 $this->db
->freeResult( $this );
77 * @throws RuntimeException
79 private function getDB() {
81 throw new RuntimeException( get_class( $this ) . ' needs a DB handle for iteration.' );
88 if ( $this->numRows() ) {
89 $this->getDB()->dataSeek( $this, 0 );
92 $this->currentRow
= null;
96 if ( is_null( $this->currentRow
) ) {
100 return $this->currentRow
;
109 $this->currentRow
= $this->fetchObject();
111 return $this->currentRow
;
115 return $this->current() !== false;