$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;
}
}
}
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;
* @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;
$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 );
$this->mServer, $this->mDBname, $this->mTrxShortId );
}
- if ( $this->getFlag( DBO_DEBUG ) ) {
+ if ( $this->getFlag( self::DBO_DEBUG ) ) {
$this->queryLogger->debug( "{$this->mDBname} {$commentedSql}" );
}
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 :)
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 );
$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;
}
}
$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 );
$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)."
}
// 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 );
}
$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;
}
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...
$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,
$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;
}
if ( (int)$this->port > 0 ) {
$connectVars['port'] = (int)$this->port;
}
- if ( $this->mFlags & DBO_SSL ) {
+ if ( $this->mFlags & self::DBO_SSL ) {
$connectVars['sslmode'] = 1;
}
$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 {
<?php
-
/**
* @defgroup Database Database
*
/** @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.
/**@{
* 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 );
/**@}*/
}
$serverInfo['hostName'] = $serverName;
$serverInfo['load'] = $load;
- $serverInfo += [ 'flags' => DBO_DEFAULT ];
+ $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ];
$servers[] = $serverInfo;
}
* @ingroup Database
* @author Aaron Schulz
*/
-require_once __DIR__ . '/../defines.php';
/**
* Database cluster connection, tracking, load balancing, and transaction manager interface
* @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
? [ 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)
}
# 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 )
/**
* 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 );
* }
$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 {
$db->setLBInfo( $server );
$db->setLazyMasterHandle(
- $this->getLazyConnectionRef( DB_MASTER, [], $db->getDomainID() )
+ $this->getLazyConnectionRef( self::DB_MASTER, [], $db->getDomainID() )
);
$db->setTableAliases( $this->tableAliases );
* @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.
* @param IDatabase $conn
*/
private function undoTransactionRoundFlags( IDatabase $conn ) {
- if ( $conn->getFlag( DBO_DEFAULT ) ) {
+ if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) {
$conn->restoreFlags( $conn::RESTORE_PRIOR );
}
}
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
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 );
if ( !$pos ) {
// Get the current master position
- $dbw = $this->getConnection( DB_MASTER );
+ $dbw = $this->getConnection( self::DB_MASTER );
$pos = $dbw->getMasterPos();
$this->reuseConnection( $dbw );
}