Add DBO_* class constants and defines.php alias the class constants
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 23 Sep 2016 19:41:22 +0000 (12:41 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 23 Sep 2016 19:58:44 +0000 (19:58 +0000)
Change-Id: If13b23ef849d4cf4c711b0ec2bf2e8a795f90738

includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabaseMysql.php
includes/libs/rdbms/database/DatabaseMysqli.php
includes/libs/rdbms/database/DatabasePostgres.php
includes/libs/rdbms/database/DatabaseSqlite.php
includes/libs/rdbms/database/IDatabase.php
includes/libs/rdbms/defines.php
includes/libs/rdbms/lbfactory/LBFactoryMulti.php
includes/libs/rdbms/loadbalancer/ILoadBalancer.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php

index f56f380..3cd2eaa 100644 (file)
@@ -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 );
 
index 87330b0..9ab7c64 100644 (file)
@@ -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...
index fb983bd..c34f901 100644 (file)
@@ -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;
                }
 
index 84439f4..a69a5fa 100644 (file)
@@ -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;
                }
 
index f201dad..c33d3b3 100644 (file)
@@ -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 {
index 56eb002..711bdb0 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /**
  * @defgroup Database Database
  *
@@ -74,6 +73,27 @@ interface IDatabase {
        /** @var int Combine list with OR clauses */
        const LIST_OR = 4;
 
+       /** @var int Enable debug logging */
+       const DBO_DEBUG = 1;
+       /** @var int Disable query buffering (only one result set can be iterated at a time) */
+       const DBO_NOBUFFER = 2;
+       /** @var int Ignore query errors (internal use only!) */
+       const DBO_IGNORE = 4;
+       /** @var int Autoatically start transaction on first query (work with ILoadBalancer rounds) */
+       const DBO_TRX = 8;
+       /** @var int Use DBO_TRX in non-CLI mode */
+       const DBO_DEFAULT = 16;
+       /** @var int Use DB persistent connections if possible */
+       const DBO_PERSISTENT = 32;
+       /** @var int DBA session mode; mostly for Oracle */
+       const DBO_SYSDBA = 64;
+       /** @var int Schema file mode; mostly for Oracle */
+       const DBO_DDLMODE = 128;
+       /** @var int Enable SSL/TLS in connection protocol */
+       const DBO_SSL = 256;
+       /** @var int Enable compression in connection protocol */
+       const DBO_COMPRESS = 512;
+
        /**
         * A string describing the current software version, and possibly
         * other details in a user-friendly way. Will be listed on Special:Version, etc.
index b420ca1..692a704 100644 (file)
@@ -3,22 +3,22 @@
 /**@{
  * Database related constants
  */
-define( 'DBO_DEBUG', 1 );
-define( 'DBO_NOBUFFER', 2 );
-define( 'DBO_IGNORE', 4 );
-define( 'DBO_TRX', 8 ); // automatically start transaction on first query
-define( 'DBO_DEFAULT', 16 );
-define( 'DBO_PERSISTENT', 32 );
-define( 'DBO_SYSDBA', 64 ); // for oracle maintenance
-define( 'DBO_DDLMODE', 128 ); // when using schema files: mostly for Oracle
-define( 'DBO_SSL', 256 );
-define( 'DBO_COMPRESS', 512 );
+define( 'DBO_DEBUG', IDatabase::DBO_DEBUG );
+define( 'DBO_NOBUFFER', IDatabase::DBO_NOBUFFER );
+define( 'DBO_IGNORE', IDatabase::DBO_IGNORE );
+define( 'DBO_TRX', IDatabase::DBO_TRX );
+define( 'DBO_DEFAULT', IDatabase::DBO_DEFAULT );
+define( 'DBO_PERSISTENT', IDatabase::DBO_PERSISTENT );
+define( 'DBO_SYSDBA', IDatabase::DBO_SYSDBA );
+define( 'DBO_DDLMODE', IDatabase::DBO_DDLMODE );
+define( 'DBO_SSL', IDatabase::DBO_SSL );
+define( 'DBO_COMPRESS', IDatabase::DBO_COMPRESS );
 /**@}*/
 
 /**@{
  * Valid database indexes
  * Operation-based indexes
  */
-define( 'DB_REPLICA', -1 );     # Read from a replica (or only server)
-define( 'DB_MASTER', -2 );    # Write to master (or only server)
+define( 'DB_REPLICA', ILoadBalancer::DB_REPLICA );
+define( 'DB_MASTER', ILoadBalancer::DB_MASTER );
 /**@}*/
index 6993dac..2fb8c4b 100644 (file)
@@ -359,7 +359,7 @@ class LBFactoryMulti extends LBFactory {
                        }
                        $serverInfo['hostName'] = $serverName;
                        $serverInfo['load'] = $load;
-                       $serverInfo += [ 'flags' => DBO_DEFAULT ];
+                       $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ];
 
                        $servers[] = $serverInfo;
                }
index 3e1261e..cd51e66 100644 (file)
@@ -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
index bda185a..5a1a8ba 100644 (file)
@@ -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 );
                }