[#883] DDL fixes

This commit is contained in:
Lukas Eder 2014-05-07 17:29:23 +02:00
parent 05bad4f137
commit 9e21abc166
2 changed files with 27 additions and 23 deletions

View File

@ -44,6 +44,7 @@ import static java.util.Arrays.asList;
import static org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.FIREBIRD;
// ...
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.table;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@ -135,7 +136,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
try {
// TODO: Re-use jOOQ API for this
create().execute("create table t (a int)");
create().execute("insert into t values (1)");
create().insertInto(table("t"), field("a")).values(1).execute();
assertEquals(asList(1), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").add("b", SQLDataType.INTEGER).execute();
@ -155,15 +156,6 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
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 {
create().dropTable("t").execute();
@ -174,16 +166,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
try {
// TODO: Re-use jOOQ API for this
create().execute("create table t (a int)");
create().execute("insert into t values (1)");
create().insertInto(table("t"), field("a")).values(1).execute();
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 {
create().dropTable("t").execute();
@ -193,10 +179,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
public void testAlterTableAlterDefault() throws Exception {
try {
// TODO: Re-use jOOQ API for this
create().execute("create table t (a int, b varchar)");
create().execute("create table t (a int, b " + SQLDataType.VARCHAR.length(10).getCastTypeName(create().configuration()) + ")");
create().alterTable("t").alter("b").defaultValue("empty").execute();
create().execute("insert into t (a) values (1)");
create().insertInto(table("t"), field("a")).values(1).execute();
assertEquals("empty", create().fetchOne("select b from t").getValue(0));
}
finally {
@ -208,7 +194,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
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)");
create().insertInto(table("t"), field("a"), field("b"), field("c")).values(1, 2, 3).execute();
assertEquals(asList(1, 2, 3), asList(create().fetchOne(table("t")).intoArray()));
create().alterTable("t").drop("c").execute();
@ -226,7 +212,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
// 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)");
create().insertInto(table("t"), field("a"), field("b"), field("c")).values(1, 2, 3).execute();
assertEquals(asList(1, 2, 3), asList(create().fetchOne(table("t")).intoArray()));
create().dropTable("t").execute();

View File

@ -40,12 +40,15 @@
*/
package org.jooq.impl;
import static java.util.Arrays.asList;
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_ALTER_DEFAULT;
import static org.jooq.Clause.ALTER_TABLE_DROP;
import static org.jooq.Clause.ALTER_TABLE_TABLE;
// ...
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.inline;
@ -58,6 +61,7 @@ import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.SQLDialect;
import org.jooq.Table;
/**
@ -165,6 +169,8 @@ class AlterTableImpl extends AbstractQuery implements
@Override
public final void accept(Context<?> ctx) {
SQLDialect family = ctx.configuration().dialect().family();
ctx.start(ALTER_TABLE_TABLE)
.keyword("alter table").sql(" ").visit(table)
.end(ALTER_TABLE_TABLE);
@ -191,8 +197,20 @@ class AlterTableImpl extends AbstractQuery implements
.qualify(true);
if (alterType != null) {
ctx.sql(" ")
.keyword(alterType.getCastTypeName(ctx.configuration()));
if (asList(POSTGRES).contains(family)) {
ctx.sql(" ").keyword("type");
}
/* [pro] xx
xxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x
xxxxxxxxx xxxxxxxxxxxxxxx xxxx xxxxxxx
x
xx [/pro] */
ctx.sql(" ").keyword(alterType.getCastTypeName(ctx.configuration()));
if (!alterType.nullable()) {
ctx.sql(" ").keyword("not null");
}
}
else if (alterDefault != null) {
ctx.start(ALTER_TABLE_ALTER_DEFAULT)