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.


git-svn-id: https://develop.svn.wordpress.org/trunk@27072 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2014-02-02 21:38:34 +00:00
parent bb49691c63
commit 0558df56ce
2 changed files with 129 additions and 0 deletions

View File

@ -509,6 +509,16 @@ class wpdb {
*/
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
*
@ -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.
*
@ -1177,6 +1237,8 @@ class wpdb {
$this->set_charset( $this->dbh );
$this->set_sql_mode();
$this->ready = true;
$this->select( $this->dbname, $this->dbh );

View File

@ -126,4 +126,71 @@ class Tests_DB extends WP_UnitTestCase {
$sql = $wpdb->prepare( "UPDATE test_table SET string_column = '%%f is a float, %%d is an int %d, %%s is a string', field = %s", 3, '4' );
$this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql );
}
/**
* Test that SQL modes are set correctly
* @ticket 26847
*/
public function test_set_sql_mode() {
global $wpdb;
$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$new_modes = array( 'IGNORE_SPACE', 'NO_AUTO_CREATE_USER' );
$wpdb->set_sql_mode( $new_modes );
$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$this->assertEquals( implode( ',', $new_modes ), $check_new_modes );
$wpdb->set_sql_mode( explode( ',', $current_modes ) );
}
/**
* Test that incompatible SQL modes are blocked
* @ticket 26847
*/
public function test_set_incompatible_sql_mode() {
global $wpdb;
$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' );
$wpdb->set_sql_mode( $new_modes );
$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$this->assertFalse( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) );
$wpdb->set_sql_mode( explode( ',', $current_modes ) );
}
/**
* Test that incompatible SQL modes can be changed
* @ticket 26847
*/
public function test_set_allowed_incompatible_sql_mode() {
global $wpdb;
$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' );
add_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1, 1 );
$wpdb->set_sql_mode( $new_modes );
remove_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1 );
$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$this->assertTrue( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) );
$wpdb->set_sql_mode( explode( ',', $current_modes ) );
}
public function filter_allowed_incompatible_sql_mode( $modes ) {
$pos = array_search( 'NO_ZERO_DATE', $modes );
$this->assertGreaterThanOrEqual( 0, $pos );
if ( FALSE === $pos ) {
return $modes;
}
unset( $modes[ $pos ] );
return $modes;
}
}