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
31 function __construct( $data ) {
51 * This allows us to distinguish a blob from a normal string and an array of strings
56 function __construct( $data ) {
66 * Base for all database-specific classes representing information about database fields
77 * Name of table this field belongs to
89 * Whether this field can store NULL values
92 function isNullable();
96 * Result wrapper for grabbing data queried by someone else
99 class ResultWrapper
implements Iterator
{
100 var $db, $result, $pos = 0, $currentRow = null;
103 * Create a new result object from a result resource and a Database object
105 * @param DatabaseBase $database
106 * @param resource $result
108 function __construct( $database, $result ) {
109 $this->db
= $database;
111 if ( $result instanceof ResultWrapper
) {
112 $this->result
= $result->result
;
114 $this->result
= $result;
119 * Get the number of rows in a result object
124 return $this->db
->numRows( $this );
128 * Fetch the next row from the given result object, in object form.
129 * Fields can be retrieved with $row->fieldname, with fields acting like
133 * @throws DBUnexpectedError Thrown if the database returns an error
135 function fetchObject() {
136 return $this->db
->fetchObject( $this );
140 * Fetch the next row from the given result object, in associative array
141 * form. Fields are retrieved with $row['fieldname'].
144 * @throws DBUnexpectedError Thrown if the database returns an error
146 function fetchRow() {
147 return $this->db
->fetchRow( $this );
151 * Free a result object
154 $this->db
->freeResult( $this );
155 unset( $this->result
);
160 * Change the position of the cursor in a result object.
161 * See mysql_data_seek()
163 * @param $row integer
165 function seek( $row ) {
166 $this->db
->dataSeek( $this, $row );
169 /*********************
171 * Note that using these in combination with the non-iterator functions
172 * above may cause rows to be skipped or repeated.
176 if ( $this->numRows() ) {
177 $this->db
->dataSeek( $this, 0 );
180 $this->currentRow
= null;
187 if ( is_null( $this->currentRow
) ) {
190 return $this->currentRow
;
205 $this->currentRow
= $this->fetchObject();
206 return $this->currentRow
;
213 return $this->current() !== false;
218 * Overloads the relevant methods of the real ResultsWrapper so it
219 * doesn't go anywhere near an actual database.
221 class FakeResultWrapper
extends ResultWrapper
{
222 var $result = array();
223 var $db = null; // And it's going to stay that way :D
225 var $currentRow = null;
227 function __construct( $array ) {
228 $this->result
= $array;
235 return count( $this->result
);
238 function fetchRow() {
239 if ( $this->pos
< count( $this->result
) ) {
240 $this->currentRow
= $this->result
[$this->pos
];
242 $this->currentRow
= false;
245 if ( is_object( $this->currentRow
) ) {
246 return get_object_vars( $this->currentRow
);
248 return $this->currentRow
;
252 function seek( $row ) {
259 // Callers want to be able to access fields with $this->fieldName
260 function fetchObject() {
262 if ( $this->currentRow
) {
263 return (object)$this->currentRow
;
271 $this->currentRow
= null;
275 return $this->fetchObject();
280 * Used by DatabaseBase::buildLike() to represent characters that have special meaning in SQL LIKE clauses
281 * and thus need no escaping. Don't instantiate it manually, use DatabaseBase::anyChar() and anyString() instead.
287 * Store a string into a LikeMatch marker object.
291 public function __construct( $s ) {
296 * Return the original stored string.
300 public function toString() {
306 * An object representing a master or slave position in a replicated setup.
308 interface DBMasterPos
{