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.

Built from https://develop.svn.wordpress.org/branches/4.0@32413


git-svn-id: http://core.svn.wordpress.org/branches/4.0@32383 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2015-05-06 21:04:34 +00:00
parent a820c920f9
commit 6dbcbfae60
1 changed files with 10 additions and 7 deletions

View File

@ -2245,7 +2245,10 @@ class wpdb {
} }
$charsets = $columns = array(); $charsets = $columns = array();
$results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
$table_parts = explode( '.', $table );
$table = '`' . implode( '`.`', $table_parts ) . '`';
$results = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
if ( ! $results ) { if ( ! $results ) {
return new WP_Error( 'wpdb_get_table_charset_failure' ); return new WP_Error( 'wpdb_get_table_charset_failure' );
} }
@ -2818,16 +2821,16 @@ class wpdb {
. '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?' . '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
. '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?' . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
. '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?' . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
. ')\s+`?([\w-]+)`?/is', $query, $maybe ) ) { . ')\s+((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)/is', $query, $maybe ) ) {
return $maybe[1]; return str_replace( '`', '', $maybe[1] );
} }
// SHOW TABLE STATUS and SHOW TABLES // SHOW TABLE STATUS and SHOW TABLES
if ( preg_match( '/^\s*(?:' if ( preg_match( '/^\s*(?:'
. 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)' . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
. '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)' . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
. ')\W([\w-]+)\W/is', $query, $maybe ) ) { . ')\W((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\W/is', $query, $maybe ) ) {
return $maybe[1]; return str_replace( '`', '', $maybe[1] );
} }
// Big pattern for the rest of the table-related queries. // Big pattern for the rest of the table-related queries.
@ -2845,8 +2848,8 @@ class wpdb {
. '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE' . '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
. '|(?:GRANT|REVOKE).*ON\s+TABLE' . '|(?:GRANT|REVOKE).*ON\s+TABLE'
. '|SHOW\s+(?:.*FROM|.*TABLE)' . '|SHOW\s+(?:.*FROM|.*TABLE)'
. ')\s+\(*\s*`?([\w-]+)`?\s*\)*/is', $query, $maybe ) ) { . ')\s+\(*\s*((?:[0-9a-zA-Z$_.`]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is', $query, $maybe ) ) {
return $maybe[1]; return str_replace( '`', '', $maybe[1] );
} }
return false; return false;