Kill some more mysql 4 related things from the installer
[lhc/web/wiklou.git] / includes / db / DatabaseUtility.php
1 <?php
2 /**
3 * Utility class.
4 * @ingroup Database
5 */
6 class DBObject {
7 public $mData;
8
9 function __construct( $data ) {
10 $this->mData = $data;
11 }
12
13 function isLOB() {
14 return false;
15 }
16
17 function data() {
18 return $this->mData;
19 }
20 }
21
22 /**
23 * Utility class
24 * @ingroup Database
25 *
26 * This allows us to distinguish a blob from a normal string and an array of strings
27 */
28 class Blob {
29 private $mData;
30
31 function __construct( $data ) {
32 $this->mData = $data;
33 }
34
35 function fetch() {
36 return $this->mData;
37 }
38 }
39
40 /**
41 * Base for all database-specific classes representing information about database fields
42 * @ingroup Database
43 */
44 interface Field {
45 /**
46 * Field name
47 * @return string
48 */
49 function name();
50
51 /**
52 * Name of table this field belongs to
53 * @return string
54 */
55 function tableName();
56
57 /**
58 * Database type
59 * @return string
60 */
61 function type();
62
63 /**
64 * Whether this field can store NULL values
65 * @return bool
66 */
67 function isNullable();
68 }
69
70 /**
71 * Result wrapper for grabbing data queried by someone else
72 * @ingroup Database
73 */
74 class ResultWrapper implements Iterator {
75 var $db, $result, $pos = 0, $currentRow = null;
76
77 /**
78 * Create a new result object from a result resource and a Database object
79 *
80 * @param DatabaseBase $database
81 * @param resource $result
82 */
83 function __construct( $database, $result ) {
84 $this->db = $database;
85
86 if ( $result instanceof ResultWrapper ) {
87 $this->result = $result->result;
88 } else {
89 $this->result = $result;
90 }
91 }
92
93 /**
94 * Get the number of rows in a result object
95 *
96 * @return integer
97 */
98 function numRows() {
99 return $this->db->numRows( $this );
100 }
101
102 /**
103 * Fetch the next row from the given result object, in object form.
104 * Fields can be retrieved with $row->fieldname, with fields acting like
105 * member variables.
106 *
107 * @return object
108 * @throws DBUnexpectedError Thrown if the database returns an error
109 */
110 function fetchObject() {
111 return $this->db->fetchObject( $this );
112 }
113
114 /**
115 * Fetch the next row from the given result object, in associative array
116 * form. Fields are retrieved with $row['fieldname'].
117 *
118 * @return Array
119 * @throws DBUnexpectedError Thrown if the database returns an error
120 */
121 function fetchRow() {
122 return $this->db->fetchRow( $this );
123 }
124
125 /**
126 * Free a result object
127 */
128 function free() {
129 $this->db->freeResult( $this );
130 unset( $this->result );
131 unset( $this->db );
132 }
133
134 /**
135 * Change the position of the cursor in a result object.
136 * See mysql_data_seek()
137 *
138 * @param $row integer
139 */
140 function seek( $row ) {
141 $this->db->dataSeek( $this, $row );
142 }
143
144 /*********************
145 * Iterator functions
146 * Note that using these in combination with the non-iterator functions
147 * above may cause rows to be skipped or repeated.
148 */
149
150 function rewind() {
151 if ( $this->numRows() ) {
152 $this->db->dataSeek( $this, 0 );
153 }
154 $this->pos = 0;
155 $this->currentRow = null;
156 }
157
158 /**
159 * @return int
160 */
161 function current() {
162 if ( is_null( $this->currentRow ) ) {
163 $this->next();
164 }
165 return $this->currentRow;
166 }
167
168 /**
169 * @return int
170 */
171 function key() {
172 return $this->pos;
173 }
174
175 /**
176 * @return int
177 */
178 function next() {
179 $this->pos++;
180 $this->currentRow = $this->fetchObject();
181 return $this->currentRow;
182 }
183
184 /**
185 * @return bool
186 */
187 function valid() {
188 return $this->current() !== false;
189 }
190 }
191
192 /**
193 * Overloads the relevant methods of the real ResultsWrapper so it
194 * doesn't go anywhere near an actual database.
195 */
196 class FakeResultWrapper extends ResultWrapper {
197 var $result = array();
198 var $db = null; // And it's going to stay that way :D
199 var $pos = 0;
200 var $currentRow = null;
201
202 function __construct( $array ) {
203 $this->result = $array;
204 }
205
206 /**
207 * @return int
208 */
209 function numRows() {
210 return count( $this->result );
211 }
212
213 function fetchRow() {
214 if ( $this->pos < count( $this->result ) ) {
215 $this->currentRow = $this->result[$this->pos];
216 } else {
217 $this->currentRow = false;
218 }
219 $this->pos++;
220 return $this->currentRow;
221 }
222
223 function seek( $row ) {
224 $this->pos = $row;
225 }
226
227 function free() {}
228
229 // Callers want to be able to access fields with $this->fieldName
230 function fetchObject() {
231 $this->fetchRow();
232 if ( $this->currentRow ) {
233 return (object)$this->currentRow;
234 } else {
235 return false;
236 }
237 }
238
239 function rewind() {
240 $this->pos = 0;
241 $this->currentRow = null;
242 }
243
244 function next() {
245 return $this->fetchObject();
246 }
247 }
248
249 /**
250 * Used by DatabaseBase::buildLike() to represent characters that have special meaning in SQL LIKE clauses
251 * and thus need no escaping. Don't instantiate it manually, use DatabaseBase::anyChar() and anyString() instead.
252 */
253 class LikeMatch {
254 private $str;
255
256 /**
257 * Store a string into a LikeMatch marker object.
258 *
259 * @param String $s
260 */
261 public function __construct( $s ) {
262 $this->str = $s;
263 }
264
265 /**
266 * Return the original stored string.
267 *
268 * @return String
269 */
270 public function toString() {
271 return $this->str;
272 }
273 }
274
275 /**
276 * An object representing a master or slave position in a replicated setup.
277 */
278 interface DBMasterPos {
279 }
280