Ensure compatibility with MySQL 5.6 which has stricter SQL modes by default.

Disables NO_ZERO_DATE, ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, STRICT_ALL_TABLES, TRADITIONAL. Introduces wpdb::set_sql_mode() with an incompatible_sql_modes filter so a plugin can alter the set mode after the fact.

props pento.
fixes #26847.

Built from https://develop.svn.wordpress.org/trunk@27072


git-svn-id: http://core.svn.wordpress.org/trunk@26945 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2014-02-02 21:39:13 +00:00
parent 28cd7371c4
commit 9f250b2ef7

View File

@ -509,6 +509,16 @@ class wpdb {
*/ */
public $is_mysql = null; public $is_mysql = null;
/**
* A list of incompatible SQL modes.
*
* @since 3.9.0
* @access protected
* @var array
*/
protected $incompatible_modes = array( 'NO_ZERO_DATE', 'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' );
/** /**
* Connects to the database server and selects a database * Connects to the database server and selects a database
* *
@ -648,6 +658,56 @@ class wpdb {
} }
} }
/**
* Change the current SQL mode, and ensure its WordPress compatibility.
*
* If no modes are passed, it will ensure the current MySQL server
* modes are compatible.
*
* @since 3.9.0
*
* @param array $modes Optional. A list of SQL modes to set.
*/
function set_sql_mode( $modes = array() ) {
if ( empty( $modes ) ) {
$res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh );
if ( empty( $res ) ) {
return;
}
$modes_str = mysql_result( $res, 0 );
if ( empty( $modes_str ) ) {
return;
}
$modes = explode( ',', $modes_str );
}
$modes = array_change_key_case( $modes, CASE_UPPER );
/**
* Filter the list of incompatible SQL modes to exclude.
*
* @since 3.9.0
*
* @see wpdb::$incompatible_modes
*
* @param array $incompatible_modes An array of incompatible modes
*/
$incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
foreach( $modes as $i => $mode ) {
if ( in_array( $mode, $incompatible_modes ) ) {
unset( $modes[ $i ] );
}
}
$modes_str = implode( ',', $modes );
mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh );
}
/** /**
* Sets the table prefix for the WordPress tables. * Sets the table prefix for the WordPress tables.
* *
@ -1177,6 +1237,8 @@ class wpdb {
$this->set_charset( $this->dbh ); $this->set_charset( $this->dbh );
$this->set_sql_mode();
$this->ready = true; $this->ready = true;
$this->select( $this->dbname, $this->dbh ); $this->select( $this->dbname, $this->dbh );