Move IDatabase/IMaintainableDatabase to Rdbms namespace
[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 class_alias( 'Wikimedia\Rdbms\IMaintainableDatabase', 'IMaintainableDatabase' );