Condition.php in Drupal 8
File
core/lib/Drupal/Core/Entity/Query/Sql/Condition.php
View source
<?php
namespace Drupal\Core\Entity\Query\Sql;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Database\Query\Condition as SqlCondition;
use Drupal\Core\Entity\Query\ConditionBase;
use Drupal\Core\Entity\Query\ConditionInterface;
class Condition extends ConditionBase {
protected $nestedInsideOrCondition = FALSE;
protected $query;
public function compile($conditionContainer) {
$sql_query = $conditionContainer instanceof SelectInterface ? $conditionContainer : $conditionContainer->sqlQuery;
$tables = $this->query
->getTables($sql_query);
foreach ($this->conditions as $condition) {
if ($condition['field'] instanceof ConditionInterface) {
$sql_condition = new SqlCondition($condition['field']
->getConjunction());
$sql_condition->sqlQuery = $sql_query;
$condition['field']->nestedInsideOrCondition = $this->nestedInsideOrCondition || strtoupper($this->conjunction) === 'OR';
$condition['field']
->compile($sql_condition);
$conditionContainer
->condition($sql_condition);
}
else {
$type = $this->nestedInsideOrCondition || strtoupper($this->conjunction) === 'OR' || $condition['operator'] === 'IS NULL' ? 'LEFT' : 'INNER';
$field = $tables
->addField($condition['field'], $type, $condition['langcode']);
$condition['real_field'] = $field;
static::translateCondition($condition, $sql_query, $tables
->isFieldCaseSensitive($condition['field']));
if (isset($condition['where']) && isset($condition['where_args'])) {
$conditionContainer
->where($condition['where'], $condition['where_args']);
}
else {
$conditionContainer
->condition($field, $condition['value'], $condition['operator']);
}
}
}
}
public function exists($field, $langcode = NULL) {
return $this
->condition($field, NULL, 'IS NOT NULL', $langcode);
}
public function notExists($field, $langcode = NULL) {
return $this
->condition($field, NULL, 'IS NULL', $langcode);
}
public static function translateCondition(&$condition, SelectInterface $sql_query, $case_sensitive) {
if (is_array($condition['value'])) {
return;
}
if (empty($condition['operator'])) {
$condition['operator'] = '=';
}
switch ($condition['operator']) {
case '=':
if ($case_sensitive === FALSE) {
$condition['value'] = $sql_query
->escapeLike($condition['value']);
$condition['operator'] = 'LIKE';
}
break;
case '<>':
if ($case_sensitive === FALSE) {
$condition['value'] = $sql_query
->escapeLike($condition['value']);
$condition['operator'] = 'NOT LIKE';
}
break;
case 'STARTS_WITH':
if ($case_sensitive) {
$condition['operator'] = 'LIKE BINARY';
}
else {
$condition['operator'] = 'LIKE';
}
$condition['value'] = $sql_query
->escapeLike($condition['value']) . '%';
break;
case 'CONTAINS':
if ($case_sensitive) {
$condition['operator'] = 'LIKE BINARY';
}
else {
$condition['operator'] = 'LIKE';
}
$condition['value'] = '%' . $sql_query
->escapeLike($condition['value']) . '%';
break;
case 'ENDS_WITH':
if ($case_sensitive) {
$condition['operator'] = 'LIKE BINARY';
}
else {
$condition['operator'] = 'LIKE';
}
$condition['value'] = '%' . $sql_query
->escapeLike($condition['value']);
break;
}
}
}
Classes
Name |
Description |
Condition |
Implements entity query conditions for SQL databases. |