MediaWiki  1.34.0
ResultWrapper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use stdClass;
6 use RuntimeException;
7 use InvalidArgumentException;
8 
23 class ResultWrapper implements IResultWrapper {
25  protected $db;
27  protected $result;
28 
30  protected $pos = 0;
32  protected $currentRow;
33 
38  public function __construct( IDatabase $db, $result ) {
39  $this->db = $db;
40  if ( $result instanceof self ) {
41  $this->result = $result->result;
42  } elseif ( $result !== null ) {
43  $this->result = $result;
44  } else {
45  throw new InvalidArgumentException( "Null result resource provided" );
46  }
47  }
48 
59  public static function &unwrap( &$res ) {
60  if ( $res instanceof self ) {
61  if ( $res->result === null ) {
62  throw new RuntimeException( "The result resource was already freed" );
63  }
64 
65  return $res->result;
66  } else {
67  return $res;
68  }
69  }
70 
71  public function numRows() {
72  return $this->getDB()->numRows( $this );
73  }
74 
75  public function fetchObject() {
76  return $this->getDB()->fetchObject( $this );
77  }
78 
79  public function fetchRow() {
80  return $this->getDB()->fetchRow( $this );
81  }
82 
83  public function seek( $pos ) {
84  $this->getDB()->dataSeek( $this, $pos );
85  $this->pos = $pos;
86  }
87 
88  public function free() {
89  $this->db = null;
90  $this->result = null;
91  }
92 
93  function rewind() {
94  if ( $this->numRows() ) {
95  $this->getDB()->dataSeek( $this, 0 );
96  }
97  $this->pos = 0;
98  $this->currentRow = null;
99  }
100 
101  function current() {
102  if ( $this->currentRow === null ) {
103  $this->currentRow = $this->fetchObject();
104  }
105 
106  return $this->currentRow;
107  }
108 
109  function key() {
110  return $this->pos;
111  }
112 
113  function next() {
114  $this->pos++;
115  $this->currentRow = $this->fetchObject();
116 
117  return $this->currentRow;
118  }
119 
120  function valid() {
121  return $this->current() !== false;
122  }
123 
128  private function getDB() {
129  if ( !$this->db ) {
130  throw new RuntimeException( "Database handle was already freed" );
131  }
132 
133  return $this->db;
134  }
135 }
136 
140 class_alias( ResultWrapper::class, 'ResultWrapper' );
Wikimedia\Rdbms\ResultWrapper\key
key()
Definition: ResultWrapper.php:109
Wikimedia\Rdbms\ResultWrapper\__construct
__construct(IDatabase $db, $result)
Definition: ResultWrapper.php:38
Wikimedia\Rdbms\ResultWrapper\fetchRow
fetchRow()
Fetch the next row from the given result object, in associative array form.
Definition: ResultWrapper.php:79
Wikimedia\Rdbms\ResultWrapper\next
next()
Definition: ResultWrapper.php:113
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:23
$res
$res
Definition: testCompression.php:52
Wikimedia\Rdbms\ResultWrapper\getDB
getDB()
Definition: ResultWrapper.php:128
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Rdbms\ResultWrapper\$currentRow
stdClass bool null $currentRow
Definition: ResultWrapper.php:32
Wikimedia\Rdbms\ResultWrapper\valid
valid()
Definition: ResultWrapper.php:120
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
Wikimedia\Rdbms\ResultWrapper\free
free()
Free a result object.
Definition: ResultWrapper.php:88
Wikimedia\Rdbms\ResultWrapper\$result
mixed null $result
RDBMS driver-specific result resource.
Definition: ResultWrapper.php:27
Wikimedia\Rdbms\ResultWrapper\fetchObject
fetchObject()
Fetch the next row from the given result object, in object form.
Definition: ResultWrapper.php:75
Wikimedia\Rdbms\ResultWrapper\seek
seek( $pos)
Change the position of the cursor in a result object.
Definition: ResultWrapper.php:83
Wikimedia\Rdbms\ResultWrapper\$pos
int $pos
Definition: ResultWrapper.php:30
Wikimedia\Rdbms\ResultWrapper\$db
IDatabase $db
Definition: ResultWrapper.php:25
Wikimedia\Rdbms\ResultWrapper\rewind
rewind()
Definition: ResultWrapper.php:93
Wikimedia\Rdbms\ResultWrapper\unwrap
static & unwrap(&$res)
Get the underlying RDBMS driver-specific result resource.
Definition: ResultWrapper.php:59
Wikimedia\Rdbms\ResultWrapper\current
current()
Definition: ResultWrapper.php:101
Wikimedia\Rdbms\ResultWrapper\numRows
numRows()
Get the number of rows in a result object.
Definition: ResultWrapper.php:71