Merge branch 'MDL-49293-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2015-08-11 22:58:09 +02:00
commit 789d40a803
3 changed files with 59 additions and 15 deletions

View File

@ -1207,11 +1207,7 @@ class mssql_native_moodle_database extends moodle_database {
for ($n=count($elements)-1; $n > 0 ; $n--) { for ($n=count($elements)-1; $n > 0 ; $n--) {
array_splice($elements, $n, 0, $separator); array_splice($elements, $n, 0, $separator);
} }
$s = implode(' + ', $elements); return call_user_func_array(array($this, 'sql_concat'), $elements);
if ($s === '') {
return " '' ";
}
return " $s ";
} }
public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) { public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) {

View File

@ -1272,12 +1272,7 @@ class sqlsrv_native_moodle_database extends moodle_database {
for ($n = count($elements) - 1; $n > 0; $n--) { for ($n = count($elements) - 1; $n > 0; $n--) {
array_splice($elements, $n, 0, $separator); array_splice($elements, $n, 0, $separator);
} }
$s = implode(' + ', $elements); return call_user_func_array(array($this, 'sql_concat'), $elements);
if ($s === '') {
return " '' ";
}
return " $s ";
} }
public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) { public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) {

View File

@ -3988,12 +3988,65 @@ class core_dml_testcase extends database_driver_testcase {
} }
public function test_concat_join() { public function sql_concat_join_provider() {
return array(
// All strings.
array(
"' '",
array("'name'", "'name2'", "'name3'"),
array(),
'name name2 name3',
),
// All strings using placeholders
array(
"' '",
array("?", "?", "?"),
array('name', 'name2', 'name3'),
'name name2 name3',
),
// All integers.
array(
"' '",
array(1, 2, 3),
array(),
'1 2 3',
),
// All integers using placeholders
array(
"' '",
array("?", "?", "?"),
array(1, 2, 3),
'1 2 3',
),
// Mix of strings and integers.
array(
"' '",
array(1, "'2'", 3),
array(),
'1 2 3',
),
// Mix of strings and integers using placeholders.
array(
"' '",
array(1, '2', 3),
array(),
'1 2 3',
),
);
}
/**
* @dataProvider sql_concat_join_provider
* @param string $concat The string to use when concatanating.
* @param array $fields The fields to concatanate
* @param array $params Any parameters to provide to the query
* @param @string $expected The expected result
*/
public function test_concat_join($concat, $fields, $params, $expected) {
$DB = $this->tdb; $DB = $this->tdb;
$sql = "SELECT ".$DB->sql_concat_join("' '", array("?", "?", "?"))." AS fullname ".$DB->sql_null_from_clause(); $sql = "SELECT " . $DB->sql_concat_join($concat, $fields) . " AS result" . $DB->sql_null_from_clause();
$params = array("name", "name2", "name3");
$result = $DB->get_field_sql($sql, $params); $result = $DB->get_field_sql($sql, $params);
$this->assertEquals("name name2 name3", $result); $this->assertEquals($expected, $result);
} }
public function test_sql_fullname() { public function test_sql_fullname() {