diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 3c12c7b956..9bd4b7084d 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -407,7 +407,7 @@ public function prefixTables($sql) { * The string containing a partial or entire SQL query with all identifiers * quoted. */ - protected function quoteIdentifiers($sql) { + public function quoteIdentifiers($sql) { return str_replace(['[', ']'], $this->identifierQuote(), $sql); } diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php index daf6bd62c0..de25f69358 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php @@ -132,7 +132,7 @@ protected function prepare() { // Add a self-join to the base revision table if we're querying only the // latest revisions. if ($this->latestRevision && $revision_field) { - $this->sqlQuery->leftJoin($base_table, 'base_table_2', "base_table.$id_field = base_table_2.$id_field AND base_table.$revision_field < base_table_2.$revision_field"); + $this->sqlQuery->leftJoin($base_table, 'base_table_2', "[base_table].[$id_field] = [base_table_2].[$id_field] AND [base_table].[$revision_field] < [base_table_2].[$revision_field]"); $this->sqlQuery->isNull("base_table_2.$id_field"); } @@ -346,6 +346,7 @@ public function __toString() { // Replace table name brackets. $sql = $clone->connection->prefixTables((string) $clone->sqlQuery); + $sql = $clone->connection->quoteIdentifiers($sql); return strtr($sql, $quoted); } diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php index 7257caa7ab..7e00dc1feb 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php @@ -359,7 +359,7 @@ protected function ensureEntityTable($index_prefix, $property, $type, $langcode, // each join gets a separate alias. $key = $index_prefix . ($base_table === 'base_table' ? $table : $base_table); if (!isset($this->entityTables[$key])) { - $this->entityTables[$key] = $this->addJoin($type, $table, "%alias.$id_field = $base_table.$id_field", $langcode); + $this->entityTables[$key] = $this->addJoin($type, $table, "[%alias].[$id_field] = [$base_table].[$id_field]", $langcode); } return $this->entityTables[$key]; } @@ -385,7 +385,7 @@ protected function ensureFieldTable($index_prefix, &$field, $type, $langcode, $b if ($field->getCardinality() != 1) { $this->sqlQuery->addMetaData('simple_query', FALSE); } - $this->fieldTables[$index_prefix . $field_name] = $this->addJoin($type, $table, "%alias.$field_id_field = $base_table.$entity_id_field", $langcode, $delta); + $this->fieldTables[$index_prefix . $field_name] = $this->addJoin($type, $table, "[%alias].[$field_id_field] = [$base_table].[$entity_id_field]", $langcode, $delta); } return $this->fieldTables[$index_prefix . $field_name]; } @@ -416,12 +416,12 @@ protected function addJoin($type, $table, $join_condition, $langcode, $delta = N // tables have an hard-coded 'langcode' column. $langcode_key = $entity_type->getDataTable() == $table ? $entity_type->getKey('langcode') : 'langcode'; $placeholder = ':langcode' . $this->sqlQuery->nextPlaceholder(); - $join_condition .= ' AND %alias.' . $langcode_key . ' = ' . $placeholder; + $join_condition .= ' AND [%alias].[' . $langcode_key . '] = ' . $placeholder; $arguments[$placeholder] = $langcode; } if (isset($delta)) { $placeholder = ':delta' . $this->sqlQuery->nextPlaceholder(); - $join_condition .= ' AND %alias.delta = ' . $placeholder; + $join_condition .= ' AND [%alias].[delta] = ' . $placeholder; $arguments[$placeholder] = $delta; } return $this->sqlQuery->addJoin($type, $table, NULL, $join_condition, $arguments); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php index 01f3ab8b2c..2606e77cbd 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php @@ -1221,9 +1221,9 @@ public function testToString() { SELECT "base_table"."revision_id" AS "revision_id", "base_table"."id" AS "id" FROM {entity_test_mulrev} "base_table" -INNER JOIN {entity_test_mulrev__{$figures}} "entity_test_mulrev__$figures" ON entity_test_mulrev__$figures.entity_id = base_table.id -INNER JOIN {entity_test_mulrev__{$figures}} "entity_test_mulrev__{$figures}_2" ON entity_test_mulrev__{$figures}_2.entity_id = base_table.id -LEFT JOIN {entity_test_mulrev__{$figures}} "entity_test_mulrev__{$figures}_3" ON entity_test_mulrev__{$figures}_3.entity_id = base_table.id +INNER JOIN {entity_test_mulrev__{$figures}} "entity_test_mulrev__$figures" ON "entity_test_mulrev__$figures"."entity_id" = "base_table"."id" +INNER JOIN {entity_test_mulrev__{$figures}} "entity_test_mulrev__{$figures}_2" ON "entity_test_mulrev__{$figures}_2"."entity_id" = "base_table"."id" +LEFT JOIN {entity_test_mulrev__{$figures}} "entity_test_mulrev__{$figures}_3" ON "entity_test_mulrev__{$figures}_3"."entity_id" = "base_table"."id" WHERE ("entity_test_mulrev__{$figures}"."{$figures}_color" IN ('blue')) AND ("entity_test_mulrev__{$figures}_2"."{$figures}_color" IN ('red')) AND ("entity_test_mulrev__{$figures}_3"."{$figures}_color" IS NULL) ORDER BY "base_table"."id" ASC EOF;