Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / db / DatabaseUtility.php
blob0ea713c815364e69a730c6d97309959448702b85
1 <?php
2 /**
3 * Utility class.
4 * @ingroup Database
5 */
6 class DBObject {
7 public $mData;
9 function __construct( $data ) {
10 $this->mData = $data;
13 /**
14 * @return bool
16 function isLOB() {
17 return false;
20 function data() {
21 return $this->mData;
25 /**
26 * Utility class
27 * @ingroup Database
29 * This allows us to distinguish a blob from a normal string and an array of strings
31 class Blob {
32 private $mData;
34 function __construct( $data ) {
35 $this->mData = $data;
38 function fetch() {
39 return $this->mData;
43 /**
44 * Base for all database-specific classes representing information about database fields
45 * @ingroup Database
47 interface Field {
48 /**
49 * Field name
50 * @return string
52 function name();
54 /**
55 * Name of table this field belongs to
56 * @return string
58 function tableName();
60 /**
61 * Database type
62 * @return string
64 function type();
66 /**
67 * Whether this field can store NULL values
68 * @return bool
70 function isNullable();
73 /**
74 * Result wrapper for grabbing data queried by someone else
75 * @ingroup Database
77 class ResultWrapper implements Iterator {
78 var $db, $result, $pos = 0, $currentRow = null;
80 /**
81 * Create a new result object from a result resource and a Database object
83 * @param DatabaseBase $database
84 * @param resource $result
86 function __construct( $database, $result ) {
87 $this->db = $database;
89 if ( $result instanceof ResultWrapper ) {
90 $this->result = $result->result;
91 } else {
92 $this->result = $result;
96 /**
97 * Get the number of rows in a result object
99 * @return integer
101 function numRows() {
102 return $this->db->numRows( $this );
106 * Fetch the next row from the given result object, in object form.
107 * Fields can be retrieved with $row->fieldname, with fields acting like
108 * member variables.
110 * @return object
111 * @throws DBUnexpectedError Thrown if the database returns an error
113 function fetchObject() {
114 return $this->db->fetchObject( $this );
118 * Fetch the next row from the given result object, in associative array
119 * form. Fields are retrieved with $row['fieldname'].
121 * @return Array
122 * @throws DBUnexpectedError Thrown if the database returns an error
124 function fetchRow() {
125 return $this->db->fetchRow( $this );
129 * Free a result object
131 function free() {
132 $this->db->freeResult( $this );
133 unset( $this->result );
134 unset( $this->db );
138 * Change the position of the cursor in a result object.
139 * See mysql_data_seek()
141 * @param $row integer
143 function seek( $row ) {
144 $this->db->dataSeek( $this, $row );
147 /*********************
148 * Iterator functions
149 * Note that using these in combination with the non-iterator functions
150 * above may cause rows to be skipped or repeated.
153 function rewind() {
154 if ( $this->numRows() ) {
155 $this->db->dataSeek( $this, 0 );
157 $this->pos = 0;
158 $this->currentRow = null;
162 * @return int
164 function current() {
165 if ( is_null( $this->currentRow ) ) {
166 $this->next();
168 return $this->currentRow;
172 * @return int
174 function key() {
175 return $this->pos;
179 * @return int
181 function next() {
182 $this->pos++;
183 $this->currentRow = $this->fetchObject();
184 return $this->currentRow;
188 * @return bool
190 function valid() {
191 return $this->current() !== false;
196 * Overloads the relevant methods of the real ResultsWrapper so it
197 * doesn't go anywhere near an actual database.
199 class FakeResultWrapper extends ResultWrapper {
200 var $result = array();
201 var $db = null; // And it's going to stay that way :D
202 var $pos = 0;
203 var $currentRow = null;
205 function __construct( $array ) {
206 $this->result = $array;
210 * @return int
212 function numRows() {
213 return count( $this->result );
216 function fetchRow() {
217 if ( $this->pos < count( $this->result ) ) {
218 $this->currentRow = $this->result[$this->pos];
219 } else {
220 $this->currentRow = false;
222 $this->pos++;
223 return $this->currentRow;
226 function seek( $row ) {
227 $this->pos = $row;
230 function free() {}
232 // Callers want to be able to access fields with $this->fieldName
233 function fetchObject() {
234 $this->fetchRow();
235 if ( $this->currentRow ) {
236 return (object)$this->currentRow;
237 } else {
238 return false;
242 function rewind() {
243 $this->pos = 0;
244 $this->currentRow = null;
247 function next() {
248 return $this->fetchObject();
253 * Used by DatabaseBase::buildLike() to represent characters that have special meaning in SQL LIKE clauses
254 * and thus need no escaping. Don't instantiate it manually, use DatabaseBase::anyChar() and anyString() instead.
256 class LikeMatch {
257 private $str;
260 * Store a string into a LikeMatch marker object.
262 * @param String $s
264 public function __construct( $s ) {
265 $this->str = $s;
269 * Return the original stored string.
271 * @return String
273 public function toString() {
274 return $this->str;
279 * An object representing a master or slave position in a replicated setup.
281 interface DBMasterPos {