Html: Update list of $voidElements
[lhc/web/wiklou.git] / includes / libs / rdbms / database / resultwrapper / ResultWrapper.php
1 <?php
2 /**
3 * Result wrapper for grabbing data queried by someone else
4 * @ingroup Database
5 */
6 class ResultWrapper implements Iterator {
7 /** @var resource */
8 public $result;
9
10 /** @var IDatabase */
11 protected $db;
12
13 /** @var int */
14 protected $pos = 0;
15
16 /** @var object|null */
17 protected $currentRow = null;
18
19 /**
20 * Create a new result object from a result resource and a Database object
21 *
22 * @param IDatabase $database
23 * @param resource|ResultWrapper $result
24 */
25 function __construct( $database, $result ) {
26 $this->db = $database;
27
28 if ( $result instanceof ResultWrapper ) {
29 $this->result = $result->result;
30 } else {
31 $this->result = $result;
32 }
33 }
34
35 /**
36 * Get the number of rows in a result object
37 *
38 * @return int
39 */
40 function numRows() {
41 return $this->db->numRows( $this );
42 }
43
44 /**
45 * Fetch the next row from the given result object, in object form. Fields can be retrieved with
46 * $row->fieldname, with fields acting like member variables. If no more rows are available,
47 * false is returned.
48 *
49 * @return stdClass|bool
50 * @throws DBUnexpectedError Thrown if the database returns an error
51 */
52 function fetchObject() {
53 return $this->db->fetchObject( $this );
54 }
55
56 /**
57 * Fetch the next row from the given result object, in associative array form. Fields are
58 * retrieved with $row['fieldname']. If no more rows are available, false is returned.
59 *
60 * @return array|bool
61 * @throws DBUnexpectedError Thrown if the database returns an error
62 */
63 function fetchRow() {
64 return $this->db->fetchRow( $this );
65 }
66
67 /**
68 * Free a result object
69 */
70 function free() {
71 $this->db->freeResult( $this );
72 unset( $this->result );
73 unset( $this->db );
74 }
75
76 /**
77 * Change the position of the cursor in a result object.
78 * See mysql_data_seek()
79 *
80 * @param int $row
81 */
82 function seek( $row ) {
83 $this->db->dataSeek( $this, $row );
84 }
85
86 /*
87 * ======= Iterator functions =======
88 * Note that using these in combination with the non-iterator functions
89 * above may cause rows to be skipped or repeated.
90 */
91
92 function rewind() {
93 if ( $this->numRows() ) {
94 $this->db->dataSeek( $this, 0 );
95 }
96 $this->pos = 0;
97 $this->currentRow = null;
98 }
99
100 /**
101 * @return stdClass|array|bool
102 */
103 function current() {
104 if ( is_null( $this->currentRow ) ) {
105 $this->next();
106 }
107
108 return $this->currentRow;
109 }
110
111 /**
112 * @return int
113 */
114 function key() {
115 return $this->pos;
116 }
117
118 /**
119 * @return stdClass
120 */
121 function next() {
122 $this->pos++;
123 $this->currentRow = $this->fetchObject();
124
125 return $this->currentRow;
126 }
127
128 /**
129 * @return bool
130 */
131 function valid() {
132 return $this->current() !== false;
133 }
134 }