IMaintainableDatabase.php 11 KB

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