ConditionAggregate.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Drupal\Core\Entity\Query\Sql;
  3. use Drupal\Core\Database\Query\SelectInterface;
  4. use Drupal\Core\Entity\Query\ConditionAggregateBase;
  5. use Drupal\Core\Entity\Query\ConditionAggregateInterface;
  6. use Drupal\Core\Database\Query\Condition as SqlCondition;
  7. use Drupal\Core\Entity\Query\QueryBase;
  8. /**
  9. * Defines the aggregate condition for sql based storage.
  10. */
  11. class ConditionAggregate extends ConditionAggregateBase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function compile($conditionContainer) {
  16. // If this is not the top level condition group then the sql query is
  17. // added to the $conditionContainer object by this function itself. The
  18. // SQL query object is only necessary to pass to Query::addField() so it
  19. // can join tables as necessary. On the other hand, conditions need to be
  20. // added to the $conditionContainer object to keep grouping.
  21. $sql_query = ($conditionContainer instanceof SelectInterface) ? $conditionContainer : $conditionContainer->sqlQuery;
  22. $tables = new Tables($sql_query);
  23. foreach ($this->conditions as $condition) {
  24. if ($condition['field'] instanceof ConditionAggregateInterface) {
  25. $sql_condition = new SqlCondition($condition['field']->getConjunction());
  26. // Add the SQL query to the object before calling this method again.
  27. $sql_condition->sqlQuery = $sql_query;
  28. $condition['field']->compile($sql_condition);
  29. $sql_query->condition($sql_condition);
  30. }
  31. else {
  32. $type = ((strtoupper($this->conjunction) == 'OR') || ($condition['operator'] == 'IS NULL')) ? 'LEFT' : 'INNER';
  33. $field = $tables->addField($condition['field'], $type, $condition['langcode']);
  34. $condition_class = QueryBase::getClass($this->namespaces, 'Condition');
  35. $condition_class::translateCondition($condition, $sql_query, $tables->isFieldCaseSensitive($condition['field']));
  36. $function = $condition['function'];
  37. $placeholder = ':db_placeholder_' . $conditionContainer->nextPlaceholder();
  38. $conditionContainer->having("$function($field) {$condition['operator']} $placeholder", [$placeholder => $condition['value']]);
  39. }
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function exists($field, $function, $langcode = NULL) {
  46. return $this->condition($field, $function, NULL, 'IS NOT NULL', $langcode);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function notExists($field, $function, $langcode = NULL) {
  52. return $this->condition($field, $function, NULL, 'IS NULL', $langcode);
  53. }
  54. }