Updated calls to Linker to call them statically and removed useless parameter to...
[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 function current() {
159 if ( is_null( $this->currentRow ) ) {
160 $this->next();
161 }
162 return $this->currentRow;
163 }
164
165 function key() {
166 return $this->pos;
167 }
168
169 function next() {
170 $this->pos++;
171 $this->currentRow = $this->fetchObject();
172 return $this->currentRow;
173 }
174
175 function valid() {
176 return $this->current() !== false;
177 }
178 }
179
180 /**
181 * Overloads the relevant methods of the real ResultsWrapper so it
182 * doesn't go anywhere near an actual database.
183 */
184 class FakeResultWrapper extends ResultWrapper {
185 var $result = array();
186 var $db = null; // And it's going to stay that way :D
187 var $pos = 0;
188 var $currentRow = null;
189
190 function __construct( $array ) {
191 $this->result = $array;
192 }
193
194 /**
195 * @return int
196 */
197 function numRows() {
198 return count( $this->result );
199 }
200
201 function fetchRow() {
202 if ( $this->pos < count( $this->result ) ) {
203 $this->currentRow = $this->result[$this->pos];
204 } else {
205 $this->currentRow = false;
206 }
207 $this->pos++;
208 return $this->currentRow;
209 }
210
211 function seek( $row ) {
212 $this->pos = $row;
213 }
214
215 function free() {}
216
217 // Callers want to be able to access fields with $this->fieldName
218 function fetchObject() {
219 $this->fetchRow();
220 if ( $this->currentRow ) {
221 return (object)$this->currentRow;
222 } else {
223 return false;
224 }
225 }
226
227 function rewind() {
228 $this->pos = 0;
229 $this->currentRow = null;
230 }
231
232 function next() {
233 return $this->fetchObject();
234 }
235 }
236
237 /**
238 * Used by DatabaseBase::buildLike() to represent characters that have special meaning in SQL LIKE clauses
239 * and thus need no escaping. Don't instantiate it manually, use DatabaseBase::anyChar() and anyString() instead.
240 */
241 class LikeMatch {
242 private $str;
243
244 /**
245 * Store a string into a LikeMatch marker object.
246 *
247 * @param String $s
248 */
249 public function __construct( $s ) {
250 $this->str = $s;
251 }
252
253 /**
254 * Return the original stored string.
255 *
256 * @return String
257 */
258 public function toString() {
259 return $this->str;
260 }
261 }