You are here

class Condition in Drupal driver for SQL Server and SQL Azure 3.1.x

Same name and namespace in other branches
  1. 4.2.x src/Driver/Database/sqlsrv/Condition.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Condition
  2. 4.0.x src/Driver/Database/sqlsrv/Condition.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Condition
  3. 4.1.x src/Driver/Database/sqlsrv/Condition.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Condition

Hierarchy

Expanded class hierarchy of Condition

3 files declare their use of Condition
OrderByTest.php in tests/src/Unit/OrderByTest.php
SqlsrvConditionTest.php in tests/src/Unit/SqlsrvConditionTest.php
SqlsrvTest.php in tests/src/Kernel/SqlsrvTest.php

File

src/Driver/Database/sqlsrv/Condition.php, line 13

Namespace

Drupal\sqlsrv\Driver\Database\sqlsrv
View source
class Condition extends QueryCondition {

  /**
   * {@inheritdoc}
   *
   * Overridden to replace REGEXP expressions.
   * Should this move to Condition::condition()?
   */
  public function compile(DatabaseConnection $connection, PlaceholderInterface $queryPlaceholder) {

    // Find any REGEXP conditions and turn them into function calls.
    foreach ($this->conditions as &$condition) {
      if (isset($condition['operator'])) {
        if ($condition['operator'] == 'REGEXP' || $condition['operator'] == 'NOT REGEXP') {
          $placeholder = ':db_condition_placeholder_' . $queryPlaceholder
            ->nextPlaceholder();
          $field_fragment = $connection
            ->escapeField($condition['field']);
          $comparison = $condition['operator'] == 'REGEXP' ? '1' : '0';
          $condition['field'] = "REGEXP({$placeholder}, {$field_fragment}) = {$comparison}";
          $condition['operator'] = NULL;
          $condition['value'] = [
            $placeholder => $condition['value'],
          ];
        }
        elseif ($condition['operator'] == 'LIKE' || $condition['operator'] == 'NOT LIKE') {
          $condition['value'] = strtr($condition['value'], [
            '[' => '[[]',
            '\\%' => '[%]',
            '\\_' => '[_]',
            '\\\\' => '\\',
          ]);
        }
      }
    }
    parent::compile($connection, $queryPlaceholder);
  }

  /**
   * {@inheritdoc}
   *
   * Overridden to replace REGEXP expressions.
   * Needs to be tested for complex nested expressions.
   */
  public function where($snippet, $args = []) {
    $operator = NULL;
    if (strpos($snippet, " NOT REGEXP ") !== FALSE) {
      $operator = ' NOT REGEXP ';
    }
    elseif (strpos($snippet, " REGEXP ") !== FALSE) {
      $operator = ' REGEXP ';
    }
    if ($operator !== NULL) {
      $fragments = explode($operator, $snippet);
      $field = $fragments[0];
      $value = $fragments[1];
      $comparison = $operator == ' REGEXP ' ? '1' : '0';
      $snippet = "REGEXP({$value}, {$field}) = {$comparison}";
      $operator = NULL;
    }
    $this->conditions[] = [
      'field' => $snippet,
      'value' => $args,
      'operator' => $operator,
    ];
    $this->changed = TRUE;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Condition::$arguments protected property Array of arguments.
Condition::$changed protected property Whether the conditions have been changed.
Condition::$conditionOperatorMap protected static property Provides a map of condition operators to condition operator options.
Condition::$conditions protected property Array of conditions.
Condition::$queryPlaceholderIdentifier protected property The identifier of the query placeholder this condition has been compiled against.
Condition::$stringVersion protected property Contains the string version of the Condition.
Condition::alwaysFalse public function Sets a condition that is always false. Overrides ConditionInterface::alwaysFalse
Condition::andConditionGroup public function Creates a new group of conditions ANDed together. Overrides ConditionInterface::andConditionGroup
Condition::arguments public function Gets a complete list of all values to insert into the prepared statement. Overrides ConditionInterface::arguments
Condition::compile public function Overridden to replace REGEXP expressions. Should this move to Condition::condition()? Overrides Condition::compile
Condition::compiled public function Check whether a condition has been previously compiled. Overrides ConditionInterface::compiled
Condition::condition public function Helper function: builds the most common conditional clauses. Overrides ConditionInterface::condition
Condition::conditionGroupFactory public function Creates an object holding a group of conditions. Overrides ConditionInterface::conditionGroupFactory
Condition::conditions public function Gets the, possibly nested, list of conditions in this conditional clause. Overrides ConditionInterface::conditions
Condition::count public function Implements Countable::count().
Condition::exists public function Sets a condition that the specified subquery returns values. Overrides ConditionInterface::exists
Condition::isNotNull public function Sets a condition that the specified field be NOT NULL. Overrides ConditionInterface::isNotNull
Condition::isNull public function Sets a condition that the specified field be NULL. Overrides ConditionInterface::isNull
Condition::mapConditionOperator protected function Gets any special processing requirements for the condition operator.
Condition::notExists public function Sets a condition that the specified subquery returns no values. Overrides ConditionInterface::notExists
Condition::orConditionGroup public function Creates a new group of conditions ORed together. Overrides ConditionInterface::orConditionGroup
Condition::where public function Overridden to replace REGEXP expressions. Needs to be tested for complex nested expressions. Overrides Condition::where
Condition::__clone public function PHP magic __clone() method.
Condition::__construct Deprecated public function Constructs a Condition object.
Condition::__toString public function Implements PHP magic __toString method to convert the conditions to string.