Commit e4e0d0b4 authored by catch's avatar catch

Issue #2784739 by slasher13, daffie, miiimooo, voleger, MerryHamster,...

Issue #2784739 by slasher13, daffie, miiimooo, voleger, MerryHamster, chanderbhushan, anmolgoyal74, mohit1604, Prashant.c, Meenakshi.g, kostyashupenko, Juterpillar, sylvain lavielle, gawaksh, Lendude, mradcliffe, andypost, alexpott: Fix PostgreSQL operator in views
parent 5fb97954
......@@ -133,13 +133,14 @@ public function validate() {
*/
public function opEqual($expression) {
$placeholder = $this->placeholder();
$operator = $this->operator();
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->value]);
}
protected function opContains($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
}
/**
......@@ -165,8 +166,7 @@ protected function opContainsWord($expression) {
// Switch between the 'word' and 'allwords' operator.
$type = $this->operator == 'word' ? 'OR' : 'AND';
$group = $this->query->setWhereGroup($type);
$operator = $this->connection->mapConditionOperator('LIKE');
$operator = isset($operator['operator']) ? $operator['operator'] : 'LIKE';
$operator = $this->getConditionOperator('LIKE');
foreach ($matches as $match_key => $match) {
$temp_placeholder = $placeholder . '_' . $match_key;
......@@ -178,27 +178,32 @@ protected function opContainsWord($expression) {
protected function opStartsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
}
protected function opNotStartsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
}
protected function opEndsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
}
protected function opNotEndsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
}
protected function opNotLike($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
}
protected function opRegex($expression) {
......
......@@ -299,8 +299,29 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
}
}
/**
* Get the query operator.
*
* @return string
* Returns LIKE or NOT LIKE or the database specific equivalent based on the
* query's operator.
*/
public function operator() {
return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
return $this->getConditionOperator($this->operator == '=' ? 'LIKE' : 'NOT LIKE');
}
/**
* Get specified condition operator mapping value.
*
* @param string $operator
* Condition operator.
*
* @return string
* Specified condition operator mapping value.
*/
protected function getConditionOperator($operator) {
$mapping = $this->connection->mapConditionOperator($operator);
return isset($mapping['operator']) ? $mapping['operator'] : $operator;
}
/**
......@@ -325,7 +346,8 @@ public function opEqual($field) {
}
protected function opContains($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', 'LIKE');
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opContainsWord($field) {
......@@ -337,6 +359,7 @@ protected function opContainsWord($field) {
}
preg_match_all(static::WORDS_PATTERN, ' ' . $this->value, $matches, PREG_SET_ORDER);
$operator = $this->getConditionOperator('LIKE');
foreach ($matches as $match) {
$phrase = FALSE;
// Strip off phrase quotes
......@@ -347,7 +370,7 @@ protected function opContainsWord($field) {
$words = trim($match[2], ',?!();:-');
$words = $phrase ? [$words] : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
foreach ($words as $word) {
$where->condition($field, '%' . $this->connection->escapeLike(trim($word, " ,!?")) . '%', 'LIKE');
$where->condition($field, '%' . $this->connection->escapeLike(trim($word, " ,!?")) . '%', $operator);
}
}
......@@ -361,23 +384,28 @@ protected function opContainsWord($field) {
}
protected function opStartsWith($field) {
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', 'LIKE');
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opNotStartsWith($field) {
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', 'NOT LIKE');
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opEndsWith($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), 'LIKE');
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), $operator);
}
protected function opNotEndsWith($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), 'NOT LIKE');
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), $operator);
}
protected function opNotLike($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', 'NOT LIKE');
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opShorterThan($field) {
......
......@@ -69,7 +69,7 @@ public function testFilterCombineContains() {
'name',
'job',
],
'value' => 'ing',
'value' => 'iNg',
],
]);
......@@ -281,6 +281,294 @@ public function testNonFieldsRow() {
$this->assertStringNotContainsString('CONCAT_WS(', $view->query->query());
}
/**
* Tests the Combine field filter using the 'equal' operator.
*/
public function testFilterCombineEqual() {
$view = Views::getView('test_view');
$view->setDisplay();
$fields = $view->displayHandlers->get('default')->getOption('fields');
$view->displayHandlers->get('default')->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
// Change the filtering.
$view->displayHandlers->get('default')->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => '=',
'fields' => [
'job',
],
'value' => 'sInger',
],
]);
$this->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
];
$this->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
/**
* Tests the Combine field filter using the 'starts' operator.
*/
public function testFilterCombineStarts() {
$view = Views::getView('test_view');
$view->setDisplay();
$fields = $view->displayHandlers->get('default')->getOption('fields');
$view->displayHandlers->get('default')->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
// Change the filtering.
$view->displayHandlers->get('default')->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'starts',
'fields' => [
'job',
],
'value' => 'sIn',
],
]);
$this->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
];
$this->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
/**
* Tests the Combine field filter using the 'not_starts' operator.
*/
public function testFilterCombineNotStarts() {
$view = Views::getView('test_view');
$view->setDisplay();
$fields = $view->displayHandlers->get('default')->getOption('fields');
$view->displayHandlers->get('default')->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
// Change the filtering.
$view->displayHandlers->get('default')->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'not_starts',
'fields' => [
'job',
],
'value' => 'sIn',
],
]);
$this->executeView($view);
$resultset = [
[
'name' => 'Ringo',
'job' => 'Drummer',
],
[
'name' => 'Paul',
'job' => 'Songwriter',
],
[
'name' => 'Meredith',
'job' => 'Speaker',
],
];
$this->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
/**
* Tests the Combine field filter using the 'ends' operator.
*/
public function testFilterCombineEnds() {
$view = Views::getView('test_view');
$view->setDisplay();
$fields = $view->displayHandlers->get('default')->getOption('fields');
$view->displayHandlers->get('default')->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
// Change the filtering.
$view->displayHandlers->get('default')->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'ends',
'fields' => [
'job',
],
'value' => 'Ger',
],
]);
$this->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
];
$this->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
/**
* Tests the Combine field filter using the 'not_ends' operator.
*/
public function testFilterCombineNotEnds() {
$view = Views::getView('test_view');
$view->setDisplay();
$fields = $view->displayHandlers->get('default')->getOption('fields');
$view->displayHandlers->get('default')->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
// Change the filtering.
$view->displayHandlers->get('default')->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'not_ends',
'fields' => [
'job',
],
'value' => 'Ger',
],
]);
$this->executeView($view);
$resultset = [
[
'name' => 'Ringo',
'job' => 'Drummer',
],
[
'name' => 'Paul',
'job' => 'Songwriter',
],
[
'name' => 'Meredith',
'job' => 'Speaker',
],
];
$this->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
/**
* Tests the Combine field filter using the 'not' operator.
*/
public function testFilterCombineNot() {
$view = Views::getView('test_view');
$view->setDisplay();
$fields = $view->displayHandlers->get('default')->getOption('fields');
$view->displayHandlers->get('default')->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
// Change the filtering.
$view->displayHandlers->get('default')->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'not',
'fields' => [
'job',
],
'value' => 'singer',
],
]);
$this->executeView($view);
$resultset = [
[
'name' => 'Ringo',
'job' => 'Drummer',
],
[
'name' => 'Paul',
'job' => 'Songwriter',
],
[
'name' => 'Meredith',
'job' => 'Speaker',
],
];
$this->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
/**
* Additional data to test the NULL issue.
*/
......
......@@ -207,7 +207,7 @@ public function testFilterStringContains() {
'field' => 'name',
'relationship' => 'none',
'operator' => 'contains',
'value' => 'ing',
'value' => 'iNg',
],
]);
......@@ -253,7 +253,7 @@ public function testFilterStringWord() {
'field' => 'description',
'relationship' => 'none',
'operator' => 'word',
'value' => 'actor',
'value' => 'aCtor',
],
]);
......@@ -382,7 +382,7 @@ public function testFilterStringStarts() {
'field' => 'description',
'relationship' => 'none',
'operator' => 'starts',
'value' => 'George',
'value' => 'gEorge',
],
]);
......@@ -427,7 +427,7 @@ public function testFilterStringNotStarts() {
'field' => 'description',
'relationship' => 'none',
'operator' => 'not_starts',
'value' => 'George',
'value' => 'gEorge',
],
]);
......@@ -486,7 +486,7 @@ public function testFilterStringEnds() {
'field' => 'description',
'relationship' => 'none',
'operator' => 'ends',
'value' => 'Beatles.',
'value' => 'bEatles.',
],
]);
......@@ -537,7 +537,7 @@ public function testFilterStringNotEnds() {
'field' => 'description',
'relationship' => 'none',
'operator' => 'not_ends',
'value' => 'Beatles.',
'value' => 'bEatles.',
],
]);
......@@ -590,7 +590,7 @@ public function testFilterStringNot() {
'field' => 'description',
'relationship' => 'none',
'operator' => 'not',
'value' => 'Beatles.',
'value' => 'bEatles.',
],
]);
......
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