Problem/Motivation

Documentation on SelectInterface::leftJoin():

   * @param $table
   *   The table against which to join. May be a string or another SelectQuery
   *   object. If a query object is passed, it will be used as a subselect.
   *   Unless the table name starts with the database / schema name and a dot
   *   it will be prefixed.

Which means you can pass the fully qualified table name to this method, inlcuding the database name. If your database name is "drupal-8" (valid in MySQL) you should be able to pass in "drupal-8.node" if you want to join to the node table for example. The problem is that some escape*() method on Drupal\Core\Database\Connection strips the "-" character from that string. Your query will have a join to "drupal8.node", which of course refers to a completely different database and MySQL will throw an access denied error or an exception that the database does not exist.

Proposed resolution

Never use "-" in your MySQL database names, although it is allowed!

Or we could fix the MySQL driver to properly support "-".

Remaining tasks

?

User interface changes

none.

API changes

none.

Data model changes

none.

Comments

klausi created an issue. See original summary.

andypost’s picture

As docs said dash is allowed only if quoted, so maybe better to change strategy from removing chars to proper quoting?

http://dev.mysql.com/doc/refman/5.7/en/identifiers.html
https://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html
https://www.sqlite.org/lang_keywords.html

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joachim’s picture

Status: Active » Closed (duplicate)

Duplicate of #1426084: Provide backtick escaping for MySQL in DB abstraction layer, which is a more comprehensive fix?

alexpott’s picture

Title: "-" should be treated as valid character in MySQL database names. » Support "-" in database table names
Status: Closed (duplicate) » Postponed
Related issues: +#2986452: Database reserved keywords need to be quoted as per the ANSI standard

This is not quite addressed by #2986452: Database reserved keywords need to be quoted as per the ANSI standard which is a new issue that addresses a lot of #1426084: Provide backtick escaping for MySQL in DB abstraction layer for an non-db specific point of view. But 2986452 does add the necessary quoting of table names so is an essential first step. This issue will be postponed on that and then once that lands should be used to add test coverage for all supported databases to ensure that we can handle hyphens in table names in all of them.

sjerdo’s picture

Version: 8.4.x-dev » 8.7.x-dev
kriboogh’s picture

Status: Postponed » Closed (duplicate)