3 namespace Wikimedia\Rdbms
;
7 use InvalidArgumentException
;
10 * Result wrapper for grabbing data queried from an IDatabase object
12 * Only IDatabase-related classes should construct these. Other code may
13 * use the FakeResultWrapper class for convenience or compatibility shims.
15 * Note that using the Iterator methods in combination with the non-Iterator
16 * IDatabase result iteration functions may cause rows to be skipped or repeated.
18 * By default, this will use the iteration methods of the IDatabase handle if provided.
19 * Subclasses can override methods to make it solely work on the result resource instead.
23 class ResultWrapper
implements IResultWrapper
{
26 /** @var mixed|null RDBMS driver-specific result resource */
31 /** @var stdClass|bool|null */
32 protected $currentRow;
35 * @param IDatabase $db Database handle that the result comes from
36 * @param self|mixed $result RDBMS driver-specific result resource
38 public function __construct( IDatabase
$db, $result ) {
40 if ( $result instanceof self
) {
41 $this->result
= $result->result
;
42 } elseif ( $result !== null ) {
43 $this->result
= $result;
45 throw new InvalidArgumentException( "Null result resource provided" );
50 * Get the underlying RDBMS driver-specific result resource
52 * The result resource field should not be accessed from non-Database related classes.
53 * It is database class specific and is stored here to associate iterators with queries.
55 * @param self|mixed &$res
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" );
71 public function numRows() {
72 return $this->getDB()->numRows( $this );
75 public function fetchObject() {
76 return $this->getDB()->fetchObject( $this );
79 public function fetchRow() {
80 return $this->getDB()->fetchRow( $this );
83 public function seek( $pos ) {
84 $this->getDB()->dataSeek( $this, $pos );
88 public function free() {
94 if ( $this->numRows() ) {
95 $this->getDB()->dataSeek( $this, 0 );
98 $this->currentRow
= null;
102 if ( $this->currentRow
=== null ) {
103 $this->currentRow
= $this->fetchObject();
106 return $this->currentRow
;
115 $this->currentRow
= $this->fetchObject();
117 return $this->currentRow
;
121 return $this->current() !== false;
126 * @throws RuntimeException
128 private function getDB() {
130 throw new RuntimeException( "Database handle was already freed" );
138 * @deprecated since 1.29
140 class_alias( ResultWrapper
::class, 'ResultWrapper' );