Commit dc5b0a47 authored by alexpott's avatar alexpott
Browse files

Issue #1565972 by mradcliffe, bzrudi71, pjohans, c960657, daffie,...

Issue #1565972 by mradcliffe, bzrudi71, pjohans, c960657, daffie, tim.plunkett: Remove static caching of array in mapConditionOperator()
parent 4bbbcd67
......@@ -35,6 +35,19 @@ class Connection extends DatabaseConnection {
*/
const CONNECTION_FAILURE = '08006';
/**
* A map of condition operators to PostgreSQL operators.
*
* In PostgreSQL, 'LIKE' is case-sensitive. ILKE should be used for
* case-insensitive statements.
*/
protected static $postgresqlConditionOperatorMap = [
'LIKE' => ['operator' => 'ILIKE'],
'LIKE BINARY' => ['operator' => 'LIKE'],
'NOT LIKE' => ['operator' => 'NOT ILIKE'],
'REGEXP' => ['operator' => '~*'],
];
/**
* The list of PostgreSQL reserved key words.
*
......@@ -323,15 +336,7 @@ public function createDatabase($database) {
}
public function mapConditionOperator($operator) {
static $specials = array(
// In PostgreSQL, 'LIKE' is case-sensitive. For case-insensitive LIKE
// statements, we need to use ILIKE instead.
'LIKE' => array('operator' => 'ILIKE'),
'LIKE BINARY' => array('operator' => 'LIKE'),
'NOT LIKE' => array('operator' => 'NOT ILIKE'),
'REGEXP' => array('operator' => '~*'),
);
return isset($specials[$operator]) ? $specials[$operator] : NULL;
return isset(static::$postgresqlConditionOperatorMap[$operator]) ? static::$postgresqlConditionOperatorMap[$operator] : NULL;
}
/**
......
......@@ -23,6 +23,18 @@ class Connection extends DatabaseConnection {
*/
protected $willRollback;
/**
* A map of condition operators to SQLite operators.
*
* We don't want to override any of the defaults.
*/
protected static $sqliteConditionOperatorMap = [
'LIKE' => ['postfix' => " ESCAPE '\\'"],
'NOT LIKE' => ['postfix' => " ESCAPE '\\'"],
'LIKE BINARY' => ['postfix' => " ESCAPE '\\'", 'operator' => 'GLOB'],
'NOT LIKE BINARY' => ['postfix' => " ESCAPE '\\'", 'operator' => 'NOT GLOB'],
];
/**
* All databases attached to the current database. This is used to allow
* prefixes to be safely handled without locking the table
......@@ -382,14 +394,7 @@ public function createDatabase($database) {
}
public function mapConditionOperator($operator) {
// We don't want to override any of the defaults.
static $specials = array(
'LIKE' => array('postfix' => " ESCAPE '\\'"),
'NOT LIKE' => array('postfix' => " ESCAPE '\\'"),
'LIKE BINARY' => array('postfix' => " ESCAPE '\\'", 'operator' => 'GLOB'),
'NOT LIKE BINARY' => array('postfix' => " ESCAPE '\\'", 'operator' => 'NOT GLOB'),
);
return isset($specials[$operator]) ? $specials[$operator] : NULL;
return isset(static::$sqliteConditionOperatorMap[$operator]) ? static::$sqliteConditionOperatorMap[$operator] : NULL;
}
/**
......
......@@ -10,6 +10,31 @@
*/
class Condition implements ConditionInterface, \Countable {
/**
* Provides a map of condition operators to condition operator options.
*/
protected static $conditionOperatorMap = array(
'BETWEEN' => array('delimiter' => ' AND '),
'NOT BETWEEN' => array('delimiter' => ' AND '),
'IN' => array('delimiter' => ', ', 'prefix' => '(', 'postfix' => ')'),
'NOT IN' => array('delimiter' => ', ', 'prefix' => '(', 'postfix' => ')'),
'IS NULL' => array('use_value' => FALSE),
'IS NOT NULL' => array('use_value' => FALSE),
// Use backslash for escaping wildcard characters.
'LIKE' => array('postfix' => " ESCAPE '\\\\'"),
'NOT LIKE' => array('postfix' => " ESCAPE '\\\\'"),
// Exists expects an already bracketed subquery as right hand part. Do
// not define additional brackets.
'EXISTS' => array(),
'NOT EXISTS' => array(),
// These ones are here for performance reasons.
'=' => array(),
'<' => array(),
'>' => array(),
'>=' => array(),
'<=' => array(),
);
/**
* Array of conditions.
*
......@@ -345,36 +370,14 @@ function __clone() {
* array if there are no extra handling directives.
*/
protected function mapConditionOperator($operator) {
// $specials does not use drupal_static as its value never changes.
static $specials = array(
'BETWEEN' => array('delimiter' => ' AND '),
'NOT BETWEEN' => array('delimiter' => ' AND '),
'IN' => array('delimiter' => ', ', 'prefix' => '(', 'postfix' => ')'),
'NOT IN' => array('delimiter' => ', ', 'prefix' => '(', 'postfix' => ')'),
'IS NULL' => array('use_value' => FALSE),
'IS NOT NULL' => array('use_value' => FALSE),
// Use backslash for escaping wildcard characters.
'LIKE' => array('postfix' => " ESCAPE '\\\\'"),
'NOT LIKE' => array('postfix' => " ESCAPE '\\\\'"),
// Exists expects an already bracketed subquery as right hand part. Do
// not define additional brackets.
'EXISTS' => array(),
'NOT EXISTS' => array(),
// These ones are here for performance reasons.
'=' => array(),
'<' => array(),
'>' => array(),
'>=' => array(),
'<=' => array(),
);
if (isset($specials[$operator])) {
$return = $specials[$operator];
if (isset(static::$conditionOperatorMap[$operator])) {
$return = static::$conditionOperatorMap[$operator];
}
else {
// We need to upper case because PHP index matches are case sensitive but
// do not need the more expensive Unicode::strtoupper() because SQL statements are ASCII.
$operator = strtoupper($operator);
$return = isset($specials[$operator]) ? $specials[$operator] : array();
$return = isset(static::$conditionOperatorMap[$operator]) ? static::$conditionOperatorMap[$operator] : array();
}
$return += array('operator' => $operator);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment