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