Make WordPress Core

Changeset 32413


Ignore:
Timestamp:
05/06/2015 09:03:13 PM (10 years ago)
Author:
johnbillion
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.0 branch.

Props pento, willstedt for the initial patch.

See #32090.

Location:
branches/4.0
Files:
2 edited

Legend:

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

    r32403 r32413  
    22462246
    22472247        $charsets = $columns = array();
    2248         $results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
     2248
     2249        $table_parts = explode( '.', $table );
     2250        $table = '`' . implode( '`.`', $table_parts ) . '`';
     2251        $results = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
    22492252        if ( ! $results ) {
    22502253            return new WP_Error( 'wpdb_get_table_charset_failure' );
     
    28192822                . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
    28202823                . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
    2821                 . ')\s+`?([\w-]+)`?/is', $query, $maybe ) ) {
    2822             return $maybe[1];
     2824                . ')\s+((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)/is', $query, $maybe ) ) {
     2825            return str_replace( '`', '', $maybe[1] );
    28232826        }
    28242827
     
    28272830                . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    28282831                . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    2829                 . ')\W([\w-]+)\W/is', $query, $maybe ) ) {
    2830             return $maybe[1];
     2832                . ')\W((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\W/is', $query, $maybe ) ) {
     2833            return str_replace( '`', '', $maybe[1] );
    28312834        }
    28322835
     
    28462849                . '|(?:GRANT|REVOKE).*ON\s+TABLE'
    28472850                . '|SHOW\s+(?:.*FROM|.*TABLE)'
    2848                 . ')\s+\(*\s*`?([\w-]+)`?\s*\)*/is', $query, $maybe ) ) {
    2849             return $maybe[1];
     2851                . ')\s+\(*\s*((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is', $query, $maybe ) ) {
     2852            return str_replace( '`', '', $maybe[1] );
    28502853        }
    28512854
  • branches/4.0/tests/phpunit/tests/db.php

    r32388 r32413  
    492492    function data_get_table_from_query() {
    493493        $table = 'a_test_table_name';
     494        $db_table = '`a_test_db`.`another_test_table`';
    494495
    495496        $queries = array(
     
    594595        );
    595596
    596         foreach ( $queries as &$query ) {
    597             $query = array( $query, $table );
     597        $querycount = count( $queries );
     598        for ( $ii = 0; $ii < $querycount; $ii++ ) {
     599            $db_query = str_replace( $table, $db_table, $queries[ $ii ] );
     600            $expected_db_table = str_replace( '`', '', $db_table );
     601
     602            $queries[ $ii ] = array( $queries[ $ii ], $table );
     603            $queries[] = array( $db_query, $expected_db_table );
    598604        }
    599605        return $queries;
Note: See TracChangeset for help on using the changeset viewer.