From 42a1e71f858bfb4777ee3438109b993e39d52a7b Mon Sep 17 00:00:00 2001 From: mutec Date: Tue, 24 Nov 2015 23:45:40 +0100 Subject: [PATCH] Implement DROP PRIMARY KEY statement implemented the possibility to drop primary keys of a table with "ALTER TABLE table_name DROP PRIMARY KEY;" not sure how it works with PostgreSQL, someone might have a look before merging please --- .../database/editor/DatabaseEditor.class.php | 11 ++++++++-- .../editor/MySQLDatabaseEditor.class.php | 9 +++++++++ .../editor/PostgreSQLDatabaseEditor.class.php | 9 +++++++++ .../system/database/util/SQLParser.class.php | 14 +++++++++++++ .../PackageInstallationSQLParser.class.php | 20 +++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php b/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php index 9e44eda6cb..24f8e94e53 100644 --- a/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php @@ -111,14 +111,21 @@ abstract class DatabaseEditor { * @param array $indexData */ abstract public function addForeignKey($tableName, $indexName, $indexData); - + /** * Drops an existing index. - * + * * @param string $tableName * @param string $indexName */ abstract public function dropIndex($tableName, $indexName); + + /** + * Drops existing primary keys. + * + * @param string $tableName + */ + abstract public function dropPrimaryKey($tableName); /** * Drops an existing foreign key. diff --git a/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php b/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php index 6086e25a1b..c0db46d2dc 100644 --- a/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php @@ -180,6 +180,15 @@ class MySQLDatabaseEditor extends DatabaseEditor { $statement->execute(); } + /** + * @see \wcf\system\database\editor\DatabaseEditor::dropPrimaryKey() + */ + public function dropPrimaryKey($tableName) { + $sql = "ALTER TABLE ".$tableName." DROP PRIMARY KEY"; + $statement = $this->dbObj->prepareStatement($sql); + $statement->execute(); + } + /** * @see \wcf\system\database\editor\DatabaseEditor::dropForeignKey() */ diff --git a/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php b/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php index 9389b40eaf..2465525dba 100644 --- a/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php @@ -317,6 +317,15 @@ class PostgreSQLDatabaseEditor extends DatabaseEditor { $statement->execute(); } + /** + * @see \wcf\system\database\editor\DatabaseEditor::dropPrimaryKey() + */ + public function dropPrimaryKey($tableName) { + $sql = "ALTER TABLE ".$tableName." DROP CONSTRAINT ".$indexName." CASCADE"; + $statement = $this->dbObj->prepareStatement($sql); + $statement->execute(); + } + /** * @see \wcf\system\database\editor\DatabaseEditor::dropForeignKey() */ diff --git a/wcfsetup/install/files/lib/system/database/util/SQLParser.class.php b/wcfsetup/install/files/lib/system/database/util/SQLParser.class.php index c337055a5d..dfa7a03ec3 100644 --- a/wcfsetup/install/files/lib/system/database/util/SQLParser.class.php +++ b/wcfsetup/install/files/lib/system/database/util/SQLParser.class.php @@ -163,6 +163,10 @@ class SQLParser { else if (preg_match('~^ALTER\s+TABLE\s+(\w+)\s+DROP\s+(?:INDEX|KEY)\s+(\w+)~is', $query, $match)) { $this->executeDropIndexStatement($match[1], $match[2]); } + // drop primary key + else if (preg_match('~^ALTER\s+TABLE\s+(\w+)\s+DROP\s+PRIMARY\s+KEY~is', $query, $match)) { + $this->executeDropPrimaryKeyStatement($match[1]); + } // drop foreign key else if (preg_match('~^ALTER\s+TABLE\s+(\w+)\s+DROP\s+FOREIGN KEY\s+(\w+)~is', $query, $match)) { $this->executeDropForeignKeyStatement($match[1], self::getGenericIndexName($match[1], $match[2], 'fk')); @@ -288,6 +292,16 @@ class SQLParser { WCF::getDB()->getEditor()->dropIndex($tableName, $indexName); } + /** + * Executes a 'DROP PRIMARY KEY' statement. + * + * @param string $tableName + * @param string $primaryKeyName + */ + protected function executeDropPrimaryKeyStatement($tableName) { + WCF::getDB()->getEditor()->dropPrimaryKey($tableName); + } + /** * Executes a 'DROP FOREIGN KEY' statement. * diff --git a/wcfsetup/install/files/lib/system/package/PackageInstallationSQLParser.class.php b/wcfsetup/install/files/lib/system/package/PackageInstallationSQLParser.class.php index 268c7e5347..ecf4784dbe 100644 --- a/wcfsetup/install/files/lib/system/package/PackageInstallationSQLParser.class.php +++ b/wcfsetup/install/files/lib/system/package/PackageInstallationSQLParser.class.php @@ -383,6 +383,26 @@ class PackageInstallationSQLParser extends SQLParser { } } + /** + * @see \wcf\system\database\util\SQLParser::executeDropPrimaryKeyStatement() + */ + protected function executeDropPrimaryKeyStatement($tableName) { + if ($this->test) { + if ($ownerPackageID = $this->getIndexOwnerID($tableName, '')) { + if ($ownerPackageID != $this->package->packageID) { + throw new SystemException("Cannot drop primary key from '".$tableName."'. A package can only drop own indices."); + } + } + } + else { + // log + $this->indexLog[] = array('tableName' => $tableName, 'indexName' => '', 'packageID' => $this->package->packageID, 'action' => 'delete'); + + // execute + parent::executeDropPrimaryKeyStatement($tableName); + } + } + /** * @see \wcf\system\database\util\SQLParser::executeDropForeignKeyStatement() */ -- 2.20.1