]> git.mjollnir.org Git - moodle.git/commitdiff
Now MySQL looks for existing index names. MDL-9256
authorstronk7 <stronk7>
Sun, 8 Apr 2007 22:59:54 +0000 (22:59 +0000)
committerstronk7 <stronk7>
Sun, 8 Apr 2007 22:59:54 +0000 (22:59 +0000)
Merged from MOODLE_18_STABLE

lib/xmldb/classes/generators/XMLDBGenerator.class.php
lib/xmldb/classes/generators/mssql/mssql.class.php
lib/xmldb/classes/generators/mysql/mysql.class.php
lib/xmldb/classes/generators/oci8po/oci8po.class.php
lib/xmldb/classes/generators/postgres7/postgres7.class.php

index 646b32a083e295c316f7c753b176cf0c0bc29281..40642f0f1ae3d3d6ca15788883b888b9fca53a3c 100644 (file)
@@ -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
     }
index 7c8195b62add881226f085acc303bc611edad5d9..452c44668e2a36a5cb88e31a375f8140c099e8b9 100644 (file)
@@ -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':
index 312d5c7298ab289f39c82f17d24a45d30e9bf58c..f63f753c7e4dbb3756b1021dbc427637da2b7020 100644 (file)
@@ -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
      */
index 9b872c4ba1d1a145331467eff549b2557913c4e4..40b00e51ce30e5b6a5c61356a2d997405d4af41c 100644 (file)
@@ -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':
index 4bb95921c5d8d8d09768aed49430f994be28bcd6..04fae195f60951ff583ba570958339bf9577dddb 100644 (file)
@@ -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':