]> git.mjollnir.org Git - moodle.git/commitdiff
Added rename_index()
authorstronk7 <stronk7>
Sat, 30 Sep 2006 18:24:14 +0000 (18:24 +0000)
committerstronk7 <stronk7>
Sat, 30 Sep 2006 18:24:14 +0000 (18:24 +0000)
Adding support for index renaming. Note that MySQL doesn't support this
but this shouldn't be important at all, mainly because, under xmldb,
accesses to index-names are always done with find_index_name() and
never explicity.
Anyway, this function could be useful in the future to normalize index
names in some Health center or another similar place.

lib/ddllib.php

index e7f79018200e439b5b317dd7561529676aa54fcc..6e7a87ba51371c11d2129bb589e06a96e946eb4c 100644 (file)
@@ -917,7 +917,6 @@ function drop_index($table, $index, $continue=true, $feedback=true) {
         return true; //Index doesn't exist, nothing to do
     }
 
-
     if(!$sqlarr = $table->getDropIndexSQL($CFG->dbtype, $CFG->prefix, $index, false)) {
         return true; //Empty array = nothing to do = no error
     }
@@ -925,4 +924,52 @@ function drop_index($table, $index, $continue=true, $feedback=true) {
     return execute_sql_arr($sqlarr, $continue, $feedback);
 }
 
+/**
+ * This function will rename the index in the table passed as arguments
+ * Before renaming the index, the function will check it exists
+ *
+ * @uses $CFG, $db
+ * @param XMLDBTable table object (just the name is mandatory)
+ * @param XMLDBIndex index object (full specs are required)
+ * @param string new name of the index
+ * @param boolean continue to specify if must continue on error (true) or stop (false)
+ * @param boolean feedback to specify to show status info (true) or not (false)
+ * @return boolean true on success, false on error
+ */
+function rename_index($table, $index, $newname, $continue=true, $feedback=true) {
+
+    global $CFG, $db;
+
+    $status = true;
+
+    if (strtolower(get_class($table)) != 'xmldbtable') {
+        return false;
+    }
+    if (strtolower(get_class($index)) != 'xmldbindex') {
+        return false;
+    }
+
+/// Check index exists
+    if (!index_exists($table, $index)) {
+        debugging('Index ' . $index->getName() . ' do not exist. Skipping its renaming', DEBUG_DEVELOPER);
+        return true; //Index doesn't exist, nothing to do
+    }
+
+/// Check newname isn't empty
+    if (!$newname) {
+        debugging('New name for index ' . $index->getName() . ' is empty! Skipping its renaming', DEBUG_DEVELOPER);
+        return true; //Index doesn't exist, nothing to do
+    }
+
+/// Assign the new name to the index
+    $index->setName($newname);
+
+    if(!$sqlarr = $table->getRenameIndexSQL($CFG->dbtype, $CFG->prefix, $index, false)) {
+        debugging('Some DBs do not support index renaming (MySQL). Skipping its renaming', DEBUG_DEVELOPER);
+        return true; //Empty array = nothing to do = no error
+    }
+
+    return execute_sql_arr($sqlarr, $continue, $feedback);
+}
+
 ?>