MDL-51052 dml: support for sql_like() under utf8_bin
authorCharles Fulton <fultonc@lafayette.edu>
Tue, 11 Aug 2015 12:35:09 +0000 (12:35 +0000)
committerDavid Monllao <davidm@moodle.com>
Fri, 18 Sep 2015 01:56:43 +0000 (09:56 +0800)
lib/dml/mysqli_native_moodle_database.php
lib/dml/tests/dml_test.php

index 9d7b767..8c61536 100644 (file)
@@ -1523,7 +1523,12 @@ class mysqli_native_moodle_database extends moodle_database {
             if ($accentsensitive) {
                 return "LOWER($fieldname) $LIKE LOWER($param) COLLATE utf8_bin ESCAPE '$escapechar'";
             } else {
-                return "$fieldname $LIKE $param ESCAPE '$escapechar'";
+                // Set a case sensitive collation if using utf8_bin.
+                if ($this->get_dbcollation() == 'utf8_bin') {
+                    return "$fieldname $LIKE $param COLLATE utf8_unicode_ci ESCAPE '$escapechar'";
+                } else {
+                    return "$fieldname $LIKE $param ESCAPE '$escapechar'";
+                }
             }
         }
     }
index b064d99..8b16565 100644 (file)
@@ -3870,6 +3870,11 @@ class core_dml_testcase extends database_driver_testcase {
         $records = $DB->get_records_sql($sql, array('aui'));
         $this->assertCount(1, $records);
 
+        // Test LIKE under unusual collations.
+        $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', false, false);
+        $records = $DB->get_records_sql($sql, array("%dup_r%"));
+        $this->assertCount(2, $records);
+
         $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', true, true, true); // NOT LIKE.
         $records = $DB->get_records_sql($sql, array("%o%"));
         $this->assertCount(3, $records);