]> git.mjollnir.org Git - moodle.git/commitdiff
Initial commit of rename_table(). Not ended!
authorstronk7 <stronk7>
Sat, 30 Sep 2006 22:40:15 +0000 (22:40 +0000)
committerstronk7 <stronk7>
Sat, 30 Sep 2006 22:40:15 +0000 (22:40 +0000)
admin/xmldb/actions/test/test.class.php
lib/ddllib.php
lib/xmldb/classes/generators/XMLDBGenerator.class.php
lib/xmldb/classes/generators/mssql/mssql.class.php

index c89b5ba01b7c58bf7c2c436bc9ec06130153b5ee..8a886cf87b3abf60c34a247925541bcb81d37fd4 100644 (file)
@@ -690,6 +690,36 @@ class test extends XMLDBAction {
             $tests['rename key (experimental. DO NOT USE IT)'] = $test;
         }
 
+    /// 39th test. Renaming one field
+        if ($test->status && 1==2) {
+        /// Get SQL code and execute it
+            $test = new stdClass;
+            $field = new XMLDBField('type');
+            $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
+            
+            $test->sql = $table->getRenameFieldSQL($CFG->dbtype, $CFG->prefix, $field, 'newnameforthefield', true);
+            $test->status = rename_field($table, $field, 'newnameforthefield', false, false);
+            if (!$test->status) {
+                $test->error = $db->ErrorMsg();
+            }
+            $tests['rename field'] = $test;
+        }
+
+    /// 40th test. Renaming one table
+        if ($test->status) {
+        /// Get SQL code and execute it
+            $test = new stdClass;
+            
+            $test->sql = $table->getRenameTableSQL($CFG->dbtype, $CFG->prefix, 'newnameforthetable', true);
+            $db->debug = true;
+            $test->status = rename_table($table, 'newnameforthetable', false, false);
+            $db->debug = false;
+            if (!$test->status) {
+                $test->error = $db->ErrorMsg();
+            }
+            $tests['rename table'] = $test;
+        }
+
 
 
     /// TODO: Check here values of the inserted records to see that everything ha the correct value
