3 * This file contains database-related utility classes.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
28 * This allows us to distinguish a blob from a normal string and an array of strings
34 function __construct( $data ) {
44 * Base for all database-specific classes representing information about database fields
55 * Name of table this field belongs to
67 * Whether this field can store NULL values
70 function isNullable();
74 * Result wrapper for grabbing data queried by someone else
77 class ResultWrapper
implements Iterator
{
81 /** @var DatabaseBase */
87 /** @var object|null */
88 protected $currentRow = null;
91 * Create a new result object from a result resource and a Database object
93 * @param DatabaseBase $database
94 * @param resource|ResultWrapper $result
96 function __construct( $database, $result ) {
97 $this->db
= $database;
99 if ( $result instanceof ResultWrapper
) {
100 $this->result
= $result->result
;
102 $this->result
= $result;
107 * Get the number of rows in a result object
112 return $this->db
->numRows( $this );
116 * Fetch the next row from the given result object, in object form. Fields can be retrieved with
117 * $row->fieldname, with fields acting like member variables. If no more rows are available,
120 * @return stdClass|bool
121 * @throws DBUnexpectedError Thrown if the database returns an error
123 function fetchObject() {
124 return $this->db
->fetchObject( $this );
128 * Fetch the next row from the given result object, in associative array form. Fields are
129 * retrieved with $row['fieldname']. If no more rows are available, false is returned.
132 * @throws DBUnexpectedError Thrown if the database returns an error
134 function fetchRow() {
135 return $this->db
->fetchRow( $this );
139 * Free a result object
142 $this->db
->freeResult( $this );
143 unset( $this->result
);
148 * Change the position of the cursor in a result object.
149 * See mysql_data_seek()
153 function seek( $row ) {
154 $this->db
->dataSeek( $this, $row );
158 * ======= Iterator functions =======
159 * Note that using these in combination with the non-iterator functions
160 * above may cause rows to be skipped or repeated.
164 if ( $this->numRows() ) {
165 $this->db
->dataSeek( $this, 0 );
168 $this->currentRow
= null;
172 * @return stdClass|array|bool
175 if ( is_null( $this->currentRow
) ) {
179 return $this->currentRow
;
194 $this->currentRow
= $this->fetchObject();
196 return $this->currentRow
;
203 return $this->current() !== false;
208 * Overloads the relevant methods of the real ResultsWrapper so it
209 * doesn't go anywhere near an actual database.
211 class FakeResultWrapper
extends ResultWrapper
{
213 public $result = array();
215 /** @var null And it's going to stay that way :D */
216 protected $db = null;
221 /** @var array|stdClass|bool */
222 protected $currentRow = null;
225 * @param array $array
227 function __construct( $array ) {
228 $this->result
= $array;
235 return count( $this->result
);
241 function fetchRow() {
242 if ( $this->pos
< count( $this->result
) ) {
243 $this->currentRow
= $this->result
[$this->pos
];
245 $this->currentRow
= false;
248 if ( is_object( $this->currentRow
) ) {
249 return get_object_vars( $this->currentRow
);
251 return $this->currentRow
;
255 function seek( $row ) {
263 * Callers want to be able to access fields with $this->fieldName
264 * @return bool|stdClass
266 function fetchObject() {
268 if ( $this->currentRow
) {
269 return (object)$this->currentRow
;
277 $this->currentRow
= null;
281 * @return bool|stdClass
284 return $this->fetchObject();
289 * Used by DatabaseBase::buildLike() to represent characters that have special
290 * meaning in SQL LIKE clauses and thus need no escaping. Don't instantiate it
291 * manually, use DatabaseBase::anyChar() and anyString() instead.
298 * Store a string into a LikeMatch marker object.
302 public function __construct( $s ) {
307 * Return the original stored string.
311 public function toString() {
317 * An object representing a master or slave position in a replicated setup.
319 * The implementation details of this opaque type are up to the database subclass.
321 interface DBMasterPos
{
323 * @return float UNIX timestamp
326 public function asOfTime();
329 * @param DBMasterPos $pos
330 * @return bool Whether this position is at or higher than $pos
333 public function hasReached( DBMasterPos
$pos );
339 public function __toString();