MDL-60976 dml: Optimise replace_all_text()

Avoid updating fields that do not match the search string.
This commit is contained in:
Matt Clarkson 2017-12-06 10:41:01 +13:00
parent 109aa07648
commit 1c06098b6b

View File

@ -2402,21 +2402,25 @@ abstract class moodle_database {
// Enclose the column name by the proper quotes if it's a reserved word.
$columnname = $this->get_manager()->generator->getEncQuoted($column->name);
$searchsql = $this->sql_like($columnname, '?');
$searchparam = '%'.$this->sql_like_escape($search).'%';
$sql = "UPDATE {".$table."}
SET $columnname = REPLACE($columnname, ?, ?)
WHERE $columnname IS NOT NULL";
WHERE $searchsql";
if ($column->meta_type === 'X') {
$this->execute($sql, array($search, $replace));
$this->execute($sql, array($search, $replace, $searchparam));
} else if ($column->meta_type === 'C') {
if (core_text::strlen($search) < core_text::strlen($replace)) {
$colsize = $column->max_length;
$sql = "UPDATE {".$table."}
SET $columnname = " . $this->sql_substr("REPLACE(" . $columnname . ", ?, ?)", 1, $colsize) . "
WHERE $columnname IS NOT NULL";
WHERE $searchsql";
}
$this->execute($sql, array($search, $replace));
$this->execute($sql, array($search, $replace, $searchparam));
}
}