Skip to main content

risingwave_frontend/handler/
drop_view.rs

1// Copyright 2022 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use pgwire::pg_response::{PgResponse, StatementType};
16use risingwave_sqlparser::ast::ObjectName;
17
18use super::RwPgResponse;
19use crate::binder::Binder;
20use crate::catalog::RelationKind;
21use crate::catalog::root_catalog::SchemaPath;
22use crate::error::Result;
23use crate::handler::HandlerArgs;
24
25pub async fn handle_drop_view(
26    handler_args: HandlerArgs,
27    table_name: ObjectName,
28    if_exists: bool,
29    cascade: bool,
30) -> Result<RwPgResponse> {
31    let session = handler_args.session;
32    let db_name = &session.database();
33    let (schema_name, table_name) = Binder::resolve_schema_qualified_name(db_name, &table_name)?;
34    let search_path = session.config().search_path();
35    let user_name = &session.user_name();
36
37    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
38
39    let view_id = {
40        let reader = session.env().catalog_reader().read_guard();
41        let (view, schema_name) =
42            match reader.get_view_by_name(&session.database(), schema_path, &table_name) {
43                Ok((t, s)) => (t, s),
44                Err(e) => {
45                    return if if_exists {
46                        Ok(RwPgResponse::builder(StatementType::DROP_VIEW)
47                            .notice(format!("view \"{}\" does not exist, skipping", table_name))
48                            .into())
49                    } else {
50                        Err(reader
51                            .wrong_kind_for_drop(
52                                db_name,
53                                schema_path,
54                                &table_name,
55                                RelationKind::View,
56                            )
57                            .unwrap_or(e)
58                            .into())
59                    };
60                }
61            };
62
63        session.check_privilege_for_drop_alter(schema_name, &**view)?;
64
65        view.id
66    };
67
68    let catalog_writer = session.catalog_writer()?;
69    catalog_writer.drop_view(view_id, cascade).await?;
70
71    Ok(PgResponse::empty_result(StatementType::DROP_VIEW))
72}