Commit 595d6d9a authored by catch's avatar catch

Issue #1935300 by dawehner, chx: Don't hardwire Database in Views filters, add regexp to DBTNG.

parent 05ff0c42
......@@ -230,6 +230,7 @@ public function mapConditionOperator($operator) {
// statements, we need to use ILIKE instead.
'LIKE' => array('operator' => 'ILIKE'),
'NOT LIKE' => array('operator' => 'NOT ILIKE'),
'REGEXP' => array('operator' => '~*'),
);
return isset($specials[$operator]) ? $specials[$operator] : NULL;
}
......
......@@ -128,6 +128,7 @@ public static function open(array &$connection_options = array()) {
$pdo->sqliteCreateFunction('substring', array(__CLASS__, 'sqlFunctionSubstring'), 3);
$pdo->sqliteCreateFunction('substring_index', array(__CLASS__, 'sqlFunctionSubstringIndex'), 3);
$pdo->sqliteCreateFunction('rand', array(__CLASS__, 'sqlFunctionRand'));
$pdo->sqliteCreateFunction('regexp', array(__CLASS__, 'sqlFunctionRegexp'));
// Execute sqlite init_commands.
if (isset($connection_options['init_commands'])) {
......@@ -236,6 +237,15 @@ public static function sqlFunctionRand($seed = NULL) {
return mt_rand() / mt_getrandmax();
}
/**
* SQLite compatibility implementation for the REGEXP SQL operator.
*
* The REGEXP operator is a special syntax for the regexp() user function.
*/
public static function sqlFunctionRegexp($string, $pattern) {
return preg_match('#' . str_replace('#', '\#', $pattern) . '#i', $string);
}
/**
* SQLite-specific implementation of DatabaseConnection::prepare().
*
......
......@@ -367,6 +367,75 @@ function testRandomOrder() {
$this->assertEqual($sorted_ids_second_set, $sorted_ids, 'After sorting the second random list, the result matches the sorted version of the first random list.');
}
/**
* Tests that filter by a regular expression works as expected.
*/
public function testRegexCondition() {
$test_groups[] = array(
'regex' => 'hn$',
'expected' => array(
'John',
),
);
$test_groups[] = array(
'regex' => '^Pau',
'expected' => array(
'Paul',
),
);
$test_groups[] = array(
'regex' => 'Ringo|George',
'expected' => array(
'Ringo', 'George',
),
);
$database = $this->container->get('database');
foreach ($test_groups as $test_group) {
$query = $database->select('test', 't');
$query->addField('t', 'name');
$query->condition('t.name', $test_group['regex'], 'REGEXP');
$result = $query->execute()->fetchCol();
$this->assertEqual(count($result), count($test_group['expected']), 'Returns the expected number of rows.');
$this->assertEqual(sort($result), sort($test_group['expected']), 'Returns the expected rows.');
}
// Ensure that filter by "#" still works due to the quoting.
$database->insert('test')
->fields(array(
'name' => 'Pete',
'age' => 26,
'job' => '#Drummer',
))
->execute();
$test_groups = array();
$test_groups[] = array(
'regex' => '#Drummer',
'expected' => array(
'Pete',
),
);
$test_groups[] = array(
'regex' => '#Singer',
'expected' => array(
),
);
foreach ($test_groups as $test_group) {
$query = $database->select('test', 't');
$query->addField('t', 'name');
$query->condition('t.job', $test_group['regex'], 'REGEXP');
$result = $query->execute()->fetchCol();
$this->assertEqual(count($result), count($test_group['expected']), 'Returns the expected number of rows.');
$this->assertEqual(sort($result), sort($test_group['expected']), 'Returns the expected rows.');
}
}
/**
* Tests that aliases are renamed when they are duplicates.
*/
......
......@@ -128,7 +128,7 @@ protected function opNotLike($expression) {
protected function opRegex($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression RLIKE $placeholder", array($placeholder => $this->value));
$this->query->addWhereExpression($this->options['group'], "$expression REGEXP $placeholder", array($placeholder => $this->value));
}
protected function opEmpty($expression) {
......
......@@ -85,6 +85,12 @@ function operators() {
'short' => t('not between'),
'values' => 2,
),
'regular_expression' => array(
'title' => t('Regular expression'),
'short' => t('regex'),
'method' => 'op_regex',
'values' => 1,
),
);
// if the definition allows for the empty operator, add it.
......@@ -105,18 +111,6 @@ function operators() {
);
}
// Add regexp support for MySQL.
if (Database::getConnection()->databaseType() == 'mysql') {
$operators += array(
'regular_expression' => array(
'title' => t('Regular expression'),
'short' => t('regex'),
'method' => 'opRegex',
'values' => 1,
),
);
}
return $operators;
}
......@@ -275,8 +269,14 @@ protected function opEmpty($field) {
$this->query->addWhere($this->options['group'], $field, NULL, $operator);
}
/**
* Filters by a regular expression.
*
* @param string $field
* The expression pointing to the queries field, for example "foo.bar".
*/
protected function opRegex($field) {
$this->query->addWhere($this->options['group'], $field, $this->value, 'RLIKE');
$this->query->addWhere($this->options['group'], $field, $this->value, 'REGEXP');
}
public function adminSummary() {
......
......@@ -110,6 +110,12 @@ function operators() {
'method' => 'opLongerThan',
'values' => 1,
),
'regular_expression' => array(
'title' => t('Regular expression'),
'short' => t('regex'),
'method' => 'opRegex',
'values' => 1,
),
);
// if the definition allows for the empty operator, add it.
if (!empty($this->definition['allow empty'])) {
......@@ -128,17 +134,6 @@ function operators() {
),
);
}
// Add regexp support for MySQL.
if (Database::getConnection()->databaseType() == 'mysql') {
$operators += array(
'regular_expression' => array(
'title' => t('Regular expression'),
'short' => t('regex'),
'method' => 'opRegex',
'values' => 1,
),
);
}
return $operators;
}
......@@ -331,8 +326,14 @@ protected function opLongerThan($field) {
$this->query->addWhereExpression($this->options['group'], "LENGTH($field) > $placeholder", array($placeholder => $this->value));
}
/**
* Filters by a regular expression.
*
* @param string $field
* The expression pointing to the queries field, for example "foo.bar".
*/
protected function opRegex($field) {
$this->query->addWhere($this->options['group'], $field, $this->value, 'RLIKE');
$this->query->addWhere($this->options['group'], $field, $this->value, 'REGEXP');
}
protected function opEmpty($field) {
......
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