Make WordPress Core

Changeset 32416


Ignore:
Timestamp:
05/06/2015 09:13:49 PM (11 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 3.7 branch.

Props pento, willstedt for the initial patch.

See #32090.

Location:
branches/3.7
Files:
2 edited

Legend:

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

    r32407 r32416  
    18251825
    18261826                $charsets = $columns = array();
    1827                 $results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
     1827
     1828                $table_parts = explode( '.', $table );
     1829                $table = '`' . implode( '`.`', $table_parts ) . '`';
     1830                $results = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
    18281831                if ( ! $results ) {
    18291832                        return new WP_Error( 'wpdb_get_table_charset_failure' );
    … …  
    23982401                                . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
    23992402                                . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
    2400                                 . ')\s+`?([\w-]+)`?/is', $query, $maybe ) ) {
    2401                         return $maybe[1];
     2403                                . ')\s+((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)/is', $query, $maybe ) ) {
     2404                        return str_replace( '`', '', $maybe[1] );
    24022405                }
    24032406
    … …  
    24062409                                . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    24072410                                . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    2408                                 . ')\W([\w-]+)\W/is', $query, $maybe ) ) {
    2409                         return $maybe[1];
     2411                                . ')\W((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\W/is', $query, $maybe ) ) {
     2412                        return str_replace( '`', '', $maybe[1] );
    24102413                }
    24112414
    … …  
    24252428                                . '|(?:GRANT|REVOKE).*ON\s+TABLE'
    24262429                                . '|SHOW\s+(?:.*FROM|.*TABLE)'
    2427                                 . ')\s+\(*\s*`?([\w-]+)`?\s*\)*/is', $query, $maybe ) ) {
    2428                         return $maybe[1];
     2430                                . ')\s+\(*\s*((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is', $query, $maybe ) ) {
     2431                        return str_replace( '`', '', $maybe[1] );
    24292432                }
    24302433
  • branches/3.7/tests/phpunit/tests/db.php

    r32391 r32416  
    291291        function data_get_table_from_query() {
    292292                $table = 'a_test_table_name';
     293                $db_table = '`a_test_db`.`another_test_table`';
    293294
    294295                $queries = array(
    … …  
    393394                );
    394395
    395                 foreach ( $queries as &$query ) {
    396                         $query = array( $query, $table );
     396                $querycount = count( $queries );
     397                for ( $ii = 0; $ii < $querycount; $ii++ ) {
     398                        $db_query = str_replace( $table, $db_table, $queries[ $ii ] );
     399                        $expected_db_table = str_replace( '`', '', $db_table );
     400
     401                        $queries[ $ii ] = array( $queries[ $ii ], $table );
     402                        $queries[] = array( $db_query, $expected_db_table );
    397403                }
    398404                return $queries;
Note: See TracChangeset for help on using the changeset viewer.