diff --git a/jOOQ/src/main/java/org/jooq/AlterTableStep.java b/jOOQ/src/main/java/org/jooq/AlterTableStep.java index 7c2a260787..c3bb21d68b 100644 --- a/jOOQ/src/main/java/org/jooq/AlterTableStep.java +++ b/jOOQ/src/main/java/org/jooq/AlterTableStep.java @@ -58,6 +58,8 @@ import static org.jooq.SQLDialect.POSTGRES; // ... // ... +import org.jooq.impl.DSL; + /** * The step in the ALTER TABLE where the action can be decided. @@ -123,6 +125,13 @@ public interface AlterTableStep { @Support AlterTableFinalStep addColumn(Field field, DataType type); + /** + * Add an ADD CONSTRAINT clause to the ALTER TABLE + * statement. + */ + @Support + AlterTableFinalStep addConstraint(Constraint constraint); + /** * Add an ADD COLUMN clause to the ALTER TABLE * statement. @@ -161,4 +170,20 @@ public interface AlterTableStep { */ @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) AlterTableDropStep dropColumn(String field); + + /** + * Add a DROP CONSTRAINT clause to the ALTER TABLE + * statement. + */ + @Support + AlterTableFinalStep drop(Constraint constraint); + + /** + * Add a DROP CONSTRAINT clause to the ALTER TABLE + * statement. + * + * @see DSL#constraint(String) + */ + @Support + AlterTableFinalStep dropConstraint(String constraint); } diff --git a/jOOQ/src/main/java/org/jooq/Clause.java b/jOOQ/src/main/java/org/jooq/Clause.java index de3431d199..3bcaad1f9f 100644 --- a/jOOQ/src/main/java/org/jooq/Clause.java +++ b/jOOQ/src/main/java/org/jooq/Clause.java @@ -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 // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/Constraint.java b/jOOQ/src/main/java/org/jooq/Constraint.java new file mode 100644 index 0000000000..f8fa5e08ee --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/Constraint.java @@ -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 { + +} diff --git a/jOOQ/src/main/java/org/jooq/ConstraintTypeStep.java b/jOOQ/src/main/java/org/jooq/ConstraintTypeStep.java new file mode 100644 index 0000000000..1fd73f150b --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/ConstraintTypeStep.java @@ -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 { + +} diff --git a/jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java index 06c30a96bf..bef2484fd7 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java @@ -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 AlterTableImpl addColumn(Field field, DataType 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 AlterTableImpl alterColumn(Field 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 diff --git a/jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java b/jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java new file mode 100644 index 0000000000..b4370c077b --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java @@ -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. + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 8128541381..6d7666f736 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -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 // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/Utils.java b/jOOQ/src/main/java/org/jooq/impl/Utils.java index 0884a48f9f..270df0221a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Utils.java +++ b/jOOQ/src/main/java/org/jooq/impl/Utils.java @@ -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.