Skip to content

Commit

Permalink
MDL-51052 dml: More info about mysql and case & accent sensitiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Sep 18, 2015
1 parent 3d47155 commit 2bdeb8e
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/dml/mysqli_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1502,10 +1502,13 @@ public function sql_cast_char2real($fieldname, $text=false) {
/**
* Returns 'LIKE' part of a query.
*
* Note that mysql does not support $casesensitive = true and $accentsensitive = false.
* More information in http://bugs.mysql.com/bug.php?id=19567.
*
* @param string $fieldname usually name of the table column
* @param string $param usually bound query parameter (?, :named)
* @param bool $casesensitive use case sensitive search
* @param bool $accensensitive use accent sensitive search (not all databases support accent insensitive)
* @param bool $accensensitive use accent sensitive search (ignored if $casesensitive is true)
* @param bool $notlike true means "NOT LIKE"
* @param string $escapechar escape char for '%' and '_'
* @return string SQL code fragment
Expand All @@ -1517,19 +1520,24 @@ public function sql_like($fieldname, $param, $casesensitive = true, $accentsensi
$escapechar = $this->mysqli->real_escape_string($escapechar); // prevents problems with C-style escapes of enclosing '\'

$LIKE = $notlike ? 'NOT LIKE' : 'LIKE';

if ($casesensitive) {
// Current MySQL versions do not support case sensitive and accent insensitive.
return "$fieldname $LIKE $param COLLATE utf8_bin ESCAPE '$escapechar'";

} else if ($accentsensitive) {
// Case insensitive and accent sensitive, we can force a binary comparison once all texts are using the same case.
return "LOWER($fieldname) $LIKE LOWER($param) COLLATE utf8_bin ESCAPE '$escapechar'";

} else {
if ($accentsensitive) {
return "LOWER($fieldname) $LIKE LOWER($param) COLLATE utf8_bin ESCAPE '$escapechar'";
} else {
// 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'";
}
// Case insensitive and accent insensitive.
$collation = '';
if ($this->get_dbcollation() == 'utf8_bin') {
// Force a case insensitive comparison if using utf8_bin.
$collation = 'COLLATE utf8_unicode_ci';
}

return "$fieldname $LIKE $param $collation ESCAPE '$escapechar'";
}
}

Expand Down

0 comments on commit 2bdeb8e

Please sign in to comment.