From c5e4e72a08ddabb35b714ef31112b98583efe6ed Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 6 May 2014 18:57:27 +0200 Subject: [PATCH] [#883] Supported ALTER TABLE for H2 SQLDialect --- .../org/jooq/test/_/testcases/DDLTests.java | 100 +++++++- .../java/org/jooq/test/jOOQAbstractTest.java | 20 ++ .../org/jooq/AlterSequenceRestartStep.java | 2 +- .../java/org/jooq/AlterTableAlterStep.java | 68 ++++++ .../java/org/jooq/AlterTableDropStep.java | 65 ++++++ .../java/org/jooq/AlterTableFinalStep.java | 51 ++++ .../main/java/org/jooq/AlterTableStep.java | 88 +++++++ jOOQ/src/main/java/org/jooq/Clause.java | 49 ++++ jOOQ/src/main/java/org/jooq/DSLContext.java | 38 ++- .../java/org/jooq/impl/AlterSequenceImpl.java | 8 +- .../java/org/jooq/impl/AlterTableImpl.java | 221 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 43 ++++ .../java/org/jooq/impl/DefaultDSLContext.java | 18 ++ .../java/org/jooq/test/VisitContextTest.java | 84 ++++++- 14 files changed, 846 insertions(+), 9 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/AlterTableAlterStep.java create mode 100644 jOOQ/src/main/java/org/jooq/AlterTableDropStep.java create mode 100644 jOOQ/src/main/java/org/jooq/AlterTableFinalStep.java create mode 100644 jOOQ/src/main/java/org/jooq/AlterTableStep.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java diff --git a/jOOQ-test/src/test/java/org/jooq/test/_/testcases/DDLTests.java b/jOOQ-test/src/test/java/org/jooq/test/_/testcases/DDLTests.java index aca9cc8f9e..86a1559293 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/_/testcases/DDLTests.java +++ b/jOOQ-test/src/test/java/org/jooq/test/_/testcases/DDLTests.java @@ -44,7 +44,9 @@ import static java.util.Arrays.asList; import static org.jooq.SQLDialect.DERBY; import static org.jooq.SQLDialect.FIREBIRD; // ... +import static org.jooq.impl.DSL.table; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.sql.Date; @@ -55,6 +57,8 @@ import org.jooq.Record6; import org.jooq.Sequence; import org.jooq.TableRecord; import org.jooq.UpdatableRecord; +import org.jooq.exception.DataAccessException; +import org.jooq.impl.SQLDataType; import org.jooq.test.BaseTest; import org.jooq.test.jOOQAbstractTest; @@ -86,7 +90,6 @@ extends BaseTest { +public interface AlterSequenceRestartStep { /** * Restart the sequence at its initial value. diff --git a/jOOQ/src/main/java/org/jooq/AlterTableAlterStep.java b/jOOQ/src/main/java/org/jooq/AlterTableAlterStep.java new file mode 100644 index 0000000000..f650c798c1 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterTableAlterStep.java @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2009-2014, 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; + +import org.jooq.api.annotation.State; + +/** + * The step in the ALTER TABLE DSL used to ALTER + * columns. + * + * @author Lukas Eder + */ +@State +public interface AlterTableAlterStep { + + /** + * Specify a new column DEFAULT. + */ + AlterTableFinalStep defaultValue(T literal); + + /** + * Specify a new column DEFAULT. + */ + AlterTableFinalStep defaultValue(Field expression); + + /** + * Specify a new column data type. + */ + AlterTableFinalStep set(DataType type); +} diff --git a/jOOQ/src/main/java/org/jooq/AlterTableDropStep.java b/jOOQ/src/main/java/org/jooq/AlterTableDropStep.java new file mode 100644 index 0000000000..baafeebf5e --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterTableDropStep.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2009-2014, 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; + +import org.jooq.api.annotation.State; + +/** + * The step in the ALTER TABLE DSL used to DROP + * columns. + * + * @author Lukas Eder + */ +@State +public interface AlterTableDropStep extends AlterTableFinalStep { + + /** + * Add a CASCADE clause to the + * ALTER TABLE .. DROP COLUMN statement. + */ + AlterTableFinalStep cascade(); + + /** + * Add a RESTRICT clause to the + * ALTER TABLE .. DROP COLUMN statement. + */ + AlterTableFinalStep restrict(); +} diff --git a/jOOQ/src/main/java/org/jooq/AlterTableFinalStep.java b/jOOQ/src/main/java/org/jooq/AlterTableFinalStep.java new file mode 100644 index 0000000000..a9557a5578 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterTableFinalStep.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2009-2014, 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 final step in the ALTER TABLE DSL. + * + * @author Lukas Eder + */ +public interface AlterTableFinalStep extends Query { + +} diff --git a/jOOQ/src/main/java/org/jooq/AlterTableStep.java b/jOOQ/src/main/java/org/jooq/AlterTableStep.java new file mode 100644 index 0000000000..19b4e2373b --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterTableStep.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2009-2014, 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; + +import org.jooq.api.annotation.State; + +/** + * The step in the ALTER TABLE where the action can be decided. + * + * @author Lukas Eder + */ +@State +public interface AlterTableStep { + + /** + * Add an ALTER COLUMN clause to the ALTER TABLE + * statement. + */ + AlterTableAlterStep alter(Field field); + + /** + * Add an ALTER COLUMN clause to the ALTER TABLE + * statement. + */ + AlterTableAlterStep alter(String field); + + /** + * Add an ADD COLUMN clause to the ALTER TABLE + * statement. + */ + AlterTableFinalStep add(Field field, DataType type); + + /** + * Add an ADD COLUMN clause to the ALTER TABLE + * statement. + */ + AlterTableFinalStep add(String field, DataType type); + + /** + * Add an DROP COLUMN clause to the ALTER TABLE + * statement. + */ + AlterTableDropStep drop(Field field); + + /** + * Add an DROP COLUMN clause to the ALTER TABLE + * statement. + */ + AlterTableDropStep drop(String field); +} diff --git a/jOOQ/src/main/java/org/jooq/Clause.java b/jOOQ/src/main/java/org/jooq/Clause.java index 9c626ede6f..c808bef1d3 100644 --- a/jOOQ/src/main/java/org/jooq/Clause.java +++ b/jOOQ/src/main/java/org/jooq/Clause.java @@ -799,6 +799,55 @@ public enum Clause { */ ALTER_SEQUENCE_RESTART, + /** + * A complete ALTER ALTER_TABLE statement. + */ + ALTER_TABLE, + + /** + * A TABLE clause within an {@link #ALTER_TABLE} statement. + *

+ * This clause surrounds + *

    + *
  • the ALTER TABLE keywords
  • + *
  • the table that is being altered
  • + *
+ */ + ALTER_TABLE_TABLE, + + /** + * A ADD clause within an {@link #ALTER_TABLE} statement. + *

+ * This clause surrounds + *

    + *
  • the ADD keywords
  • + *
  • the column that is being added
  • + *
+ */ + ALTER_TABLE_ADD, + + /** + * A ALTER clause within an {@link #ALTER_TABLE} statement. + *

+ * This clause surrounds + *

    + *
  • the ALTER keywords
  • + *
  • the column that is being altered
  • + *
+ */ + ALTER_TABLE_ALTER, + + /** + * A DROP clause within an {@link #ALTER_TABLE} statement. + *

+ * This clause surrounds + *

    + *
  • the DROP keywords
  • + *
  • the column that is being dropped
  • + *
+ */ + ALTER_TABLE_DROP, + // ------------------------------------------------------------------------- // Other clauses // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 7363ca645f..7ca5fd8f60 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -4758,7 +4758,7 @@ public interface DSLContext { /** * Create a new DSL ALTER SEQUENCE statement. * - * @see DSLContext#alterSequence(Sequence) + * @see DSL#alterSequence(Sequence) */ @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) @Transition( @@ -4767,6 +4767,42 @@ public interface DSLContext { ) AlterSequenceRestartStep alterSequence(Sequence sequence); + /** + * Create a new DSL ALTER SEQUENCE statement. + * + * @see DSL#alterSequence(Sequence) + */ + @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) + @Transition( + name = "ALTER SEQUENCE", + args = "Sequence" + ) + AlterSequenceRestartStep alterSequence(String sequence); + + /** + * Create a new DSL ALTER TABLE statement. + * + * @see DSL#alterTable(Table) + */ + @Support + @Transition( + name = "ALTER TABLE", + args = "Table" + ) + AlterTableStep alterTable(Table table); + + /** + * Create a new DSL ALTER TABLE statement. + * + * @see DSL#alterTable(Table) + */ + @Support + @Transition( + name = "ALTER TABLE", + args = "Table" + ) + AlterTableStep alterTable(String table); + /** * Create a new DSL truncate statement. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/AlterSequenceImpl.java b/jOOQ/src/main/java/org/jooq/impl/AlterSequenceImpl.java index 6b7cb093f2..7d2e799170 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AlterSequenceImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/AlterSequenceImpl.java @@ -54,7 +54,7 @@ import org.jooq.Sequence; /** * @author Lukas Eder */ -class AlterSequenceImpl extends AbstractQuery implements +class AlterSequenceImpl extends AbstractQuery implements // Cascading interface implementations for AlterSequence behaviour AlterSequenceRestartStep, @@ -96,9 +96,8 @@ class AlterSequenceImpl extends AbstractQuery implements @Override public final void accept(Context ctx) { - ctx.start(ALTER_SEQUENCE) + ctx.start(ALTER_SEQUENCE_SEQUENCE) .keyword("alter sequence") - .start(ALTER_SEQUENCE_SEQUENCE) .sql(" ").visit(sequence) .end(ALTER_SEQUENCE_SEQUENCE) .start(ALTER_SEQUENCE_RESTART); @@ -112,8 +111,7 @@ class AlterSequenceImpl extends AbstractQuery implements .sql(" ").sql(with.toString()); } - ctx.end(ALTER_SEQUENCE_RESTART) - .end(ALTER_SEQUENCE); + ctx.end(ALTER_SEQUENCE_RESTART); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java b/jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java new file mode 100644 index 0000000000..9091417553 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java @@ -0,0 +1,221 @@ +/** + * Copyright (c) 2009-2014, 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.ALTER_TABLE; +import static org.jooq.Clause.ALTER_TABLE_ADD; +import static org.jooq.Clause.ALTER_TABLE_ALTER; +import static org.jooq.Clause.ALTER_TABLE_DROP; +import static org.jooq.Clause.ALTER_TABLE_TABLE; +import static org.jooq.impl.DSL.field; +import static org.jooq.impl.DSL.inline; + +import org.jooq.AlterTableAlterStep; +import org.jooq.AlterTableDropStep; +import org.jooq.AlterTableFinalStep; +import org.jooq.AlterTableStep; +import org.jooq.Clause; +import org.jooq.Configuration; +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; +import org.jooq.Table; + +/** + * @author Lukas Eder + */ +@SuppressWarnings({ "rawtypes", "unchecked" }) +class AlterTableImpl extends AbstractQuery implements + + // Cascading interface implementations for AlterSequence behaviour + AlterTableStep, + AlterTableDropStep, + AlterTableAlterStep { + + /** + * Generated UID + */ + private static final long serialVersionUID = 8904572826501186329L; + 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; + + AlterTableImpl(Configuration configuration, Table table) { + super(configuration); + + this.table = table; + } + + // ------------------------------------------------------------------------ + // XXX: DSL API + // ------------------------------------------------------------------------ + + @Override + public final AlterTableImpl add(String field, DataType type) { + return add((Field) field(field, type), type); + } + + @Override + public final AlterTableImpl add(Field field, DataType type) { + add = field; + addType = type; + return this; + } + + @Override + public final AlterTableImpl alter(String field) { + return alter(field(field)); + } + + @Override + public final AlterTableImpl alter(Field field) { + alter = field; + return this; + } + + @Override + public final AlterTableImpl set(DataType type) { + alterType = type; + return this; + } + + @Override + public final AlterTableImpl defaultValue(Object literal) { + return defaultValue(inline(literal)); + } + + @Override + public final AlterTableImpl defaultValue(Field expression) { + alterDefault = expression; + return this; + } + + @Override + public final AlterTableImpl drop(String field) { + return drop(field(field)); + } + + @Override + public final AlterTableImpl drop(Field field) { + drop = field; + return this; + } + + @Override + public final AlterTableFinalStep cascade() { + dropCascade = true; + return this; + } + + @Override + public final AlterTableFinalStep restrict() { + dropCascade = false; + return this; + } + + // ------------------------------------------------------------------------ + // XXX: QueryPart API + // ------------------------------------------------------------------------ + + @Override + public final void accept(Context ctx) { + ctx.start(ALTER_TABLE_TABLE) + .keyword("alter table").sql(" ").visit(table) + .end(ALTER_TABLE_TABLE); + + if (add != null) { + ctx.start(ALTER_TABLE_ADD) + .sql(" ").keyword("add").sql(" ") + .qualify(false) + .visit(add).sql(" ") + .qualify(true) + .keyword(addType.getCastTypeName(ctx.configuration())); + + if (!addType.nullable()) { + ctx.sql(" ").keyword("not null"); + } + + ctx.end(ALTER_TABLE_ADD); + } + else if (alter != null) { + ctx.start(ALTER_TABLE_ALTER) + .sql(" ").keyword("alter").sql(" ") + .qualify(false) + .visit(alter) + .qualify(true); + + if (alterType != null) { + ctx.sql(" ") + .keyword(alterType.getCastTypeName(ctx.configuration())); + } + else if (alterDefault != null) { + ctx.sql(" ").keyword("set default").sql(" ").visit(alterDefault); + } + + ctx.end(ALTER_TABLE_ALTER); + } + else if (drop != null) { + ctx.start(ALTER_TABLE_DROP) + .sql(" ").keyword("drop").sql(" ") + .qualify(false) + .visit(drop) + .qualify(true); + + if (dropCascade) { + ctx.sql(" ").keyword("cascade"); + } + + ctx.end(ALTER_TABLE_DROP); + } + } + + @Override + public final Clause[] clauses(Context ctx) { + return CLAUSES; + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 3a307c4e32..ea3f147bdf 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -79,6 +79,7 @@ import javax.sql.DataSource; import org.jooq.AggregateFunction; import org.jooq.AlterSequenceRestartStep; +import org.jooq.AlterTableStep; // ... import org.jooq.Case; import org.jooq.CommonTableExpression; @@ -4379,6 +4380,48 @@ public class DSL { return using(new DefaultConfiguration()).alterSequence(sequence); } + /** + * Create a new DSL ALTER SEQUENCE statement. + * + * @see DSLContext#alterSequence(Sequence) + */ + @Support({ FIREBIRD, H2, HSQLDB, POSTGRES }) + @Transition( + name = "ALTER SEQUENCE", + args = "Sequence" + ) + public static AlterSequenceRestartStep alterSequence(String sequence) { + return using(new DefaultConfiguration()).alterSequence(sequence); + } + + /** + * Create a new DSL ALTER TABLE statement. + * + * @see DSL#alterTable(Table) + */ + @Support + @Transition( + name = "ALTER TABLE", + args = "Table" + ) + public static AlterTableStep alterTable(Table table) { + return using(new DefaultConfiguration()).alterTable(table); + } + + /** + * Create a new DSL ALTER TABLE statement. + * + * @see DSL#alterTable(Table) + */ + @Support + @Transition( + name = "ALTER TABLE", + args = "Table" + ) + public static AlterTableStep alterTable(String table) { + return using(new DefaultConfiguration()).alterTable(table); + } + /** * Create a new DSL truncate statement. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 9294159232..a5c404a15d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -45,6 +45,8 @@ import static org.jooq.conf.ParamType.NAMED; import static org.jooq.impl.DSL.field; import static org.jooq.impl.DSL.fieldByName; import static org.jooq.impl.DSL.queryPart; +import static org.jooq.impl.DSL.sequence; +import static org.jooq.impl.DSL.table; import static org.jooq.impl.DSL.template; import static org.jooq.impl.DSL.trueCondition; import static org.jooq.impl.Utils.list; @@ -70,6 +72,7 @@ import javax.annotation.Generated; import javax.sql.DataSource; import org.jooq.AlterSequenceRestartStep; +import org.jooq.AlterTableStep; import org.jooq.Attachable; import org.jooq.Batch; import org.jooq.BatchBindStep; @@ -1558,6 +1561,21 @@ public class DefaultDSLContext implements DSLContext, Serializable { return new AlterSequenceImpl(configuration, sequence); } + @Override + public AlterSequenceRestartStep alterSequence(String sequence) { + return alterSequence(sequence(sequence)); + } + + @Override + public AlterTableStep alterTable(Table table) { + return new AlterTableImpl(configuration, table); + } + + @Override + public AlterTableStep alterTable(String table) { + return alterTable(table(table)); + } + @Override public TruncateIdentityStep truncate(Table table) { return new TruncateImpl(configuration, table); diff --git a/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java b/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java index 4057a058a0..4b1a068299 100644 --- a/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java +++ b/jOOQ/src/test/java/org/jooq/test/VisitContextTest.java @@ -41,7 +41,14 @@ package org.jooq.test; import static java.util.Arrays.asList; -import static org.junit.Assert.fail; +import static org.jooq.Clause.ALTER_SEQUENCE; +import static org.jooq.Clause.ALTER_SEQUENCE_RESTART; +import static org.jooq.Clause.ALTER_SEQUENCE_SEQUENCE; +import static org.jooq.Clause.ALTER_TABLE; +import static org.jooq.Clause.ALTER_TABLE_ADD; +import static org.jooq.Clause.ALTER_TABLE_ALTER; +import static org.jooq.Clause.ALTER_TABLE_DROP; +import static org.jooq.Clause.ALTER_TABLE_TABLE; import static org.jooq.Clause.CONDITION; import static org.jooq.Clause.CONDITION_AND; import static org.jooq.Clause.CONDITION_BETWEEN; @@ -94,6 +101,8 @@ import static org.jooq.Clause.SELECT_START_WITH; import static org.jooq.Clause.SELECT_UNION_ALL; import static org.jooq.Clause.SELECT_WHERE; import static org.jooq.Clause.SELECT_WINDOW; +import static org.jooq.Clause.SEQUENCE; +import static org.jooq.Clause.SEQUENCE_REFERENCE; import static org.jooq.Clause.TABLE; import static org.jooq.Clause.TABLE_ALIAS; import static org.jooq.Clause.TABLE_REFERENCE; @@ -124,6 +133,7 @@ import static org.jooq.test.data.Table1.FIELD_NAME1; import static org.jooq.test.data.Table1.TABLE1; import static org.jooq.test.data.Table2.FIELD_ID2; import static org.jooq.test.data.Table2.TABLE2; +import static org.junit.Assert.fail; import java.sql.SQLException; import java.util.ArrayList; @@ -137,6 +147,7 @@ import org.jooq.RenderContext; import org.jooq.VisitContext; import org.jooq.VisitListener; import org.jooq.impl.DSL; +import org.jooq.impl.SQLDataType; import org.jooq.tools.jdbc.MockConnection; import org.jooq.tools.jdbc.MockDataProvider; import org.jooq.tools.jdbc.MockExecuteContext; @@ -723,6 +734,77 @@ public class VisitContextTest extends AbstractTest { .returning(FIELD_ID1, FIELD_NAME1)); } + @Test + public void test_ALTER_SEQUENCE() { + + // Postgres supports sequences + ctx.configuration().set(POSTGRES); + assertEvents(asList( + asList(ALTER_SEQUENCE), + asList(ALTER_SEQUENCE, ALTER_SEQUENCE_SEQUENCE), + asList(ALTER_SEQUENCE, ALTER_SEQUENCE_SEQUENCE, SEQUENCE), + asList(ALTER_SEQUENCE, ALTER_SEQUENCE_SEQUENCE, SEQUENCE, SEQUENCE_REFERENCE), + asList(ALTER_SEQUENCE, ALTER_SEQUENCE_RESTART) + ), + ctx.alterSequence("seq").restart()); + } + + @Test + public void test_ALTER_TABLE_ADD() { + assertEvents(asList( + asList(ALTER_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE, TABLE_REFERENCE), + asList(ALTER_TABLE, ALTER_TABLE_ADD), + asList(ALTER_TABLE, ALTER_TABLE_ADD, FIELD), + asList(ALTER_TABLE, ALTER_TABLE_ADD, FIELD, FIELD_REFERENCE) + ), + ctx.alterTable(TABLE1).add(FIELD_NAME1, SQLDataType.VARCHAR)); + } + + @Test + public void test_ALTER_TABLE_ALTER_TYPE() { + assertEvents(asList( + asList(ALTER_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE, TABLE_REFERENCE), + asList(ALTER_TABLE, ALTER_TABLE_ALTER), + asList(ALTER_TABLE, ALTER_TABLE_ALTER, FIELD), + asList(ALTER_TABLE, ALTER_TABLE_ALTER, FIELD, FIELD_REFERENCE) + ), + ctx.alterTable(TABLE1).alter(FIELD_NAME1).set(SQLDataType.INTEGER)); + } + + @Test + public void test_ALTER_TABLE_ALTER_DEFAULT() { + assertEvents(asList( + asList(ALTER_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE, TABLE_REFERENCE), + asList(ALTER_TABLE, ALTER_TABLE_ALTER), + asList(ALTER_TABLE, ALTER_TABLE_ALTER, FIELD), + asList(ALTER_TABLE, ALTER_TABLE_ALTER, FIELD, FIELD_REFERENCE) + ), + ctx.alterTable(TABLE1).alter(FIELD_NAME1).defaultValue("no name")); + } + + @Test + public void test_ALTER_TABLE_DROP() { + assertEvents(asList( + asList(ALTER_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE), + asList(ALTER_TABLE, ALTER_TABLE_TABLE, TABLE, TABLE_REFERENCE), + asList(ALTER_TABLE, ALTER_TABLE_DROP), + asList(ALTER_TABLE, ALTER_TABLE_DROP, FIELD), + asList(ALTER_TABLE, ALTER_TABLE_DROP, FIELD, FIELD_REFERENCE) + ), + ctx.alterTable(TABLE1).drop(FIELD_NAME1)); + } + @Test public void test_CONDITION_simple() { assertEvents(asList(