MDL-74912 mod_forum: Fix regexp word boundaries markers in MySQL 8

This commit is contained in:
Matteo Scaramuccia
2022-11-05 10:07:26 +01:00
parent fee1b8ce5f
commit b82c95a075
4 changed files with 62 additions and 2 deletions
+24
View File
@@ -2515,6 +2515,30 @@ abstract class moodle_database {
return '';
}
/**
* Returns the word-beginning boundary marker if this database driver supports regex syntax when searching.
* @return string The word-beginning boundary marker. Otherwise, an empty string.
*/
public function sql_regex_get_word_beginning_boundary_marker() {
if ($this->sql_regex_supported()) {
return '[[:<:]]';
}
return '';
}
/**
* Returns the word-end boundary marker if this database driver supports regex syntax when searching.
* @return string The word-end boundary marker. Otherwise, an empty string.
*/
public function sql_regex_get_word_end_boundary_marker() {
if ($this->sql_regex_supported()) {
return '[[:>:]]';
}
return '';
}
/**
* Returns the SQL that allows to find intersection of two or more queries
*
+32
View File
@@ -1889,6 +1889,38 @@ class mysqli_native_moodle_database extends moodle_database {
return $collation . ($positivematch ? 'REGEXP' : 'NOT REGEXP');
}
/**
* Returns the word-beginning boundary marker based on MySQL version.
* @return string The word-beginning boundary marker.
*/
public function sql_regex_get_word_beginning_boundary_marker() {
$ismysql = ($this->get_dbtype() == 'mysqli' || $this->get_dbtype() == 'auroramysql');
$ismysqlge8d0d4 = ($ismysql && version_compare($this->get_server_info()['version'], '8.0.4', '>='));
if ($ismysqlge8d0d4) {
return '\\b';
}
// Prior to MySQL 8.0.4, MySQL used the Henry Spencer regular expression library to support regular expression operations,
// rather than International Components for Unicode (ICU).
// MariaDB still supports the "old marker" (MDEV-5357).
return '[[:<:]]';
}
/**
* Returns the word-end boundary marker based on MySQL version.
* @return string The word-end boundary marker.
*/
public function sql_regex_get_word_end_boundary_marker() {
$ismysql = ($this->get_dbtype() == 'mysqli' || $this->get_dbtype() == 'auroramysql');
$ismysqlge8d0d4 = ($ismysql && version_compare($this->get_server_info()['version'], '8.0.4', '>='));
if ($ismysqlge8d0d4) {
return '\\b';
}
// Prior to MySQL 8.0.4, MySQL used the Henry Spencer regular expression library to support regular expression operations,
// rather than International Components for Unicode (ICU).
// MariaDB still supports the "old marker" (MDEV-5357).
return '[[:>:]]';
}
/**
* Returns the SQL to be used in order to an UNSIGNED INTEGER column to SIGNED.
*
+4 -2
View File
@@ -418,6 +418,8 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $
if ($DB->sql_regex_supported()) {
$REGEXP = $DB->sql_regex(true);
$NOTREGEXP = $DB->sql_regex(false);
$regexwordbegin = $DB->sql_regex_get_word_beginning_boundary_marker();
$regexwordend = $DB->sql_regex_get_word_end_boundary_marker();
}
$params = array();
@@ -456,8 +458,8 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $
break;
case TOKEN_EXACT:
$SQLString .= "(($datafield $REGEXP :$name1) OR ($metafield $REGEXP :$name2))";
$params[$name1] = "[[:<:]]".$value."[[:>:]]";
$params[$name2] = "[[:<:]]".$value."[[:>:]]";
$params[$name1] = $regexwordbegin.$value.$regexwordend;
$params[$name2] = $regexwordbegin.$value.$regexwordend;
break;
case TOKEN_META:
if ($metafield != '') {
+2
View File
@@ -60,6 +60,8 @@ Declaration is as follow:
* New function set_additional_classes() has been implemented to add additional classes to action_menu.
* Most Behat functionality for the Moodle App has been removed from core, refer to the documentation in order to upgrade your testing
setup: https://moodledev.io/general/app/development/testing/acceptance-testing#upgrading-tests-from-an-older-version
* New DML driver methods `$DB->sql_regex_get_word_beginning_boundary_marker` and `$DB->sql_regex_get_word_end_boundary_marker`
for managing word boundary markers in a database driver supporting regex syntax when searching.
=== 4.0 ===