[#6472] Add support for ALTER TABLE .. ALTER COLUMN .. { SET | DROP } NOT NULL
This commit is contained in:
parent
9b180d7b14
commit
d83db4d52b
@ -44,7 +44,11 @@ alterTableStatement = 'ALTER TABLE' [ 'IF EXISTS' ] tableName break
|
||||
| 'DEFAULT' concat
|
||||
)
|
||||
}
|
||||
| ( 'ALTER' | 'MODIFY' ) [ 'COLUMN' ] identifier dataType [ [ 'NOT' ] 'NULL' ]
|
||||
| ( 'ALTER' | 'MODIFY' ) [ 'COLUMN' ] identifier
|
||||
(
|
||||
dataType [ [ 'NOT' ] 'NULL' ]
|
||||
| ( 'SET' | 'DROP' ) 'NOT NULL'
|
||||
)
|
||||
| 'DROP COLUMN' identifier [ 'CASCADE' | 'RESTRICT' ]
|
||||
| 'DROP CONSTRAINT' constraintName
|
||||
| 'RENAME' [ ( 'COLUMN' | 'INDEX' | 'CONSTRAINT' ) identifier ] 'TO' identifier
|
||||
|
||||
@ -80,4 +80,16 @@ public interface AlterTableAlterStep<T> {
|
||||
*/
|
||||
@Support({ CUBRID, DERBY, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
AlterTableFinalStep set(DataType<?> type);
|
||||
|
||||
/**
|
||||
* Make the column <code>NOT NULL</code>.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
AlterTableFinalStep setNotNull();
|
||||
|
||||
/**
|
||||
* Make the column nullable.
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
AlterTableFinalStep dropNotNull();
|
||||
}
|
||||
|
||||
@ -1067,6 +1067,17 @@ public enum Clause {
|
||||
*/
|
||||
ALTER_TABLE_ALTER_DEFAULT,
|
||||
|
||||
/**
|
||||
* A <code>SET NULL</code> or <code>DROP NULL</code> clause within an
|
||||
* {@link #ALTER_TABLE} statement.
|
||||
* <p>
|
||||
* This clause surrounds
|
||||
* <ul>
|
||||
* <li>the nullability clause being altered</li>
|
||||
* </ul>
|
||||
*/
|
||||
ALTER_TABLE_ALTER_NULL,
|
||||
|
||||
/**
|
||||
* A <code>DROP</code> clause within an {@link #ALTER_TABLE} statement.
|
||||
* <p>
|
||||
|
||||
@ -39,12 +39,15 @@ 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_ALTER_NULL;
|
||||
import static org.jooq.Clause.ALTER_TABLE_DROP;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME_COLUMN;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME_CONSTRAINT;
|
||||
import static org.jooq.Clause.ALTER_TABLE_RENAME_INDEX;
|
||||
import static org.jooq.Clause.ALTER_TABLE_TABLE;
|
||||
import static org.jooq.Nullability.NOT_NULL;
|
||||
import static org.jooq.Nullability.NULL;
|
||||
// ...
|
||||
// ...
|
||||
import static org.jooq.SQLDialect.CUBRID;
|
||||
@ -73,6 +76,7 @@ import static org.jooq.impl.Keywords.K_DEFAULT;
|
||||
import static org.jooq.impl.Keywords.K_DROP;
|
||||
import static org.jooq.impl.Keywords.K_DROP_COLUMN;
|
||||
import static org.jooq.impl.Keywords.K_DROP_CONSTRAINT;
|
||||
import static org.jooq.impl.Keywords.K_DROP_NOT_NULL;
|
||||
import static org.jooq.impl.Keywords.K_ELSE;
|
||||
import static org.jooq.impl.Keywords.K_END_IF;
|
||||
import static org.jooq.impl.Keywords.K_EXCEPTION;
|
||||
@ -93,6 +97,7 @@ import static org.jooq.impl.Keywords.K_RENAME_TO;
|
||||
import static org.jooq.impl.Keywords.K_SET;
|
||||
import static org.jooq.impl.Keywords.K_SET_DATA_TYPE;
|
||||
import static org.jooq.impl.Keywords.K_SET_DEFAULT;
|
||||
import static org.jooq.impl.Keywords.K_SET_NOT_NULL;
|
||||
import static org.jooq.impl.Keywords.K_THEN;
|
||||
import static org.jooq.impl.Keywords.K_TO;
|
||||
import static org.jooq.impl.Keywords.K_TYPE;
|
||||
@ -163,6 +168,7 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
|
||||
private Field<?> alterColumn;
|
||||
private Nullability alterColumnNullability;
|
||||
private DataType<?> alterColumnType;
|
||||
private Field<?> alterColumnDefault;
|
||||
private Field<?> dropColumn;
|
||||
@ -389,6 +395,18 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl setNotNull() {
|
||||
alterColumnNullability = NOT_NULL;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropNotNull() {
|
||||
alterColumnNullability = NULL;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl defaultValue(Object literal) {
|
||||
return defaultValue(Tools.field(literal));
|
||||
@ -781,6 +799,11 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
ctx.sql(' ').visit(alterColumnDefault)
|
||||
.end(ALTER_TABLE_ALTER_DEFAULT);
|
||||
}
|
||||
else if (alterColumnNullability != null) {
|
||||
ctx.start(ALTER_TABLE_ALTER_NULL)
|
||||
.sql(' ').visit(alterColumnNullability.nullable() ? K_DROP_NOT_NULL : K_SET_NOT_NULL)
|
||||
.end(ALTER_TABLE_ALTER_NULL);
|
||||
}
|
||||
|
||||
ctx.end(ALTER_TABLE_ALTER);
|
||||
}
|
||||
|
||||
@ -100,6 +100,7 @@ final class Keywords {
|
||||
static final Keyword K_DROP_COLUMN = keyword("drop column");
|
||||
static final Keyword K_DROP_CONSTRAINT = keyword("drop constraint");
|
||||
static final Keyword K_DROP_INDEX = keyword("drop index");
|
||||
static final Keyword K_DROP_NOT_NULL = keyword("drop not null");
|
||||
static final Keyword K_DROP_SCHEMA = keyword("drop schema");
|
||||
static final Keyword K_DROP_TABLE = keyword("drop table");
|
||||
static final Keyword K_DROP_VIEW = keyword("drop view");
|
||||
@ -228,6 +229,7 @@ final class Keywords {
|
||||
static final Keyword K_SET = keyword("set");
|
||||
static final Keyword K_SET_DATA_TYPE = keyword("set data type");
|
||||
static final Keyword K_SET_DEFAULT = keyword("set default");
|
||||
static final Keyword K_SET_NOT_NULL = keyword("set not null");
|
||||
static final Keyword K_SIBLINGS = keyword("siblings");
|
||||
static final Keyword K_SKIP = keyword("skip");
|
||||
static final Keyword K_SQL = keyword("sql");
|
||||
|
||||
@ -1758,7 +1758,11 @@ class ParserImpl implements Parser {
|
||||
private static final DDLQuery parseAlterTableAlterColumn(ParserContext ctx, AlterTableStep s1) {
|
||||
TableField<?, ?> field = parseFieldName(ctx);
|
||||
|
||||
if (parseKeywordIf(ctx, "TYPE") || parseKeywordIf(ctx, "SET DATA TYPE"))
|
||||
if (parseKeywordIf(ctx, "DROP NOT NULL"))
|
||||
return s1.alter(field).dropNotNull();
|
||||
else if (parseKeywordIf(ctx, "SET NOT NULL"))
|
||||
return s1.alter(field).setNotNull();
|
||||
else if (parseKeywordIf(ctx, "TYPE") || parseKeywordIf(ctx, "SET DATA TYPE"))
|
||||
;
|
||||
|
||||
DataType<?> type = parseDataType(ctx);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user