From b438bce9498321d67b02307c3f831dd9a01afc8e Mon Sep 17 00:00:00 2001 From: Jason Fowler Date: Wed, 5 Oct 2011 16:20:50 +0800 Subject: [PATCH 1/2] MDL-29496 Search, Course - Code applied from patch to allow courses with a blank summary to be found in searchs - Thanks to Andrew Nicols for the patch --- lib/datalib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/datalib.php b/lib/datalib.php index 478156cd71232..503462708fb3c 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -717,7 +717,7 @@ function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $record $params = array(); $i = 0; - $concat = $DB->sql_concat('c.summary', "' '", 'c.fullname', "' '", 'c.idnumber', "' '", 'c.shortname'); + $concat = $DB->sql_concat("COALESCE(c.summary, ". $DB->sql_empty() .")", "' '", 'c.fullname', "' '", 'c.idnumber', "' '", 'c.shortname'); foreach ($searchterms as $searchterm) { $i++; From fc0aebb595ce50210b97cb1946b149f1be1b6180 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 17 Oct 2011 13:07:28 +0200 Subject: [PATCH 2/2] MDL-29496 unittests - verify COALESCE() cross-db compatibility --- lib/dml/simpletest/testdml.php | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index 5f37e105dee55..8428fec2c2810 100644 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -3378,6 +3378,48 @@ function test_sql_ilike() { $DB->get_records_sql($sql, $params); } + function test_coalesce() { + $DB = $this->tdb; + + // Testing not-null ocurrences, return 1st + $sql = "SELECT COALESCE('returnthis', 'orthis', 'orwhynotthis') AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array())); + $sql = "SELECT COALESCE(:paramvalue, 'orthis', 'orwhynotthis') AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => 'returnthis'))); + + // Testing null ocurrences, return 2nd + $sql = "SELECT COALESCE(null, 'returnthis', 'orthis') AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array())); + $sql = "SELECT COALESCE(:paramvalue, 'returnthis', 'orthis') AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => null))); + $sql = "SELECT COALESCE(null, :paramvalue, 'orthis') AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => 'returnthis'))); + + // Testing null ocurrences, return 3rd + $sql = "SELECT COALESCE(null, null, 'returnthis') AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array())); + $sql = "SELECT COALESCE(null, :paramvalue, 'returnthis') AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => null))); + $sql = "SELECT COALESCE(null, null, :paramvalue) AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => 'returnthis'))); + + // Testing all null ocurrences, return null + // Note: under mssql, if all elements are nulls, at least one must be a "typed" null, hence + // we cannot test this in a cross-db way easily, so next 2 tests are using + // different queries depending of the DB family + $customnull = $DB->get_dbfamily() == 'mssql' ? 'CAST(null AS varchar)' : 'null'; + $sql = "SELECT COALESCE(null, null, " . $customnull . ") AS test" . $DB->sql_null_from_clause(); + $this->assertNull($DB->get_field_sql($sql, array())); + $sql = "SELECT COALESCE(null, :paramvalue, " . $customnull . ") AS test" . $DB->sql_null_from_clause(); + $this->assertNull($DB->get_field_sql($sql, array('paramvalue' => null))); + + // Check there are not problems with whitespace strings + $sql = "SELECT COALESCE(null, '', null) AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('', $DB->get_field_sql($sql, array())); + $sql = "SELECT COALESCE(null, :paramvalue, null) AS test" . $DB->sql_null_from_clause(); + $this->assertEqual('', $DB->get_field_sql($sql, array('paramvalue' => ''))); + } + function test_sql_concat() { $DB = $this->tdb; $dbman = $DB->get_manager();