3 * Result wrapper for grabbing data queried from an IDatabase object
5 * Note that using the Iterator methods in combination with the non-Iterator
6 * DB result iteration functions may cause rows to be skipped or repeated.
8 * By default, this will use the iteration methods of the IDatabase handle if provided.
9 * Subclasses can override methods to make it solely work on the result resource instead.
10 * If no database is provided, and the subclass does not override the DB iteration methods,
11 * then a RuntimeException will be thrown when iteration is attempted.
13 * The result resource field should not be accessed from non-Database related classes.
14 * It is database class specific and is stored here to associate iterators with queries.
18 class ResultWrapper
implements Iterator
{
19 /** @var resource|array|null Optional underlying result handle for subclass usage */
22 /** @var IDatabase|null */
27 /** @var stdClass|null */
28 protected $currentRow = null;
31 * Create a row iterator from a result resource and an optional Database object
33 * Only Database-related classes should construct ResultWrapper. Other code may
34 * use the FakeResultWrapper subclass for convenience or compatibility shims, however.
36 * @param IDatabase|null $db Optional database handle
37 * @param ResultWrapper|array|resource $result Optional underlying result handle
39 public function __construct( IDatabase
$db = null, $result ) {
41 if ( $result instanceof ResultWrapper
) {
42 $this->result
= $result->result
;
44 $this->result
= $result;
49 * Get the number of rows in a result object
53 public function numRows() {
54 return $this->getDB()->numRows( $this );
58 * Fetch the next row from the given result object, in object form. Fields can be retrieved with
59 * $row->fieldname, with fields acting like member variables. If no more rows are available,
62 * @return stdClass|bool
63 * @throws DBUnexpectedError Thrown if the database returns an error
65 public function fetchObject() {
66 return $this->getDB()->fetchObject( $this );
70 * Fetch the next row from the given result object, in associative array form. Fields are
71 * retrieved with $row['fieldname']. If no more rows are available, false is returned.
74 * @throws DBUnexpectedError Thrown if the database returns an error
76 public function fetchRow() {
77 return $this->getDB()->fetchRow( $this );
81 * Change the position of the cursor in a result object.
82 * See mysql_data_seek()
86 public function seek( $row ) {
87 $this->getDB()->dataSeek( $this, $row );
91 * Free a result object
93 * This either saves memory in PHP (buffered queries) or on the server (unbuffered queries).
94 * In general, queries are not large enough in result sets for this to be worth calling.
96 public function free() {
98 $this->db
->freeResult( $this );
101 $this->result
= null;
106 * @throws RuntimeException
108 private function getDB() {
110 throw new RuntimeException( get_class( $this ) . ' needs a DB handle for iteration.' );
117 if ( $this->numRows() ) {
118 $this->getDB()->dataSeek( $this, 0 );
121 $this->currentRow
= null;
125 * @return stdClass|array|bool
128 if ( is_null( $this->currentRow
) ) {
132 return $this->currentRow
;
147 $this->currentRow
= $this->fetchObject();
149 return $this->currentRow
;
153 return $this->current() !== false;