Same filename in this branch
- 10 core/modules/workspaces/src/EntityQuery/Query.php
- 10 core/lib/Drupal/Core/Database/Query/Query.php
- 10 core/lib/Drupal/Core/Config/Entity/Query/Query.php
- 10 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php
- 10 core/lib/Drupal/Core/Entity/Query/Null/Query.php
- 10 core/lib/Drupal/Core/Entity/Query/Sql/Query.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Query.php
- 9 core/lib/Drupal/Core/Entity/Query/Sql/Query.php
Namespace
Drupal\Core\Entity\Query\Sql
File
core/lib/Drupal/Core/Entity/Query/Sql/Query.php
View source
<?php
namespace Drupal\Core\Entity\Query\Sql;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Query\QueryBase;
use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Entity\Query\QueryInterface;
class Query extends QueryBase implements QueryInterface {
protected $sqlQuery;
protected $tables;
protected $sqlFields = [];
protected $sqlGroupBy = [];
protected $connection;
public function __construct(EntityTypeInterface $entity_type, $conjunction, Connection $connection, array $namespaces) {
parent::__construct($entity_type, $conjunction, $namespaces);
$this->connection = $connection;
}
public function execute() {
return $this
->alter()
->prepare()
->compile()
->addSort()
->finish()
->result();
}
protected function prepare() {
if ($this->allRevisions) {
if (!($base_table = $this->entityType
->getRevisionTable())) {
throw new QueryException("No revision table for " . $this->entityTypeId . ", invalid query.");
}
}
else {
if (!($base_table = $this->entityType
->getBaseTable())) {
throw new QueryException("No base table for " . $this->entityTypeId . ", invalid query.");
}
}
$simple_query = TRUE;
if ($this->entityType
->getDataTable()) {
$simple_query = FALSE;
}
$this->sqlQuery = $this->connection
->select($base_table, 'base_table', [
'conjunction' => $this->conjunction,
]);
$this->tables = NULL;
$this->sqlQuery
->addMetaData('entity_type', $this->entityTypeId);
$id_field = $this->entityType
->getKey('id');
if (!($revision_field = $this->entityType
->getKey('revision'))) {
$this->sqlFields["base_table.{$id_field}"] = [
'base_table',
$id_field,
];
$this->sqlFields["base_table.{$id_field}" . '_1'] = [
'base_table',
$id_field,
];
}
else {
$this->sqlFields["base_table.{$revision_field}"] = [
'base_table',
$revision_field,
];
$this->sqlFields["base_table.{$id_field}"] = [
'base_table',
$id_field,
];
}
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
->isNull("base_table_2.{$id_field}");
}
if (is_null($this->accessCheck)) {
throw new QueryException('Entity queries must explicitly set whether the query should be access checked or not. See Drupal\\Core\\Entity\\Query\\QueryInterface::accessCheck().');
}
if ($this->accessCheck) {
$this->sqlQuery
->addTag($this->entityTypeId . '_access');
}
$this->sqlQuery
->addTag('entity_query');
$this->sqlQuery
->addTag('entity_query_' . $this->entityTypeId);
if (isset($this->alterTags)) {
foreach ($this->alterTags as $tag => $value) {
$this->sqlQuery
->addTag($tag);
}
}
if (isset($this->alterMetaData)) {
foreach ($this->alterMetaData as $key => $value) {
$this->sqlQuery
->addMetaData($key, $value);
}
}
$this->sqlQuery
->addMetaData('all_revisions', $this->allRevisions);
$this->sqlQuery
->addMetaData('simple_query', $simple_query);
return $this;
}
protected function compile() {
$this->condition
->compile($this->sqlQuery);
return $this;
}
protected function addSort() {
if ($this->count) {
$this->sort = [];
}
$sort = [];
if ($this->sort) {
foreach ($this->sort as $key => $data) {
$sort[$key] = $this
->getSqlField($data['field'], $data['langcode']);
}
}
$simple_query = $this
->isSimpleQuery();
if (!$simple_query) {
foreach ($this->sqlFields as $field) {
$group_by = "{$field[0]}.{$field[1]}";
$this->sqlGroupBy[$group_by] = $group_by;
}
}
foreach ($sort as $key => $sql_alias) {
$direction = $this->sort[$key]['direction'];
if ($simple_query || isset($this->sqlGroupBy[$sql_alias])) {
$this->sqlQuery
->orderBy($sql_alias, $direction);
if (!isset($this->sqlFields[$sql_alias])) {
$this->sqlFields[$sql_alias] = explode('.', $sql_alias);
}
}
else {
$function = $direction == 'ASC' ? 'min' : 'max';
$expression = "{$function}({$sql_alias})";
$expression_alias = $this->sqlQuery
->addExpression($expression);
$this->sqlQuery
->orderBy($expression_alias, $direction);
}
}
return $this;
}
protected function finish() {
$this
->initializePager();
if ($this->range) {
$this->sqlQuery
->range($this->range['start'], $this->range['length']);
}
foreach ($this->sqlGroupBy as $field) {
$this->sqlQuery
->groupBy($field);
}
foreach ($this->sqlFields as $field) {
$this->sqlQuery
->addField($field[0], $field[1], $field[2] ?? NULL);
}
return $this;
}
protected function result() {
if ($this->count) {
return (int) $this->sqlQuery
->countQuery()
->execute()
->fetchField();
}
return $this->sqlQuery
->execute()
->fetchAllKeyed();
}
protected function getSqlField($field, $langcode) {
if (!isset($this->tables)) {
$this->tables = $this
->getTables($this->sqlQuery);
}
$base_property = "base_table.{$field}";
if (isset($this->sqlFields[$base_property])) {
return $base_property;
}
else {
return $this->tables
->addField($field, 'LEFT', $langcode);
}
}
protected function isSimpleQuery() {
return !$this->pager && !$this->range && !$this->count || $this->sqlQuery
->getMetaData('simple_query');
}
public function __clone() {
parent::__clone();
$this->sqlFields = [];
$this->sqlGroupBy = [];
}
public function getTables(SelectInterface $sql_query) {
$class = static::getClass($this->namespaces, 'Tables');
return new $class($sql_query);
}
public function __toString() {
$clone = clone $this;
$clone
->prepare()
->compile()
->addSort()
->finish();
$quoted = [];
foreach ($clone->sqlQuery
->getArguments() as $key => $value) {
$quoted[$key] = is_null($value) ? 'NULL' : $this->connection
->quote($value);
}
$sql = $clone->connection
->prefixTables((string) $clone->sqlQuery);
$sql = $clone->connection
->quoteIdentifiers($sql);
return strtr($sql, $quoted);
}
}
Classes
Name |
Description |
Query |
The SQL storage entity query class. |