Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Query / Sql / Condition.php
1 <?php
2
3 namespace Drupal\Core\Entity\Query\Sql;
4
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;
9
10 /**
11  * Implements entity query conditions for SQL databases.
12  */
13 class Condition extends ConditionBase {
14
15   /**
16    * Whether this condition is nested inside an OR condition.
17    *
18    * @var bool
19    */
20   protected $nestedInsideOrCondition = FALSE;
21
22   /**
23    * The SQL entity query object this condition belongs to.
24    *
25    * @var \Drupal\Core\Entity\Query\Sql\Query
26    */
27   protected $query;
28
29   /**
30    * {@inheritdoc}
31    */
32   public function compile($conditionContainer) {
33
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);
49       }
50       else {
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']));
55
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']);
59         }
60         else {
61           $conditionContainer->condition($field, $condition['value'], $condition['operator']);
62         }
63       }
64     }
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function exists($field, $langcode = NULL) {
71     return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function notExists($field, $langcode = NULL) {
78     return $this->condition($field, NULL, 'IS NULL', $langcode);
79   }
80
81   /**
82    * Translates the string operators to SQL equivalents.
83    *
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
90    *   not define it.
91    *
92    * @see \Drupal\Core\Database\Query\ConditionInterface::condition()
93    */
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'])) {
97       return;
98     }
99
100     // Ensure that the default operator is set to simplify the cases below.
101     if (empty($condition['operator'])) {
102       $condition['operator'] = '=';
103     }
104     switch ($condition['operator']) {
105       case '=':
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';
111         }
112         break;
113       case '<>':
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';
119         }
120         break;
121       case 'STARTS_WITH':
122         if ($case_sensitive) {
123           $condition['operator'] = 'LIKE BINARY';
124         }
125         else {
126           $condition['operator'] = 'LIKE';
127         }
128         $condition['value'] = $sql_query->escapeLike($condition['value']) . '%';
129         break;
130
131       case 'CONTAINS':
132         if ($case_sensitive) {
133           $condition['operator'] = 'LIKE BINARY';
134         }
135         else {
136           $condition['operator'] = 'LIKE';
137         }
138         $condition['value'] = '%' . $sql_query->escapeLike($condition['value']) . '%';
139         break;
140
141       case 'ENDS_WITH':
142         if ($case_sensitive) {
143           $condition['operator'] = 'LIKE BINARY';
144         }
145         else {
146           $condition['operator'] = 'LIKE';
147         }
148         $condition['value'] = '%' . $sql_query->escapeLike($condition['value']);
149         break;
150     }
151   }
152
153 }