00fe78c27f3d9c689d702d8e248f1d21897b00f2
[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    * The SQL entity query object this condition belongs to.
17    *
18    * @var \Drupal\Core\Entity\Query\Sql\Query
19    */
20   protected $query;
21
22   /**
23    * {@inheritdoc}
24    */
25   public function compile($conditionContainer) {
26
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);
41       }
42       else {
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']));
47
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']);
51         }
52         else {
53           $conditionContainer->condition($field, $condition['value'], $condition['operator']);
54         }
55       }
56     }
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function exists($field, $langcode = NULL) {
63     return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function notExists($field, $langcode = NULL) {
70     return $this->condition($field, NULL, 'IS NULL', $langcode);
71   }
72
73   /**
74    * Translates the string operators to SQL equivalents.
75    *
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
82    *   not define it.
83    *
84    * @see \Drupal\Core\Database\Query\ConditionInterface::condition()
85    */
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'])) {
89       return;
90     }
91
92     // Ensure that the default operator is set to simplify the cases below.
93     if (empty($condition['operator'])) {
94       $condition['operator'] = '=';
95     }
96     switch ($condition['operator']) {
97       case '=':
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';
103         }
104         break;
105       case '<>':
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';
111         }
112         break;
113       case 'STARTS_WITH':
114         if ($case_sensitive) {
115           $condition['operator'] = 'LIKE BINARY';
116         }
117         else {
118           $condition['operator'] = 'LIKE';
119         }
120         $condition['value'] = $sql_query->escapeLike($condition['value']) . '%';
121         break;
122
123       case 'CONTAINS':
124         if ($case_sensitive) {
125           $condition['operator'] = 'LIKE BINARY';
126         }
127         else {
128           $condition['operator'] = 'LIKE';
129         }
130         $condition['value'] = '%' . $sql_query->escapeLike($condition['value']) . '%';
131         break;
132
133       case 'ENDS_WITH':
134         if ($case_sensitive) {
135           $condition['operator'] = 'LIKE BINARY';
136         }
137         else {
138           $condition['operator'] = 'LIKE';
139         }
140         $condition['value'] = '%' . $sql_query->escapeLike($condition['value']);
141         break;
142     }
143   }
144
145 }