[#8650] Add support for MySQL's ALTER TABLE .. DROP PRIMARY KEY syntax

This commit is contained in:
Lukas Eder 2019-05-20 10:25:34 +02:00
parent ebce61b0fc
commit 159a2bbb26
4 changed files with 74 additions and 3 deletions

View File

@ -586,7 +586,14 @@ public interface AlterTableStep {
AlterTableFinalStep dropConstraint(String constraint);
/**
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
* Add a <code>DROP PRIMARY KEY</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
@Support({ MARIADB, MYSQL })
AlterTableFinalStep dropPrimaryKey();
/**
* Add a <code>DROP FOREIGN KEY</code> clause to the <code>ALTER TABLE</code>
* statement.
*
* @see DSL#constraint(String)
@ -595,7 +602,7 @@ public interface AlterTableStep {
AlterTableFinalStep dropForeignKey(Constraint constraint);
/**
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
* Add a <code>DROP FOREIGN KEY</code> clause to the <code>ALTER TABLE</code>
* statement.
*
* @see DSL#constraint(String)
@ -604,7 +611,7 @@ public interface AlterTableStep {
AlterTableFinalStep dropForeignKey(Name constraint);
/**
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
* Add a <code>DROP FOREIGN KEY</code> clause to the <code>ALTER TABLE</code>
* statement.
*
* @see DSL#constraint(String)

View File

@ -71,6 +71,7 @@ import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
import static org.jooq.impl.ConstraintType.FOREIGN_KEY;
import static org.jooq.impl.ConstraintType.PRIMARY_KEY;
import static org.jooq.impl.DSL.commentOnTable;
import static org.jooq.impl.DSL.constraint;
import static org.jooq.impl.DSL.field;
@ -104,6 +105,7 @@ import static org.jooq.impl.Keywords.K_LIKE;
import static org.jooq.impl.Keywords.K_MODIFY;
import static org.jooq.impl.Keywords.K_NOT_NULL;
import static org.jooq.impl.Keywords.K_NULL;
import static org.jooq.impl.Keywords.K_PRIMARY_KEY;
import static org.jooq.impl.Keywords.K_RAISE;
import static org.jooq.impl.Keywords.K_RENAME;
import static org.jooq.impl.Keywords.K_RENAME_COLUMN;
@ -686,6 +688,12 @@ final class AlterTableImpl extends AbstractRowCountQuery implements
return dropConstraint(DSL.constraint(constraint));
}
@Override
public final AlterTableImpl dropPrimaryKey() {
dropConstraintType = PRIMARY_KEY;
return this;
}
@Override
public final AlterTableImpl dropForeignKey(Constraint constraint) {
dropConstraint = constraint;
@ -1271,6 +1279,11 @@ final class AlterTableImpl extends AbstractRowCountQuery implements
ctx.data().remove(DATA_CONSTRAINT_REFERENCE);
ctx.end(ALTER_TABLE_DROP);
}
else if (dropConstraintType == PRIMARY_KEY) {
ctx.start(ALTER_TABLE_DROP);
ctx.visit(K_DROP).sql(' ').visit(K_PRIMARY_KEY);
ctx.end(ALTER_TABLE_DROP);
}
if (!omitAlterTable)
ctx.formatIndentEnd();

View File

@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
/**
* @author Lukas Eder
*/
enum ConstraintType {
PRIMARY_KEY,
UNIQUE,
FOREIGN_KEY,
CHECK
}

View File

@ -3611,6 +3611,9 @@ final class ParserImpl implements Parser {
if (parseKeywordIf(ctx, "CONSTRAINT")) {
return s1.dropConstraint(parseIdentifier(ctx));
}
else if (parseKeywordIf(ctx, "PRIMARY KEY")) {
return s1.dropPrimaryKey();
}
else if (parseKeywordIf(ctx, "FOREIGN KEY")) {
return s1.dropForeignKey(parseIdentifier(ctx));
}