Commit 78ac4561 authored by alexpott's avatar alexpott
Browse files

Issue #2427311 by sun, daffie: SQLite does not natively support CONCAT_WS()

parent d63490f9
...@@ -122,6 +122,7 @@ public static function open(array &$connection_options = array()) { ...@@ -122,6 +122,7 @@ public static function open(array &$connection_options = array()) {
$pdo->sqliteCreateFunction('length', 'strlen', 1); $pdo->sqliteCreateFunction('length', 'strlen', 1);
$pdo->sqliteCreateFunction('md5', 'md5', 1); $pdo->sqliteCreateFunction('md5', 'md5', 1);
$pdo->sqliteCreateFunction('concat', array(__CLASS__, 'sqlFunctionConcat')); $pdo->sqliteCreateFunction('concat', array(__CLASS__, 'sqlFunctionConcat'));
$pdo->sqliteCreateFunction('concat_ws', array(__CLASS__, 'sqlFunctionConcatWs'));
$pdo->sqliteCreateFunction('substring', array(__CLASS__, 'sqlFunctionSubstring'), 3); $pdo->sqliteCreateFunction('substring', array(__CLASS__, 'sqlFunctionSubstring'), 3);
$pdo->sqliteCreateFunction('substring_index', array(__CLASS__, 'sqlFunctionSubstringIndex'), 3); $pdo->sqliteCreateFunction('substring_index', array(__CLASS__, 'sqlFunctionSubstringIndex'), 3);
$pdo->sqliteCreateFunction('rand', array(__CLASS__, 'sqlFunctionRand')); $pdo->sqliteCreateFunction('rand', array(__CLASS__, 'sqlFunctionRand'));
...@@ -199,6 +200,25 @@ public static function sqlFunctionConcat() { ...@@ -199,6 +200,25 @@ public static function sqlFunctionConcat() {
return implode('', $args); 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. * SQLite compatibility implementation for the SUBSTRING() SQL function.
*/ */
......
...@@ -20,7 +20,7 @@ class BasicSyntaxTest extends DatabaseTestBase { ...@@ -20,7 +20,7 @@ class BasicSyntaxTest extends DatabaseTestBase {
/** /**
* Tests string concatenation. * Tests string concatenation.
*/ */
function testBasicConcat() { function testConcatLiterals() {
$result = db_query('SELECT CONCAT(:a1, CONCAT(:a2, CONCAT(:a3, CONCAT(:a4, :a5))))', array( $result = db_query('SELECT CONCAT(:a1, CONCAT(:a2, CONCAT(:a3, CONCAT(:a4, :a5))))', array(
':a1' => 'This', ':a1' => 'This',
':a2' => ' ', ':a2' => ' ',
...@@ -34,7 +34,7 @@ function testBasicConcat() { ...@@ -34,7 +34,7 @@ function testBasicConcat() {
/** /**
* Tests string concatenation with field values. * 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( $result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', array(
':a1' => 'The age of ', ':a1' => 'The age of ',
':a2' => ' is ', ':a2' => ' is ',
...@@ -44,6 +44,31 @@ function testFieldConcat() { ...@@ -44,6 +44,31 @@ function testFieldConcat() {
$this->assertIdentical($result->fetchField(), 'The age of John is 25.', 'Field CONCAT works.'); $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. * Tests escaping of LIKE wildcards.
*/ */
......
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