]> git.mjollnir.org Git - moodle.git/commitdiff
Basic rename_field() function added.
authorstronk7 <stronk7>
Sun, 1 Oct 2006 23:02:39 +0000 (23:02 +0000)
committerstronk7 <stronk7>
Sun, 1 Oct 2006 23:02:39 +0000 (23:02 +0000)
TODO: Automatic sequence/trigger/check constraint recreation
      under PG, MSSQL & Oracle

lib/ddllib.php

index 5e7e2c6473c310c0a4ec2c0e752f8dac10c37d4c..6b0a8a418e05000bb909b39816ca71349d238110 100644 (file)
@@ -822,6 +822,50 @@ function change_field_default($table, $field, $continue=true, $feedback=true) {
     return execute_sql_arr($sqlarr, $continue, $feedback);
 }
 
+/**
+ * This function will rename the field in the table passed as arguments
+ * Before renaming the field, the function will check it exists
+ *
+ * @uses $CFG, $db
+ * @param XMLDBTable table object (just the name is mandatory)
+ * @param XMLDBField index object (full specs are required)
+ * @param string new name of the field
+ * @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_field($table, $field, $newname, $continue=true, $feedback=true) {
+
+    global $CFG, $db;
+
+    $status = true;
+
+    if (strtolower(get_class($table)) != 'xmldbtable') {
+        return false;
+    }
+    if (strtolower(get_class($field)) != 'xmldbfield') {
+        return false;
+    }
+
+/// Check field exists
+    if (!field_exists($table, $field)) {
+        debugging('Field ' . $field->getName() . ' do not exist. Rename skipped', DEBUG_DEVELOPER);
+        return true; //Field doesn't exist, nothing to do
+    }
+
+/// Check newname isn't empty
+    if (!$newname) {
+        debugging('New name for field ' . $field->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER);
+        return true; //Field doesn't exist, nothing to do
+    }
+
+    if(!$sqlarr = $table->getRenameFieldSQL($CFG->dbtype, $CFG->prefix, $field, $newname, false)) {
+        return true; //Empty array = nothing to do = no error
+    }
+
+    return execute_sql_arr($sqlarr, $continue, $feedback);
+}
+
 /**
  * This function will create the key in the table passed as arguments
  *