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 
59  public static function unwrap( $res ) {
60  wfDeprecated( __METHOD__, '1.37' );
61  if ( $res instanceof MysqliResultWrapper ) {
62  return $res->getInternalResult();
63  } elseif ( $res instanceof SqliteResultWrapper ) {
64  return $res->getInternalResult();
65  } elseif ( $res instanceof PostgresResultWrapper ) {
66  return $res->getInternalResult();
67  } elseif ( $res instanceof IResultWrapper ) {
68  throw new \InvalidArgumentException( __METHOD__ . ': $res does not support unwrap()' );
69  } else {
70  return $res;
71  }
72  }
73 
80  abstract protected function doNumRows();
81 
90  abstract protected function doFetchObject();
91 
99  abstract protected function doFetchRow();
100 
107  abstract protected function doSeek( $pos );
108 
112  abstract protected function doFree();
113 
119  abstract protected function doGetFieldNames();
120 
121  public function numRows() {
122  return $this->doNumRows();
123  }
124 
125  public function count() {
126  return $this->doNumRows();
127  }
128 
129  public function fetchObject() {
130  $this->currentPos = $this->nextPos++;
131  $this->currentRow = $this->doFetchObject();
132  return $this->currentRow;
133  }
134 
135  public function fetchRow() {
136  $this->currentPos = $this->nextPos++;
137  $this->currentRow = $this->doFetchRow();
138  return $this->currentRow;
139  }
140 
141  public function seek( $pos ) {
142  $numRows = $this->numRows();
143  // Allow seeking to zero if there are no results
144  $max = $numRows ? $numRows - 1 : 0;
145  if ( $pos < 0 || $pos > $max ) {
146  throw new OutOfBoundsException( __METHOD__ . ': invalid position' );
147  }
148  if ( $numRows ) {
149  $this->doSeek( $pos );
150  }
151  $this->nextPos = $pos;
152  $this->currentPos = $pos;
153  $this->currentRow = null;
154  }
155 
156  public function free() {
157  $this->doFree();
158  $this->currentRow = false;
159  }
160 
161  public function rewind() {
162  $this->seek( 0 );
163  }
164 
165  public function current() {
166  if ( $this->currentRow === null ) {
167  $this->currentRow = $this->fetchObject();
168  }
169 
170  return $this->currentRow;
171  }
172 
173  public function key() {
174  return $this->currentPos;
175  }
176 
177  public function next() {
178  return $this->fetchObject();
179  }
180 
181  public function valid() {
182  return $this->currentPos >= 0
183  && $this->currentPos < $this->numRows();
184  }
185 
186  public function getFieldNames() {
187  if ( $this->fieldNames === null ) {
188  $this->fieldNames = $this->doGetFieldNames();
189  }
190  return $this->fieldNames;
191  }
192 }
193 
197 class_alias( ResultWrapper::class, 'ResultWrapper' );
Wikimedia\Rdbms\ResultWrapper\doFetchRow
doFetchRow()
Get the next row as an array containing the data duplicated, once with string keys and once with nume...
Wikimedia\Rdbms\ResultWrapper\$fieldNames
string[] null $fieldNames
Cache of field names.
Definition: ResultWrapper.php:45
Wikimedia\Rdbms\ResultWrapper\key
key()
Definition: ResultWrapper.php:173
Wikimedia\Rdbms\ResultWrapper\doFetchObject
doFetchObject()
Get the next row as a stdClass object, or false if iteration has proceeded past the end.
Wikimedia\Rdbms\ResultWrapper\doFree
doFree()
Free underlying data.
Wikimedia\Rdbms\ResultWrapper\fetchRow
fetchRow()
Fetch the next row from the given result object, in associative array form.
Definition: ResultWrapper.php:135
Wikimedia\Rdbms\ResultWrapper\getFieldNames
getFieldNames()
Get the names of the fields in the result.
Definition: ResultWrapper.php:186
Wikimedia\Rdbms\SqliteResultWrapper
Definition: SqliteResultWrapper.php:9
Wikimedia\Rdbms\ResultWrapper\next
next()
Definition: ResultWrapper.php:177
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\ResultWrapper\$nextPos
int $nextPos
The offset of the row that would be returned by the next call to fetchObject().
Definition: ResultWrapper.php:28
Wikimedia\Rdbms\ResultWrapper\$currentPos
int $currentPos
The offset of the current row that would be returned by current() and may have been previously return...
Definition: ResultWrapper.php:34
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:23
$res
$res
Definition: testCompression.php:57
Wikimedia\Rdbms\ResultWrapper\valid
valid()
Definition: ResultWrapper.php:181
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:26
Wikimedia\Rdbms\ResultWrapper\$currentRow
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...
Definition: ResultWrapper.php:40
Wikimedia\Rdbms\ResultWrapper\free
free()
Free a result object.
Definition: ResultWrapper.php:156
Wikimedia\Rdbms\MysqliResultWrapper
Definition: MysqliResultWrapper.php:7
Wikimedia\Rdbms\ResultWrapper\fetchObject
fetchObject()
Fetch the next row from the given result object, in object form.
Definition: ResultWrapper.php:129
Wikimedia\Rdbms\ResultWrapper\seek
seek( $pos)
Change the position of the cursor in a result object.
Definition: ResultWrapper.php:141
Wikimedia\Rdbms\ResultWrapper\doGetFieldNames
doGetFieldNames()
Get the field names in the result set.
Wikimedia\Rdbms\ResultWrapper\unwrap
static unwrap( $res)
Get the underlying RDBMS driver-specific result resource.
Definition: ResultWrapper.php:59
Wikimedia\Rdbms\ResultWrapper\doNumRows
doNumRows()
Get the number of rows in the result set.
Wikimedia\Rdbms\ResultWrapper\doSeek
doSeek( $pos)
Modify the current cursor position to the row with the specified offset.
Wikimedia\Rdbms\ResultWrapper\count
count()
Get the number of rows in a result object.
Definition: ResultWrapper.php:125
Wikimedia\Rdbms\ResultWrapper\rewind
rewind()
Definition: ResultWrapper.php:161
Wikimedia\Rdbms\ResultWrapper\current
current()
Definition: ResultWrapper.php:165
Wikimedia\Rdbms\ResultWrapper\numRows
numRows()
Get the number of rows in a result object.
Definition: ResultWrapper.php:121
Wikimedia\Rdbms\PostgresResultWrapper
Definition: PostgresResultWrapper.php:7