From 63ce63820e3dbf1d68df0b64ef31ace389c6bef8 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 23 Sep 2016 12:41:22 -0700 Subject: [PATCH] Add DBO_* class constants and defines.php alias the class constants Change-Id: If13b23ef849d4cf4c711b0ec2bf2e8a795f90738 --- includes/libs/rdbms/database/Database.php | 38 ++++++++++--------- .../libs/rdbms/database/DatabaseMysql.php | 6 +-- .../libs/rdbms/database/DatabaseMysqli.php | 6 +-- .../libs/rdbms/database/DatabasePostgres.php | 2 +- .../libs/rdbms/database/DatabaseSqlite.php | 2 +- includes/libs/rdbms/database/IDatabase.php | 22 ++++++++++- includes/libs/rdbms/defines.php | 24 ++++++------ .../libs/rdbms/lbfactory/LBFactoryMulti.php | 2 +- .../libs/rdbms/loadbalancer/ILoadBalancer.php | 7 ++-- .../libs/rdbms/loadbalancer/LoadBalancer.php | 24 ++++++------ 10 files changed, 78 insertions(+), 55 deletions(-) diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index f56f380085..3cd2eaad9f 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -248,11 +248,11 @@ abstract class Database implements IDatabase, LoggerAwareInterface { $this->agent = str_replace( '/', '-', $params['agent'] ); $this->mFlags = $params['flags']; - if ( $this->mFlags & DBO_DEFAULT ) { + if ( $this->mFlags & self::DBO_DEFAULT ) { if ( $this->cliMode ) { - $this->mFlags &= ~DBO_TRX; + $this->mFlags &= ~self::DBO_TRX; } else { - $this->mFlags |= DBO_TRX; + $this->mFlags |= self::DBO_TRX; } } @@ -407,9 +407,11 @@ abstract class Database implements IDatabase, LoggerAwareInterface { } public function bufferResults( $buffer = null ) { - $res = !$this->getFlag( DBO_NOBUFFER ); + $res = !$this->getFlag( self::DBO_NOBUFFER ); if ( $buffer !== null ) { - $buffer ? $this->clearFlag( DBO_NOBUFFER ) : $this->setFlag( DBO_NOBUFFER ); + $buffer + ? $this->clearFlag( self::DBO_NOBUFFER ) + : $this->setFlag( self::DBO_NOBUFFER ); } return $res; @@ -428,9 +430,11 @@ abstract class Database implements IDatabase, LoggerAwareInterface { * @return bool The previous value of the flag. */ protected function ignoreErrors( $ignoreErrors = null ) { - $res = $this->getFlag( DBO_IGNORE ); + $res = $this->getFlag( self::DBO_IGNORE ); if ( $ignoreErrors !== null ) { - $ignoreErrors ? $this->setFlag( DBO_IGNORE ) : $this->clearFlag( DBO_IGNORE ); + $ignoreErrors + ? $this->setFlag( self::DBO_IGNORE ) + : $this->clearFlag( self::DBO_IGNORE ); } return $res; @@ -828,7 +832,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface { $commentedSql = preg_replace( '/\s|$/', " /* $fname {$this->agent} */ ", $sql, 1 ); # Start implicit transactions that wrap the request if DBO_TRX is enabled - if ( !$this->mTrxLevel && $this->getFlag( DBO_TRX ) + if ( !$this->mTrxLevel && $this->getFlag( self::DBO_TRX ) && $this->isTransactableQuery( $sql ) ) { $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL ); @@ -842,7 +846,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface { $this->mServer, $this->mDBname, $this->mTrxShortId ); } - if ( $this->getFlag( DBO_DEBUG ) ) { + if ( $this->getFlag( self::DBO_DEBUG ) ) { $this->queryLogger->debug( "{$this->mDBname} {$commentedSql}" ); } @@ -2586,7 +2590,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface { return; } - $autoTrx = $this->getFlag( DBO_TRX ); // automatic begin() enabled? + $autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled? /** @var Exception $e */ $e = null; // first exception do { // callbacks may add callbacks :) @@ -2599,12 +2603,12 @@ abstract class Database implements IDatabase, LoggerAwareInterface { foreach ( $callbacks as $callback ) { try { list( $phpCallback ) = $callback; - $this->clearFlag( DBO_TRX ); // make each query its own transaction + $this->clearFlag( self::DBO_TRX ); // make each query its own transaction call_user_func_array( $phpCallback, [ $trigger ] ); if ( $autoTrx ) { - $this->setFlag( DBO_TRX ); // restore automatic begin() + $this->setFlag( self::DBO_TRX ); // restore automatic begin() } else { - $this->clearFlag( DBO_TRX ); // restore auto-commit + $this->clearFlag( self::DBO_TRX ); // restore auto-commit } } catch ( Exception $ex ) { call_user_func( $this->errorLogger, $ex ); @@ -2688,7 +2692,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface { $this->begin( $fname, self::TRANSACTION_INTERNAL ); // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result // in all changes being in one transaction to keep requests transactional. - if ( !$this->getFlag( DBO_TRX ) ) { + if ( !$this->getFlag( self::DBO_TRX ) ) { $this->mTrxAutomaticAtomic = true; } } @@ -2740,7 +2744,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface { $this->queryLogger->error( $msg ); return; // join the main transaction set } - } elseif ( $this->getFlag( DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) { + } elseif ( $this->getFlag( self::DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) { // @TODO: make this an exception at some point $msg = "$fname: Implicit transaction expected (DBO_TRX set)."; $this->queryLogger->error( $msg ); @@ -2852,7 +2856,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface { $this->queryLogger->error( "$fname: No transaction to rollback, something got out of sync." ); return; // nothing to do - } elseif ( $this->getFlag( DBO_TRX ) ) { + } elseif ( $this->getFlag( self::DBO_TRX ) ) { throw new DBUnexpectedError( $this, "$fname: Expected mass rollback of all peer databases (DBO_TRX set)." @@ -3019,7 +3023,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface { } // This will reconnect if possible or return false if not - $this->clearFlag( DBO_TRX, self::REMEMBER_PRIOR ); + $this->clearFlag( self::DBO_TRX, self::REMEMBER_PRIOR ); $ok = ( $this->query( self::PING_QUERY, __METHOD__, true ) !== false ); $this->restoreFlags( self::RESTORE_PRIOR ); diff --git a/includes/libs/rdbms/database/DatabaseMysql.php b/includes/libs/rdbms/database/DatabaseMysql.php index 87330b0e05..9ab7c64c1e 100644 --- a/includes/libs/rdbms/database/DatabaseMysql.php +++ b/includes/libs/rdbms/database/DatabaseMysql.php @@ -59,10 +59,10 @@ class DatabaseMysql extends DatabaseMysqlBase { } $connFlags = 0; - if ( $this->mFlags & DBO_SSL ) { + if ( $this->mFlags & self::DBO_SSL ) { $connFlags |= MYSQL_CLIENT_SSL; } - if ( $this->mFlags & DBO_COMPRESS ) { + if ( $this->mFlags & self::DBO_COMPRESS ) { $connFlags |= MYSQL_CLIENT_COMPRESS; } @@ -81,7 +81,7 @@ class DatabaseMysql extends DatabaseMysqlBase { if ( $i > 1 ) { usleep( 1000 ); } - if ( $this->mFlags & DBO_PERSISTENT ) { + if ( $this->mFlags & self::DBO_PERSISTENT ) { $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags ); } else { # Create a new connection... diff --git a/includes/libs/rdbms/database/DatabaseMysqli.php b/includes/libs/rdbms/database/DatabaseMysqli.php index fb983bdde0..c34f9018ec 100644 --- a/includes/libs/rdbms/database/DatabaseMysqli.php +++ b/includes/libs/rdbms/database/DatabaseMysqli.php @@ -82,7 +82,7 @@ class DatabaseMysqli extends DatabaseMysqlBase { $mysqli = mysqli_init(); $connFlags = 0; - if ( $this->mFlags & DBO_SSL ) { + if ( $this->mFlags & self::DBO_SSL ) { $connFlags |= MYSQLI_CLIENT_SSL; $mysqli->ssl_set( $this->sslKeyPath, @@ -92,10 +92,10 @@ class DatabaseMysqli extends DatabaseMysqlBase { $this->sslCiphers ); } - if ( $this->mFlags & DBO_COMPRESS ) { + if ( $this->mFlags & self::DBO_COMPRESS ) { $connFlags |= MYSQLI_CLIENT_COMPRESS; } - if ( $this->mFlags & DBO_PERSISTENT ) { + if ( $this->mFlags & self::DBO_PERSISTENT ) { $realServer = 'p:' . $realServer; } diff --git a/includes/libs/rdbms/database/DatabasePostgres.php b/includes/libs/rdbms/database/DatabasePostgres.php index 84439f4852..a69a5fac6c 100644 --- a/includes/libs/rdbms/database/DatabasePostgres.php +++ b/includes/libs/rdbms/database/DatabasePostgres.php @@ -109,7 +109,7 @@ class DatabasePostgres extends DatabaseBase { if ( (int)$this->port > 0 ) { $connectVars['port'] = (int)$this->port; } - if ( $this->mFlags & DBO_SSL ) { + if ( $this->mFlags & self::DBO_SSL ) { $connectVars['sslmode'] = 1; } diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php index f201dad006..c33d3b3587 100644 --- a/includes/libs/rdbms/database/DatabaseSqlite.php +++ b/includes/libs/rdbms/database/DatabaseSqlite.php @@ -171,7 +171,7 @@ class DatabaseSqlite extends DatabaseBase { $this->dbPath = $fileName; try { - if ( $this->mFlags & DBO_PERSISTENT ) { + if ( $this->mFlags & self::DBO_PERSISTENT ) { $this->mConn = new PDO( "sqlite:$fileName", '', '', [ PDO::ATTR_PERSISTENT => true ] ); } else { diff --git a/includes/libs/rdbms/database/IDatabase.php b/includes/libs/rdbms/database/IDatabase.php index 56eb002303..711bdb0d2d 100644 --- a/includes/libs/rdbms/database/IDatabase.php +++ b/includes/libs/rdbms/database/IDatabase.php @@ -1,5 +1,4 @@ DBO_DEFAULT ]; + $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ]; $servers[] = $serverInfo; } diff --git a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php index 3e1261e99b..cd51e660c3 100644 --- a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php @@ -21,7 +21,6 @@ * @ingroup Database * @author Aaron Schulz */ -require_once __DIR__ . '/../defines.php'; /** * Database cluster connection, tracking, load balancing, and transaction manager interface @@ -74,10 +73,10 @@ require_once __DIR__ . '/../defines.php'; * @ingroup Database */ interface ILoadBalancer { - /** @var integer Request a master DB connection */ - const DB_MASTER = DB_MASTER; /** @var integer Request a replica DB connection */ - const DB_REPLICA = DB_REPLICA; + const DB_REPLICA = -1; + /** @var integer Request a master DB connection */ + const DB_MASTER = -2; /** * Construct a manager of IDatabase connection objects diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index bda185a266..5a1a8ba2f9 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -530,10 +530,10 @@ class LoadBalancer implements ILoadBalancer { ? [ false ] // check one "group": the generic pool : (array)$groups; - $masterOnly = ( $i == DB_MASTER || $i == $this->getWriterIndex() ); + $masterOnly = ( $i == self::DB_MASTER || $i == $this->getWriterIndex() ); $oldConnsOpened = $this->connsOpened; // connections open now - if ( $i == DB_MASTER ) { + if ( $i == self::DB_MASTER ) { $i = $this->getWriterIndex(); } else { # Try to find an available server in any the query groups (in order) @@ -547,7 +547,7 @@ class LoadBalancer implements ILoadBalancer { } # Operation-based index - if ( $i == DB_REPLICA ) { + if ( $i == self::DB_REPLICA ) { $this->mLastError = 'Unknown error'; // reset error string # Try the general server pool if $groups are unavailable. $i = in_array( false, $groups, true ) @@ -589,7 +589,7 @@ class LoadBalancer implements ILoadBalancer { /** * This can happen in code like: * foreach ( $dbs as $db ) { - * $conn = $lb->getConnection( DB_REPLICA, [], $db ); + * $conn = $lb->getConnection( $lb::DB_REPLICA, [], $db ); * ... * $lb->reuseConnection( $conn ); * } @@ -816,7 +816,7 @@ class LoadBalancer implements ILoadBalancer { $server['agent'] = $this->agent; // Use DBO_DEFAULT flags by default for LoadBalancer managed databases. Assume that the // application calls LoadBalancer::commitMasterChanges() before the PHP script completes. - $server['flags'] = isset( $server['flags'] ) ? $server['flags'] : DBO_DEFAULT; + $server['flags'] = isset( $server['flags'] ) ? $server['flags'] : IDatabase::DBO_DEFAULT; // Create a live connection object try { @@ -829,7 +829,7 @@ class LoadBalancer implements ILoadBalancer { $db->setLBInfo( $server ); $db->setLazyMasterHandle( - $this->getLazyConnectionRef( DB_MASTER, [], $db->getDomainID() ) + $this->getLazyConnectionRef( self::DB_MASTER, [], $db->getDomainID() ) ); $db->setTableAliases( $this->tableAliases ); @@ -1170,10 +1170,10 @@ class LoadBalancer implements ILoadBalancer { * @param IDatabase $conn */ private function applyTransactionRoundFlags( IDatabase $conn ) { - if ( $conn->getFlag( DBO_DEFAULT ) ) { + if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) { // DBO_TRX is controlled entirely by CLI mode presence with DBO_DEFAULT. // Force DBO_TRX even in CLI mode since a commit round is expected soon. - $conn->setFlag( DBO_TRX, $conn::REMEMBER_PRIOR ); + $conn->setFlag( $conn::DBO_TRX, $conn::REMEMBER_PRIOR ); // If config has explicitly requested DBO_TRX be either on or off by not // setting DBO_DEFAULT, then respect that. Forcing no transactions is useful // for things like blob stores (ExternalStore) which want auto-commit mode. @@ -1184,7 +1184,7 @@ class LoadBalancer implements ILoadBalancer { * @param IDatabase $conn */ private function undoTransactionRoundFlags( IDatabase $conn ) { - if ( $conn->getFlag( DBO_DEFAULT ) ) { + if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) { $conn->restoreFlags( $conn::RESTORE_PRIOR ); } } @@ -1238,7 +1238,7 @@ class LoadBalancer implements ILoadBalancer { if ( !$this->laggedReplicaMode && $this->getServerCount() > 1 ) { try { // See if laggedReplicaMode gets set - $conn = $this->getConnection( DB_REPLICA, false, $domain ); + $conn = $this->getConnection( self::DB_REPLICA, false, $domain ); $this->reuseConnection( $conn ); } catch ( DBConnectionError $e ) { // Avoid expensive re-connect attempts and failures @@ -1305,7 +1305,7 @@ class LoadBalancer implements ILoadBalancer { function () use ( $domain, $conn ) { $this->trxProfiler->setSilenced( true ); try { - $dbw = $conn ?: $this->getConnection( DB_MASTER, [], $domain ); + $dbw = $conn ?: $this->getConnection( self::DB_MASTER, [], $domain ); $readOnly = (int)$dbw->serverIsReadOnly(); if ( !$conn ) { $this->reuseConnection( $dbw ); @@ -1432,7 +1432,7 @@ class LoadBalancer implements ILoadBalancer { if ( !$pos ) { // Get the current master position - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnection( self::DB_MASTER ); $pos = $dbw->getMasterPos(); $this->reuseConnection( $dbw ); } -- 2.20.1