[#883] Supported ALTER TABLE for H2 SQLDialect

This commit is contained in:
Lukas Eder 2014-05-06 18:57:27 +02:00
parent 990b5c683b
commit c5e4e72a08
14 changed files with 846 additions and 9 deletions

View File

@ -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<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
}
@SuppressWarnings("unchecked")
public void testAlterSequence() throws Exception {
if (cSequences() == null || asList(DERBY).contains(dialect().family())) {
log.info("SKIPPING", "Skipping ALTER SEQUENCE test");
@ -127,4 +130,99 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertEquals(i++, create().nextval(S_AUTHOR_ID).intValue());
assertEquals(i++, create().nextval(S_AUTHOR_ID).intValue());
}
public void testAlterTableAdd() throws Exception {
try {
// TODO: Re-use jOOQ API for this
create().execute("create table t (a int)");
create().execute("insert into t values (1)");
assertEquals(asList(1), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").add("b", SQLDataType.INTEGER).execute();
assertEquals(asList(1, null), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").add("c", SQLDataType.NUMERIC).execute();
assertEquals(asList(1, null, null), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").add("d", SQLDataType.NUMERIC.precision(5)).execute();
assertEquals(asList(1, null, null, null), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").add("e", SQLDataType.NUMERIC.precision(5, 2)).execute();
assertEquals(asList(1, null, null, null, null), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").add("f", SQLDataType.VARCHAR).execute();
assertEquals(asList(1, null, null, null, null, null), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").add("g", SQLDataType.VARCHAR.length(5)).execute();
assertEquals(asList(1, null, null, null, null, null, null), asList(create().fetchOne(table("t")).intoArray()));
try {
create().alterTable("t").add("h", SQLDataType.INTEGER.nullable(false)).execute();
fail();
}
catch (DataAccessException expected) {}
create().execute("delete t");
create().alterTable("t").add("h", SQLDataType.INTEGER.nullable(false)).execute();
}
finally {
// TODO: Re-use jOOQ API for this
create().execute("drop table t");
}
}
public void testAlterTableAlterType() throws Exception {
try {
// TODO: Re-use jOOQ API for this
create().execute("create table t (a int)");
create().execute("insert into t values (1)");
create().alterTable("t").alter("a").set(SQLDataType.VARCHAR).execute();
assertEquals("1", create().fetchOne("select * from t").getValue(0));
create().alterTable("t").alter("a").set(SQLDataType.VARCHAR.nullable(false)).execute();
try {
create().execute("update t set t = null");
}
catch (DataAccessException expected) {}
}
finally {
// TODO: Re-use jOOQ API for this
create().execute("drop table t");
}
}
public void testAlterTableAlterDefault() throws Exception {
try {
// TODO: Re-use jOOQ API for this
create().execute("create table t (a int, b varchar)");
create().alterTable("t").alter("b").defaultValue("empty").execute();
create().execute("insert into t (a) values (1)");
assertEquals("empty", create().fetchOne("select b from t").getValue(0));
}
finally {
// TODO: Re-use jOOQ API for this
create().execute("drop table t");
}
}
public void testAlterTableDrop() throws Exception {
try {
// TODO: Re-use jOOQ API for this
create().execute("create table t (a int, b int, c int)");
create().execute("insert into t values (1, 2, 3)");
assertEquals(asList(1, 2, 3), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").drop("c").execute();
assertEquals(asList(1, 2), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").drop("b").execute();
assertEquals(asList(1), asList(create().fetchOne(table("t")).intoArray()));
}
finally {
// TODO: Re-use jOOQ API for this
create().execute("drop table t");
}
}
}

View File

@ -1143,6 +1143,26 @@ public abstract class jOOQAbstractTest<
new DDLTests(this).testAlterSequence();
}
@Test
public void testAlterTableAdd() throws Exception {
new DDLTests(this).testAlterTableAdd();
}
@Test
public void testAlterTableAlterType() throws Exception {
new DDLTests(this).testAlterTableAlterType();
}
@Test
public void testAlterTableAlterDefault() throws Exception {
new DDLTests(this).testAlterTableAlterDefault();
}
@Test
public void testAlterTableDrop() throws Exception {
new DDLTests(this).testAlterTableDrop();
}
@Test
public void testTruncate() throws Exception {
new TruncateTests(this).testTruncate();

View File

@ -58,7 +58,7 @@ import org.jooq.api.annotation.Transition;
* @author Lukas Eder
*/
@State
public interface AlterSequenceRestartStep<T> {
public interface AlterSequenceRestartStep<T extends Number> {
/**
* Restart the sequence at its initial value.

View File

@ -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 <code>ALTER TABLE</code> DSL used to <code>ALTER</code>
* columns.
*
* @author Lukas Eder
*/
@State
public interface AlterTableAlterStep<T> {
/**
* Specify a new column <code>DEFAULT</code>.
*/
AlterTableFinalStep defaultValue(T literal);
/**
* Specify a new column <code>DEFAULT</code>.
*/
AlterTableFinalStep defaultValue(Field<T> expression);
/**
* Specify a new column data type.
*/
AlterTableFinalStep set(DataType<?> type);
}

View File

@ -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 <code>ALTER TABLE</code> DSL used to <code>DROP</code>
* columns.
*
* @author Lukas Eder
*/
@State
public interface AlterTableDropStep extends AlterTableFinalStep {
/**
* Add a <code>CASCADE</code> clause to the
* <code>ALTER TABLE .. DROP COLUMN</code> statement.
*/
AlterTableFinalStep cascade();
/**
* Add a <code>RESTRICT</code> clause to the
* <code>ALTER TABLE .. DROP COLUMN</code> statement.
*/
AlterTableFinalStep restrict();
}

View File

@ -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 <code>ALTER TABLE</code> DSL.
*
* @author Lukas Eder
*/
public interface AlterTableFinalStep extends Query {
}

View File

@ -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 <code>ALTER TABLE</code> where the action can be decided.
*
* @author Lukas Eder
*/
@State
public interface AlterTableStep {
/**
* Add an <code>ALTER COLUMN</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
<T> AlterTableAlterStep<T> alter(Field<T> field);
/**
* Add an <code>ALTER COLUMN</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
AlterTableAlterStep<Object> alter(String field);
/**
* Add an <code>ADD COLUMN</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
<T> AlterTableFinalStep add(Field<T> field, DataType<T> type);
/**
* Add an <code>ADD COLUMN</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
AlterTableFinalStep add(String field, DataType<?> type);
/**
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
AlterTableDropStep drop(Field<?> field);
/**
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
AlterTableDropStep drop(String field);
}

View File

@ -799,6 +799,55 @@ public enum Clause {
*/
ALTER_SEQUENCE_RESTART,
/**
* A complete <code>ALTER ALTER_TABLE</code> statement.
*/
ALTER_TABLE,
/**
* A <code>TABLE</code> clause within an {@link #ALTER_TABLE} statement.
* <p>
* This clause surrounds
* <ul>
* <li>the <code>ALTER TABLE</code> keywords</li>
* <li>the table that is being altered</li>
* </ul>
*/
ALTER_TABLE_TABLE,
/**
* A <code>ADD</code> clause within an {@link #ALTER_TABLE} statement.
* <p>
* This clause surrounds
* <ul>
* <li>the <code>ADD</code> keywords</li>
* <li>the column that is being added</li>
* </ul>
*/
ALTER_TABLE_ADD,
/**
* A <code>ALTER</code> clause within an {@link #ALTER_TABLE} statement.
* <p>
* This clause surrounds
* <ul>
* <li>the <code>ALTER</code> keywords</li>
* <li>the column that is being altered</li>
* </ul>
*/
ALTER_TABLE_ALTER,
/**
* A <code>DROP</code> clause within an {@link #ALTER_TABLE} statement.
* <p>
* This clause surrounds
* <ul>
* <li>the <code>DROP</code> keywords</li>
* <li>the column that is being dropped</li>
* </ul>
*/
ALTER_TABLE_DROP,
// -------------------------------------------------------------------------
// Other clauses
// -------------------------------------------------------------------------

View File

@ -4758,7 +4758,7 @@ public interface DSLContext {
/**
* Create a new DSL <code>ALTER SEQUENCE</code> statement.
*
* @see DSLContext#alterSequence(Sequence)
* @see DSL#alterSequence(Sequence)
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
@Transition(
@ -4767,6 +4767,42 @@ public interface DSLContext {
)
<T extends Number> AlterSequenceRestartStep<T> alterSequence(Sequence<T> sequence);
/**
* Create a new DSL <code>ALTER SEQUENCE</code> statement.
*
* @see DSL#alterSequence(Sequence)
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
@Transition(
name = "ALTER SEQUENCE",
args = "Sequence"
)
AlterSequenceRestartStep<BigInteger> alterSequence(String sequence);
/**
* Create a new DSL <code>ALTER TABLE</code> statement.
*
* @see DSL#alterTable(Table)
*/
@Support
@Transition(
name = "ALTER TABLE",
args = "Table"
)
AlterTableStep alterTable(Table<?> table);
/**
* Create a new DSL <code>ALTER TABLE</code> statement.
*
* @see DSL#alterTable(Table)
*/
@Support
@Transition(
name = "ALTER TABLE",
args = "Table"
)
AlterTableStep alterTable(String table);
/**
* Create a new DSL truncate statement.
* <p>

View File

@ -54,7 +54,7 @@ import org.jooq.Sequence;
/**
* @author Lukas Eder
*/
class AlterSequenceImpl<T extends Number> extends AbstractQuery implements
class AlterSequenceImpl<T extends Number> extends AbstractQuery implements
// Cascading interface implementations for AlterSequence behaviour
AlterSequenceRestartStep<T>,
@ -96,9 +96,8 @@ class AlterSequenceImpl<T extends Number> 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<T extends Number> extends AbstractQuery implements
.sql(" ").sql(with.toString());
}
ctx.end(ALTER_SEQUENCE_RESTART)
.end(ALTER_SEQUENCE);
ctx.end(ALTER_SEQUENCE_RESTART);
}
@Override

View File

@ -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 <T> AlterTableImpl add(Field<T> field, DataType<T> type) {
add = field;
addType = type;
return this;
}
@Override
public final AlterTableImpl alter(String field) {
return alter(field(field));
}
@Override
public final <T> AlterTableImpl alter(Field<T> 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;
}
}

View File

@ -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 <code>ALTER SEQUENCE</code> statement.
*
* @see DSLContext#alterSequence(Sequence)
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
@Transition(
name = "ALTER SEQUENCE",
args = "Sequence"
)
public static AlterSequenceRestartStep<BigInteger> alterSequence(String sequence) {
return using(new DefaultConfiguration()).alterSequence(sequence);
}
/**
* Create a new DSL <code>ALTER TABLE</code> 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 <code>ALTER TABLE</code> 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.
* <p>

View File

@ -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<T>(configuration, sequence);
}
@Override
public AlterSequenceRestartStep<BigInteger> 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 <R extends Record> TruncateIdentityStep<R> truncate(Table<R> table) {
return new TruncateImpl<R>(configuration, table);

View File

@ -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(