risingwave_frontend/handler/
use_db.rs1use pgwire::pg_response::StatementType;
16use risingwave_common::acl::AclMode;
17use risingwave_pb::user::grant_privilege::Object as GrantObject;
18use risingwave_sqlparser::ast::ObjectName;
19
20use crate::Binder;
21use crate::error::Result;
22use crate::handler::privilege::ObjectCheckItem;
23use crate::handler::{HandlerArgs, RwPgResponse};
24
25pub fn handle_use_db(handler_args: HandlerArgs, database_name: ObjectName) -> Result<RwPgResponse> {
26 let session = handler_args.session;
27 let database_name = Binder::resolve_database_name(database_name)?;
28
29 let (database_id, owner_id) = {
30 let catalog_reader = session.env().catalog_reader();
31 let reader = catalog_reader.read_guard();
32 let db = reader.get_database_by_name(&database_name)?;
33 (db.id(), db.owner)
34 };
35 session.check_privileges(&[ObjectCheckItem::new(
36 owner_id,
37 AclMode::Connect,
38 GrantObject::DatabaseId(database_id),
39 )])?;
40
41 let mut builder = RwPgResponse::builder(StatementType::USE);
42 builder = builder.notice(format!(
43 "You are now connected to database \"{}\" as user \"{}\".",
44 database_name,
45 session.user_name()
46 ));
47
48 session.reset_config("search_path")?;
50
51 session.update_database(database_name);
52
53 Ok(builder.into())
54}