Changeset 27072
- Timestamp:
- 02/02/2014 09:38:34 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/wp-db.php
r27056 r27072 511 511 512 512 /** 513 * A list of incompatible SQL modes. 514 * 515 * @since 3.9.0 516 * @access protected 517 * @var array 518 */ 519 protected $incompatible_modes = array( 'NO_ZERO_DATE', 'ONLY_FULL_GROUP_BY', 520 'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' ); 521 522 /** 513 523 * Connects to the database server and selects a database 514 524 * … … 647 657 } 648 658 } 659 } 660 661 /** 662 * Change the current SQL mode, and ensure its WordPress compatibility. 663 * 664 * If no modes are passed, it will ensure the current MySQL server 665 * modes are compatible. 666 * 667 * @since 3.9.0 668 * 669 * @param array $modes Optional. A list of SQL modes to set. 670 */ 671 function set_sql_mode( $modes = array() ) { 672 if ( empty( $modes ) ) { 673 $res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh ); 674 if ( empty( $res ) ) { 675 return; 676 } 677 678 $modes_str = mysql_result( $res, 0 ); 679 680 if ( empty( $modes_str ) ) { 681 return; 682 } 683 684 $modes = explode( ',', $modes_str ); 685 } 686 687 $modes = array_change_key_case( $modes, CASE_UPPER ); 688 689 /** 690 * Filter the list of incompatible SQL modes to exclude. 691 * 692 * @since 3.9.0 693 * 694 * @see wpdb::$incompatible_modes 695 * 696 * @param array $incompatible_modes An array of incompatible modes 697 */ 698 $incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes ); 699 700 foreach( $modes as $i => $mode ) { 701 if ( in_array( $mode, $incompatible_modes ) ) { 702 unset( $modes[ $i ] ); 703 } 704 } 705 706 $modes_str = implode( ',', $modes ); 707 708 mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh ); 649 709 } 650 710 … … 1177 1237 1178 1238 $this->set_charset( $this->dbh ); 1239 1240 $this->set_sql_mode(); 1179 1241 1180 1242 $this->ready = true; -
trunk/tests/phpunit/tests/db.php
r25374 r27072 127 127 $this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql ); 128 128 } 129 130 /** 131 * Test that SQL modes are set correctly 132 * @ticket 26847 133 */ 134 public function test_set_sql_mode() { 135 global $wpdb; 136 137 $current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); 138 139 $new_modes = array( 'IGNORE_SPACE', 'NO_AUTO_CREATE_USER' ); 140 $wpdb->set_sql_mode( $new_modes ); 141 $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); 142 $this->assertEquals( implode( ',', $new_modes ), $check_new_modes ); 143 144 $wpdb->set_sql_mode( explode( ',', $current_modes ) ); 145 } 146 147 /** 148 * Test that incompatible SQL modes are blocked 149 * @ticket 26847 150 */ 151 public function test_set_incompatible_sql_mode() { 152 global $wpdb; 153 154 $current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); 155 156 $new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' ); 157 $wpdb->set_sql_mode( $new_modes ); 158 $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); 159 $this->assertFalse( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) ); 160 161 $wpdb->set_sql_mode( explode( ',', $current_modes ) ); 162 } 163 164 /** 165 * Test that incompatible SQL modes can be changed 166 * @ticket 26847 167 */ 168 public function test_set_allowed_incompatible_sql_mode() { 169 global $wpdb; 170 171 $current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); 172 173 $new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' ); 174 175 add_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1, 1 ); 176 $wpdb->set_sql_mode( $new_modes ); 177 remove_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1 ); 178 179 $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); 180 $this->assertTrue( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) ); 181 182 $wpdb->set_sql_mode( explode( ',', $current_modes ) ); 183 } 184 185 public function filter_allowed_incompatible_sql_mode( $modes ) { 186 $pos = array_search( 'NO_ZERO_DATE', $modes ); 187 $this->assertGreaterThanOrEqual( 0, $pos ); 188 189 if ( FALSE === $pos ) { 190 return $modes; 191 } 192 193 unset( $modes[ $pos ] ); 194 return $modes; 195 } 129 196 }
Note: See TracChangeset
for help on using the changeset viewer.