diff --git a/includes/database/mysql/database.inc b/includes/database/mysql/database.inc index 24b7d28fa..3d6a22499 100644 --- a/includes/database/mysql/database.inc +++ b/includes/database/mysql/database.inc @@ -428,9 +428,27 @@ class DatabaseConnection_mysql extends DatabaseConnection { } } + /** + * Overridden to quote table and database names with backticks. + */ public function query($query, array $args = array(), $options = array()) { - $query = preg_replace('/{([^}]+)}/', '`\1`', $query); - // This to make Drush work + // Match {table} as well as {db.table} and quote both DB and table with + // backticks. + $query = preg_replace_callback('/{((.*?)\.)?(.*?)}/', function($matches) { + $quoted = ''; + + // If there is an explicit database, quote it, too. + if ($matches[2] !== '') { + $quoted .= '`' . $matches[2] . '`.'; + } + + // Quote the table name. + $quoted .= '`' . $matches[3] . '`'; + + return $quoted; + }, $query); + + // Make Drush work. $query = strtr($query, array( ' system.' => ' `system`.', '`system` system' => '`system` `system`',