diff --git a/lib/ddllib.php b/lib/ddllib.php index b05a6a64dc9..249ca42469b 100644 --- a/lib/ddllib.php +++ b/lib/ddllib.php @@ -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); +} + ?> diff --git a/lib/xmldb/classes/XMLDBTable.class.php b/lib/xmldb/classes/XMLDBTable.class.php index 9cc67d1d022..2ea9f5e6b46 100644 --- a/lib/xmldb/classes/XMLDBTable.class.php +++ b/lib/xmldb/classes/XMLDBTable.class.php @@ -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; + } } ?> diff --git a/lib/xmldb/classes/generators/XMLDBGenerator.class.php b/lib/xmldb/classes/generators/XMLDBGenerator.class.php index 6a2492578fb..7404c1e8daa 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -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;'; } /**