use of org.jooq.exception.DataDefinitionException in project jOOQ by jOOQ.
the class Interpreter method accept0.
private final void accept0(TruncateImpl<?> query) {
Table<?> table = query.$table();
MutableSchema schema = getSchema(table.getSchema());
MutableTable existing = schema.table(table);
if (existing == null)
throw notExists(table);
else if (!existing.options.type().isTable())
throw objectNotTable(table);
else if (query.$cascade() != Cascade.CASCADE && existing.hasReferencingKeys())
throw new DataDefinitionException("Cannot truncate table referenced by other tables. Use CASCADE: " + table);
}
use of org.jooq.exception.DataDefinitionException in project jOOQ by jOOQ.
the class Interpreter method accept0.
private final void accept0(DropDomainImpl query) {
Domain<?> domain = query.$domain();
MutableSchema schema = getSchema(domain.getSchema());
MutableDomain existing = schema.domain(domain);
if (existing == null) {
if (!query.$ifExists())
throw notExists(domain);
return;
}
if (query.$cascade() != Cascade.CASCADE && !existing.fields.isEmpty())
throw new DataDefinitionException("Domain " + domain.getQualifiedName() + " is still being referenced by fields.");
List<MutableField> field = new ArrayList<>(existing.fields);
for (MutableField mf : field) dropColumns(mf.table, existing.fields, CASCADE);
schema.domains.remove(existing);
}
use of org.jooq.exception.DataDefinitionException 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.exception.DataDefinitionException in project jOOQ by jOOQ.
the class SQLiteDatabase method loadCheckConstraints.
@Override
protected void loadCheckConstraints(DefaultRelations r) throws SQLException {
DSLContext ctx = create().configuration().deriveSettings(s -> s.withInterpreterDelayForeignKeyDeclarations(true)).dsl();
SchemaDefinition schema = getSchemata().get(0);
for (Record record : ctx.select(SQLiteMaster.TBL_NAME, SQLiteMaster.SQL).from(SQLITE_MASTER).where(SQLiteMaster.SQL.likeIgnoreCase("%CHECK%")).orderBy(SQLiteMaster.TBL_NAME)) {
TableDefinition table = getTable(schema, record.get(SQLiteMaster.TBL_NAME));
if (table != null) {
String sql = record.get(SQLiteMaster.SQL);
try {
Query query = ctx.parser().parseQuery(sql);
for (Table<?> t : ctx.meta(query).getTables(table.getName())) {
for (Check<?> check : t.getChecks()) {
r.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, check.getName(), ctx.renderInlined(check.condition()), check.enforced()));
}
}
} catch (ParserException e) {
log.info("Cannot parse SQL: " + sql, e);
} catch (DataDefinitionException e) {
log.info("Cannot interpret SQL: " + sql, e);
}
}
}
}
use of org.jooq.exception.DataDefinitionException in project jOOQ by jOOQ.
the class SQLiteTableDefinition method interpretedTable.
Table<?> interpretedTable() {
if (interpretedTable == null) {
try {
Configuration c = create().configuration().derive();
c.settings().withParseWithMetaLookups(THROW_ON_FAILURE);
Query query = create().parser().parseQuery(getSource());
for (Table<?> t : create().meta(query).getTables(getInputName())) return interpretedTable = t;
} catch (ParserException e) {
log.info("Cannot parse SQL: " + getSource(), e);
} catch (DataDefinitionException e) {
log.info("Cannot interpret SQL: " + getSource(), e);
}
}
return interpretedTable;
}
Aggregations