Module storage: randomly choose between Function and $.globalEval
[mediawiki.git] / includes / db / DatabaseUtility.php
blobde5be08811208b09a729a2d2258a3930c501a727
1 <?php
2 /**
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
20 * @file
21 * @ingroup Database
24 /**
25 * Utility class.
26 * @ingroup Database
28 class DBObject {
29 public $mData;
31 function __construct( $data ) {
32 $this->mData = $data;
35 /**
36 * @return bool
38 function isLOB() {
39 return false;
42 function data() {
43 return $this->mData;
47 /**
48 * Utility class
49 * @ingroup Database
51 * This allows us to distinguish a blob from a normal string and an array of strings
53 class Blob {
54 private $mData;
56 function __construct( $data ) {
57 $this->mData = $data;
60 function fetch() {
61 return $this->mData;
65 /**
66 * Base for all database-specific classes representing information about database fields
67 * @ingroup Database
69 interface Field {
70 /**
71 * Field name
72 * @return string
74 function name();
76 /**
77 * Name of table this field belongs to
78 * @return string
80 function tableName();
82 /**
83 * Database type
84 * @return string
86 function type();
88 /**
89 * Whether this field can store NULL values
90 * @return bool
92 function isNullable();
95 /**
96 * Result wrapper for grabbing data queried by someone else
97 * @ingroup Database
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;
113 } else {
114 $this->result = $result;
119 * Get the number of rows in a result object
121 * @return integer
123 function numRows() {
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
130 * member variables.
132 * @return object
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'].
143 * @return Array
144 * @throws DBUnexpectedError Thrown if the database returns an error
146 function fetchRow() {
147 return $this->db->fetchRow( $this );
151 * Free a result object
153 function free() {
154 $this->db->freeResult( $this );
155 unset( $this->result );
156 unset( $this->db );
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 /*********************
170 * Iterator functions
171 * Note that using these in combination with the non-iterator functions
172 * above may cause rows to be skipped or repeated.
175 function rewind() {
176 if ( $this->numRows() ) {
177 $this->db->dataSeek( $this, 0 );
179 $this->pos = 0;
180 $this->currentRow = null;
184 * @return int
186 function current() {
187 if ( is_null( $this->currentRow ) ) {
188 $this->next();
191 return $this->currentRow;
195 * @return int
197 function key() {
198 return $this->pos;
202 * @return int
204 function next() {
205 $this->pos++;
206 $this->currentRow = $this->fetchObject();
208 return $this->currentRow;
212 * @return bool
214 function valid() {
215 return $this->current() !== false;
220 * Overloads the relevant methods of the real ResultsWrapper so it
221 * doesn't go anywhere near an actual database.
223 class FakeResultWrapper extends ResultWrapper {
224 var $result = array();
225 var $db = null; // And it's going to stay that way :D
226 var $pos = 0;
227 var $currentRow = null;
229 function __construct( $array ) {
230 $this->result = $array;
234 * @return int
236 function numRows() {
237 return count( $this->result );
240 function fetchRow() {
241 if ( $this->pos < count( $this->result ) ) {
242 $this->currentRow = $this->result[$this->pos];
243 } else {
244 $this->currentRow = false;
246 $this->pos++;
247 if ( is_object( $this->currentRow ) ) {
248 return get_object_vars( $this->currentRow );
249 } else {
250 return $this->currentRow;
254 function seek( $row ) {
255 $this->pos = $row;
258 function free() {
261 // Callers want to be able to access fields with $this->fieldName
262 function fetchObject() {
263 $this->fetchRow();
264 if ( $this->currentRow ) {
265 return (object)$this->currentRow;
266 } else {
267 return false;
271 function rewind() {
272 $this->pos = 0;
273 $this->currentRow = null;
276 function next() {
277 return $this->fetchObject();
282 * Used by DatabaseBase::buildLike() to represent characters that have special
283 * meaning in SQL LIKE clauses and thus need no escaping. Don't instantiate it
284 * manually, use DatabaseBase::anyChar() and anyString() instead.
286 class LikeMatch {
287 private $str;
290 * Store a string into a LikeMatch marker object.
292 * @param string $s
294 public function __construct( $s ) {
295 $this->str = $s;
299 * Return the original stored string.
301 * @return String
303 public function toString() {
304 return $this->str;
309 * An object representing a master or slave position in a replicated setup.
311 interface DBMasterPos {