MediaWiki  master
ResultWrapper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use OutOfBoundsException;
6 use stdClass;
7 
23 abstract class ResultWrapper implements IResultWrapper {
28  protected $nextPos = 0;
29 
34  protected $currentPos = 0;
35 
40  protected $currentRow;
41 
45  private $fieldNames;
46 
53  abstract protected function doNumRows();
54 
63  abstract protected function doFetchObject();
64 
72  abstract protected function doFetchRow();
73 
80  abstract protected function doSeek( $pos );
81 
85  abstract protected function doFree();
86 
92  abstract protected function doGetFieldNames();
93 
94  public function numRows() {
95  return $this->doNumRows();
96  }
97 
98  public function count() {
99  return $this->doNumRows();
100  }
101 
102  public function fetchObject() {
103  $this->currentPos = $this->nextPos++;
104  $this->currentRow = $this->doFetchObject();
105  return $this->currentRow;
106  }
107 
108  public function fetchRow() {
109  $this->currentPos = $this->nextPos++;
110  $this->currentRow = $this->doFetchRow();
111  return $this->currentRow;
112  }
113 
114  public function seek( $pos ) {
115  $numRows = $this->numRows();
116  // Allow seeking to zero if there are no results
117  $max = $numRows ? $numRows - 1 : 0;
118  if ( $pos < 0 || $pos > $max ) {
119  throw new OutOfBoundsException( __METHOD__ . ': invalid position' );
120  }
121  if ( $numRows ) {
122  $this->doSeek( $pos );
123  }
124  $this->nextPos = $pos;
125  $this->currentPos = $pos;
126  $this->currentRow = null;
127  }
128 
129  public function free() {
130  $this->doFree();
131  $this->currentRow = false;
132  }
133 
134  #[\ReturnTypeWillChange]
135  public function rewind() {
136  $this->seek( 0 );
137  }
138 
139  public function current() {
140  if ( $this->currentRow === null ) {
141  $this->currentRow = $this->fetchObject();
142  }
143 
144  return $this->currentRow;
145  }
146 
147  public function key() {
148  return $this->currentPos;
149  }
150 
151  public function next(): void {
152  $this->fetchObject();
153  }
154 
155  #[\ReturnTypeWillChange]
156  public function valid() {
157  return $this->currentPos >= 0
158  && $this->currentPos < $this->numRows();
159  }
160 
161  public function getFieldNames() {
162  if ( $this->fieldNames === null ) {
163  $this->fieldNames = $this->doGetFieldNames();
164  }
165  return $this->fieldNames;
166  }
167 }
168 
172 class_alias( ResultWrapper::class, 'ResultWrapper' );
Result wrapper for grabbing data queried from an IDatabase object.
doGetFieldNames()
Get the field names in the result set.
count()
Get the number of rows in a result object.
doFetchObject()
Get the next row as a stdClass object, or false if iteration has proceeded past the end.
free()
Free a result object.
getFieldNames()
Get the names of the fields in the result.
fetchObject()
Fetch the next row from the given result object, in object form.
int $currentPos
The offset of the current row that would be returned by current() and may have been previously return...
doFetchRow()
Get the next row as an array containing the data duplicated, once with string keys and once with nume...
doSeek( $pos)
Modify the current cursor position to the row with the specified offset.
stdClass array bool null $currentRow
The row at $this->currentPos, or null if it has not yet been retrieved, or false if the current row w...
doNumRows()
Get the number of rows in the result set.
string[] null $fieldNames
Cache of field names.
doFree()
Free underlying data.
seek( $pos)
Change the position of the cursor in a result object.
fetchRow()
Fetch the next row from the given result object, in associative array form.
numRows()
Get the number of rows in a result object.
int $nextPos
The offset of the row that would be returned by the next call to fetchObject().
Result wrapper for grabbing data queried from an IDatabase object.