Implement DROP PRIMARY KEY statement
authormutec <florian@mysterycode.de>
Tue, 24 Nov 2015 22:45:40 +0000 (23:45 +0100)
committermutec <florian@mysterycode.de>
Tue, 24 Nov 2015 22:45:40 +0000 (23:45 +0100)
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

wcfsetup/install/files/lib/system/database/editor/DatabaseEditor.class.php
wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php
wcfsetup/install/files/lib/system/database/editor/PostgreSQLDatabaseEditor.class.php
wcfsetup/install/files/lib/system/database/util/SQLParser.class.php
wcfsetup/install/files/lib/system/package/PackageInstallationSQLParser.class.php

index 9e44eda6cb5c1bce7c4fcf410505d0f921bc09bc..24f8e94e53c548c9f2e415a177a1d90d5ea7aa18 100644 (file)
@@ -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.
index 6086e25a1bb80ded7aac4935c2c44d2e1192f72c..c0db46d2dc25bb95e012f99fd4bf30a93704f374 100644 (file)
@@ -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()
         */
index 9389b40eaf696e2f6433a1d26c3ef313e3f82c4d..2465525dba2d95f53127b70735cf4755265aeec4 100644 (file)
@@ -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()
         */
index c337055a5deb2b51f58799ba47ac0b7d573b14de..dfa7a03ec3ac0e9cf4f8b5fdd76480e6afbd1d97 100644 (file)
@@ -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.
         * 
index 268c7e53477bf6184d2a2fa755bfc9ba8740e823..ecf4784dbe1c18cdbdd649fc5ff7998eb7fb5232 100644 (file)
@@ -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()
         */