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);
70 * Adds the aggregations to the query.
72 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
73 * Returns the called object.
75 protected function addAggregate() {
76 if ($this->aggregate) {
77 foreach ($this->aggregate as $aggregate) {
78 $sql_field = $this->getSqlField($aggregate['field'], $aggregate['langcode']);
79 $this->sqlExpressions[$aggregate['alias']] = $aggregate['function'] . "($sql_field)";
86 * Builds the aggregation conditions part of the query.
88 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
89 * Returns the called object.
91 protected function compileAggregate() {
92 $this->conditionAggregate->compile($this->sqlQuery);
97 * Adds the groupby values to the actual query.
99 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
100 * Returns the called object.
102 protected function addGroupBy() {
103 foreach ($this->groupBy as $group_by) {
104 $field = $group_by['field'];
105 $sql_field = $this->getSqlField($field, $group_by['langcode']);
106 $this->sqlGroupBy[$sql_field] = $sql_field;
107 list($table, $real_sql_field) = explode('.', $sql_field);
108 $this->sqlFields[$sql_field] = [$table, $real_sql_field, $this->createSqlAlias($field, $real_sql_field)];
115 * Builds the aggregation sort part of the query.
117 * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
118 * Returns the called object.
120 protected function addSortAggregate() {
122 foreach ($this->sortAggregate as $alias => $sort) {
123 $this->sqlQuery->orderBy($alias, $sort['direction']);
131 * Overrides \Drupal\Core\Entity\Query\Sql\Query::finish().
133 * Adds the sql expressions to the query.
135 protected function finish() {
136 foreach ($this->sqlExpressions as $alias => $expression) {
137 $this->sqlQuery->addExpression($expression, $alias);
139 return parent::finish();
143 * Builds a sql alias as expected in the result.
145 * @param string $field
146 * The field as passed in by the caller.
147 * @param string $sql_field
148 * The sql field as returned by getSqlField.
150 * The SQL alias expected in the return value. The dots in $sql_field are
151 * replaced with underscores and if a default fallback to .value happened,
152 * the _value is stripped.
154 public function createSqlAlias($field, $sql_field) {
155 $alias = str_replace('.', '_', $sql_field);
156 // If the alias contains of field_*_value remove the _value at the end.
157 if (substr($alias, 0, 6) === 'field_' && substr($field, -6) !== '_value' && substr($alias, -6) === '_value') {
158 $alias = substr($alias, 0, -6);
164 * Overrides \Drupal\Core\Entity\Query\Sql\Query::result().
167 * Returns the aggregated result, or a number if it's a count query.
169 protected function result() {
171 return parent::result();
174 foreach ($this->sqlQuery->execute() as $row) {
175 $return[] = (array)$row;