$name = '';
+ /// Implement one basic cache to avoid object name duplication
+ /// along all the request life, but never to return cached results
+ if (!isset($used_names)) {
+ static $used_names = array();
+ }
+
/// Use standard naming. See http://docs.moodle.org/en/XMLDB_key_and_index_naming
$tablearr = explode ('_', $tablename);
foreach ($tablearr as $table) {
}
/// If the calculated name is in the cache, or if we detect it by introspecting the DB let's modify if
- if ($this->isNameInUse($namewithsuffix, $suffix, $tablename)) {
+ if (in_array($namewithsuffix, $used_names) || $this->isNameInUse($namewithsuffix, $suffix, $tablename)) {
$counter = 2;
/// If have free space, we add 2
if (strlen($namewithsuffix) < $this->names_max_length) {
$newnamewithsuffix = $newnamewithsuffix . '_' . $suffix;
}
/// Now iterate until not used name is found, incrementing the counter
- while ($this->isNameInUse($newnamewithsuffix, $suffix, $tablename)) {
+ while (in_array($newnamewithsuffix, $used_names) || $this->isNameInUse($newnamewithsuffix, $suffix, $tablename)) {
$counter++;
$newname = substr($name, 0, strlen($newname)-1) . $counter;
$newnamewithsuffix = $newname;
$namewithsuffix = $newnamewithsuffix;
}
+ /// Add the name to the cache
+ $used_names[$tablename.'-'.$fields.'-'.$suffix] = $namewithsuffix;
+
/// Quote it if necessary (reserved words)
$namewithsuffix = $this->getEncQuoted($namewithsuffix);