]> git.mjollnir.org Git - moodle.git/commitdiff
Initial support for DROP TABLE adedd.
authorstronk7 <stronk7>
Thu, 7 Sep 2006 17:28:46 +0000 (17:28 +0000)
committerstronk7 <stronk7>
Thu, 7 Sep 2006 17:28:46 +0000 (17:28 +0000)
lib/ddllib.php
lib/xmldb/classes/XMLDBTable.class.php
lib/xmldb/classes/generators/XMLDBGenerator.class.php

index b05a6a64dc95eb3b8e210f45264a13a98d85a89b..249ca42469b1604ebbcca0b72ed6c191e659cd76 100644 (file)
@@ -299,4 +299,29 @@ function create_table($table) {
     return execute_sql_arr($sqlarr);
 }
 
+/**
+ * This function will drop the table passed as argument
+ * and all the associated objects (keys, indexes, constaints, sequences, triggers)
+ * will be dropped too.
+ *
+ * @param XMLDBtable table object containing the basic table info
+ * @return boolean true on success, false on error
+ */
+function drop_table($table) {
+
+    global $CFG, $db;
+
+    $status = true;
+
+    if (strtolower(get_class($table)) != 'xmldbtable') {
+        return false;
+    }
+
+    if(!$sqlarr = $table->getDropTableSQL($CFG->dbtype, $CFG->prefix, false)) {
+        return false;
+    }
+
+    return execute_sql_arr($sqlarr);
+}
+
 ?>
index 9cc67d1d022ba3f7501ba14528886efead7bc2b2..2ea9f5e6b464b90a5f0a0af45fa3c5cd65985ef1 100644 (file)
@@ -800,6 +800,24 @@ class XMLDBTable extends XMLDBObject {
         }
         return $results;
     }
+
+    /** 
+     * This function will return the SQL code needed to drop the table for the specified DB and
+     * prefix. Just one simple wrapper over generators.
+     */
+    function getDropTableSQL ($dbtype, $prefix, $statement_end=true) {
+
+        $results = array();
+
+        $classname = 'XMLDB' . $dbtype;
+        $generator = new $classname();
+        $generator->setPrefix($prefix);
+        $results = $generator->getDropTableSQL($this);
+        if ($statement_end) {
+            $results = $generator->getEndedStatements($results);
+        }
+        return $results;
+    }
 }
 
 ?>
index 6a2492578fbefc599b334ed06e82de3ee09ab102..7404c1e8daa4f142db15a4b79d0d19bbae9f7eb4 100644 (file)
@@ -81,7 +81,12 @@ class XMLDBgenerator {
     var $rename_table_sql = 'ALTER TABLE OLDNAME RENAME TO NEWNAME'; //SQL sentence to rename one table, both
                                   //OLDNAME and NEWNAME are dinamically replaced
 
-    var $rename_table_extra_code = false; //Does the generatos need to add code after table renaming
+    var $rename_table_extra_code = false; //Does the generatos need to add code after table rename
+
+    var $drop_table_sql = 'DROP TABLE TABLENAME'; //SQL sentence to drop one table
+                                  //TABLENAME is dinamically replaced
+
+    var $drop_table_extra_code = false; //Does the generatos need to add code after table drop
 
     var $prefix;         // Prefix to be used for all the DB objects
 
@@ -412,12 +417,37 @@ class XMLDBgenerator {
 
         $results = array();  //Array where all the sentences will be stored
 
-        $rename = str_replace('OLDNAME', $this->getEncQuoted($this->prefix . $xmldb_table->getName), $rename_table_sql);
+        $rename = str_replace('OLDNAME', $this->getEncQuoted($this->prefix . $xmldb_table->getName()), $this->rename_table_sql);
         $rename = str_replace('NEWNAME', $this->getEncQuoted($this->prefix . $newname), $rename_table_sql);
 
         $results[] = $rename;
 
     /// TODO, call to getRenameTableExtraSQL() if $rename_table_extra_code is enabled. It will add sequence regeneration code.
+        if ($this->rename_table_extra_code) {
+            $extra_sentences = getDropTableExtraSQL();
+            $results = array_merge($results, $extra_sentences);
+        }
+
+        return $results;
+    }
+
+    /**
+     * Given one correct XMLDBTable and the new name, returns the SQL statements
+     * to drop it (inside one array)
+     */ 
+    function getDropTableSQL($xmldb_table) {
+
+        $results = array();  //Array where all the sentences will be stored
+
+        $rename = str_replace('TABLENAME', $this->getEncQuoted($this->prefix . $xmldb_table->getName()), $this->drop_table_sql);
+
+        $results[] = $rename;
+
+    /// TODO, call to getDropTableExtraSQL() if $rename_table_extra_code is enabled. It will add sequence/trigger drop code.
+        if ($this->drop_table_extra_code) {
+            $extra_sentences = getDropTableExtraSQL();
+            $results = array_merge($results, $extra_sentences);
+        }
 
         return $results;
     }
@@ -643,7 +673,14 @@ class XMLDBgenerator {
      * Returns the code (array of statements) needed to execute extra statements on table rename
      */
     function getRenameTableExtraSQL ($xmldb_table) {
-        return 'Code for table comment goes to getCommentSQL(). Can be disabled with add_table_comments=false;';
+        return 'Code for table rename goes to getRenameTableExtraSQL(). Can be disabled with rename_table_extra_code=false;';
+    }
+
+    /**
+     * Returns the code (array of statements) needed to execute extra statements on table drop
+     */
+    function getDropTableExtraSQL ($xmldb_table) {
+        return 'Code for table drop goes to getDropTableExtraSQL(). Can be disabled with drop_table_extra_code=false;';
     }
 
     /**