index 600dab92374ba8cccd705e0ee36090f514807d43..9d035969a5f865c54c405b7cce88b38a5c8046d7 100644 (file)
@@ -527,7 +527,7 @@ function create_table($table, $continue=true, $feedback=true) {
 
 /// Check table doesn't exist
     if (table_exists($table)) {
-        debugging('Table ' . $table->getName() . ' exists. Skipping its creation', DEBUG_DEVELOPER);
+        debugging('Table ' . $table->getName() . ' exists. Create skipped', DEBUG_DEVELOPER);
         return true; //Table exists, nothing to do
     }
 
@@ -561,7 +561,7 @@ function drop_table($table, $continue=true, $feedback=true) {
 
 /// Check table exists
     if (!table_exists($table)) {
-        debugging('Table ' . $table->getName() . ' don not exist. Skipping its deletion', DEBUG_DEVELOPER);
+        debugging('Table ' . $table->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
         return true; //Table don't exist, nothing to do
     }
 
@@ -572,6 +572,46 @@ function drop_table($table, $continue=true, $feedback=true) {
     return execute_sql_arr($sqlarr, $continue, $feedback);
 }
 
+/**
+ * This function will rename the table passed as argument
+ * Before renaming the index, the function will check it exists
+ *
+ * @uses $CFG, $db
+ * @param XMLDBTable table object (just the name is mandatory)
+ * @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_table($table, $newname, $continue=true, $feedback=true) {
+
+    global $CFG, $db;
+
+    $status = true;
+
+    if (strtolower(get_class($table)) != 'xmldbtable') {
+        return false;
+    }
+
+/// Check table exists
+    if (!table_exists($table)) {
+        debugging('Table ' . $table->getName() . ' do not exist. Rename skipped', DEBUG_DEVELOPER);
+        return true; //Table doesn't exist, nothing to do
+    }
+
+/// Check newname isn't empty
+    if (!$newname) {
+        debugging('New name for table ' . $index->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER);
+        return true; //Table doesn't exist, nothing to do
+    }
+
+    if(!$sqlarr = $table->getRenameTableSQL($CFG->dbtype, $CFG->prefix, $newname, false)) {
+        return true; //Empty array = nothing to do = no error
+    }
+
+    return execute_sql_arr($sqlarr, $continue, $feedback);
+}
+
 /**
  * This function will add the field to the table passed as arguments
  *
@@ -597,7 +637,7 @@ function add_field($table, $field, $continue=true, $feedback=true) {
 
 /// Check the field doesn't exist
     if (field_exists($table, $field)) {
-        debugging('Field ' . $field->getName() . ' exists. Skipping its creation', DEBUG_DEVELOPER);
+        debugging('Field ' . $field->getName() . ' exists. Create skipped', DEBUG_DEVELOPER);
         return true;
     }
 
@@ -633,7 +673,7 @@ function drop_field($table, $field, $continue=true, $feedback=true) {
 
 /// Check the field exists
     if (!field_exists($table, $field)) {
-        debugging('Field ' . $field->getName() . ' do not exist. Skipping its deletion', DEBUG_DEVELOPER);
+        debugging('Field ' . $field->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
         return true;
     }
 
@@ -805,7 +845,7 @@ function add_key($table, $key, $continue=true, $feedback=true) {
         return false;
     }
     if ($key->getType() == XMLDB_KEY_PRIMARY) { // Prevent PRIMARY to be added (only in create table, being serious  :-P)
-        debugging('Primary Keys can be added at table creation time only', DEBUG_DEVELOPER);
+        debugging('Primary Keys can be added at table create time only', DEBUG_DEVELOPER);
         return true;
     }
 
@@ -879,7 +919,7 @@ function rename_key($table, $key, $newname, $continue=true, $feedback=true) {
 
 /// Check newname isn't empty
     if (!$newname) {
-        debugging('New name for key ' . $key->getName() . ' is empty! Skipping its renaming', DEBUG_DEVELOPER);
+        debugging('New name for key ' . $key->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER);
         return true; //Key doesn't exist, nothing to do
     }
 
@@ -887,7 +927,7 @@ function rename_key($table, $key, $newname, $continue=true, $feedback=true) {
     $key->setName($newname);
 
     if(!$sqlarr = $table->getRenameKeySQL($CFG->dbtype, $CFG->prefix, $key, false)) {
-        debugging('Some DBs do not support key renaming (MySQL, PostgreSQL, MsSQL). Skipping its renaming', DEBUG_DEVELOPER);
+        debugging('Some DBs do not support key renaming (MySQL, PostgreSQL, MsSQL). Rename skipped', DEBUG_DEVELOPER);
         return true; //Empty array = nothing to do = no error
     }
 
@@ -920,7 +960,7 @@ function add_index($table, $index, $continue=true, $feedback=true) {
 
 /// Check index doesn't exist
     if (index_exists($table, $index)) {
-        debugging('Index ' . $index->getName() . ' exists. Skipping its creation', DEBUG_DEVELOPER);
+        debugging('Index ' . $index->getName() . ' exists. Create skipped', DEBUG_DEVELOPER);
         return true; //Index exists, nothing to do
     }
 
@@ -957,7 +997,7 @@ function drop_index($table, $index, $continue=true, $feedback=true) {
 
 /// Check index exists
     if (!index_exists($table, $index)) {
-        debugging('Index ' . $index->getName() . ' do not exist. Skipping its deletion', DEBUG_DEVELOPER);
+        debugging('Index ' . $index->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
         return true; //Index doesn't exist, nothing to do
     }
 
@@ -998,13 +1038,13 @@ function rename_index($table, $index, $newname, $continue=true, $feedback=true)
 
 /// Check index exists
     if (!index_exists($table, $index)) {
-        debugging('Index ' . $index->getName() . ' do not exist. Skipping its renaming', DEBUG_DEVELOPER);
+        debugging('Index ' . $index->getName() . ' do not exist. Rename skipped', 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);
+        debugging('New name for index ' . $index->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER);
         return true; //Index doesn't exist, nothing to do
     }
 
@@ -1012,7 +1052,7 @@ function rename_index($table, $index, $newname, $continue=true, $feedback=true)
     $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);
+        debugging('Some DBs do not support index renaming (MySQL). Rename skipped', DEBUG_DEVELOPER);
         return true; //Empty array = nothing to do = no error
     }
 
index 69ca4fdec7e5684c3acb1cb3fe219c67f0e00b1b..053016b3bc40cb170580ea1774efa055353d1963 100644 (file)
@@ -91,6 +91,7 @@ class XMLDBgenerator {
 
     var $concat_character = '||'; //Characters to be used as concatenation operator. If not defined
                                   //MySQL CONCAT function will be used
+
     var $rename_table_sql = 'ALTER TABLE OLDNAME RENAME TO NEWNAME'; //SQL sentence to rename one table, both
                                   //OLDNAME and NEWNAME are dinamically replaced
 
@@ -471,9 +472,6 @@ class XMLDBgenerator {
 
         $newt = new XMLDBTable($newname); //Temporal table for name calculations
 
-        $oldtablename = $this->getTableName($xmldb_table);
-        $newtablename = $this->getTableName($newt);
-
         $rename = str_replace('OLDNAME', $this->getTableName($xmldb_table), $this->rename_table_sql);
         $rename = str_replace('NEWNAME', $this->getTableName($newt), $rename);
 
index b427f4f588ebefffbbe8ed1eaadca5374e5b0f4a..92e48aa7356f362e6160b3a4ee94832e0c98e6b2 100644 (file)
@@ -53,6 +53,9 @@ class XMLDBmssql extends XMLDBgenerator {
     var $concat_character = '+'; //Characters to be used as concatenation operator. If not defined
                                   //MySQL CONCAT function will be use
 
+    var $rename_table_sql = "sp_rename 'OLDNAME', 'NEWNAME'"; //SQL sentence to rename one table, both
+                                  //OLDNAME and NEWNAME are dinamically replaced
+
     var $drop_index_sql = 'DROP INDEX TABLENAME.INDEXNAME'; //SQL sentence to drop one index
                                                                //TABLENAME, INDEXNAME are dinamically replaced