rdbms: update IDatabase::getLag comments
[lhc/web/wiklou.git] / includes / libs / rdbms / database / IDatabase.php
index e5e2076..545c8e3 100644 (file)
@@ -49,6 +49,11 @@ interface IDatabase {
        /** @var string Transaction is requested internally via DBO_TRX/startAtomic() */
        const TRANSACTION_INTERNAL = 'implicit';
 
+       /** @var string Atomic section is not cancelable */
+       const ATOMIC_NOT_CANCELABLE = '';
+       /** @var string Atomic section is cancelable */
+       const ATOMIC_CANCELABLE = 'cancelable';
+
        /** @var string Transaction operation comes from service managing all DBs */
        const FLUSHING_ALL_PEERS = 'flush';
        /** @var string Transaction operation comes from the database class internally */
@@ -228,6 +233,7 @@ interface IDatabase {
         * Should return true if unsure.
         *
         * @return bool
+        * @deprecated Since 1.31; use lastDoneWrites()
         */
        public function doneWrites();
 
@@ -454,17 +460,6 @@ interface IDatabase {
         */
        public function lastError();
 
-       /**
-        * mysql_fetch_field() wrapper
-        * Returns false if the field doesn't exist
-        *
-        * @param string $table Table name
-        * @param string $field Field name
-        *
-        * @return Field
-        */
-       public function fieldInfo( $table, $field );
-
        /**
         * Get the number of rows affected by the last write query
         * @see https://secure.php.net/mysql_affected_rows
@@ -503,12 +498,6 @@ interface IDatabase {
         */
        public function close();
 
-       /**
-        * @param string $error Fallback error message, used if none is given by DB
-        * @throws DBConnectionError
-        */
-       public function reportConnectionError( $error = 'Unknown error' );
-
        /**
         * Run an SQL query and return the result. Normally throws a DBQueryError
         * on failure. If errors are ignored, returns false instead.
@@ -537,19 +526,6 @@ interface IDatabase {
         */
        public function query( $sql, $fname = __METHOD__, $tempIgnore = false );
 
-       /**
-        * Report a query error. Log the error, and if neither the object ignore
-        * flag nor the $tempIgnore flag is set, throw a DBQueryError.
-        *
-        * @param string $error
-        * @param int $errno
-        * @param string $sql
-        * @param string $fname
-        * @param bool $tempIgnore
-        * @throws DBQueryError
-        */
-       public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false );
-
        /**
         * Free a result object returned by query() or select(). It's usually not
         * necessary to call this, just use unset() or let the variable holding
@@ -628,6 +604,11 @@ interface IDatabase {
         * This includes the user table in the query, with the alias "a" available
         * for use in field names (e.g. a.user_name).
         *
+        * A derived table, defined by the result of selectSQLText(), requires an alias
+        * key and a Subquery instance value which wraps the SQL query, for example:
+        *
+        *    [ 'c' => new Subquery( 'SELECT ...' ) ]
+        *
         * Joins using parentheses for grouping (since MediaWiki 1.31) may be
         * constructed using nested arrays. For example,
         *
@@ -777,15 +758,15 @@ interface IDatabase {
         * doing UNION queries, where the SQL text of each query is needed. In general,
         * however, callers outside of Database classes should just use select().
         *
+        * @see IDatabase::select()
+        *
         * @param string|array $table Table name
         * @param string|array $vars Field names
         * @param string|array $conds Conditions
         * @param string $fname Caller function name
         * @param string|array $options Query options
         * @param string|array $join_conds Join conditions
-        *
-        * @return string SQL query string.
-        * @see IDatabase::select()
+        * @return string SQL query string
         */
        public function selectSQLText(
                $table, $vars, $conds = '', $fname = __METHOD__,
@@ -825,7 +806,7 @@ interface IDatabase {
         * Takes the same arguments as IDatabase::select().
         *
         * @param string $table Table name
-        * @param string $vars Unused
+        * @param string $var Column for which NULL values are not counted [default "*"]
         * @param array|string $conds Filters on the table
         * @param string $fname Function name for profiling
         * @param array $options Options for select
@@ -834,7 +815,7 @@ interface IDatabase {
         * @throws DBError
         */
        public function estimateRowCount(
-               $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
+               $table, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
        );
 
        /**
@@ -847,7 +828,7 @@ interface IDatabase {
         * @since 1.27 Added $join_conds parameter
         *
         * @param array|string $tables Table names
-        * @param string $vars Unused
+        * @param string $var Column for which NULL values are not counted [default "*"]
         * @param array|string $conds Filters on the table
         * @param string $fname Function name for profiling
         * @param array $options Options for select
@@ -856,7 +837,7 @@ interface IDatabase {
         * @throws DBError
         */
        public function selectRowCount(
-               $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
+               $tables, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
        );
 
        /**
@@ -893,16 +874,6 @@ interface IDatabase {
         */
        public function tableExists( $table, $fname = __METHOD__ );
 
-       /**
-        * Determines if a given index is unique
-        *
-        * @param string $table
-        * @param string $index
-        *
-        * @return bool
-        */
-       public function indexUnique( $table, $index );
-
        /**
         * INSERT wrapper, inserts an array into a table.
         *
@@ -1087,6 +1058,25 @@ interface IDatabase {
         */
        public function buildIntegerCast( $field );
 
+       /**
+        * Equivalent to IDatabase::selectSQLText() except wraps the result in Subqyery
+        *
+        * @see IDatabase::selectSQLText()
+        *
+        * @param string|array $table Table name
+        * @param string|array $vars Field names
+        * @param string|array $conds Conditions
+        * @param string $fname Caller function name
+        * @param string|array $options Query options
+        * @param string|array $join_conds Join conditions
+        * @return Subquery
+        * @since 1.31
+        */
+       public function buildSelectSubquery(
+               $table, $vars, $conds = '', $fname = __METHOD__,
+               $options = [], $join_conds = []
+       );
+
        /**
         * Returns true if DBs are assumed to be on potentially different servers
         *
@@ -1556,25 +1546,27 @@ interface IDatabase {
        /**
         * Begin an atomic section of statements
         *
-        * If a transaction has been started already, just keep track of the given
-        * section name to make sure the transaction is not committed pre-maturely.
-        * This function can be used in layers (with sub-sections), so use a stack
-        * to keep track of the different atomic sections. If there is no transaction,
-        * start one implicitly.
+        * If a transaction has been started already, (optionally) sets a savepoint
+        * and tracks the given section name to make sure the transaction is not
+        * committed pre-maturely. This function can be used in layers (with
+        * sub-sections), so use a stack to keep track of the different atomic
+        * sections. If there is no transaction, one is started implicitly.
         *
         * The goal of this function is to create an atomic section of SQL queries
         * without having to start a new transaction if it already exists.
         *
-        * All atomic levels *must* be explicitly closed using IDatabase::endAtomic(),
-        * and any database transactions cannot be began or committed until all atomic
-        * levels are closed. There is no such thing as implicitly opening or closing
-        * an atomic section.
+        * All atomic levels *must* be explicitly closed using IDatabase::endAtomic()
+        * or IDatabase::cancelAtomic(), and any database transactions cannot be
+        * began or committed until all atomic levels are closed. There is no such
+        * thing as implicitly opening or closing an atomic section.
         *
         * @since 1.23
         * @param string $fname
+        * @param string $cancelable Pass self::ATOMIC_CANCELABLE to use a
+        *  savepoint and enable self::cancelAtomic() for this section.
         * @throws DBError
         */
-       public function startAtomic( $fname = __METHOD__ );
+       public function startAtomic( $fname = __METHOD__, $cancelable = self::ATOMIC_NOT_CANCELABLE );
 
        /**
         * Ends an atomic section of SQL statements
@@ -1589,6 +1581,28 @@ interface IDatabase {
         */
        public function endAtomic( $fname = __METHOD__ );
 
+       /**
+        * Cancel an atomic section of SQL statements
+        *
+        * This will roll back only the statements executed since the start of the
+        * most recent atomic section, and close that section. If a transaction was
+        * open before the corresponding startAtomic() call, any statements before
+        * that call are *not* rolled back and the transaction remains open. If the
+        * corresponding startAtomic() implicitly started a transaction, that
+        * transaction is rolled back.
+        *
+        * Note that a call to IDatabase::rollback() will also roll back any open
+        * atomic sections.
+        *
+        * @note As a micro-optimization to save a few DB calls, this method may only
+        *  be called when startAtomic() was called with the ATOMIC_CANCELABLE flag.
+        * @since 1.31
+        * @see IDatabase::startAtomic
+        * @param string $fname
+        * @throws DBError
+        */
+       public function cancelAtomic( $fname = __METHOD__ );
+
        /**
         * Run a callback to do an atomic set of updates for this database
         *
@@ -1596,17 +1610,18 @@ interface IDatabase {
         *   - This database object
         *   - The value of $fname
         *
-        * If any exception occurs in the callback, then rollback() will be called and the error will
-        * be re-thrown. It may also be that the rollback itself fails with an exception before then.
-        * In any case, such errors are expected to terminate the request, without any outside caller
-        * attempting to catch errors and commit anyway. Note that any rollback undoes all prior
-        * atomic section and uncommitted updates, which trashes the current request, requiring an
-        * error to be displayed.
+        * If any exception occurs in the callback, then cancelAtomic() will be
+        * called to back out any statements executed by the callback and the error
+        * will be re-thrown. It may also be that the cancel itself fails with an
+        * exception before then. In any case, such errors are expected to
+        * terminate the request, without any outside caller attempting to catch
+        * errors and commit anyway.
         *
-        * This can be an alternative to explicit startAtomic()/endAtomic() calls.
+        * This can be an alternative to explicit startAtomic()/endAtomic()/cancelAtomic() calls.
         *
         * @see Database::startAtomic
         * @see Database::endAtomic
+        * @see Database::cancelAtomic
         *
         * @param string $fname Caller name (usually __METHOD__)
         * @param callable $callback Callback that issues DB updates
@@ -1614,7 +1629,9 @@ interface IDatabase {
         * @throws DBError
         * @throws RuntimeException
         * @throws UnexpectedValueException
-        * @since 1.27
+        * @since 1.27; prior to 1.31 this did a rollback() instead of
+        *  cancelAtomic(), and assumed no callers up the stack would ever try to
+        *  catch the exception.
         */
        public function doAtomicSection( $fname, callable $callback );
 
@@ -1698,16 +1715,6 @@ interface IDatabase {
         */
        public function flushSnapshot( $fname = __METHOD__ );
 
-       /**
-        * List all tables on the database
-        *
-        * @param string $prefix Only show tables with this prefix, e.g. mw_
-        * @param string $fname Calling function name
-        * @throws DBError
-        * @return array
-        */
-       public function listTables( $prefix = null, $fname = __METHOD__ );
-
        /**
         * Convert a timestamp in one of the formats accepted by wfTimestamp()
         * to the format used for inserting into timestamp fields in this DBMS.
@@ -1745,11 +1752,9 @@ interface IDatabase {
        public function ping( &$rtt = null );
 
        /**
-        * Get replica DB lag. Currently supported only by MySQL.
+        * Get the amount of replication lag for this database server
         *
-        * Note that this function will generate a fatal error on many
-        * installations. Most callers should use LoadBalancer::safeGetLag()
-        * instead.
+        * Callers should avoid using this method while a transaction is active
         *
         * @return int|bool Database replication lag in seconds or false on error
         * @throws DBError