From: stronk7 Date: Sun, 1 Oct 2006 23:02:39 +0000 (+0000) Subject: Basic rename_field() function added. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a2851e5f55b456d71f865092b510661e761086eb;p=moodle.git Basic rename_field() function added. TODO: Automatic sequence/trigger/check constraint recreation under PG, MSSQL & Oracle --- diff --git a/lib/ddllib.php b/lib/ddllib.php index 5e7e2c6473..6b0a8a418e 100644 --- a/lib/ddllib.php +++ b/lib/ddllib.php @@ -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 *