[#4044] Add support for ALTER TABLE DROP CONSTRAINT statements
This commit is contained in:
parent
f5f70565ef
commit
2d4791e2ab
@ -58,6 +58,8 @@ import static org.jooq.SQLDialect.POSTGRES;
|
||||
// ...
|
||||
// ...
|
||||
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
|
||||
/**
|
||||
* The step in the <code>ALTER TABLE</code> where the action can be decided.
|
||||
@ -123,6 +125,13 @@ public interface AlterTableStep {
|
||||
@Support
|
||||
<T> AlterTableFinalStep addColumn(Field<T> field, DataType<T> type);
|
||||
|
||||
/**
|
||||
* Add an <code>ADD CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support
|
||||
AlterTableFinalStep addConstraint(Constraint constraint);
|
||||
|
||||
/**
|
||||
* Add an <code>ADD COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
@ -161,4 +170,20 @@ public interface AlterTableStep {
|
||||
*/
|
||||
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
|
||||
AlterTableDropStep dropColumn(String field);
|
||||
|
||||
/**
|
||||
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support
|
||||
AlterTableFinalStep drop(Constraint constraint);
|
||||
|
||||
/**
|
||||
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*
|
||||
* @see DSL#constraint(String)
|
||||
*/
|
||||
@Support
|
||||
AlterTableFinalStep dropConstraint(String constraint);
|
||||
}
|
||||
|
||||
@ -48,6 +48,12 @@ package org.jooq;
|
||||
*/
|
||||
public enum Clause {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Clauses used in a any type of statement to model constraint references
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
CONSTRAINT,
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Clauses used in a any type of statement to model catalog references
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
50
jOOQ/src/main/java/org/jooq/Constraint.java
Normal file
50
jOOQ/src/main/java/org/jooq/Constraint.java
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* 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.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
/**
|
||||
* A DDL constraint.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface Constraint extends QueryPart {
|
||||
|
||||
}
|
||||
51
jOOQ/src/main/java/org/jooq/ConstraintTypeStep.java
Normal file
51
jOOQ/src/main/java/org/jooq/ConstraintTypeStep.java
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* 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.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
/**
|
||||
* The step in the {@link Constraint} construction DSL API that allows for
|
||||
* specifying the constraint type.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface ConstraintTypeStep extends Constraint {
|
||||
|
||||
}
|
||||
@ -53,6 +53,7 @@ import static org.jooq.impl.DSL.field;
|
||||
import static org.jooq.impl.DSL.inline;
|
||||
import static org.jooq.impl.DSL.name;
|
||||
import static org.jooq.impl.DSL.sql;
|
||||
import static org.jooq.impl.Utils.DATA_DROP_CONSTRAINT;
|
||||
import static org.jooq.impl.Utils.toSQLDDLTypeDeclaration;
|
||||
|
||||
import org.jooq.AlterTableAlterStep;
|
||||
@ -61,6 +62,7 @@ import org.jooq.AlterTableFinalStep;
|
||||
import org.jooq.AlterTableStep;
|
||||
import org.jooq.Clause;
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.Constraint;
|
||||
import org.jooq.Context;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.DataType;
|
||||
@ -86,13 +88,15 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
private static final Clause[] CLAUSES = { ALTER_TABLE };
|
||||
|
||||
private final Table<?> table;
|
||||
private Field<?> add;
|
||||
private DataType<?> addType;
|
||||
private Field<?> alter;
|
||||
private DataType<?> alterType;
|
||||
private Field<?> alterDefault;
|
||||
private Field<?> drop;
|
||||
private boolean dropCascade;
|
||||
private Field<?> addColumn;
|
||||
private DataType<?> addColumnType;
|
||||
private Constraint addConstraint;
|
||||
private Field<?> alterColumn;
|
||||
private DataType<?> alterColumnType;
|
||||
private Field<?> alterColumnDefault;
|
||||
private Field<?> dropColumn;
|
||||
private boolean dropColumnCascade;
|
||||
private Constraint dropConstraint;
|
||||
|
||||
AlterTableImpl(Configuration configuration, Table<?> table) {
|
||||
super(configuration);
|
||||
@ -121,8 +125,14 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
@Override
|
||||
public final <T> AlterTableImpl addColumn(Field<T> field, DataType<T> type) {
|
||||
add = field;
|
||||
addType = type;
|
||||
addColumn = field;
|
||||
addColumnType = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl addConstraint(Constraint constraint) {
|
||||
addConstraint = constraint;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -143,13 +153,13 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
@Override
|
||||
public final <T> AlterTableImpl alterColumn(Field<T> field) {
|
||||
alter = field;
|
||||
alterColumn = field;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl set(DataType type) {
|
||||
alterType = type;
|
||||
alterColumnType = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -160,7 +170,7 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl defaultValue(Field expression) {
|
||||
alterDefault = expression;
|
||||
alterColumnDefault = expression;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -181,19 +191,30 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumn(Field<?> field) {
|
||||
drop = field;
|
||||
dropColumn = field;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl drop(Constraint constraint) {
|
||||
dropConstraint = constraint;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropConstraint(String constraint) {
|
||||
return drop(DSL.constraint(constraint));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableFinalStep cascade() {
|
||||
dropCascade = true;
|
||||
dropColumnCascade = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableFinalStep restrict() {
|
||||
dropCascade = false;
|
||||
dropColumnCascade = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -207,7 +228,7 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
/* [pro] xx
|
||||
xx xxx xxxxxx xxxxxxx xxxxxx xxxxx xx xxxxx xxxxxxx xxxxxxx xxx xx xxx xxx x xxxxx xxxxxx xx xx xxxx
|
||||
xx xxxxxxx xx xxxxxxxxx xx xxxxxxxxxxxx xx xxxxx x
|
||||
xx xxxxxxx xx xxxxxxxxx xx xxxxxxxxxxxxxxxxxx xx xxxxx x
|
||||
xxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
x
|
||||
xxxx
|
||||
@ -224,7 +245,7 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
.keyword("alter table").sql(" ").visit(table)
|
||||
.end(ALTER_TABLE_TABLE);
|
||||
|
||||
if (add != null) {
|
||||
if (addColumn != null) {
|
||||
ctx.start(ALTER_TABLE_ADD)
|
||||
.sql(" ").keyword("add").sql(" ");
|
||||
|
||||
@ -234,12 +255,12 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
xx [/pro] */
|
||||
|
||||
ctx.qualify(false)
|
||||
.visit(add).sql(" ")
|
||||
.visit(addColumn).sql(" ")
|
||||
.qualify(true);
|
||||
|
||||
toSQLDDLTypeDeclaration(ctx, addType);
|
||||
toSQLDDLTypeDeclaration(ctx, addColumnType);
|
||||
|
||||
if (!addType.nullable()) {
|
||||
if (!addColumnType.nullable()) {
|
||||
ctx.sql(" ").keyword("not null");
|
||||
}
|
||||
|
||||
@ -256,7 +277,12 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
ctx.end(ALTER_TABLE_ADD);
|
||||
}
|
||||
else if (alter != null) {
|
||||
|
||||
// TODO [#3338] Implement adding of constraints.
|
||||
else if (addConstraint != null) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
else if (alterColumn != null) {
|
||||
ctx.start(ALTER_TABLE_ALTER);
|
||||
|
||||
switch (family) {
|
||||
@ -275,7 +301,7 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
case MYSQL:
|
||||
// MySQL's CHANGE COLUMN clause has a mandatory RENAMING syntax...
|
||||
ctx.sql(" ").keyword("change column")
|
||||
.sql(" ").qualify(false).visit(alter).qualify(true);
|
||||
.sql(" ").qualify(false).visit(alterColumn).qualify(true);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -285,10 +311,10 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
ctx.sql(" ")
|
||||
.qualify(false)
|
||||
.visit(alter)
|
||||
.visit(alterColumn)
|
||||
.qualify(true);
|
||||
|
||||
if (alterType != null) {
|
||||
if (alterColumnType != null) {
|
||||
switch (family) {
|
||||
/* [pro] xx
|
||||
xxxx xxxx
|
||||
@ -302,13 +328,13 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
}
|
||||
|
||||
ctx.sql(" ");
|
||||
toSQLDDLTypeDeclaration(ctx, alterType);
|
||||
toSQLDDLTypeDeclaration(ctx, alterColumnType);
|
||||
|
||||
if (!alterType.nullable()) {
|
||||
if (!alterColumnType.nullable()) {
|
||||
ctx.sql(" ").keyword("not null");
|
||||
}
|
||||
}
|
||||
else if (alterDefault != null) {
|
||||
else if (alterColumnDefault != null) {
|
||||
ctx.start(ALTER_TABLE_ALTER_DEFAULT);
|
||||
|
||||
switch (family) {
|
||||
@ -323,13 +349,13 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
break;
|
||||
}
|
||||
|
||||
ctx.sql(" ").visit(alterDefault)
|
||||
ctx.sql(" ").visit(alterColumnDefault)
|
||||
.end(ALTER_TABLE_ALTER_DEFAULT);
|
||||
}
|
||||
|
||||
ctx.end(ALTER_TABLE_ALTER);
|
||||
}
|
||||
else if (drop != null) {
|
||||
else if (dropColumn != null) {
|
||||
ctx.start(ALTER_TABLE_DROP);
|
||||
|
||||
switch (family) {
|
||||
@ -351,7 +377,7 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
ctx.sql(" ")
|
||||
.qualify(false)
|
||||
.visit(drop)
|
||||
.visit(dropColumn)
|
||||
.qualify(true);
|
||||
|
||||
switch (family) {
|
||||
@ -365,12 +391,24 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
break;
|
||||
}
|
||||
|
||||
if (dropCascade) {
|
||||
if (dropColumnCascade) {
|
||||
ctx.sql(" ").keyword("cascade");
|
||||
}
|
||||
|
||||
ctx.end(ALTER_TABLE_DROP);
|
||||
}
|
||||
else if (dropConstraint != null) {
|
||||
ctx.start(ALTER_TABLE_DROP);
|
||||
ctx.data(DATA_DROP_CONSTRAINT, true);
|
||||
|
||||
ctx.sql(" ")
|
||||
.keyword("drop")
|
||||
.sql(" ")
|
||||
.visit(dropConstraint);
|
||||
|
||||
ctx.data().remove(DATA_DROP_CONSTRAINT);
|
||||
ctx.end(ALTER_TABLE_DROP);
|
||||
}
|
||||
}
|
||||
|
||||
/* [pro] xx
|
||||
@ -378,8 +416,8 @@ class AlterTableImpl extends AbstractQuery implements
|
||||
xxxxxxxxxx xxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
|
||||
xxxxxx xxxxxxxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
xxxxxx xxxxxxxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
xxxxxx xxxxxxxxxxxxxxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
xxxxxx xxxxxxxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
xxxxxx xxxxxxxxxxxxxxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
|
||||
xx xxxxxxx xxxxx xxxxxxxxxxx xxxx xxx xxxxxx xx x xxxxxxxxxxx xxxx xxx xxx xxxxxx
|
||||
xx xxxxxxxxxx xxxxxxxxx xx xxxx xx
|
||||
|
||||
84
jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java
Normal file
84
jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java
Normal file
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* 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.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.Clause.CONSTRAINT;
|
||||
import static org.jooq.impl.DSL.name;
|
||||
import static org.jooq.impl.Utils.DATA_DROP_CONSTRAINT;
|
||||
|
||||
import org.jooq.Clause;
|
||||
import org.jooq.ConstraintTypeStep;
|
||||
import org.jooq.Context;
|
||||
import org.jooq.Name;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
class ConstraintImpl extends AbstractQueryPart implements ConstraintTypeStep {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 1018023703769802616L;
|
||||
private static final Clause[] CLAUSES = { CONSTRAINT };
|
||||
|
||||
private final Name name;
|
||||
|
||||
ConstraintImpl(String name) {
|
||||
this.name = name(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Clause[] clauses(Context<?> ctx) {
|
||||
return CLAUSES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.keyword("constraint")
|
||||
.sql(" ")
|
||||
.visit(name);
|
||||
|
||||
if (ctx.data(DATA_DROP_CONSTRAINT) == null) {
|
||||
// TODO [#3338] Implement adding of constraints.
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -100,6 +100,7 @@ import org.jooq.CommonTableExpression;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.ConnectionProvider;
|
||||
import org.jooq.ConstraintTypeStep;
|
||||
import org.jooq.CreateIndexStep;
|
||||
import org.jooq.CreateSequenceFinalStep;
|
||||
import org.jooq.CreateTableAsStep;
|
||||
@ -4221,6 +4222,15 @@ public class DSL {
|
||||
return using(new DefaultConfiguration()).delete(table);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX DDL Clauses
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Support
|
||||
public static ConstraintTypeStep constraint(String name) {
|
||||
return new ConstraintImpl(name);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX DDL Statements
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -295,6 +295,11 @@ final class Utils {
|
||||
*/
|
||||
static final String DATA_LIST_ALREADY_INDENTED = "org.jooq.configuration.list-already-indented";
|
||||
|
||||
/**
|
||||
* [#3338] Whether a constraint is being dropped.
|
||||
*/
|
||||
static final String DATA_DROP_CONSTRAINT = "org.jooq.configuration.drop-constraint";
|
||||
|
||||
/**
|
||||
* [#2965] These are {@link ConcurrentHashMap}s containing caches for
|
||||
* reflection information.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user