summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/Entity/Query/Sql/Condition.php
blob: 19f6a3ddb81e63399430960625850e0a28b73bde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\Query\Sql\Condition.
 */

namespace Drupal\Core\Entity\Query\Sql;

use Drupal\Core\Entity\Query\ConditionBase;
use Drupal\Core\Entity\Query\ConditionInterface;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Database\Query\Condition as SqlCondition;

class Condition extends ConditionBase {

  /**
   * The SQL entity query object this condition belongs to.
   *
   * @var \Drupal\Core\Entity\Query\Sql\Query
   */
  protected $query;

  /**
   * Implements Drupal\Core\Entity\Query\ConditionInterface::compile().
   */
  public function compile($conditionContainer) {
    // If this is not the top level condition group then the sql query is
    // added to the $conditionContainer object by this function itself. The
    // SQL query object is only necessary to pass to Query::addField() so it
    // can join tables as necessary. On the other hand, conditions need to be
    // added to the $conditionContainer object to keep grouping.
    $sqlQuery = $conditionContainer instanceof SelectInterface ? $conditionContainer : $conditionContainer->sqlQuery;
    $tables = $this->query->getTables($sqlQuery);
    foreach ($this->conditions as $condition) {
      if ($condition['field'] instanceOf ConditionInterface) {
        $sqlCondition = new SqlCondition($condition['field']->getConjunction());
        // Add the SQL query to the object before calling this method again.
        $sqlCondition->sqlQuery = $sqlQuery;
        $condition['field']->compile($sqlCondition);
        $sqlQuery->condition($sqlCondition);
      }
      else {
        $type = strtoupper($this->conjunction) == 'OR' || $condition['operator'] == 'IS NULL' ? 'LEFT' : 'INNER';
        $this->translateCondition($condition);
        $field = $tables->addField($condition['field'], $type, $condition['langcode']);
        $conditionContainer->condition($field, $condition['value'], $condition['operator']);
      }
    }
  }

  /**
   * Implements Drupal\Core\Entity\Query\ConditionInterface::exists().
   */
  public function exists($field, $langcode = NULL) {
    return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
  }

  /**
   * Implements Drupal\Core\Entity\Query\ConditionInterface::notExists().
   */
  public function notExists($field, $langcode = NULL) {
    return $this->condition($field, NULL, 'IS NULL', $langcode);
  }

  /**
   * Translates the string operators to SQL equivalents.
   *
   * @param array $condition
   */
  protected function translateCondition(&$condition) {
    switch ($condition['operator']) {
      case 'STARTS_WITH':
        $condition['value'] .= '%';
        $condition['operator'] = 'LIKE';
        break;

      case 'CONTAINS':
        $condition['value'] = '%' . $condition['value'] . '%';
        $condition['operator'] = 'LIKE';
        break;

      case 'ENDS_WITH':
        $condition['value'] = '%' . $condition['value'];
        $condition['operator'] = 'LIKE';
        break;

    }
  }

}