dbmcp_postgres/tools/
drop_database.rs1use std::borrow::Cow;
4
5use dbmcp_server::types::{DropDatabaseRequest, MessageResponse};
6use dbmcp_sql::Connection as _;
7use dbmcp_sql::SqlError;
8use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
9use rmcp::model::{ErrorData, ToolAnnotations};
10
11use crate::PostgresHandler;
12use crate::connection::quote_ident;
13
14pub(crate) struct DropDatabaseTool;
16
17impl DropDatabaseTool {
18 const NAME: &'static str = "dropDatabase";
19 const TITLE: &'static str = "Drop Database";
20 const DESCRIPTION: &'static str = include_str!("../../assets/tools/drop_database.md");
21}
22
23impl ToolBase for DropDatabaseTool {
24 type Parameter = DropDatabaseRequest;
25 type Output = MessageResponse;
26 type Error = ErrorData;
27
28 fn name() -> Cow<'static, str> {
29 Self::NAME.into()
30 }
31
32 fn title() -> Option<String> {
33 Some(Self::TITLE.into())
34 }
35
36 fn description() -> Option<Cow<'static, str>> {
37 Some(Self::DESCRIPTION.into())
38 }
39
40 fn annotations() -> Option<ToolAnnotations> {
41 Some(
42 ToolAnnotations::new()
43 .read_only(false)
44 .destructive(true)
45 .idempotent(false)
46 .open_world(false),
47 )
48 }
49}
50
51impl AsyncTool<PostgresHandler> for DropDatabaseTool {
52 async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
53 handler.drop_database(params).await
54 }
55}
56
57impl PostgresHandler {
58 pub async fn drop_database(
70 &self,
71 DropDatabaseRequest { database }: DropDatabaseRequest,
72 ) -> Result<MessageResponse, ErrorData> {
73 if self.config.read_only {
74 return Err(SqlError::ReadOnlyViolation.into());
75 }
76
77 if self.connection.default_database_name() == database.as_str() {
79 return Err(SqlError::Query(format!("Cannot drop the currently connected database '{database}'.")).into());
80 }
81
82 let drop_sql = format!("DROP DATABASE {}", quote_ident(&database));
83 self.connection.execute(drop_sql.as_str(), None).await?;
84
85 self.connection.invalidate(&database).await;
86
87 Ok(MessageResponse {
88 message: format!("Database '{database}' dropped successfully."),
89 })
90 }
91}