You are here

public function Condition::compile in Drupal driver for SQL Server and SQL Azure 8.2

Same name and namespace in other branches
  1. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Condition.php \Drupal\Driver\Database\sqlsrv\Condition::compile()

Overridden to replace REGEXP expressions. Should this move to Condition::condition()?

Overrides Condition::compile

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Condition.php, line 21

Class

Condition

Namespace

Drupal\Driver\Database\sqlsrv

Code

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);
}