QueryAggregate.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Drupal\Core\Entity\Query\Sql;
  3. use Drupal\Core\Entity\Query\QueryAggregateInterface;
  4. /**
  5. * The SQL storage entity query aggregate class.
  6. */
  7. class QueryAggregate extends Query implements QueryAggregateInterface {
  8. /**
  9. * Stores the sql expressions used to build the sql query.
  10. *
  11. * @var array
  12. * An array of expressions.
  13. */
  14. protected $sqlExpressions = [];
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function execute() {
  19. return $this
  20. ->prepare()
  21. ->addAggregate()
  22. ->compile()
  23. ->compileAggregate()
  24. ->addGroupBy()
  25. ->addSort()
  26. ->addSortAggregate()
  27. ->finish()
  28. ->result();
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function prepare() {
  34. parent::prepare();
  35. // Throw away the id fields.
  36. $this->sqlFields = [];
  37. return $this;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function conditionAggregateGroupFactory($conjunction = 'AND') {
  43. $class = static::getClass($this->namespaces, 'ConditionAggregate');
  44. return new $class($conjunction, $this, $this->namespaces);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function existsAggregate($field, $function, $langcode = NULL) {
  50. return $this->conditionAggregate->exists($field, $function, $langcode);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function notExistsAggregate($field, $function, $langcode = NULL) {
  56. return $this->conditionAggregate->notExists($field, $function, $langcode);
  57. }
  58. /**
  59. * Adds the aggregations to the query.
  60. *
  61. * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
  62. * Returns the called object.
  63. */
  64. protected function addAggregate() {
  65. if ($this->aggregate) {
  66. foreach ($this->aggregate as $aggregate) {
  67. $sql_field = $this->getSqlField($aggregate['field'], $aggregate['langcode']);
  68. $this->sqlExpressions[$aggregate['alias']] = $aggregate['function'] . "($sql_field)";
  69. }
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Builds the aggregation conditions part of the query.
  75. *
  76. * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
  77. * Returns the called object.
  78. */
  79. protected function compileAggregate() {
  80. $this->conditionAggregate->compile($this->sqlQuery);
  81. return $this;
  82. }
  83. /**
  84. * Adds the groupby values to the actual query.
  85. *
  86. * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
  87. * Returns the called object.
  88. */
  89. protected function addGroupBy() {
  90. foreach ($this->groupBy as $group_by) {
  91. $field = $group_by['field'];
  92. $sql_field = $this->getSqlField($field, $group_by['langcode']);
  93. $this->sqlGroupBy[$sql_field] = $sql_field;
  94. list($table, $real_sql_field) = explode('.', $sql_field);
  95. $this->sqlFields[$sql_field] = [$table, $real_sql_field, $this->createSqlAlias($field, $real_sql_field)];
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Builds the aggregation sort part of the query.
  101. *
  102. * @return \Drupal\Core\Entity\Query\Sql\QueryAggregate
  103. * Returns the called object.
  104. */
  105. protected function addSortAggregate() {
  106. if (!$this->count) {
  107. foreach ($this->sortAggregate as $alias => $sort) {
  108. $this->sqlQuery->orderBy($alias, $sort['direction']);
  109. }
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Overrides \Drupal\Core\Entity\Query\Sql\Query::finish().
  115. *
  116. * Adds the sql expressions to the query.
  117. */
  118. protected function finish() {
  119. foreach ($this->sqlExpressions as $alias => $expression) {
  120. $this->sqlQuery->addExpression($expression, $alias);
  121. }
  122. return parent::finish();
  123. }
  124. /**
  125. * Builds a sql alias as expected in the result.
  126. *
  127. * @param string $field
  128. * The field as passed in by the caller.
  129. * @param string $sql_field
  130. * The sql field as returned by getSqlField.
  131. * @return string
  132. * The SQL alias expected in the return value. The dots in $sql_field are
  133. * replaced with underscores and if a default fallback to .value happened,
  134. * the _value is stripped.
  135. */
  136. public function createSqlAlias($field, $sql_field) {
  137. $alias = str_replace('.', '_', $sql_field);
  138. // If the alias contains of field_*_value remove the _value at the end.
  139. if (substr($alias, 0, 6) === 'field_' && substr($field, -6) !== '_value' && substr($alias, -6) === '_value') {
  140. $alias = substr($alias, 0, -6);
  141. }
  142. return $alias;
  143. }
  144. /**
  145. * Overrides \Drupal\Core\Entity\Query\Sql\Query::result().
  146. *
  147. * @return array|int
  148. * Returns the aggregated result, or a number if it's a count query.
  149. */
  150. protected function result() {
  151. if ($this->count) {
  152. return parent::result();
  153. }
  154. $return = [];
  155. foreach ($this->sqlQuery->execute() as $row) {
  156. $return[] = (array) $row;
  157. }
  158. return $return;
  159. }
  160. }