/** @var array Map of (name => 1) for locks obtained via lock() */
private $mNamedLocksHeld = [];
+ /** @var array Map of (table name => 1) for TEMPORARY tables */
+ private $mSessionTempTables = [];
/** @var IDatabase|null Lazy handle to the master DB this server replicates from */
private $lazyMasterHandle;
$user = $params['user'];
$password = $params['password'];
$dbName = $params['dbname'];
- $flags = $params['flags'];
$this->mSchema = $params['schema'];
$this->mTablePrefix = $params['tablePrefix'];
- $this->cliMode = isset( $params['cliMode'] )
- ? $params['cliMode']
- : ( PHP_SAPI === 'cli' );
- $this->agent = isset( $params['agent'] )
- ? str_replace( '/', '-', $params['agent'] ) // escape for comment
- : '';
+ $this->cliMode = $params['cliMode'];
+ // Agent name is added to SQL queries in a comment, so make sure it can't break out
+ $this->agent = str_replace( '/', '-', $params['agent'] );
- $this->mFlags = $flags;
+ $this->mFlags = $params['flags'];
if ( $this->mFlags & DBO_DEFAULT ) {
if ( $this->cliMode ) {
$this->mFlags &= ~DBO_TRX;
? $params['srvCache']
: new HashBagOStuff();
- $this->profiler = isset( $params['profiler'] ) ? $params['profiler'] : null;
- $this->trxProfiler = isset( $params['trxProfiler'] )
- ? $params['trxProfiler']
- : new TransactionProfiler();
- $this->connLogger = isset( $params['connLogger'] )
- ? $params['connLogger']
- : new \Psr\Log\NullLogger();
- $this->queryLogger = isset( $params['queryLogger'] )
- ? $params['queryLogger']
- : new \Psr\Log\NullLogger();
+ $this->profiler = $params['profiler'];
+ $this->trxProfiler = $params['trxProfiler'];
+ $this->connLogger = $params['connLogger'];
+ $this->queryLogger = $params['queryLogger'];
+
+ // Set initial dummy domain until open() sets the final DB/prefix
+ $this->currentDomain = DatabaseDomain::newUnspecified();
if ( $user ) {
$this->open( $server, $user, $password, $dbName );
}
// Set the domain object after open() sets the relevant fields
- $this->currentDomain = ( $this->mDBname != '' )
- ? new DatabaseDomain( $this->mDBname, null, $this->mTablePrefix )
- : DatabaseDomain::newUnspecified();
+ if ( $this->mDBname != '' ) {
+ // Domains with server scope but a table prefix are not used by IDatabase classes
+ $this->currentDomain = new DatabaseDomain( $this->mDBname, null, $this->mTablePrefix );
+ }
}
/**
$p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
$p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : '';
$p['schema'] = isset( $p['schema'] ) ? $p['schema'] : '';
- $p['foreign'] = isset( $p['foreign'] ) ? $p['foreign'] : false;
-
- $conn = new $class( $p );
- if ( isset( $p['connLogger'] ) ) {
- $conn->connLogger = $p['connLogger'];
+ $p['cliMode'] = isset( $p['cliMode'] ) ? $p['cliMode'] : ( PHP_SAPI === 'cli' );
+ $p['agent'] = isset( $p['agent'] ) ? $p['agent'] : '';
+ if ( !isset( $p['connLogger'] ) ) {
+ $p['connLogger'] = new \Psr\Log\NullLogger();
}
- if ( isset( $p['queryLogger'] ) ) {
- $conn->queryLogger = $p['queryLogger'];
+ if ( !isset( $p['queryLogger'] ) ) {
+ $p['queryLogger'] = new \Psr\Log\NullLogger();
}
- if ( isset( $p['errorLogger'] ) ) {
- $conn->errorLogger = $p['errorLogger'];
- } else {
- $conn->errorLogger = function ( Exception $e ) {
+ $p['profiler'] = isset( $p['profiler'] ) ? $p['profiler'] : null;
+ if ( !isset( $p['trxProfiler'] ) ) {
+ $p['trxProfiler'] = new TransactionProfiler();
+ }
+ if ( !isset( $p['errorLogger'] ) ) {
+ $p['errorLogger'] = function ( Exception $e ) {
trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_WARNING );
};
}
+
+ $conn = new $class( $p );
} else {
$conn = null;
}
return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET' ], true );
}
+ /**
+ * @param string $sql A SQL query
+ * @return bool Whether $sql is SQL for creating/dropping a new TEMPORARY table
+ */
+ protected function registerTempTableOperation( $sql ) {
+ if ( preg_match(
+ '/^(CREATE|DROP)\s+TEMPORARY\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?[`"\']?(\w+)[`"\']?/i',
+ $sql,
+ $matches
+ ) ) {
+ list( , $verb, $table ) = $matches;
+ if ( $verb === 'CREATE' ) {
+ $this->mSessionTempTables[$table] = 1;
+ } else {
+ unset( $this->mSessionTempTables[$table] );
+ }
+
+ return true;
+ } elseif ( preg_match(
+ '/^(?:INSERT\s+(?:\w+\s+)?INTO|UPDATE|DELETE\s+FROM)\s+[`"\']?(\w+)[`"\']?/i',
+ $sql,
+ $matches
+ ) ) {
+ return isset( $this->mSessionTempTables[$matches[1]] );
+ }
+
+ return false;
+ }
+
public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
$priorWritesPending = $this->writesOrCallbacksPending();
$this->mLastQuery = $sql;
- $isWrite = $this->isWriteQuery( $sql );
+ $isWrite = $this->isWriteQuery( $sql ) && !$this->registerTempTableOperation( $sql );
if ( $isWrite ) {
$reason = $this->getReadOnlyReason();
if ( $reason !== false ) {
$lastError = $this->lastError();
$lastErrno = $this->lastErrno();
# Update state tracking to reflect transaction loss due to disconnection
- $this->handleTransactionLoss();
+ $this->handleSessionLoss();
if ( $this->reconnect() ) {
$msg = __METHOD__ . ": lost connection to {$this->getServer()}; reconnected";
$this->connLogger->warning( $msg );
$tempIgnore = false; // not recoverable
}
# Update state tracking to reflect transaction loss
- $this->handleTransactionLoss();
+ $this->handleSessionLoss();
}
$this->reportQueryError(
return true;
}
- private function handleTransactionLoss() {
+ private function handleSessionLoss() {
$this->mTrxLevel = 0;
$this->mTrxIdleCallbacks = []; // bug 65263
$this->mTrxPreCommitCallbacks = []; // bug 65263
+ $this->mSessionTempTables = [];
+ $this->mNamedLocksHeld = [];
try {
// Handle callbacks in mTrxEndCallbacks
$this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
}
if ( $s === null ) {
return 'NULL';
+ } elseif ( is_bool( $s ) ) {
+ return (int)$s;
} else {
# This will also quote numeric values. This should be harmless,
# and protects against weird problems that occur when they really
return (string)$this->mConn;
}
+ /**
+ * Make sure that copies do not share the same client binding handle
+ * @throws DBConnectionError
+ */
+ public function __clone() {
+ $this->connLogger->warning(
+ "Cloning " . get_class( $this ) . " is not recomended; forking connection:\n" .
+ ( new RuntimeException() )->getTraceAsString()
+ );
+
+ if ( $this->isOpen() ) {
+ // Open a new connection resource without messing with the old one
+ $this->mOpened = false;
+ $this->mConn = false;
+ $this->mTrxLevel = 0; // no trx anymore
+ $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
+ $this->lastPing = microtime( true );
+ }
+ }
+
/**
* Called by serialize. Throw an exception when DB connection is serialized.
* This causes problems on some database engines because the connection is
}
/**
- * Run a few simple sanity checks
+ * Run a few simple sanity checks and close dangling connections
*/
public function __destruct() {
if ( $this->mTrxLevel && $this->mTrxDoneWrites ) {
$fnames = implode( ', ', $danglingWriters );
trigger_error( "DB transaction writes or callbacks still pending ($fnames)." );
}
+
+ if ( $this->mConn ) {
+ // Avoid connection leaks for sanity
+ $this->closeConnection();
+ $this->mConn = false;
+ $this->mOpened = false;
+ }
}
}