diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php index bca72c460c1038837ffe7038e7525de26943a7e3..bf2a9ac079b64dfa1455a601402f06990a19dea0 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php @@ -122,6 +122,7 @@ public static function open(array &$connection_options = array()) { $pdo->sqliteCreateFunction('length', 'strlen', 1); $pdo->sqliteCreateFunction('md5', 'md5', 1); $pdo->sqliteCreateFunction('concat', array(__CLASS__, 'sqlFunctionConcat')); + $pdo->sqliteCreateFunction('concat_ws', array(__CLASS__, 'sqlFunctionConcatWs')); $pdo->sqliteCreateFunction('substring', array(__CLASS__, 'sqlFunctionSubstring'), 3); $pdo->sqliteCreateFunction('substring_index', array(__CLASS__, 'sqlFunctionSubstringIndex'), 3); $pdo->sqliteCreateFunction('rand', array(__CLASS__, 'sqlFunctionRand')); @@ -199,6 +200,25 @@ public static function sqlFunctionConcat() { return implode('', $args); } + /** + * SQLite compatibility implementation for the CONCAT_WS() SQL function. + * + * @see http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_concat-ws + */ + public static function sqlFunctionConcatWs() { + $args = func_get_args(); + $separator = array_shift($args); + // If the separator is NULL, the result is NULL. + if ($separator === FALSE || is_null($separator)) { + return NULL; + } + // Skip any NULL values after the separator argument. + $args = array_filter($args, function ($value) { + return !is_null($value); + }); + return implode($separator, $args); + } + /** * SQLite compatibility implementation for the SUBSTRING() SQL function. */ diff --git a/core/modules/system/src/Tests/Database/BasicSyntaxTest.php b/core/modules/system/src/Tests/Database/BasicSyntaxTest.php index ca6275cf19bb4967f966445c3686e6da9b9ec305..77702de790cfe2de6abad330104af6a68c92ce2c 100644 --- a/core/modules/system/src/Tests/Database/BasicSyntaxTest.php +++ b/core/modules/system/src/Tests/Database/BasicSyntaxTest.php @@ -20,7 +20,7 @@ class BasicSyntaxTest extends DatabaseTestBase { /** * Tests string concatenation. */ - function testBasicConcat() { + function testConcatLiterals() { $result = db_query('SELECT CONCAT(:a1, CONCAT(:a2, CONCAT(:a3, CONCAT(:a4, :a5))))', array( ':a1' => 'This', ':a2' => ' ', @@ -34,7 +34,7 @@ function testBasicConcat() { /** * Tests string concatenation with field values. */ - function testFieldConcat() { + function testConcatFields() { $result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', array( ':a1' => 'The age of ', ':a2' => ' is ', @@ -44,6 +44,31 @@ function testFieldConcat() { $this->assertIdentical($result->fetchField(), 'The age of John is 25.', 'Field CONCAT works.'); } + /** + * Tests string concatenation with separator. + */ + function testConcatWsLiterals() { + $result = db_query("SELECT CONCAT_WS(', ', :a1, NULL, :a2, :a3, :a4)", array( + ':a1' => 'Hello', + ':a2' => NULL, + ':a3' => '', + ':a4' => 'world.', + )); + $this->assertIdentical($result->fetchField(), 'Hello, , world.'); + } + + /** + * Tests string concatenation with separator, with field values. + */ + function testConcatWsFields() { + $result = db_query("SELECT CONCAT_WS('-', :a1, name, :a2, age) FROM {test} WHERE age = :age", array( + ':a1' => 'name', + ':a2' => 'age', + ':age' => 25, + )); + $this->assertIdentical($result->fetchField(), 'name-John-age-25'); + } + /** * Tests escaping of LIKE wildcards. */