3 namespace Drupal\Core\Entity\Query\Sql;
5 use Drupal\Core\Entity\Query\QueryAggregateInterface;
8 * The SQL storage entity query aggregate class.
10 class QueryAggregate extends Query implements QueryAggregateInterface {
13 * Stores the sql expressions used to build the sql query.
16 * An array of expressions.
18 protected $sqlExpressions = [];
23 public function execute() {
39 public function prepare() {
41 // Throw away the id fields.
42 $this->sqlFields = [];
49 public function conditionAggregateGroupFactory($conjunction = 'AND') {
50 $class = static::getClass($this->namespaces, 'ConditionAggregate');
51 return new $class($conjunction, $this, $this->namespaces);
57 public function existsAggregate($field, $function, $langcode = NULL) {
58 return $this->conditionAggregate->exists($field, $function, $langcode);
64 public function notExistsAggregate($field, $function, $langcode = NULL) {
65 return $this->conditionAggregate->notExists($field, $function, $langcode);
69 * Adds the aggregations to the query.
71 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
72 * Returns the called object.
74 protected function addAggregate() {
75 if ($this->aggregate) {
76 foreach ($this->aggregate as $aggregate) {
77 $sql_field = $this->getSqlField($aggregate['field'], $aggregate['langcode']);
78 $this->sqlExpressions[$aggregate['alias']] = $aggregate['function'] . "($sql_field)";
85 * Builds the aggregation conditions part of the query.
87 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
88 * Returns the called object.
90 protected function compileAggregate() {
91 $this->conditionAggregate->compile($this->sqlQuery);
96 * Adds the groupby values to the actual query.
98 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
99 * Returns the called object.
101 protected function addGroupBy() {
102 foreach ($this->groupBy as $group_by) {
103 $field = $group_by['field'];
104 $sql_field = $this->getSqlField($field, $group_by['langcode']);
105 $this->sqlGroupBy[$sql_field] = $sql_field;
106 list($table, $real_sql_field) = explode('.', $sql_field);
107 $this->sqlFields[$sql_field] = [$table, $real_sql_field, $this->createSqlAlias($field, $real_sql_field)];
114 * Builds the aggregation sort part of the query.
116 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
117 * Returns the called object.
119 protected function addSortAggregate() {
121 foreach ($this->sortAggregate as $alias => $sort) {
122 $this->sqlQuery->orderBy($alias, $sort['direction']);
129 * Overrides \Drupal\Core\Entity\Query\Sql\Query::finish().
131 * Adds the sql expressions to the query.
133 protected function finish() {
134 foreach ($this->sqlExpressions as $alias => $expression) {
135 $this->sqlQuery->addExpression($expression, $alias);
137 return parent::finish();
141 * Builds a sql alias as expected in the result.
143 * @param string $field
144 * The field as passed in by the caller.
145 * @param string $sql_field
146 * The sql field as returned by getSqlField.
148 * The SQL alias expected in the return value. The dots in $sql_field are
149 * replaced with underscores and if a default fallback to .value happened,
150 * the _value is stripped.
152 public function createSqlAlias($field, $sql_field) {
153 $alias = str_replace('.', '_', $sql_field);
154 // If the alias contains of field_*_value remove the _value at the end.
155 if (substr($alias, 0, 6) === 'field_' && substr($field, -6) !== '_value' && substr($alias, -6) === '_value') {
156 $alias = substr($alias, 0, -6);
162 * Overrides \Drupal\Core\Entity\Query\Sql\Query::result().
165 * Returns the aggregated result, or a number if it's a count query.
167 protected function result() {
169 return parent::result();
172 foreach ($this->sqlQuery->execute() as $row) {
173 $return[] = (array) $row;