From 78ac4561d30ae87ca4ef99a064d8a96208112f60 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 17 Feb 2015 09:10:56 +0000 Subject: [PATCH] Issue #2427311 by sun, daffie: SQLite does not natively support CONCAT_WS() --- .../Database/Driver/sqlite/Connection.php | 20 +++++++++++++ .../src/Tests/Database/BasicSyntaxTest.php | 29 +++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php index bca72c460c10..bf2a9ac079b6 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 ca6275cf19bb..77702de790cf 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. */ -- GitLab