Make WordPress Core

Changeset 32370


Ignore:
Timestamp:
05/06/2015 05:16:54 AM (11 years ago)
Author:
pento
Message:

WPDB: Allow queries to reference tables in the dbname.tablename format, and allow table names to contain any valid character, rather than just ASCII.

Merge of [32368] to the 4.2 branch.

Props pento, willstedt for the initial patch.

See #32090.

Location:
branches/4.2
Files:
2 edited

Legend:

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

    r32367 r32370  
    22672267
    22682268                $charsets = $columns = array();
    2269                 $results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
     2269
     2270                $table_parts = explode( '.', $table );
     2271                $table = '`' . implode( '`.`', $table_parts ) . '`';
     2272                $results = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
    22702273                if ( ! $results ) {
    22712274                        return new WP_Error( 'wpdb_get_table_charset_failure' );
    … …  
    28392842                                . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
    28402843                                . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
    2841                                 . ')\s+`?([\w-]+)`?/is', $query, $maybe ) ) {
    2842                         return $maybe[1];
     2844                                . ')\s+((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)/is', $query, $maybe ) ) {
     2845                        return str_replace( '`', '', $maybe[1] );
    28432846                }
    28442847
    … …  
    28472850                                . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    28482851                                . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    2849                                 . ')\W([\w-]+)\W/is', $query, $maybe ) ) {
    2850                         return $maybe[1];
     2852                                . ')\W((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\W/is', $query, $maybe ) ) {
     2853                        return str_replace( '`', '', $maybe[1] );
    28512854                }
    28522855
    … …  
    28662869                                . '|(?:GRANT|REVOKE).*ON\s+TABLE'
    28672870                                . '|SHOW\s+(?:.*FROM|.*TABLE)'
    2868                                 . ')\s+\(*\s*`?([\w-]+)`?\s*\)*/is', $query, $maybe ) ) {
    2869                         return $maybe[1];
     2871                                . ')\s+\(*\s*((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is', $query, $maybe ) ) {
     2872                        return str_replace( '`', '', $maybe[1] );
    28702873                }
    28712874
  • branches/4.2/tests/phpunit/tests/db.php

    r32367 r32370  
    527527        function data_get_table_from_query() {
    528528                $table = 'a_test_table_name';
     529                $db_table = '`a_test_db`.`another_test_table`';
    529530
    530531                $queries = array(
    … …  
    629630                );
    630631
    631                 foreach ( $queries as &$query ) {
    632                         $query = array( $query, $table );
     632                $querycount = count( $queries );
     633                for ( $ii = 0; $ii < $querycount; $ii++ ) {
     634                        $db_query = str_replace( $table, $db_table, $queries[ $ii ] );
     635                        $expected_db_table = str_replace( '`', '', $db_table );
     636
     637                        $queries[ $ii ] = array( $queries[ $ii ], $table );
     638                        $queries[] = array( $db_query, $expected_db_table );
    633639                }
    634640                return $queries;
Note: See TracChangeset for help on using the changeset viewer.