Skip to content

Commit

Permalink
Issue #3131379 by daffie, Beakerboy, Lendude, alexpott: CONCAT_WS req…
Browse files Browse the repository at this point in the history
…uires at least 3 arguments

(cherry picked from commit 5c72f9f050d4bb36cb47ea98f918479ce10d123b)
  • Loading branch information
alexpott committed May 12, 2020
1 parent 70a433e commit 778ba61
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 14 additions & 10 deletions modules/views/src/Plugin/views/filter/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,21 @@ public function query() {
}
}
if ($fields) {
$count = count($fields);
$separated_fields = [];
foreach ($fields as $key => $field) {
$separated_fields[] = $field;
if ($key < $count - 1) {
$separated_fields[] = "' '";
}
// We do not use the CONCAT_WS operator when there is only a single field.
// Using the CONCAT_WS operator with a single field is not a problem for
// the by core supported databases. Only the MS SQL Server requires the
// CONCAT_WS operator to be used with at least three arguments.
if (count($fields) == 1) {
$expression = reset($fields);
}
else {
// Multiple fields are separated by 3 spaces so that so that search
// strings that contain spaces are still only matched to single field
// values and not to multi-field values that exist only because we do
// the concatenation/LIKE trick.
$expression = implode(", ' ', ", $fields);
$expression = "CONCAT_WS(' ', $expression)";
}
$expression = implode(', ', $separated_fields);
$expression = "CONCAT_WS(' ', $expression)";

$info = $this->operators();
if (!empty($info[$this->operator]['method'])) {
$this->{$info[$this->operator]['method']}($expression);
Expand Down
8 changes: 8 additions & 0 deletions modules/views/tests/src/Kernel/Handler/FilterCombineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public function testFilterCombineAllWords() {
],
];
$this->assertIdenticalResultset($view, $resultset, $this->columnMap);

// Confirm that the query with multiple filters used the "CONCAT_WS"
// operator.
$this->assertStringContainsString('CONCAT_WS(', $view->query->query());
}

/**
Expand Down Expand Up @@ -271,6 +275,10 @@ public function testNonFieldsRow() {
$errors = $view->validate();
// Check that the right error is shown.
$this->assertEquals(t('%display: %filter can only be used on displays that use fields. Set the style or row format for that display to one using fields to use the combine field filter.', ['%filter' => 'Global: Combine fields filter', '%display' => 'Master']), reset($errors['default']));

// Confirm that the query with single filter does not use the "CONCAT_WS"
// operator.
$this->assertStringNotContainsString('CONCAT_WS(', $view->query->query());
}

/**
Expand Down

0 comments on commit 778ba61

Please sign in to comment.