You are here

public function Connection::escapeLike in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::escapeLike()

Escapes characters that work as wildcard characters in a LIKE pattern.

The wildcard characters "%" and "_" as well as backslash are prefixed with a backslash. Use this to do a search for a verbatim string without any wildcard behavior.

For example, the following does a case-insensitive query for all rows whose name starts with $prefix:

$result = $injected_connection
  ->query('SELECT * FROM person WHERE name LIKE :pattern', array(
  ':pattern' => $injected_connection
    ->escapeLike($prefix) . '%',
));

Backslash is defined as escape character for LIKE patterns in Drupal\Core\Database\Query\Condition::mapConditionOperator().

Parameters

string $string: The string to escape.

Return value

string The escaped string.

Overrides Connection::escapeLike

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 420
Definition of Drupal\Driver\Database\sqlsrv\Connection

Class

Connection
Temporary tables: temporary table support is done by means of global temporary tables (#) to avoid the use of DIRECT QUERIES. You can enable and disable the use of direct queries with $this->driver_settings->defaultDirectQuery =…

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function escapeLike($string) {
  return preg_replace('/([\\[\\]%_])/', '[$1]', $string);
}