MediaWiki  master
ResultWrapper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use stdClass;
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' );
mixed null $result
RDBMS driver-specific result resource.
__construct(IDatabase $db, $result)
Result wrapper for grabbing data queried from an IDatabase object.
fetchObject()
Fetch the next row from the given result object, in object form.
stdClass bool null $currentRow
Result wrapper for grabbing data queried from an IDatabase object.
numRows()
Get the number of rows in a result object.
seek( $pos)
Change the position of the cursor in a result object.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
free()
Free a result object.
fetchRow()
Fetch the next row from the given result object, in associative array form.
static & unwrap(&$res)
Get the underlying RDBMS driver-specific result resource.