From: stronk7 Date: Sat, 30 Sep 2006 18:23:00 +0000 (+0000) Subject: Adding support for index renaming. Note that MySQL doesn't support this X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=1c86ef5b1ceaea3411015b5033cd7b316b737dbc;p=moodle.git Adding support for index renaming. Note that MySQL doesn't support this but this shouldn't be important at all, mainly because, under xmldb, accesses to index-names are always done with find_index_name() and never explicity. Anyway, this function could be useful in the future to normalize index names in some Health center or another similar place. --- diff --git a/admin/xmldb/actions/test/test.class.php b/admin/xmldb/actions/test/test.class.php index 9de98e9750..ee16bf6ed0 100644 --- a/admin/xmldb/actions/test/test.class.php +++ b/admin/xmldb/actions/test/test.class.php @@ -645,7 +645,7 @@ class test extends XMLDBAction { $tests['delete enumlist from one field'] = $test; } - /// 36th test. Creating the default for one field + /// 36th test. Creating the enum for one field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; @@ -660,6 +660,23 @@ class test extends XMLDBAction { $tests['add enumlist to one field'] = $test; } + /// 37th test. Renaming one index + if ($test->status) { + /// Get SQL code and execute it + $test = new stdClass; + $index = new XMLDBIndex('newnamefortheindex'); + $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'course')); + + $test->sql = $table->getRenameIndexSQL($CFG->dbtype, $CFG->prefix, $index, true); + $db->debug = true; + $test->status = rename_index($table, $index, 'newnamefortheindex', false, false); + $db->debug = false; + if (!$test->status) { + $test->error = $db->ErrorMsg(); + } + $tests['rename index'] = $test; + } + diff --git a/lib/xmldb/classes/XMLDBTable.class.php b/lib/xmldb/classes/XMLDBTable.class.php index 11c9c1e0e2..917f112b76 100644 --- a/lib/xmldb/classes/XMLDBTable.class.php +++ b/lib/xmldb/classes/XMLDBTable.class.php @@ -998,6 +998,24 @@ class XMLDBTable extends XMLDBObject { } return $results; } + + /** + * This function will return the SQL code needed to rename one index from the table for the specified DB and + * prefix. Just one simple wrapper over generators. + */ + function getRenameIndexSQL ($dbtype, $prefix, $xmldb_index, $statement_end=true) { + + $results = array(); + + $classname = 'XMLDB' . $dbtype; + $generator = new $classname(); + $generator->setPrefix($prefix); + $results = $generator->getRenameIndexSQL($this, $xmldb_index); + 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 feae0b6ba0..a2a148932b 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -112,6 +112,9 @@ class XMLDBgenerator { var $drop_index_sql = 'DROP INDEX INDEXNAME'; //SQL sentence to drop one index //TABLENAME, INDEXNAME are dinamically replaced + var $rename_index_sql = 'ALTER INDEX OLDINDEXNAME RENAME TO NEWINDEXNAME'; //SQL sentence to rename one index + //TABLENAME, OLDINDEXNAME, NEWINDEXNAME are dinamically replaced + var $prefix; // Prefix to be used for all the DB objects var $reserved_words; // List of reserved words (in order to quote them properly) @@ -763,6 +766,29 @@ class XMLDBgenerator { return $results; } + /** + * Given one XMLDBTable and one XMLDBIndex, return the SQL statements needded to rename the index in the table + */ + + function getRenameIndexSQL($xmldb_table, $xmldb_index) { + + $results = array(); + + /// Get the real index name + $dbindexname = find_index_name($xmldb_table, $xmldb_index); + + /// Replace TABLENAME and INDEXNAME as needed + $renamesql = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->rename_index_sql); + $renamesql = str_replace('OLDINDEXNAME', $dbindexname, $renamesql); + $renamesql = str_replace('NEWINDEXNAME', $xmldb_index->getName(), $renamesql); + + /// Some DB doesn't support index renaming (MySQL) so this can be empty + if ($renamesql) { + $results[] = $renamesql; + } + + return $results; + } /** * Given three strings (table name, list of fields (comma separated) and suffix), create the proper object name diff --git a/lib/xmldb/classes/generators/mssql/mssql.class.php b/lib/xmldb/classes/generators/mssql/mssql.class.php index 761556cdb7..75083b6a00 100644 --- a/lib/xmldb/classes/generators/mssql/mssql.class.php +++ b/lib/xmldb/classes/generators/mssql/mssql.class.php @@ -56,6 +56,9 @@ class XMLDBmssql extends XMLDBgenerator { var $drop_index_sql = 'DROP INDEX TABLENAME.INDEXNAME'; //SQL sentence to drop one index //TABLENAME, INDEXNAME are dinamically replaced + var $rename_index_sql = "sp_rename 'OLDINDEXNAME', 'NEWINDEXNAME', 'INDEX'"; //SQL sentence to rename one index + //TABLENAME, OLDINDEXNAME, NEWINDEXNAME are dinamically replaced + /** * Creates one new XMLDBmssql */ diff --git a/lib/xmldb/classes/generators/mysql/mysql.class.php b/lib/xmldb/classes/generators/mysql/mysql.class.php index 99c7b1cc2c..d2ecb3e371 100644 --- a/lib/xmldb/classes/generators/mysql/mysql.class.php +++ b/lib/xmldb/classes/generators/mysql/mysql.class.php @@ -65,6 +65,9 @@ class XMLDBmysql extends XMLDBGenerator { var $drop_index_sql = 'ALTER TABLE TABLENAME DROP INDEX INDEXNAME'; //SQL sentence to drop one index //TABLENAME, INDEXNAME are dinamically replaced + var $rename_index_sql = null; //SQL sentence to rename one index (MySQL doesn't support this!) + //TABLENAME, OLDINDEXNAME, NEWINDEXNAME are dinamically replaced + /** * Creates one new XMLDBmysql */