X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fdb%2FDatabase.php;h=4d1b1c0f04038096aa286eebc6f9d02ddd5f4ae1;hb=57222f96f1cdbce2259f0e51664b10b9d795ebe6;hp=cf0eab794cb0e255100818295d39d5ed69e70159;hpb=1e1764da09c8084c25dd197c43defada4aab2993;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/db/Database.php b/includes/db/Database.php index cf0eab794c..ebcf540433 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -58,7 +58,6 @@ abstract class DatabaseBase implements IDatabase { protected $mSchema; protected $mFlags; protected $mForeign; - protected $mErrorCount = 0; protected $mLBInfo = array(); protected $mDefaultBigSelects = null; protected $mSchemaVars = false; @@ -125,9 +124,9 @@ abstract class DatabaseBase implements IDatabase { /** * Array of levels of atomicity within transactions * - * @var SplStack + * @var array */ - private $mTrxAtomicLevels; + private $mTrxAtomicLevels = array(); /** * Record if the current transaction was started implicitly by DatabaseBase::startAtomic @@ -259,15 +258,6 @@ abstract class DatabaseBase implements IDatabase { return $this->mTrxLevel ? $this->mTrxTimestamp : null; } - /** - * Get/set the number of errors logged. Only useful when errors are ignored - * @param int $count The count to set, or omitted to leave it unchanged. - * @return int The error count - */ - public function errorCount( $count = null ) { - return wfSetVar( $this->mErrorCount, $count ); - } - /** * Get/set the table prefix. * @param string $prefix The table prefix to set, or omitted to leave it unchanged. @@ -332,24 +322,6 @@ abstract class DatabaseBase implements IDatabase { } } - /** - * Set lag time in seconds for a fake slave - * - * @param mixed $lag Valid values for this parameter are determined by the - * subclass, but should be a PHP scalar or array that would be sensible - * as part of $wgLBFactoryConf. - */ - public function setFakeSlaveLag( $lag ) { - } - - /** - * Make this connection a fake master - * - * @param bool $enabled - */ - public function setFakeMaster( $enabled = true ) { - } - /** * @return TransactionProfiler */ @@ -507,11 +479,7 @@ abstract class DatabaseBase implements IDatabase { * - DBO_PERSISTENT: use persistant database connection */ public function setFlag( $flag ) { - global $wgDebugDBTransactions; $this->mFlags |= $flag; - if ( ( $flag & DBO_TRX ) && $wgDebugDBTransactions ) { - wfDebug( "Implicit transactions are now enabled.\n" ); - } } /** @@ -526,11 +494,7 @@ abstract class DatabaseBase implements IDatabase { * - DBO_PERSISTENT: use persistant database connection */ public function clearFlag( $flag ) { - global $wgDebugDBTransactions; $this->mFlags &= ~$flag; - if ( ( $flag & DBO_TRX ) && $wgDebugDBTransactions ) { - wfDebug( "Implicit transactions are now disabled.\n" ); - } } /** @@ -635,9 +599,7 @@ abstract class DatabaseBase implements IDatabase { * @param array $params Parameters passed from DatabaseBase::factory() */ function __construct( array $params ) { - global $wgDBprefix, $wgDBmwschema, $wgCommandLineMode, $wgDebugDBTransactions; - - $this->mTrxAtomicLevels = new SplStack; + global $wgDBprefix, $wgDBmwschema, $wgCommandLineMode; $server = $params['host']; $user = $params['user']; @@ -652,14 +614,8 @@ abstract class DatabaseBase implements IDatabase { if ( $this->mFlags & DBO_DEFAULT ) { if ( $wgCommandLineMode ) { $this->mFlags &= ~DBO_TRX; - if ( $wgDebugDBTransactions ) { - wfDebug( "Implicit transaction open disabled.\n" ); - } } else { $this->mFlags |= DBO_TRX; - if ( $wgDebugDBTransactions ) { - wfDebug( "Implicit transaction open enabled.\n" ); - } } } @@ -956,7 +912,7 @@ abstract class DatabaseBase implements IDatabase { * for a successful read query, or false on failure if $tempIgnore set */ public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) { - global $wgUser, $wgDebugDBTransactions, $wgDebugDumpSqlLength; + global $wgUser; $this->mLastQuery = $sql; @@ -986,9 +942,6 @@ abstract class DatabaseBase implements IDatabase { $commentedSql = preg_replace( '/\s|$/', " /* $fname $userName */ ", $sql, 1 ); if ( !$this->mTrxLevel && $this->getFlag( DBO_TRX ) && $this->isTransactableQuery( $sql ) ) { - if ( $wgDebugDBTransactions ) { - wfDebug( "Implicit transaction start.\n" ); - } $this->begin( __METHOD__ . " ($fname)" ); $this->mTrxAutomatic = true; } @@ -1020,15 +973,7 @@ abstract class DatabaseBase implements IDatabase { } if ( $this->debug() ) { - static $cnt = 0; - - $cnt++; - $sqlx = $wgDebugDumpSqlLength ? substr( $commentedSql, 0, $wgDebugDumpSqlLength ) - : $commentedSql; - $sqlx = strtr( $sqlx, "\t\n", ' ' ); - - $master = $isMaster ? 'master' : 'slave'; - wfDebug( "Query {$this->mDBname} ($cnt) ($master): $sqlx\n" ); + wfDebugLog( 'queries', sprintf( "%s: %s", $this->mDBname, $sql ) ); } $queryId = MWDebug::query( $sql, $fname, $isMaster ); @@ -1089,8 +1034,8 @@ abstract class DatabaseBase implements IDatabase { $res = $this->resultObject( $ret ); // Destroy profile sections in the opposite order to their creation - $queryProfSection = false; - $totalProfSection = false; + ScopedCallback::consume( $queryProfSection ); + ScopedCallback::consume( $totalProfSection ); if ( $isWriteQuery && $this->mTrxLevel ) { $this->mTrxWriteDuration += $queryRuntime; @@ -1111,8 +1056,6 @@ abstract class DatabaseBase implements IDatabase { * @throws DBQueryError */ public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) { - ++$this->mErrorCount; - if ( $this->ignoreErrors() || $tempIgnore ) { wfDebug( "SQL ERROR (ignored): $error\n" ); } else { @@ -1262,6 +1205,7 @@ abstract class DatabaseBase implements IDatabase { * @param string|array $options The query options. See DatabaseBase::select() for details. * * @return bool|mixed The value from the field, or false on failure. + * @throws DBUnexpectedError */ public function selectField( $table, $var, $cond = '', $fname = __METHOD__, $options = array() @@ -1359,9 +1303,9 @@ abstract class DatabaseBase implements IDatabase { $preLimitTail .= $this->makeOrderBy( $options ); // if (isset($options['LIMIT'])) { - // $tailOpts .= $this->limitResult('', $options['LIMIT'], - // isset($options['OFFSET']) ? $options['OFFSET'] - // : false); + // $tailOpts .= $this->limitResult('', $options['LIMIT'], + // isset($options['OFFSET']) ? $options['OFFSET'] + // : false); // } if ( isset( $noKeyOptions['FOR UPDATE'] ) ) { @@ -1785,7 +1729,7 @@ abstract class DatabaseBase implements IDatabase { * * @return string */ - static function generalizeSQL( $sql ) { + protected static function generalizeSQL( $sql ) { # This does the same as the regexp below would do, but in such a way # as to avoid crashing php on some large strings. # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql ); @@ -1901,9 +1845,6 @@ abstract class DatabaseBase implements IDatabase { * This causes a multi-row INSERT on DBMSs that support it. The keys in * each subarray must be identical to each other, and in the same order. * - * Usually throws a DBQueryError on failure. If errors are explicitly ignored, - * returns success. - * * $options is an array of options, with boolean options encoded as values * with numeric keys, in the same style as $options in * DatabaseBase::select(). Supported options are: @@ -1919,6 +1860,9 @@ abstract class DatabaseBase implements IDatabase { * @param string $fname Calling function name (use __METHOD__) for logs/profiling * @param array $options Array of options * + * @throws DBQueryError Usually throws a DBQueryError on failure. If errors are explicitly ignored, + * returns success. + * * @return bool */ public function insert( $table, $a, $fname = __METHOD__, $options = array() ) { @@ -2449,7 +2393,7 @@ abstract class DatabaseBase implements IDatabase { if ( !$alias || (string)$alias === (string)$name ) { return $name; } else { - return $name . ' AS ' . $alias; //PostgreSQL needs AS + return $name . ' AS ' . $alias; // PostgreSQL needs AS } } @@ -3189,6 +3133,17 @@ abstract class DatabaseBase implements IDatabase { return false; } + /** + * Determines if the given query error was a connection drop + * STUB + * + * @param integer|string $errno + * @return bool + */ + public function wasConnectionError( $errno ) { + return false; + } + /** * Perform a deadlock-prone transaction. * @@ -3212,11 +3167,6 @@ abstract class DatabaseBase implements IDatabase { $args = func_get_args(); $function = array_shift( $args ); $tries = self::DEADLOCK_TRIES; - if ( is_array( $function ) ) { - $fname = $function[0]; - } else { - $fname = $function; - } $this->begin( __METHOD__ ); @@ -3227,9 +3177,6 @@ abstract class DatabaseBase implements IDatabase { $retVal = call_user_func_array( $function, $args ); break; } catch ( DBQueryError $e ) { - $error = $this->lastError(); - $errno = $this->lastErrno(); - $sql = $this->lastQuery(); if ( $this->wasDeadlock() ) { // Retry after a randomized delay usleep( mt_rand( self::DEADLOCK_DELAY_MIN, self::DEADLOCK_DELAY_MAX ) ); @@ -3421,7 +3368,7 @@ abstract class DatabaseBase implements IDatabase { } } - $this->mTrxAtomicLevels->push( $fname ); + $this->mTrxAtomicLevels[] = $fname; } /** @@ -3439,13 +3386,13 @@ abstract class DatabaseBase implements IDatabase { if ( !$this->mTrxLevel ) { throw new DBUnexpectedError( $this, 'No atomic transaction is open.' ); } - if ( $this->mTrxAtomicLevels->isEmpty() || - $this->mTrxAtomicLevels->pop() !== $fname + if ( !$this->mTrxAtomicLevels || + array_pop( $this->mTrxAtomicLevels ) !== $fname ) { throw new DBUnexpectedError( $this, 'Invalid atomic section ended.' ); } - if ( $this->mTrxAtomicLevels->isEmpty() && $this->mTrxAutomaticAtomic ) { + if ( !$this->mTrxAtomicLevels && $this->mTrxAutomaticAtomic ) { $this->commit( $fname, 'flush' ); } } @@ -3466,10 +3413,8 @@ abstract class DatabaseBase implements IDatabase { * @throws DBError */ final public function begin( $fname = __METHOD__ ) { - global $wgDebugDBTransactions; - if ( $this->mTrxLevel ) { // implicit commit - if ( !$this->mTrxAtomicLevels->isEmpty() ) { + if ( $this->mTrxAtomicLevels ) { // If the current transaction was an automatic atomic one, then we definitely have // a problem. Same if there is any unclosed atomic level. throw new DBUnexpectedError( $this, @@ -3488,9 +3433,8 @@ abstract class DatabaseBase implements IDatabase { ) ) ); } else { - // if the transaction was automatic and has done write operations, - // log it if $wgDebugDBTransactions is enabled. - if ( $this->mTrxDoneWrites && $wgDebugDBTransactions ) { + // if the transaction was automatic and has done write operations + if ( $this->mTrxDoneWrites ) { wfDebug( "$fname: Automatic transaction with writes in progress" . " (from {$this->mTrxFname}), performing implicit commit!\n" ); @@ -3517,7 +3461,7 @@ abstract class DatabaseBase implements IDatabase { $this->mTrxDoneWrites = false; $this->mTrxAutomatic = false; $this->mTrxAutomaticAtomic = false; - $this->mTrxAtomicLevels = new SplStack; + $this->mTrxAtomicLevels = array(); $this->mTrxIdleCallbacks = array(); $this->mTrxPreCommitCallbacks = array(); $this->mTrxShortId = wfRandomString( 12 ); @@ -3550,7 +3494,7 @@ abstract class DatabaseBase implements IDatabase { * @throws DBUnexpectedError */ final public function commit( $fname = __METHOD__, $flush = '' ) { - if ( !$this->mTrxAtomicLevels->isEmpty() ) { + if ( $this->mTrxLevel && $this->mTrxAtomicLevels ) { // There are still atomic sections open. This cannot be ignored throw new DBUnexpectedError( $this, @@ -3636,7 +3580,7 @@ abstract class DatabaseBase implements IDatabase { $this->doRollback( $fname ); $this->mTrxIdleCallbacks = array(); // cancel $this->mTrxPreCommitCallbacks = array(); // cancel - $this->mTrxAtomicLevels = new SplStack; + $this->mTrxAtomicLevels = array(); if ( $this->mTrxDoneWrites ) { $this->getTransactionProfiler()->transactionWritingOut( $this->mServer, $this->mDBname, $this->mTrxShortId ); @@ -3770,14 +3714,13 @@ abstract class DatabaseBase implements IDatabase { * Once upon a time, DatabaseBase::query() returned a bare MySQL result * resource, and it was necessary to call this function to convert it to * a wrapper. Nowadays, raw database objects are never exposed to external - * callers, so this is unnecessary in external code. For compatibility with - * old code, ResultWrapper objects are passed through unaltered. + * callers, so this is unnecessary in external code. * - * @param bool|ResultWrapper|resource $result + * @param bool|ResultWrapper|resource|object $result * @return bool|ResultWrapper */ - public function resultObject( $result ) { - if ( empty( $result ) ) { + protected function resultObject( $result ) { + if ( !$result ) { return false; } elseif ( $result instanceof ResultWrapper ) { return $result; @@ -4275,3 +4218,8 @@ abstract class DatabaseBase implements IDatabase { } } } + +abstract class Database extends DatabaseBase { + // B/C until nothing type hints for DatabaseBase + // @TODO: finish renaming DatabaseBase => Database +}