Now MySQL looks for existing index names. MDL-9256

Merged from MOODLE_18_STABLE
This commit is contained in:
stronk7
2007-04-08 22:59:54 +00:00
parent 362f2ce973
commit 9770914d5f
5 changed files with 38 additions and 6 deletions
@@ -925,7 +925,7 @@ class XMLDBgenerator {
}
/// 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)) {
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) {
@@ -939,7 +939,7 @@ class XMLDBgenerator {
$newnamewithsuffix = $newnamewithsuffix . '_' . $suffix;
}
/// Now iterate until not used name is found, incrementing the counter
while (in_array($newnamewithsuffix, $used_names) || $this->isNameInUse($newnamewithsuffix, $suffix)) {
while (in_array($newnamewithsuffix, $used_names) || $this->isNameInUse($newnamewithsuffix, $suffix, $tablename)) {
$counter++;
$newname = substr($name, 0, strlen($newname)-1) . $counter;
$newnamewithsuffix = $newname;
@@ -1080,10 +1080,11 @@ class XMLDBgenerator {
/**
* Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg)
* return if such name is currently in use (true) or no (false)
* (MySQL requires the whole XMLDBTable object to be specified, so we add it always)
* (invoked from getNameForObject()
* Only some DB have this implemented
*/
function isNameInUse($object_name, $type) {
function isNameInUse($object_name, $type, $table_name) {
return false; //For generators not implementing introspecion,
//we always return with the name being free to be used
}
@@ -469,7 +469,7 @@ class XMLDBmssql extends XMLDBgenerator {
* return if such name is currently in use (true) or no (false)
* (invoked from getNameForObject()
*/
function isNameInUse($object_name, $type) {
function isNameInUse($object_name, $type, $table_name) {
switch($type) {
case 'seq':
case 'trg':
@@ -258,6 +258,37 @@ class XMLDBmysql extends XMLDBGenerator {
return array();
}
/**
* Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg)
* return if such name is currently in use (true) or no (false)
* (invoked from getNameForObject()
*/
function isNameInUse($object_name, $type, $table_name) {
/// Calculate the real table name
$xmldb_table = new XMLDBTable($table_name);
$tname = $this->getTableName($xmldb_table);
switch($type) {
case 'ix':
case 'uix':
/// Fetch all the indexes in the table
if ($indexes = get_records_sql("SHOW INDEX FROM $tname")) {
foreach ($indexes as $index) {
/// Normalize array keys
$index = array_change_key_case((array)$index, CASE_LOWER);
/// Check if the name is being used
if (strtolower($object_name) == $index['key_name']) {
return true;
}
}
}
break;
}
return false; //No name in use found
}
/**
* Returns an array of reserved words (lowercase) for this DB
*/
@@ -551,7 +551,7 @@ class XMLDBoci8po extends XMLDBgenerator {
* return if such name is currently in use (true) or no (false)
* (invoked from getNameForObject()
*/
function isNameInUse($object_name, $type) {
function isNameInUse($object_name, $type, $table_name) {
switch($type) {
case 'ix':
case 'uix':
@@ -474,7 +474,7 @@ function getSequenceFromDB($xmldb_table) {
* return if such name is currently in use (true) or no (false)
* (invoked from getNameForObject()
*/
function isNameInUse($object_name, $type) {
function isNameInUse($object_name, $type, $table_name) {
switch($type) {
case 'ix':
case 'uix':