Merge "Convert various FormActions to OOUI"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / IMaintainableDatabase.php
1 <?php
2
3 /**
4 * This file deals with database interface functions
5 * and query specifics/optimisations.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Database
24 */
25 namespace Wikimedia\Rdbms;
26
27 use Exception;
28 use RuntimeException;
29 use DBUnexpectedError;
30
31 /**
32 * Advanced database interface for IDatabase handles that include maintenance methods
33 *
34 * This is useful for type-hints used by installer, upgrader, and background scripts
35 * that will make use of lower-level and longer-running queries, including schema changes.
36 *
37 * @ingroup Database
38 * @since 1.28
39 */
40 interface IMaintainableDatabase extends IDatabase {
41 /**
42 * Format a table name ready for use in constructing an SQL query
43 *
44 * This does two important things: it quotes the table names to clean them up,
45 * and it adds a table prefix if only given a table name with no quotes.
46 *
47 * All functions of this object which require a table name call this function
48 * themselves. Pass the canonical name to such functions. This is only needed
49 * when calling query() directly.
50 *
51 * @note This function does not sanitize user input. It is not safe to use
52 * this function to escape user input.
53 * @param string $name Database table name
54 * @param string $format One of:
55 * quoted - Automatically pass the table name through addIdentifierQuotes()
56 * so that it can be used in a query.
57 * raw - Do not add identifier quotes to the table name
58 * @return string Full database name
59 */
60 public function tableName( $name, $format = 'quoted' );
61
62 /**
63 * Fetch a number of table names into an array
64 * This is handy when you need to construct SQL for joins
65 *
66 * Example:
67 * extract( $dbr->tableNames( 'user', 'watchlist' ) );
68 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
69 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
70 *
71 * @return array
72 */
73 public function tableNames();
74
75 /**
76 * Fetch a number of table names into an zero-indexed numerical array
77 * This is handy when you need to construct SQL for joins
78 *
79 * Example:
80 * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
81 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
82 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
83 *
84 * @return array
85 */
86 public function tableNamesN();
87
88 /**
89 * Returns the size of a text field, or -1 for "unlimited"
90 *
91 * @param string $table
92 * @param string $field
93 * @return int
94 */
95 public function textFieldSize( $table, $field );
96
97 /**
98 * Read and execute SQL commands from a file.
99 *
100 * Returns true on success, error string or exception on failure (depending
101 * on object's error ignore settings).
102 *
103 * @param string $filename File name to open
104 * @param callable|null $lineCallback Optional function called before reading each line
105 * @param callable|null $resultCallback Optional function called for each MySQL result
106 * @param bool|string $fname Calling function name or false if name should be
107 * generated dynamically using $filename
108 * @param callable|null $inputCallback Optional function called for each
109 * complete line sent
110 * @return bool|string
111 * @throws Exception
112 */
113 public function sourceFile(
114 $filename,
115 callable $lineCallback = null,
116 callable $resultCallback = null,
117 $fname = false,
118 callable $inputCallback = null
119 );
120
121 /**
122 * Read and execute commands from an open file handle.
123 *
124 * Returns true on success, error string or exception on failure (depending
125 * on object's error ignore settings).
126 *
127 * @param resource $fp File handle
128 * @param callable|null $lineCallback Optional function called before reading each query
129 * @param callable|null $resultCallback Optional function called for each MySQL result
130 * @param string $fname Calling function name
131 * @param callable|null $inputCallback Optional function called for each complete query sent
132 * @return bool|string
133 */
134 public function sourceStream(
135 $fp,
136 callable $lineCallback = null,
137 callable $resultCallback = null,
138 $fname = __METHOD__,
139 callable $inputCallback = null
140 );
141
142 /**
143 * Called by sourceStream() to check if we've reached a statement end
144 *
145 * @param string &$sql SQL assembled so far
146 * @param string &$newLine New line about to be added to $sql
147 * @return bool Whether $newLine contains end of the statement
148 */
149 public function streamStatementEnd( &$sql, &$newLine );
150
151 /**
152 * Delete a table
153 * @param string $tableName
154 * @param string $fName
155 * @return bool|ResultWrapper
156 */
157 public function dropTable( $tableName, $fName = __METHOD__ );
158
159 /**
160 * Perform a deadlock-prone transaction.
161 *
162 * This function invokes a callback function to perform a set of write
163 * queries. If a deadlock occurs during the processing, the transaction
164 * will be rolled back and the callback function will be called again.
165 *
166 * Avoid using this method outside of Job or Maintenance classes.
167 *
168 * Usage:
169 * $dbw->deadlockLoop( callback, ... );
170 *
171 * Extra arguments are passed through to the specified callback function.
172 * This method requires that no transactions are already active to avoid
173 * causing premature commits or exceptions.
174 *
175 * Returns whatever the callback function returned on its successful,
176 * iteration, or false on error, for example if the retry limit was
177 * reached.
178 *
179 * @return mixed
180 * @throws DBUnexpectedError
181 * @throws Exception
182 */
183 public function deadlockLoop();
184
185 /**
186 * Lists all the VIEWs in the database
187 *
188 * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
189 * @param string $fname Name of calling function
190 * @throws RuntimeException
191 * @return array
192 */
193 public function listViews( $prefix = null, $fname = __METHOD__ );
194
195 /**
196 * Creates a new table with structure copied from existing table
197 *
198 * Note that unlike most database abstraction functions, this function does not
199 * automatically append database prefix, because it works at a lower abstraction level.
200 * The table names passed to this function shall not be quoted (this function calls
201 * addIdentifierQuotes() when needed).
202 *
203 * @param string $oldName Name of table whose structure should be copied
204 * @param string $newName Name of table to be created
205 * @param bool $temporary Whether the new table should be temporary
206 * @param string $fname Calling function name
207 * @return bool True if operation was successful
208 * @throws RuntimeException
209 */
210 public function duplicateTableStructure(
211 $oldName, $newName, $temporary = false, $fname = __METHOD__
212 );
213
214 /**
215 * Checks if table locks acquired by lockTables() are transaction-bound in their scope
216 *
217 * Transaction-bound table locks will be released when the current transaction terminates.
218 * Table locks that are not bound to a transaction are not effected by BEGIN/COMMIT/ROLLBACK
219 * and will last until either lockTables()/unlockTables() is called or the TCP connection to
220 * the database is closed.
221 *
222 * @return bool
223 * @since 1.29
224 */
225 public function tableLocksHaveTransactionScope();
226
227 /**
228 * Lock specific tables
229 *
230 * Any pending transaction should be resolved before calling this method, since:
231 * a) Doing so resets any REPEATABLE-READ snapshot of the data to a fresh one.
232 * b) Previous row and table locks from the transaction or session may be released
233 * by LOCK TABLES, which may be unsafe for the changes in such a transaction.
234 * c) The main use case of lockTables() is to avoid deadlocks and timeouts by locking
235 * entire tables in order to do long-running, batched, and lag-aware, updates. Batching
236 * and replication lag checks do not work when all the updates happen in a transaction.
237 *
238 * Always get all relevant table locks up-front in one call, since LOCK TABLES might release
239 * any prior table locks on some RDBMes (e.g MySQL).
240 *
241 * For compatibility, callers should check tableLocksHaveTransactionScope() before using
242 * this method. If locks are scoped specifically to transactions then caller must either:
243 * - a) Start a new transaction and acquire table locks for the scope of that transaction,
244 * doing all row updates within that transaction. It will not be possible to update
245 * rows in batches; this might result in high replication lag.
246 * - b) Forgo table locks entirely and avoid calling this method. Careful use of hints like
247 * LOCK IN SHARE MODE and FOR UPDATE and the use of query batching may be preferrable
248 * to using table locks with a potentially large transaction. Use of MySQL and Postges
249 * style REPEATABLE-READ (Snapshot Isolation with or without First-Committer-Rule) can
250 * also be considered for certain tasks that require a consistent view of entire tables.
251 *
252 * If session scoped locks are not supported, then calling lockTables() will trigger
253 * startAtomic(), with unlockTables() triggering endAtomic(). This will automatically
254 * start a transaction if one is not already present and cause the locks to be released
255 * when the transaction finishes (normally during the unlockTables() call).
256 *
257 * In any case, avoid using begin()/commit() in code that runs while such table locks are
258 * acquired, as that breaks in case when a transaction is needed. The startAtomic() and
259 * endAtomic() methods are safe, however, since they will join any existing transaction.
260 *
261 * @param array $read Array of tables to lock for read access
262 * @param array $write Array of tables to lock for write access
263 * @param string $method Name of caller
264 * @return bool
265 * @since 1.29
266 */
267 public function lockTables( array $read, array $write, $method );
268
269 /**
270 * Unlock all tables locked via lockTables()
271 *
272 * If table locks are scoped to transactions, then locks might not be released until the
273 * transaction ends, which could happen after this method is called.
274 *
275 * @param string $method The caller
276 * @return bool
277 * @since 1.29
278 */
279 public function unlockTables( $method );
280 }
281
282 class_alias( IMaintainableDatabase::class, 'IMaintainableDatabase' );