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