Merge "Follow-up I774a89d6 (2fabea7): use $this->msg() in HistoryAction"
[mediawiki.git] / includes / db / IORMTable.php
blob99413f99b93646d8f637ac607beb5c8f83ad2245
1 <?php
2 /**
3 * Interface for objects representing a single database table.
4 * Documentation inline and at https://www.mediawiki.org/wiki/Manual:ORMTable
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @since 1.20
23 * @file
24 * @ingroup ORM
26 * @licence GNU GPL v2 or later
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 interface IORMTable {
32 /**
33 * Returns the name of the database table objects of this type are stored in.
35 * @since 1.20
37 * @return string
39 public function getName();
41 /**
42 * Returns the name of a IORMRow implementing class that
43 * represents single rows in this table.
45 * @since 1.20
47 * @return string
49 public function getRowClass();
51 /**
52 * Returns an array with the fields and their types this object contains.
53 * This corresponds directly to the fields in the database, without prefix.
55 * field name => type
57 * Allowed types:
58 * * id
59 * * str
60 * * int
61 * * float
62 * * bool
63 * * array
64 * * blob
66 * TODO: get rid of the id field. Every row instance needs to have
67 * one so this is just causing hassle at various locations by requiring an extra check for field name.
69 * @since 1.20
71 * @return array
73 public function getFields();
75 /**
76 * Returns a list of default field values.
77 * field name => field value
79 * @since 1.20
81 * @return array
83 public function getDefaults();
85 /**
86 * Returns a list of the summary fields.
87 * These are fields that cache computed values, such as the amount of linked objects of $type.
88 * This is relevant as one might not want to do actions such as log changes when these get updated.
90 * @since 1.20
92 * @return array
94 public function getSummaryFields();
96 /**
97 * Selects the the specified fields of the records matching the provided
98 * conditions and returns them as DBDataObject. Field names get prefixed.
100 * @since 1.20
102 * @param array|string|null $fields
103 * @param array $conditions
104 * @param array $options
105 * @param string|null $functionName
107 * @return ORMResult
109 public function select( $fields = null, array $conditions = array(),
110 array $options = array(), $functionName = null );
113 * Selects the the specified fields of the records matching the provided
114 * conditions and returns them as DBDataObject. Field names get prefixed.
116 * @since 1.20
118 * @param array|string|null $fields
119 * @param array $conditions
120 * @param array $options
121 * @param string|null $functionName
123 * @return array of self
125 public function selectObjects( $fields = null, array $conditions = array(),
126 array $options = array(), $functionName = null );
129 * Do the actual select.
131 * @since 1.20
133 * @param null|string|array $fields
134 * @param array $conditions
135 * @param array $options
136 * @param null|string $functionName
138 * @return ResultWrapper
140 public function rawSelect( $fields = null, array $conditions = array(),
141 array $options = array(), $functionName = null );
144 * Selects the the specified fields of the records matching the provided
145 * conditions and returns them as associative arrays.
146 * Provided field names get prefixed.
147 * Returned field names will not have a prefix.
149 * When $collapse is true:
150 * If one field is selected, each item in the result array will be this field.
151 * If two fields are selected, each item in the result array will have as key
152 * the first field and as value the second field.
153 * If more then two fields are selected, each item will be an associative array.
155 * @since 1.20
157 * @param array|string|null $fields
158 * @param array $conditions
159 * @param array $options
160 * @param boolean $collapse Set to false to always return each result row as associative array.
161 * @param string|null $functionName
163 * @return array of array
165 public function selectFields( $fields = null, array $conditions = array(),
166 array $options = array(), $collapse = true, $functionName = null );
169 * Selects the the specified fields of the first matching record.
170 * Field names get prefixed.
172 * @since 1.20
174 * @param array|string|null $fields
175 * @param array $conditions
176 * @param array $options
177 * @param string|null $functionName
179 * @return IORMRow|bool False on failure
181 public function selectRow( $fields = null, array $conditions = array(),
182 array $options = array(), $functionName = null );
185 * Selects the the specified fields of the records matching the provided
186 * conditions. Field names do NOT get prefixed.
188 * @since 1.20
190 * @param array $fields
191 * @param array $conditions
192 * @param array $options
193 * @param string|null $functionName
195 * @return ResultWrapper
197 public function rawSelectRow( array $fields, array $conditions = array(),
198 array $options = array(), $functionName = null );
201 * Selects the the specified fields of the first record matching the provided
202 * conditions and returns it as an associative array, or false when nothing matches.
203 * This method makes use of selectFields and expects the same parameters and
204 * returns the same results (if there are any, if there are none, this method returns false).
205 * @see IORMTable::selectFields
207 * @since 1.20
209 * @param array|string|null $fields
210 * @param array $conditions
211 * @param array $options
212 * @param boolean $collapse Set to false to always return each result row as associative array.
213 * @param string|null $functionName
215 * @return mixed|array|bool False on failure
217 public function selectFieldsRow( $fields = null, array $conditions = array(),
218 array $options = array(), $collapse = true, $functionName = null );
221 * Returns if there is at least one record matching the provided conditions.
222 * Condition field names get prefixed.
224 * @since 1.20
226 * @param array $conditions
228 * @return boolean
230 public function has( array $conditions = array() );
233 * Returns the amount of matching records.
234 * Condition field names get prefixed.
236 * Note that this can be expensive on large tables.
237 * In such cases you might want to use DatabaseBase::estimateRowCount instead.
239 * @since 1.20
241 * @param array $conditions
242 * @param array $options
244 * @return integer
246 public function count( array $conditions = array(), array $options = array() );
249 * Removes the object from the database.
251 * @since 1.20
253 * @param array $conditions
254 * @param string|null $functionName
256 * @return boolean Success indicator
258 public function delete( array $conditions, $functionName = null );
261 * Get API parameters for the fields supported by this object.
263 * @since 1.20
265 * @param boolean $requireParams
266 * @param boolean $setDefaults
268 * @return array
270 public function getAPIParams( $requireParams = false, $setDefaults = false );
273 * Returns an array with the fields and their descriptions.
275 * field name => field description
277 * @since 1.20
279 * @return array
281 public function getFieldDescriptions();
284 * Get the database type used for read operations.
286 * @since 1.20
288 * @return integer DB_ enum
290 public function getReadDb();
293 * Set the database type to use for read operations.
295 * @param integer $db
297 * @since 1.20
299 public function setReadDb( $db );
302 * Update the records matching the provided conditions by
303 * setting the fields that are keys in the $values param to
304 * their corresponding values.
306 * @since 1.20
308 * @param array $values
309 * @param array $conditions
311 * @return boolean Success indicator
313 public function update( array $values, array $conditions = array() );
316 * Computes the values of the summary fields of the objects matching the provided conditions.
318 * @since 1.20
320 * @param array|string|null $summaryFields
321 * @param array $conditions
323 public function updateSummaryFields( $summaryFields = null, array $conditions = array() );
326 * Takes in an associative array with field names as keys and
327 * their values as value. The field names are prefixed with the
328 * db field prefix.
330 * @since 1.20
332 * @param array $values
334 * @return array
336 public function getPrefixedValues( array $values );
339 * Takes in a field or array of fields and returns an
340 * array with their prefixed versions, ready for db usage.
342 * @since 1.20
344 * @param array|string $fields
346 * @return array
348 public function getPrefixedFields( array $fields );
351 * Takes in a field and returns an it's prefixed version, ready for db usage.
353 * @since 1.20
355 * @param string|array $field
357 * @return string
359 public function getPrefixedField( $field );
362 * Takes an array of field names with prefix and returns the unprefixed equivalent.
364 * @since 1.20
366 * @param array $fieldNames
368 * @return array
370 public function unprefixFieldNames( array $fieldNames );
373 * Takes a field name with prefix and returns the unprefixed equivalent.
375 * @since 1.20
377 * @param string $fieldName
379 * @return string
381 public function unprefixFieldName( $fieldName );
384 * Get an instance of this class.
386 * @since 1.20
388 * @return IORMTable
390 public static function singleton();
393 * Get an array with fields from a database result,
394 * that can be fed directly to the constructor or
395 * to setFields.
397 * @since 1.20
399 * @param stdClass $result
401 * @return array
403 public function getFieldsFromDBResult( stdClass $result );
406 * Get a new instance of the class from a database result.
408 * @since 1.20
410 * @param stdClass $result
412 * @return IORMRow
414 public function newRowFromDBResult( stdClass $result );
417 * Get a new instance of the class from an array.
419 * @since 1.20
421 * @param array $data
422 * @param boolean $loadDefaults
424 * @return IORMRow
426 public function newRow( array $data, $loadDefaults = false );
429 * Return the names of the fields.
431 * @since 1.20
433 * @return array
435 public function getFieldNames();
438 * Gets if the object can take a certain field.
440 * @since 1.20
442 * @param string $name
444 * @return boolean
446 public function canHaveField( $name );