in baremaps-calcite/src/main/java/org/apache/baremaps/calcite/postgres/PostgresDdlExecutor.java [342:437]
public void execute(SqlDropObject drop,
CalcitePrepare.Context context) {
final SchemaInfo schemaInfo =
schema(context, false, drop.name);
final @Nullable CalciteSchema schema =
schemaInfo.schema(); // null if schema does not exist
final String objectName = schemaInfo.name();
boolean existed;
switch (drop.getKind()) {
case DROP_TABLE:
case DROP_MATERIALIZED_VIEW:
Table materializedView =
schema != null
&& drop.getKind() == SqlKind.DROP_MATERIALIZED_VIEW
? schema.plus().getTable(objectName)
: null;
existed = schema != null && schema.removeTable(objectName);
if (existed) {
if (materializedView instanceof Wrapper) {
((Wrapper) materializedView).maybeUnwrap(MaterializationKey.class)
.ifPresent(materializationKey -> MaterializationService.instance()
.removeMaterialization(materializationKey));
}
if (drop.getKind() == SqlKind.DROP_TABLE) {
// For PostgreSQL, we also need to drop the physical table
try {
DataSource ds = getDataSource(context);
try (Connection connection = ds.getConnection();
PreparedStatement stmt =
connection.prepareStatement("DROP TABLE IF EXISTS \"" + objectName + "\"")) {
stmt.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException("Error dropping table in PostgreSQL: " + objectName, e);
}
} else if (drop.getKind() == SqlKind.DROP_MATERIALIZED_VIEW) {
// For PostgreSQL, we also need to drop the physical materialized view
try {
DataSource ds = getDataSource(context);
try (Connection connection = ds.getConnection();
PreparedStatement stmt = connection.prepareStatement(
"DROP MATERIALIZED VIEW IF EXISTS \"" + objectName + "\"")) {
stmt.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException(
"Error dropping materialized view in PostgreSQL: " + objectName, e);
}
}
} else if (!drop.ifExists) {
throw SqlUtil.newContextException(drop.name.getParserPosition(),
RESOURCE.tableNotFound(objectName));
}
break;
case DROP_VIEW:
// Not quite right: removes any other functions with the same name
existed = schema != null && schema.removeFunction(objectName);
if (existed) {
// For PostgreSQL, we also need to drop the physical view
try {
DataSource ds = getDataSource(context);
try (Connection connection = ds.getConnection();
PreparedStatement stmt =
connection.prepareStatement("DROP VIEW IF EXISTS \"" + objectName + "\"")) {
stmt.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException("Error dropping view in PostgreSQL: " + objectName, e);
}
} else if (!drop.ifExists) {
throw SqlUtil.newContextException(drop.name.getParserPosition(),
RESOURCE.viewNotFound(objectName));
}
break;
case DROP_TYPE:
existed = schema != null && schema.removeType(objectName);
if (!existed && !drop.ifExists) {
throw SqlUtil.newContextException(drop.name.getParserPosition(),
RESOURCE.typeNotFound(objectName));
}
break;
case DROP_FUNCTION:
existed = schema != null && schema.removeFunction(objectName);
if (!existed && !drop.ifExists) {
throw SqlUtil.newContextException(drop.name.getParserPosition(),
RESOURCE.functionNotFound(objectName));
}
break;
case OTHER_DDL:
default:
throw new AssertionError(drop.getKind());
}
}