use of org.jooq.Comment in project jOOQ by jOOQ.
the class DDL method commentOn.
final List<Query> commentOn(Table<?> table) {
List<Query> result = new ArrayList<>();
if (configuration.flags().contains(COMMENT)) {
Comment tComment = table.getCommentPart();
if (!StringUtils.isEmpty(tComment.getComment()))
if (table.getTableType().isView())
result.add(ctx.commentOnView(table).is(tComment));
else
result.add(ctx.commentOnTable(table).is(tComment));
for (Field<?> field : sortIf(Arrays.asList(table.fields()), !configuration.respectColumnOrder())) {
Comment fComment = field.getCommentPart();
if (!StringUtils.isEmpty(fComment.getComment()))
result.add(ctx.commentOnColumn(field).is(fComment));
}
}
return result;
}
use of org.jooq.Comment in project jOOQ by jOOQ.
the class DefaultParseContext method parseInlineConstraints.
private final ParseInlineConstraints parseInlineConstraints(Name fieldName, DataType<?> type, List<? super Constraint> constraints, boolean primary, boolean identity, boolean readonly) {
boolean nullable = false;
boolean defaultValue = false;
boolean computed = false;
boolean onUpdate = false;
boolean unique = false;
boolean comment = false;
boolean compress = false;
Comment fieldComment = null;
identity |= type.identity();
readonly |= type.readonly();
for (; ; ) {
if (!nullable) {
if (parseKeywordIf("NULL")) {
type = type.nullable(true);
nullable = true;
continue;
} else if (parseNotNullOptionalEnable()) {
type = type.nullable(false);
nullable = true;
continue;
}
}
if (!defaultValue) {
if (!identity && parseKeywordIf("IDENTITY")) {
if (parseIf('(')) {
parseSignedIntegerLiteral();
parse(',');
parseSignedIntegerLiteral();
parse(')');
}
type = type.identity(true);
defaultValue = true;
identity = true;
continue;
} else if (!ignoreProEdition() && parseKeywordIf("READONLY") && requireProEdition()) {
} else if (parseKeywordIf("DEFAULT")) {
// [#10963] Special case nextval('<id>_seq'::regclass)
if (parseSerialIf()) {
type = type.identity(true);
} else {
// TODO: [#10116] Support this clause also in the jOOQ API
parseKeywordIf("ON NULL");
type = type.defaultValue((Field) toField(parseConcat()));
// TODO: [#10115] Support this clause also in the jOOQ API
parseKeywordIf("WITH VALUES");
defaultValue = true;
}
continue;
} else if (!computed && !ignoreProEdition() && (parseKeywordIf("AS") || parseKeywordIf("COMPUTED") && (parseKeywordIf("BY") || true)) && requireProEdition()) {
} else if (!identity && !computed && parseKeywordIf("GENERATED")) {
boolean always;
if (!(always = parseKeywordIf("ALWAYS"))) {
parseKeyword("BY DEFAULT");
// TODO: Ignored keyword from Oracle
parseKeywordIf("ON NULL");
}
if (always ? parseKeywordIf("AS IDENTITY") : parseKeyword("AS IDENTITY")) {
parseIdentityOptionIf();
type = type.identity(true);
identity = true;
} else if (!ignoreProEdition() && parseKeyword("AS") && requireProEdition()) {
}
defaultValue = true;
continue;
}
}
if (!onUpdate) {
if (parseKeywordIf("ON UPDATE")) {
// [#6132] TODO: Support this feature in the jOOQ DDL API
parseConcat();
onUpdate = true;
continue;
}
}
ConstraintTypeStep inlineConstraint = parseConstraintNameSpecification();
if (!unique) {
if (!primary && parsePrimaryKeyClusteredNonClusteredKeywordIf()) {
if (!parseKeywordIf("CLUSTERED"))
parseKeywordIf("NONCLUSTERED");
constraints.add(parseConstraintEnforcementIf(inlineConstraint == null ? primaryKey(fieldName) : inlineConstraint.primaryKey(fieldName)));
primary = true;
unique = true;
continue;
} else if (parseKeywordIf("UNIQUE")) {
if (!parseKeywordIf("KEY"))
parseKeywordIf("INDEX");
constraints.add(parseConstraintEnforcementIf(inlineConstraint == null ? unique(fieldName) : inlineConstraint.unique(fieldName)));
unique = true;
continue;
}
}
if (parseKeywordIf("CHECK")) {
constraints.add(parseCheckSpecification(inlineConstraint));
continue;
}
if (parseKeywordIf("FOREIGN KEY REFERENCES", "REFERENCES")) {
constraints.add(parseForeignKeyReferenceSpecification(inlineConstraint, new Field[] { field(fieldName) }));
continue;
}
if (!nullable) {
if (parseKeywordIf("NULL")) {
type = type.nullable(true);
nullable = true;
continue;
} else if (parseNotNullOptionalEnable()) {
type = type.nullable(false);
nullable = true;
continue;
}
}
if (inlineConstraint != null)
throw expected("CHECK", "NOT NULL", "NULL", "PRIMARY KEY", "REFERENCES", "UNIQUE");
if (!identity) {
if (parseKeywordIf("AUTO_INCREMENT") || parseKeywordIf("AUTOINCREMENT")) {
type = type.identity(true);
identity = true;
continue;
}
}
if (!comment) {
// [#10164] In a statement batch, this could already be the next statement
if (!peekKeyword("COMMENT ON") && parseKeywordIf("COMMENT")) {
if (!parseIf('='))
parseKeywordIf("IS");
fieldComment = parseComment();
comment = true;
continue;
} else if (peekKeyword("OPTIONS")) {
fieldComment = parseOptionsDescription();
comment = true;
continue;
}
}
if (!compress) {
if (!ignoreProEdition() && parseKeywordIf("NO COMPRESS") && requireProEdition()) {
} else if (!ignoreProEdition() && parseKeywordIf("COMPRESS") && requireProEdition()) {
}
}
break;
}
return new ParseInlineConstraints(type, fieldComment, primary, identity, readonly);
}
use of org.jooq.Comment in project jOOQ by jOOQ.
the class DefaultParseContext method parseOptionsDescription.
private final Comment parseOptionsDescription() {
parseKeyword("OPTIONS");
parse('(');
parseKeyword("DESCRIPTION");
parse('=');
Comment comment = parseComment();
parse(')');
return comment;
}
use of org.jooq.Comment in project jOOQ by jOOQ.
the class DefaultParseContext method parseCreateTable.
private final DDLQuery parseCreateTable(boolean temporary) {
boolean ifNotExists = parseKeywordIf("IF NOT EXISTS");
Table<?> tableName = DSL.table(parseTableName().getQualifiedName());
if (parseKeywordIf("USING"))
parseIdentifier();
CreateTableOnCommitStep onCommitStep;
CreateTableCommentStep commentStep;
List<Field<?>> fields = new ArrayList<>();
List<Constraint> constraints = new ArrayList<>();
List<Index> indexes = new ArrayList<>();
boolean primary = false;
boolean identity = false;
boolean readonly = false;
boolean ctas = false;
if (!peekSelectOrWith(true) && parseIf('(')) {
columnLoop: do {
int p = position();
ConstraintTypeStep constraint = parseConstraintNameSpecification();
if (parsePrimaryKeyClusteredNonClusteredKeywordIf()) {
if (primary)
throw exception("Duplicate primary key specification");
primary = true;
constraints.add(parsePrimaryKeySpecification(constraint));
continue columnLoop;
} else if (parseKeywordIf("UNIQUE")) {
if (!parseKeywordIf("KEY"))
parseKeywordIf("INDEX");
// [#9132] Avoid parsing "using" as an identifier
parseUsingIndexTypeIf();
// [#7268] MySQL has some legacy syntax where an index name
// can override a constraint name
Name index = parseIdentifierIf();
if (index != null)
constraint = constraint(index);
constraints.add(parseUniqueSpecification(constraint));
continue columnLoop;
} else if (parseKeywordIf("FOREIGN KEY")) {
constraints.add(parseForeignKeySpecification(constraint));
continue columnLoop;
} else if (parseKeywordIf("CHECK")) {
constraints.add(parseCheckSpecification(constraint));
continue columnLoop;
} else if (constraint == null && parseIndexOrKeyIf()) {
parseUsingIndexTypeIf();
int p2 = position();
// Look ahead if the next tokens indicate a MySQL index definition
if (parseIf('(') || (parseDataTypeIf(false) == null && parseIdentifierIf() != null && parseUsingIndexTypeIf() && parseIf('('))) {
position(p2);
indexes.add(parseIndexSpecification(tableName));
parseUsingIndexTypeIf();
continue columnLoop;
} else {
position(p);
}
} else if (constraint != null)
throw expected("CHECK", "CONSTRAINT", "FOREIGN KEY", "INDEX", "KEY", "PRIMARY KEY", "UNIQUE");
Name fieldName = parseIdentifier();
boolean skipType = peek(',') || peek(')');
// If only we had multiple return values or destructuring...
ParseInlineConstraints inlineConstraints = parseInlineConstraints(fieldName, !skipType ? parseDataType() : SQLDataType.OTHER, constraints, primary, identity, readonly);
primary = inlineConstraints.primary;
identity = inlineConstraints.identity;
fields.add(field(fieldName, inlineConstraints.type, inlineConstraints.fieldComment));
} while (parseIf(','));
if (fields.isEmpty())
throw expected("At least one column");
parse(')');
} else
ctas = true;
CreateTableElementListStep elementListStep = ifNotExists ? temporary ? dsl.createTemporaryTableIfNotExists(tableName) : dsl.createTableIfNotExists(tableName) : temporary ? dsl.createTemporaryTable(tableName) : dsl.createTable(tableName);
if (!fields.isEmpty())
elementListStep = elementListStep.columns(fields);
CreateTableElementListStep constraintStep = constraints.isEmpty() ? elementListStep : elementListStep.constraints(constraints);
CreateTableAsStep asStep = indexes.isEmpty() ? constraintStep : constraintStep.indexes(indexes);
// [#6133] Historically, the jOOQ API places the ON COMMIT clause after
// the AS clause, which doesn't correspond to dialect implementations
Function<CreateTableOnCommitStep, CreateTableCommentStep> onCommit;
if (temporary && parseKeywordIf("ON COMMIT")) {
if (parseKeywordIf("DELETE ROWS"))
onCommit = CreateTableOnCommitStep::onCommitDeleteRows;
else if (parseKeywordIf("DROP"))
onCommit = CreateTableOnCommitStep::onCommitDrop;
else if (parseKeywordIf("PRESERVE ROWS"))
onCommit = CreateTableOnCommitStep::onCommitPreserveRows;
else
throw unsupportedClause();
} else
onCommit = s -> s;
// keyword only for empty field lists
if (parseKeywordIf("AS") || fields.isEmpty() && peekSelectOrWith(true)) {
boolean previousMetaLookupsForceIgnore = metaLookupsForceIgnore();
CreateTableWithDataStep withDataStep = asStep.as((Select<Record>) metaLookupsForceIgnore(false).parseQuery(true, true));
metaLookupsForceIgnore(previousMetaLookupsForceIgnore);
onCommitStep = parseKeywordIf("WITH DATA") ? withDataStep.withData() : parseKeywordIf("WITH NO DATA") ? withDataStep.withNoData() : withDataStep;
} else if (ctas)
throw expected("AS, WITH, SELECT, or (");
else
onCommitStep = asStep;
commentStep = onCommit.apply(onCommitStep);
List<SQL> storage = new ArrayList<>();
Comment comment = null;
storageLoop: for (boolean first = true; ; first = false) {
boolean optional = first || !parseIf(',');
Keyword keyword = null;
// MySQL storage clauses (see: https://dev.mysql.com/doc/refman/5.7/en/create-table.html)
if ((keyword = parseAndGetKeywordIf("AUTO_INCREMENT")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("AVG_ROW_LENGTH")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("CHARACTER SET")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("DEFAULT CHARACTER SET")) != null || (keyword = parseAndGetKeywordIf("DEFAULT CHARSET")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("CHECKSUM")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOne()));
} else if ((keyword = parseAndGetKeywordIf("COLLATE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("DEFAULT COLLATE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else // [#10164] In a statement batch, this could already be the next statement
if (!peekKeyword("COMMENT ON") && parseKeywordIf("COMMENT")) {
if (!parseIf('='))
parseKeywordIf("IS");
comment = parseComment();
} else if (peekKeyword("OPTIONS")) {
comment = parseOptionsDescription();
} else if ((keyword = parseAndGetKeywordIf("COMPRESSION")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("CONNECTION")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("DATA DIRECTORY")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("INDEX DIRECTORY")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("DELAY_KEY_WRITE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOne()));
} else if ((keyword = parseAndGetKeywordIf("ENCRYPTION")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("ENGINE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("INSERT_METHOD")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseAndGetKeyword("NO", "FIRST", "LAST")));
} else if ((keyword = parseAndGetKeywordIf("KEY_BLOCK_SIZE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("MAX_ROWS")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("MIN_ROWS")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("PACK_KEYS")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOneDefault()));
} else if ((keyword = parseAndGetKeywordIf("PASSWORD")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("ROW_FORMAT")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseAndGetKeyword("DEFAULT", "DYNAMIC", "FIXED", "COMPRESSED", "REDUNDANT", "COMPACT")));
} else if ((keyword = parseAndGetKeywordIf("STATS_AUTO_RECALC")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOneDefault()));
} else if ((keyword = parseAndGetKeywordIf("STATS_PERSISTENT")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOneDefault()));
} else if ((keyword = parseAndGetKeywordIf("STATS_SAMPLE_PAGES")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("TABLESPACE")) != null) {
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
if ((keyword = parseAndGetKeywordIf("STORAGE")) != null)
storage.add(sql("{0} {1}", keyword, parseAndGetKeyword("DISK", "MEMORY", "DEFAULT")));
} else if ((keyword = parseAndGetKeywordIf("UNION")) != null) {
parseIf('=');
parse('(');
storage.add(sql("{0} ({1})", keyword, list(parseIdentifiers())));
parse(')');
} else if (optional)
break storageLoop;
else
throw expected("storage clause after ','");
}
CreateTableStorageStep storageStep = comment != null ? commentStep.comment(comment) : commentStep;
if (storage.size() > 0)
return storageStep.storage(new SQLConcatenationImpl(storage.toArray(EMPTY_QUERYPART)));
else
return storageStep;
}
use of org.jooq.Comment in project jOOQ by jOOQ.
the class AlterTableImpl method accept1.
private final void accept1(Context<?> ctx) {
SQLDialect family = ctx.family();
boolean omitAlterTable = (renameConstraint != null && family == HSQLDB) || (renameColumn != null && SUPPORT_RENAME_COLUMN.contains(ctx.dialect()));
boolean renameTable = renameTo != null && SUPPORT_RENAME_TABLE.contains(ctx.dialect());
boolean renameObject = renameTo != null && (false);
if (!omitAlterTable) {
ctx.start(ALTER_TABLE_TABLE).visit(renameObject ? K_RENAME_OBJECT : renameTable ? K_RENAME_TABLE : K_ALTER_TABLE);
if (ifExists && supportsIfExists(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').visit(table).end(ALTER_TABLE_TABLE).formatIndentStart().formatSeparator();
}
if (comment != null) {
ctx.visit(K_COMMENT).sql(' ').visit(comment);
} else if (renameTo != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_RENAME);
if (NO_SUPPORT_RENAME_QUALIFIED_TABLE.contains(ctx.dialect()))
ctx.qualify(false);
ctx.visit(renameObject || renameTable ? K_TO : K_RENAME_TO).sql(' ').visit(renameTo);
if (NO_SUPPORT_RENAME_QUALIFIED_TABLE.contains(ctx.dialect()))
ctx.qualify(qualify);
ctx.end(ALTER_TABLE_RENAME);
} else if (renameColumn != null) {
ctx.start(ALTER_TABLE_RENAME_COLUMN);
switch(ctx.family()) {
case DERBY:
ctx.visit(K_RENAME_COLUMN).sql(' ').visit(renameColumn).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
case H2:
case HSQLDB:
ctx.visit(K_ALTER_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_RENAME_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
case FIREBIRD:
ctx.visit(K_ALTER_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
default:
ctx.visit(K_RENAME_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
}
ctx.end(ALTER_TABLE_RENAME_COLUMN);
} else if (renameIndex != null) {
ctx.start(ALTER_TABLE_RENAME_INDEX).visit(K_RENAME_INDEX).sql(' ').qualify(false, c -> c.visit(renameIndex)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameIndexTo)).end(ALTER_TABLE_RENAME_INDEX);
} else if (renameConstraint != null) {
ctx.start(ALTER_TABLE_RENAME_CONSTRAINT);
ctx.data(DATA_CONSTRAINT_REFERENCE, true, c1 -> {
if (family == HSQLDB)
c1.visit(K_ALTER_CONSTRAINT).sql(' ').qualify(false, c2 -> c2.visit(renameConstraint)).formatSeparator().visit(K_RENAME_TO).sql(' ').qualify(false, c2 -> c2.visit(renameConstraintTo));
else
c1.visit(K_RENAME_CONSTRAINT).sql(' ').qualify(false, c2 -> c2.visit(renameConstraint)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c2 -> c2.visit(renameConstraintTo));
});
ctx.end(ALTER_TABLE_RENAME_CONSTRAINT);
} else if (add != null) {
boolean multiAdd = REQUIRE_REPEAT_ADD_ON_MULTI_ALTER.contains(ctx.dialect());
boolean parens = !multiAdd;
boolean comma = true;
ctx.start(ALTER_TABLE_ADD).visit(addColumnKeyword(ctx)).sql(' ');
if (parens)
ctx.sql('(');
boolean indent = !multiAdd && add.size() > 1;
if (indent)
ctx.formatIndentStart().formatNewLine();
for (int i = 0; i < add.size(); i++) {
if (i > 0) {
ctx.sql(comma ? "," : "").formatSeparator();
if (multiAdd)
ctx.visit(addColumnKeyword(ctx)).sql(' ');
}
TableElement part = add.get(i);
ctx.qualify(false, c -> c.visit(part));
if (part instanceof Field) {
Field<?> f = (Field<?>) part;
ctx.sql(' ');
toSQLDDLTypeDeclarationForAddition(ctx, f.getDataType());
}
}
if (indent)
ctx.formatIndentEnd().formatNewLine();
if (parens)
ctx.sql(')');
acceptFirstBeforeAfter(ctx);
ctx.end(ALTER_TABLE_ADD);
} else if (addColumn != null) {
ctx.start(ALTER_TABLE_ADD).visit(addColumnKeyword(ctx)).sql(' ');
if (ifNotExistsColumn && supportsIfNotExistsColumn(ctx))
ctx.visit(K_IF_NOT_EXISTS).sql(' ');
ctx.qualify(false, c -> c.visit(addColumn)).sql(' ');
toSQLDDLTypeDeclarationForAddition(ctx, addColumnType);
acceptFirstBeforeAfter(ctx);
ctx.end(ALTER_TABLE_ADD);
} else if (addConstraint != null) {
ctx.start(ALTER_TABLE_ADD);
ctx.visit(K_ADD).sql(' ');
ctx.visit(addConstraint);
ctx.end(ALTER_TABLE_ADD);
} else if (alterConstraint != null) {
ctx.start(ALTER_TABLE_ALTER);
ctx.data(DATA_CONSTRAINT_REFERENCE, true, c -> {
switch(family) {
default:
ctx.visit(K_ALTER);
break;
}
ctx.sql(' ').visit(K_CONSTRAINT).sql(' ').visit(alterConstraint);
ConstraintImpl.acceptEnforced(ctx, alterConstraintEnforced);
});
ctx.end(ALTER_TABLE_ALTER);
} else if (alterColumn != null) {
ctx.start(ALTER_TABLE_ALTER);
switch(family) {
case CUBRID:
case MARIADB:
case MYSQL:
{
// MySQL's CHANGE COLUMN clause has a mandatory RENAMING syntax...
if (alterColumnDefault == null && !alterColumnDropDefault)
ctx.visit(K_CHANGE_COLUMN).sql(' ').qualify(false, c -> c.visit(alterColumn));
else
ctx.visit(K_ALTER_COLUMN);
break;
}
default:
ctx.visit(K_ALTER);
break;
}
ctx.sql(' ');
ctx.qualify(false, c -> c.visit(alterColumn));
if (alterColumnType != null) {
switch(family) {
case DERBY:
ctx.sql(' ').visit(K_SET_DATA_TYPE);
break;
case FIREBIRD:
case POSTGRES:
case YUGABYTEDB:
ctx.sql(' ').visit(K_TYPE);
break;
}
ctx.sql(' ');
toSQLDDLTypeDeclaration(ctx, alterColumnType);
toSQLDDLTypeDeclarationIdentityBeforeNull(ctx, alterColumnType);
// [#3805] Some databases cannot change the type and the NOT NULL constraint in a single statement
if (!NO_SUPPORT_ALTER_TYPE_AND_NULL.contains(ctx.dialect())) {
switch(alterColumnType.nullability()) {
case NULL:
ctx.sql(' ').visit(K_NULL);
break;
case NOT_NULL:
ctx.sql(' ').visit(K_NOT_NULL);
break;
case DEFAULT:
break;
}
}
toSQLDDLTypeDeclarationIdentityAfterNull(ctx, alterColumnType);
} else if (alterColumnDefault != null) {
ctx.start(ALTER_TABLE_ALTER_DEFAULT);
switch(family) {
default:
ctx.sql(' ').visit(K_SET_DEFAULT);
break;
}
ctx.sql(' ').visit(alterColumnDefault).end(ALTER_TABLE_ALTER_DEFAULT);
} else if (alterColumnDropDefault) {
ctx.start(ALTER_TABLE_ALTER_DEFAULT);
switch(family) {
case MARIADB:
case MYSQL:
ctx.sql(' ').visit(K_SET_DEFAULT).sql(' ').visit(K_NULL);
break;
default:
ctx.sql(' ').visit(K_DROP_DEFAULT);
break;
}
ctx.end(ALTER_TABLE_ALTER_DEFAULT);
} else if (alterColumnNullability != null) {
ctx.start(ALTER_TABLE_ALTER_NULL);
switch(ctx.family()) {
default:
ctx.sql(' ').visit(alterColumnNullability.nullable() ? K_DROP_NOT_NULL : K_SET_NOT_NULL);
break;
}
ctx.end(ALTER_TABLE_ALTER_NULL);
}
ctx.end(ALTER_TABLE_ALTER);
} else if (dropColumns != null) {
ctx.start(ALTER_TABLE_DROP);
if (REQUIRE_REPEAT_DROP_ON_MULTI_ALTER.contains(ctx.dialect())) {
String separator = "";
for (Field<?> dropColumn : dropColumns) {
ctx.sql(separator);
acceptDropColumn(ctx);
if (ifExistsColumn && supportsIfExistsColumn(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').qualify(false, c -> c.visit(dropColumn));
separator = ", ";
}
} else {
acceptDropColumn(ctx);
if (ifExistsColumn && supportsIfExistsColumn(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ');
ctx.qualify(false, c -> c.visit(dropColumns));
}
acceptCascade(ctx);
ctx.end(ALTER_TABLE_DROP);
} else if (dropConstraint != null) {
ctx.start(ALTER_TABLE_DROP);
ctx.data(DATA_CONSTRAINT_REFERENCE, true, c -> {
if (dropConstraintType == FOREIGN_KEY && NO_SUPPORT_DROP_CONSTRAINT.contains(c.dialect())) {
c.visit(K_DROP).sql(' ').visit(K_FOREIGN_KEY).sql(' ').visit(dropConstraint);
} else if (dropConstraintType == PRIMARY_KEY && NO_SUPPORT_DROP_CONSTRAINT.contains(c.dialect())) {
c.visit(K_DROP).sql(' ').visit(K_PRIMARY_KEY);
} else {
// [#9382] In some dialects, unnamed UNIQUE constraints can be
// dropped by dropping their declarations.
c.visit(dropConstraint.getUnqualifiedName().empty() ? K_DROP : K_DROP_CONSTRAINT).sql(' ');
if (ifExistsConstraint && !NO_SUPPORT_IF_EXISTS_CONSTRAINT.contains(c.dialect()))
c.visit(K_IF_EXISTS).sql(' ');
c.visit(dropConstraint);
}
acceptCascade(c);
});
ctx.end(ALTER_TABLE_DROP);
} else if (dropConstraintType == PRIMARY_KEY) {
ctx.start(ALTER_TABLE_DROP);
ctx.visit(K_DROP).sql(' ').visit(K_PRIMARY_KEY);
ctx.end(ALTER_TABLE_DROP);
}
if (!omitAlterTable)
ctx.formatIndentEnd();
}
Aggregations