Make WordPress Core


Ignore:
Timestamp:
02/02/2014 09:38:34 PM (11 years ago)
Author:
nacin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r27056 r27072  
    511511
    512512    /**
     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    /**
    513523     * Connects to the database server and selects a database
    514524     *
     
    647657            }
    648658        }
     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 );
    649709    }
    650710
     
    11771237
    11781238        $this->set_charset( $this->dbh );
     1239
     1240        $this->set_sql_mode();
    11791241
    11801242        $this->ready = true;
Note: See TracChangeset for help on using the changeset viewer.