4 * This file deals with database interface functions
5 * and query specifics/optimisations.
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.
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.
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
25 namespace Wikimedia\Rdbms
;
31 * Advanced database interface for IDatabase handles that include maintenance methods
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.
39 interface IMaintainableDatabase
extends IDatabase
{
41 * Format a table name ready for use in constructing an SQL query
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.
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.
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
59 public function tableName( $name, $format = 'quoted' );
62 * Fetch a number of table names into an array
63 * This is handy when you need to construct SQL for joins
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";
72 public function tableNames();
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
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";
85 public function tableNamesN();
88 * Returns the size of a text field, or -1 for "unlimited"
90 * @param string $table
91 * @param string $field
94 public function textFieldSize( $table, $field );
97 * Read and execute SQL commands from a file.
99 * Returns true on success, error string or exception on failure (depending
100 * on object's error ignore settings).
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
109 * @return bool|string
112 public function sourceFile(
114 callable
$lineCallback = null,
115 callable
$resultCallback = null,
117 callable
$inputCallback = null
121 * Read and execute commands from an open file handle.
123 * Returns true on success, error string or exception on failure (depending
124 * on object's error ignore settings).
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
133 public function sourceStream(
135 callable
$lineCallback = null,
136 callable
$resultCallback = null,
138 callable
$inputCallback = null
142 * Called by sourceStream() to check if we've reached a statement end
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
148 public function streamStatementEnd( &$sql, &$newLine );
152 * @param string $tableName
153 * @param string $fName
154 * @return bool|ResultWrapper
156 public function dropTable( $tableName, $fName = __METHOD__
);
159 * Perform a deadlock-prone transaction.
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.
165 * Avoid using this method outside of Job or Maintenance classes.
168 * $dbw->deadlockLoop( callback, ... );
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.
174 * Returns whatever the callback function returned on its successful,
175 * iteration, or false on error, for example if the retry limit was
179 * @throws DBUnexpectedError
182 public function deadlockLoop();
185 * Lists all the VIEWs in the database
187 * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
188 * @param string $fname Name of calling function
189 * @throws RuntimeException
192 public function listViews( $prefix = null, $fname = __METHOD__
);
195 * Creates a new table with structure copied from existing table
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).
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
209 public function duplicateTableStructure(
210 $oldName, $newName, $temporary = false, $fname = __METHOD__
214 * Checks if table locks acquired by lockTables() are transaction-bound in their scope
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.
224 public function tableLocksHaveTransactionScope();
227 * Lock specific tables
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.
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).
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.
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).
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.
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
266 public function lockTables( array $read, array $write, $method );
269 * Unlock all tables locked via lockTables()
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.
274 * @param string $method The caller
278 public function unlockTables( $method );
281 class_alias( IMaintainableDatabase
::class, 'IMaintainableDatabase' );