risingwave_frontend/handler/
drop_schema.rs1use pgwire::pg_response::{PgResponse, StatementType};
16use risingwave_common::catalog::is_system_schema;
17use risingwave_sqlparser::ast::{DropMode, ObjectName};
18
19use super::RwPgResponse;
20use crate::binder::Binder;
21use crate::error::{ErrorCode, Result};
22use crate::handler::HandlerArgs;
23
24pub async fn handle_drop_schema(
25 handler_args: HandlerArgs,
26 schema_name: ObjectName,
27 if_exist: bool,
28 mode: Option<DropMode>,
29) -> Result<RwPgResponse> {
30 let session = handler_args.session;
31 let catalog_reader = session.env().catalog_reader();
32 let schema_name = Binder::resolve_schema_name(schema_name)?;
33
34 if is_system_schema(&schema_name) {
35 return Err(ErrorCode::ProtocolError(format!(
36 "cannot drop schema {} because it is required by the database system",
37 schema_name
38 ))
39 .into());
40 }
41
42 let schema = {
43 let reader = catalog_reader.read_guard();
44 match reader.get_schema_by_name(&session.database(), &schema_name) {
45 Ok(schema) => schema.clone(),
46 Err(err) => {
47 return if if_exist {
49 Ok(PgResponse::builder(StatementType::DROP_SCHEMA)
50 .notice(format!(
51 "schema \"{}\" does not exist, skipping",
52 schema_name
53 ))
54 .into())
55 } else {
56 Err(err.into())
57 };
58 }
59 }
60 };
61
62 let cascade = matches!(mode, Some(DropMode::Cascade));
65
66 session.check_privilege_for_drop_alter_db_schema(&schema)?;
67
68 let catalog_writer = session.catalog_writer()?;
69 catalog_writer.drop_schema(schema.id(), cascade).await?;
70 Ok(PgResponse::empty_result(StatementType::DROP_SCHEMA))
71}