Search in sources :

Example 1 with DropStatement

use of io.confluent.ksql.parser.tree.DropStatement in project ksql by confluentinc.

the class TopicDeleteInjector method inject.

@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
@Override
public <T extends Statement> ConfiguredStatement<T> inject(final ConfiguredStatement<T> statement) {
    if (!(statement.getStatement() instanceof DropStatement)) {
        return statement;
    }
    final DropStatement dropStatement = (DropStatement) statement.getStatement();
    if (!dropStatement.isDeleteTopic()) {
        return statement;
    }
    final SourceName sourceName = dropStatement.getName();
    final DataSource source = metastore.getSource(sourceName);
    if (source != null) {
        if (source.isSource()) {
            throw new KsqlException("Cannot delete topic for read-only source: " + sourceName.text());
        }
        checkTopicRefs(source);
        deleteTopic(source);
        final Closer closer = Closer.create();
        closer.register(() -> deleteKeySubject(source));
        closer.register(() -> deleteValueSubject(source));
        try {
            closer.close();
        } catch (final KsqlException e) {
            throw e;
        } catch (final Exception e) {
            throw new KsqlException(e);
        }
    } else if (!dropStatement.getIfExists()) {
        throw new KsqlException("Could not find source to delete topic for: " + statement);
    }
    final T withoutDelete = (T) dropStatement.withoutDeleteClause();
    final String withoutDeleteText = SqlFormatter.formatSql(withoutDelete) + ";";
    return statement.withStatement(withoutDeleteText, withoutDelete);
}
Also used : Closer(com.google.common.io.Closer) SourceName(io.confluent.ksql.name.SourceName) KsqlException(io.confluent.ksql.util.KsqlException) DropStatement(io.confluent.ksql.parser.tree.DropStatement) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource)

Aggregations

Closer (com.google.common.io.Closer)1 DataSource (io.confluent.ksql.metastore.model.DataSource)1 SourceName (io.confluent.ksql.name.SourceName)1 DropStatement (io.confluent.ksql.parser.tree.DropStatement)1 KsqlException (io.confluent.ksql.util.KsqlException)1