3 namespace Drupal\Core\Entity\Query\Sql;
5 use Drupal\Core\Database\Query\SelectInterface;
6 use Drupal\Core\Database\Query\Condition as SqlCondition;
7 use Drupal\Core\Entity\Query\ConditionBase;
8 use Drupal\Core\Entity\Query\ConditionInterface;
11 * Implements entity query conditions for SQL databases.
13 class Condition extends ConditionBase {
16 * Whether this condition is nested inside an OR condition.
20 protected $nestedInsideOrCondition = FALSE;
23 * The SQL entity query object this condition belongs to.
25 * @var \Drupal\Core\Entity\Query\Sql\Query
32 public function compile($conditionContainer) {
34 // If this is not the top level condition group then the sql query is
35 // added to the $conditionContainer object by this function itself. The
36 // SQL query object is only necessary to pass to Query::addField() so it
37 // can join tables as necessary. On the other hand, conditions need to be
38 // added to the $conditionContainer object to keep grouping.
39 $sql_query = $conditionContainer instanceof SelectInterface ? $conditionContainer : $conditionContainer->sqlQuery;
40 $tables = $this->query->getTables($sql_query);
41 foreach ($this->conditions as $condition) {
42 if ($condition['field'] instanceof ConditionInterface) {
43 $sql_condition = new SqlCondition($condition['field']->getConjunction());
44 // Add the SQL query to the object before calling this method again.
45 $sql_condition->sqlQuery = $sql_query;
46 $condition['field']->nestedInsideOrCondition = $this->nestedInsideOrCondition || strtoupper($this->conjunction) === 'OR';
47 $condition['field']->compile($sql_condition);
48 $conditionContainer->condition($sql_condition);
51 $type = $this->nestedInsideOrCondition || strtoupper($this->conjunction) === 'OR' || $condition['operator'] === 'IS NULL' ? 'LEFT' : 'INNER';
52 $field = $tables->addField($condition['field'], $type, $condition['langcode']);
53 $condition['real_field'] = $field;
54 static::translateCondition($condition, $sql_query, $tables->isFieldCaseSensitive($condition['field']));
56 // Add the translated conditions back to the condition container.
57 if (isset($condition['where']) && isset($condition['where_args'])) {
58 $conditionContainer->where($condition['where'], $condition['where_args']);
61 $conditionContainer->condition($field, $condition['value'], $condition['operator']);
70 public function exists($field, $langcode = NULL) {
71 return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
77 public function notExists($field, $langcode = NULL) {
78 return $this->condition($field, NULL, 'IS NULL', $langcode);
82 * Translates the string operators to SQL equivalents.
84 * @param array $condition
85 * The condition array.
86 * @param \Drupal\Core\Database\Query\SelectInterface $sql_query
87 * Select query instance.
88 * @param bool|null $case_sensitive
89 * If the condition should be case sensitive or not, NULL if the field does
92 * @see \Drupal\Core\Database\Query\ConditionInterface::condition()
94 public static function translateCondition(&$condition, SelectInterface $sql_query, $case_sensitive) {
95 // // There is nothing we can do for IN ().
96 if (is_array($condition['value'])) {
100 // Ensure that the default operator is set to simplify the cases below.
101 if (empty($condition['operator'])) {
102 $condition['operator'] = '=';
104 switch ($condition['operator']) {
106 // If a field explicitly requests that queries should not be case
107 // sensitive, use the LIKE operator, otherwise keep =.
108 if ($case_sensitive === FALSE) {
109 $condition['value'] = $sql_query->escapeLike($condition['value']);
110 $condition['operator'] = 'LIKE';
114 // If a field explicitly requests that queries should not be case
115 // sensitive, use the NOT LIKE operator, otherwise keep <>.
116 if ($case_sensitive === FALSE) {
117 $condition['value'] = $sql_query->escapeLike($condition['value']);
118 $condition['operator'] = 'NOT LIKE';
122 if ($case_sensitive) {
123 $condition['operator'] = 'LIKE BINARY';
126 $condition['operator'] = 'LIKE';
128 $condition['value'] = $sql_query->escapeLike($condition['value']) . '%';
132 if ($case_sensitive) {
133 $condition['operator'] = 'LIKE BINARY';
136 $condition['operator'] = 'LIKE';
138 $condition['value'] = '%' . $sql_query->escapeLike($condition['value']) . '%';
142 if ($case_sensitive) {
143 $condition['operator'] = 'LIKE BINARY';
146 $condition['operator'] = 'LIKE';
148 $condition['value'] = '%' . $sql_query->escapeLike($condition['value']);