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