From: stronk7 Date: Sun, 8 Apr 2007 22:59:54 +0000 (+0000) Subject: Now MySQL looks for existing index names. MDL-9256 X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9770914d5fd9284baade6188958eb53dccbaa6ca;p=moodle.git Now MySQL looks for existing index names. MDL-9256 Merged from MOODLE_18_STABLE --- diff --git a/lib/xmldb/classes/generators/XMLDBGenerator.class.php b/lib/xmldb/classes/generators/XMLDBGenerator.class.php index 646b32a083..40642f0f1a 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -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 } diff --git a/lib/xmldb/classes/generators/mssql/mssql.class.php b/lib/xmldb/classes/generators/mssql/mssql.class.php index 7c8195b62a..452c44668e 100644 --- a/lib/xmldb/classes/generators/mssql/mssql.class.php +++ b/lib/xmldb/classes/generators/mssql/mssql.class.php @@ -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': diff --git a/lib/xmldb/classes/generators/mysql/mysql.class.php b/lib/xmldb/classes/generators/mysql/mysql.class.php index 312d5c7298..f63f753c7e 100644 --- a/lib/xmldb/classes/generators/mysql/mysql.class.php +++ b/lib/xmldb/classes/generators/mysql/mysql.class.php @@ -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 */ diff --git a/lib/xmldb/classes/generators/oci8po/oci8po.class.php b/lib/xmldb/classes/generators/oci8po/oci8po.class.php index 9b872c4ba1..40b00e51ce 100644 --- a/lib/xmldb/classes/generators/oci8po/oci8po.class.php +++ b/lib/xmldb/classes/generators/oci8po/oci8po.class.php @@ -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': diff --git a/lib/xmldb/classes/generators/postgres7/postgres7.class.php b/lib/xmldb/classes/generators/postgres7/postgres7.class.php index 4bb95921c5..04fae195f6 100644 --- a/lib/xmldb/classes/generators/postgres7/postgres7.class.php +++ b/lib/xmldb/classes/generators/postgres7/postgres7.class.php @@ -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':