]> git.mjollnir.org Git - moodle.git/commitdiff
Added rename_field() support.
authorstronk7 <stronk7>
Sun, 1 Oct 2006 22:54:51 +0000 (22:54 +0000)
committerstronk7 <stronk7>
Sun, 1 Oct 2006 22:54:51 +0000 (22:54 +0000)
TODO: Sync sequences, triggers and check constraints

lib/xmldb/classes/generators/XMLDBGenerator.class.php

index 36f8980300800cab26e5fc7abb9a3831f67fdde0..2a6959adb01137e55e9ba554196f7379fd701280 100644 (file)
@@ -110,6 +110,11 @@ class XMLDBgenerator {
 
     var $alter_column_skip_notnull = false; //The generator will skip the null/notnull clause on alter columns
 
+    var $rename_column_sql = 'ALTER TABLE TABLENAME RENAME COLUMN OLDFIELDNAME TO NEWFIELDNAME';
+                                  ///TABLENAME, OLDFIELDNAME and NEWFIELDNAME are dianmically replaced
+
+    var $rename_column_extra_code = false; //Does the generator need to add code after column rename
+
     var $drop_index_sql = 'DROP INDEX INDEXNAME'; //SQL sentence to drop one index
                                   //TABLENAME, INDEXNAME are dinamically replaced
 
@@ -642,6 +647,29 @@ class XMLDBgenerator {
         return $results;
     }
 
+    /**
+     * Given one correct XMLDBField and the new name, returns the SQL statements
+     * to rename it (inside one array)
+     */
+    function getRenameFieldSQL($xmldb_table, $xmldb_field, $newname) {
+
+        $results = array();  //Array where all the sentences will be stored
+
+        $rename = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->rename_column_sql);
+        $rename = str_replace('OLDFIELDNAME', $xmldb_field->getName(), $rename);
+        $rename = str_replace('NEWFIELDNAME', $newname, $rename);
+
+        $results[] = $rename;
+
+    /// Call to getRenameFieldExtraSQL() if $rename_column_extra_code is enabled (will add some required sentences)
+        if ($this->rename_column_extra_code) {
+            $extra_sentences = $this->getRenameFieldExtraSQL($xmldb_table, $xmldb_field, $newname);
+            $results = array_merge($results, $extra_sentences);
+        }
+
+        return $results;
+    }
+
     /**
      * Given one XMLDBTable and one XMLDBKey, return the SQL statements needded to add the key to the table
      * note that undelying indexes will be added as parametrised by $xxxx_keys and $xxxx_index parameters
@@ -1041,6 +1069,13 @@ class XMLDBgenerator {
         return 'Code for extra enum SQL goes to getEnumExtraSQL(). Can be disabled with enum_extra_code=false';
     }
 
+    /**
+     * Returns the code (array of statements) needed to execute extra statements on field rename
+     */
+    function getRenameFieldExtraSQL ($xmldb_table, $xmldb_field) {
+        return array('Code for field rename goes to getRenameFieldExtraSQL(). Can be disabled with rename_column_extra_code=false;');
+    }
+
     /**
      * Returns the code (array of statements) needed
      * to create one sequence for the xmldb_table and xmldb_field passes