With mysqli, avoid setting the charset twice

* This uses MYSQLI_SET_CHARSET_NAME to set the charset to utf8/binary
* This replaces a live WMF hack to skip the extra round trip

Change-Id: I1718e013fcdc95163d111d460f0dd6d2190a99b1
This commit is contained in:
Aaron Schulz 2013-12-27 14:40:15 -08:00
parent 49cb843bb1
commit fbe350750b
2 changed files with 35 additions and 8 deletions

View file

@ -49,7 +49,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
* @throws DBConnectionError
*/
function open( $server, $user, $password, $dbName ) {
global $wgAllDBsAreLocalhost, $wgDBmysql5, $wgSQLMode;
global $wgAllDBsAreLocalhost, $wgSQLMode;
wfProfileIn( __METHOD__ );
# Debugging hack -- fake cluster
@ -113,13 +113,11 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
}
}
// Tell the server we're communicating with it in UTF-8.
// This may engage various charset conversions.
if ( $wgDBmysql5 ) {
$this->mysqlSetCharset( 'utf8' );
} else {
$this->mysqlSetCharset( 'binary' );
// Tell the server what we're communicating with
if ( !$this->connectInitCharset() ) {
return $this->reportConnectionError( "Error setting character set" );
}
// Set SQL mode, default is turning them all off, can be overridden or skipped with null
if ( is_string( $wgSQLMode ) ) {
$mode = $this->addQuotes( $wgSQLMode );
@ -138,6 +136,22 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
return true;
}
/**
* Set the character set information right after connection
* @return bool
*/
protected function connectInitCharset() {
global $wgDBmysql5;
if ( $wgDBmysql5 ) {
// Tell the server we're communicating with it in UTF-8.
// This may engage various charset conversions.
return $this->mysqlSetCharset( 'utf8' );
} else {
return $this->mysqlSetCharset( 'binary' );
}
}
/**
* Open a connection to a MySQL server
*

View file

@ -44,6 +44,7 @@ class DatabaseMysqli extends DatabaseMysqlBase {
}
protected function mysqlConnect( $realServer ) {
global $wgDBmysql5;
# Fail now
# Otherwise we get a suppressed fatal error, which is very hard to track down
if ( !function_exists( 'mysqli_init' ) ) {
@ -63,8 +64,15 @@ class DatabaseMysqli extends DatabaseMysqlBase {
}
$mysqli = mysqli_init();
$numAttempts = 2;
if ( $wgDBmysql5 ) {
// Tell the server we're communicating with it in UTF-8.
// This may engage various charset conversions.
$mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
} else {
$mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
}
$numAttempts = 2;
for ( $i = 0; $i < $numAttempts; $i++ ) {
if ( $i > 1 ) {
usleep( 1000 );
@ -79,6 +87,11 @@ class DatabaseMysqli extends DatabaseMysqlBase {
return false;
}
protected function connectInitCharset() {
// already done in mysqlConnect()
return true;
}
/**
* @return bool
*/