use of org.jooq.TableElement in project jOOQ by jOOQ.
the class Interpreter method accept0.
private final void accept0(AlterTableImpl query) {
Table<?> table = query.$table();
MutableSchema schema = getSchema(table.getSchema());
MutableTable existing = schema.table(table);
if (existing == null) {
if (!query.$ifExists())
throw notExists(table);
return;
} else if (!existing.options.type().isTable())
throw objectNotTable(table);
if (query.$add() != null) {
for (TableElement fc : query.$add()) if (fc instanceof Field && find(existing.fields, (Field<?>) fc) != null)
throw alreadyExists(fc);
else if (fc instanceof Constraint && !fc.getUnqualifiedName().empty() && existing.constraint((Constraint) fc) != null)
throw alreadyExists(fc);
// TODO: ReverseIterable is not a viable approach if we also allow constraints to be added this way
if (query.$addFirst()) {
for (Field<?> f : assertFields(query, reverseIterable(query.$add()))) addField(existing, 0, (UnqualifiedName) f.getUnqualifiedName(), f.getDataType());
} else if (query.$addBefore() != null) {
int index = indexOrFail(existing.fields, query.$addBefore());
for (Field<?> f : assertFields(query, reverseIterable(query.$add()))) addField(existing, index, (UnqualifiedName) f.getUnqualifiedName(), f.getDataType());
} else if (query.$addAfter() != null) {
int index = indexOrFail(existing.fields, query.$addAfter()) + 1;
for (Field<?> f : assertFields(query, reverseIterable(query.$add()))) addField(existing, index, (UnqualifiedName) f.getUnqualifiedName(), f.getDataType());
} else {
for (TableElement fc : query.$add()) if (fc instanceof Field)
addField(existing, Integer.MAX_VALUE, (UnqualifiedName) fc.getUnqualifiedName(), ((Field<?>) fc).getDataType());
else if (fc instanceof ConstraintImpl)
addConstraint(query, (ConstraintImpl) fc, existing);
else
throw unsupportedQuery(query);
}
} else if (query.$addColumn() != null) {
if (find(existing.fields, query.$addColumn()) != null)
if (!query.$ifNotExistsColumn())
throw alreadyExists(query.$addColumn());
else
return;
UnqualifiedName name = (UnqualifiedName) query.$addColumn().getUnqualifiedName();
DataType<?> dataType = query.$addColumnType();
if (query.$addFirst())
addField(existing, 0, name, dataType);
else if (query.$addBefore() != null)
addField(existing, indexOrFail(existing.fields, query.$addBefore()), name, dataType);
else if (query.$addAfter() != null)
addField(existing, indexOrFail(existing.fields, query.$addAfter()) + 1, name, dataType);
else
addField(existing, Integer.MAX_VALUE, name, dataType);
} else if (query.$addConstraint() != null) {
addConstraint(query, (ConstraintImpl) query.$addConstraint(), existing);
} else if (query.$alterColumn() != null) {
MutableField existingField = find(existing.fields, query.$alterColumn());
if (existingField == null)
if (!query.$ifExistsColumn())
throw notExists(query.$alterColumn());
else
return;
if (query.$alterColumnNullability() != null)
existingField.type = existingField.type.nullability(query.$alterColumnNullability());
else if (query.$alterColumnType() != null)
existingField.type = query.$alterColumnType().nullability(query.$alterColumnType().nullability() == Nullability.DEFAULT ? existingField.type.nullability() : query.$alterColumnType().nullability());
else if (query.$alterColumnDefault() != null)
existingField.type = existingField.type.default_((Field) query.$alterColumnDefault());
else if (query.$alterColumnDropDefault())
existingField.type = existingField.type.default_((Field) null);
else
throw unsupportedQuery(query);
} else if (query.$renameTo() != null && checkNotExists(schema, query.$renameTo())) {
existing.name((UnqualifiedName) query.$renameTo().getUnqualifiedName());
} else if (query.$renameColumn() != null) {
MutableField mf = find(existing.fields, query.$renameColumn());
if (mf == null)
throw notExists(query.$renameColumn());
else if (find(existing.fields, query.$renameColumnTo()) != null)
throw alreadyExists(query.$renameColumnTo());
else
mf.name((UnqualifiedName) query.$renameColumnTo().getUnqualifiedName());
} else if (query.$renameConstraint() != null) {
MutableConstraint mc = existing.constraint(query.$renameConstraint(), true);
if (existing.constraint(query.$renameConstraintTo()) != null)
throw alreadyExists(query.$renameConstraintTo());
else
mc.name((UnqualifiedName) query.$renameConstraintTo().getUnqualifiedName());
} else if (query.$alterConstraint() != null) {
existing.constraint(query.$alterConstraint(), true).enforced = query.$alterConstraintEnforced();
} else if (query.$dropColumns() != null) {
List<MutableField> fields = existing.fields(query.$dropColumns().toArray(EMPTY_FIELD), false);
if (fields.size() < query.$dropColumns().size() && !query.$ifExistsColumn())
existing.fields(query.$dropColumns().toArray(EMPTY_FIELD), true);
dropColumns(existing, fields, query.$dropCascade());
} else if (query.$dropConstraint() != null)
dropConstraint: {
ConstraintImpl impl = (ConstraintImpl) query.$dropConstraint();
if (impl.getUnqualifiedName().empty()) {
if (impl.$foreignKey() != null) {
throw new DataDefinitionException("Cannot drop unnamed foreign key");
} else if (impl.$check() != null) {
throw new DataDefinitionException("Cannot drop unnamed check constraint");
} else if (impl.$unique() != null) {
Iterator<MutableUniqueKey> uks = existing.uniqueKeys.iterator();
while (uks.hasNext()) {
MutableUniqueKey key = uks.next();
if (key.fieldsEquals(impl.$unique())) {
cascade(key, null, query.$dropCascade());
uks.remove();
break dropConstraint;
}
}
}
} else {
Iterator<MutableForeignKey> fks = existing.foreignKeys.iterator();
while (fks.hasNext()) {
if (fks.next().nameEquals((UnqualifiedName) impl.getUnqualifiedName())) {
fks.remove();
break dropConstraint;
}
}
if (query.$dropConstraintType() != FOREIGN_KEY) {
Iterator<MutableUniqueKey> uks = existing.uniqueKeys.iterator();
while (uks.hasNext()) {
MutableUniqueKey key = uks.next();
if (key.nameEquals((UnqualifiedName) impl.getUnqualifiedName())) {
cascade(key, null, query.$dropCascade());
uks.remove();
break dropConstraint;
}
}
Iterator<MutableCheck> chks = existing.checks.iterator();
while (chks.hasNext()) {
MutableCheck check = chks.next();
if (check.nameEquals((UnqualifiedName) impl.getUnqualifiedName())) {
chks.remove();
break dropConstraint;
}
}
if (existing.primaryKey != null) {
if (existing.primaryKey.nameEquals((UnqualifiedName) impl.getUnqualifiedName())) {
cascade(existing.primaryKey, null, query.$dropCascade());
existing.primaryKey = null;
break dropConstraint;
}
}
}
}
Iterator<DelayedForeignKey> it = delayedForeignKeyDeclarations.iterator();
while (it.hasNext()) {
DelayedForeignKey key = it.next();
if (existing.equals(key.table) && key.constraint.getUnqualifiedName().equals(impl.getUnqualifiedName())) {
it.remove();
break dropConstraint;
}
}
if (!query.$ifExistsConstraint())
throw notExists(query.$dropConstraint());
}
else if (query.$dropConstraintType() == PRIMARY_KEY) {
if (existing.primaryKey != null)
existing.primaryKey = null;
else
throw primaryKeyNotExists(table);
} else
throw unsupportedQuery(query);
}
use of org.jooq.TableElement 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