Skip to content

Commit

Permalink
MDL-40883 ddl: make sure object name never exceeds max length
Browse files Browse the repository at this point in the history
Many thanks to Ray Bon for finding the problem
  • Loading branch information
marinaglancy committed Nov 8, 2013
1 parent d214057 commit dcde391
Showing 1 changed file with 9 additions and 30 deletions.
39 changes: 9 additions & 30 deletions lib/ddl/sql_generator.php
Expand Up @@ -1086,40 +1086,19 @@ public function getNameForObject($tablename, $fields, $suffix='') {
$name .= substr(trim($field),0,3);
}
// Prepend the prefix
$name = $this->prefix . $name;
$name = trim($this->prefix . $name);

$name = substr(trim($name), 0, $this->names_max_length - 1 - strlen($suffix)); //Max names_max_length

// Add the suffix
$namewithsuffix = $name;
if ($suffix) {
$namewithsuffix = $namewithsuffix . '_' . $suffix;
}
// Make sure name does not exceed the maximum name length and add suffix.
$maxlengthwithoutsuffix = $this->names_max_length - strlen($suffix) - ($suffix ? 1 : 0);
$namewithsuffix = substr($name, 0, $maxlengthwithoutsuffix) . ($suffix ? ('_' . $suffix) : '');

// If the calculated name is in the cache, or if we detect it by introspecting the DB let's modify if
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) {
$newname = $name . $counter;
// Else replace the last char by 2
} else {
$newname = substr($name, 0, strlen($name)-1) . $counter;
}
$newnamewithsuffix = $newname;
if ($suffix) {
$newnamewithsuffix = $newnamewithsuffix . '_' . $suffix;
}
$counter = 1;
while (in_array($namewithsuffix, $used_names) || $this->isNameInUse($namewithsuffix, $suffix, $tablename)) {
// Now iterate until not used name is found, incrementing the counter
while (in_array($newnamewithsuffix, $used_names) || $this->isNameInUse($newnamewithsuffix, $suffix, $tablename)) {
$counter++;
$newname = substr($name, 0, strlen($newname)-1) . $counter;
$newnamewithsuffix = $newname;
if ($suffix) {
$newnamewithsuffix = $newnamewithsuffix . '_' . $suffix;
}
}
$namewithsuffix = $newnamewithsuffix;
$counter++;
$namewithsuffix = substr($name, 0, $maxlengthwithoutsuffix - strlen($counter)) .
$counter . ($suffix ? ('_' . $suffix) : '');
}

// Add the name to the cache
Expand Down

0 comments on commit dcde391

Please sign in to comment.