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 * The SQL entity query object this condition belongs to.
18 * @var \Drupal\Core\Entity\Query\Sql\Query
25 public function compile($conditionContainer) {
27 // If this is not the top level condition group then the sql query is
28 // added to the $conditionContainer object by this function itself. The
29 // SQL query object is only necessary to pass to Query::addField() so it
30 // can join tables as necessary. On the other hand, conditions need to be
31 // added to the $conditionContainer object to keep grouping.
32 $sql_query = $conditionContainer instanceof SelectInterface ? $conditionContainer : $conditionContainer->sqlQuery;
33 $tables = $this->query->getTables($sql_query);
34 foreach ($this->conditions as $condition) {
35 if ($condition['field'] instanceof ConditionInterface) {
36 $sql_condition = new SqlCondition($condition['field']->getConjunction());
37 // Add the SQL query to the object before calling this method again.
38 $sql_condition->sqlQuery = $sql_query;
39 $condition['field']->compile($sql_condition);
40 $conditionContainer->condition($sql_condition);
43 $type = strtoupper($this->conjunction) == 'OR' || $condition['operator'] == 'IS NULL' ? 'LEFT' : 'INNER';
44 $field = $tables->addField($condition['field'], $type, $condition['langcode']);
45 $condition['real_field'] = $field;
46 static::translateCondition($condition, $sql_query, $tables->isFieldCaseSensitive($condition['field']));
48 // Add the translated conditions back to the condition container.
49 if (isset($condition['where']) && isset($condition['where_args'])) {
50 $conditionContainer->where($condition['where'], $condition['where_args']);
53 $conditionContainer->condition($field, $condition['value'], $condition['operator']);
62 public function exists($field, $langcode = NULL) {
63 return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
69 public function notExists($field, $langcode = NULL) {
70 return $this->condition($field, NULL, 'IS NULL', $langcode);
74 * Translates the string operators to SQL equivalents.
76 * @param array $condition
77 * The condition array.
78 * @param \Drupal\Core\Database\Query\SelectInterface $sql_query
79 * Select query instance.
80 * @param bool|null $case_sensitive
81 * If the condition should be case sensitive or not, NULL if the field does
84 * @see \Drupal\Core\Database\Query\ConditionInterface::condition()
86 public static function translateCondition(&$condition, SelectInterface $sql_query, $case_sensitive) {
87 // // There is nothing we can do for IN ().
88 if (is_array($condition['value'])) {
92 // Ensure that the default operator is set to simplify the cases below.
93 if (empty($condition['operator'])) {
94 $condition['operator'] = '=';
96 switch ($condition['operator']) {
98 // If a field explicitly requests that queries should not be case
99 // sensitive, use the LIKE operator, otherwise keep =.
100 if ($case_sensitive === FALSE) {
101 $condition['value'] = $sql_query->escapeLike($condition['value']);
102 $condition['operator'] = 'LIKE';
106 // If a field explicitly requests that queries should not be case
107 // sensitive, use the NOT LIKE operator, otherwise keep <>.
108 if ($case_sensitive === FALSE) {
109 $condition['value'] = $sql_query->escapeLike($condition['value']);
110 $condition['operator'] = 'NOT LIKE';
114 if ($case_sensitive) {
115 $condition['operator'] = 'LIKE BINARY';
118 $condition['operator'] = 'LIKE';
120 $condition['value'] = $sql_query->escapeLike($condition['value']) . '%';
124 if ($case_sensitive) {
125 $condition['operator'] = 'LIKE BINARY';
128 $condition['operator'] = 'LIKE';
130 $condition['value'] = '%' . $sql_query->escapeLike($condition['value']) . '%';
134 if ($case_sensitive) {
135 $condition['operator'] = 'LIKE BINARY';
138 $condition['operator'] = 'LIKE';
140 $condition['value'] = '%' . $sql_query->escapeLike($condition['value']);