Skip to main content

mongodb/action/
drop.rs

1#[cfg(feature = "in-use-encryption")]
2use crate::bson::Document;
3
4use crate::{
5    coll::options::DropCollectionOptions,
6    db::options::DropDatabaseOptions,
7    error::Result,
8    operation::drop_database,
9    options::WriteConcern,
10    ClientSession,
11    Collection,
12    Database,
13};
14
15use super::{action_impl, deeplink, export_doc, option_setters, options_doc, CollRef};
16
17impl Database {
18    /// Drops the database, deleting all data, collections, and indexes stored in it.
19    ///
20    /// `await` will return d[`Result<()>`].
21    #[deeplink]
22    #[options_doc(drop_db)]
23    pub fn drop(&self) -> DropDatabase<'_> {
24        DropDatabase {
25            db: self,
26            options: None,
27            session: None,
28        }
29    }
30}
31
32#[cfg(feature = "sync")]
33impl crate::sync::Database {
34    /// Drops the database, deleting all data, collections, and indexes stored in it.
35    ///
36    /// [`run`](DropDatabase::run) will return d[`Result<()>`].
37    #[deeplink]
38    #[options_doc(drop_db, "run")]
39    pub fn drop(&self) -> DropDatabase<'_> {
40        self.async_database.drop()
41    }
42}
43
44/// Drops the database, deleting all data, collections, and indexes stored in it.  Construct with
45/// [`Database::drop`].
46#[must_use]
47pub struct DropDatabase<'a> {
48    db: &'a Database,
49    options: Option<DropDatabaseOptions>,
50    session: Option<&'a mut ClientSession>,
51}
52
53#[option_setters(crate::db::options::DropDatabaseOptions)]
54#[export_doc(drop_db)]
55impl<'a> DropDatabase<'a> {
56    /// Runs the drop using the provided session.
57    pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
58        self.session = Some(value.into());
59        self
60    }
61}
62
63#[action_impl]
64impl<'a> Action for DropDatabase<'a> {
65    type Future = DropDatabaseFuture;
66
67    async fn execute(mut self) -> Result<()> {
68        let op = drop_database::DropDatabase::new(self.db.clone(), self.options);
69        self.db.client().execute_operation(op, self.session).await
70    }
71}
72
73impl<T> Collection<T>
74where
75    T: Send + Sync,
76{
77    /// Drops the collection, deleting all data and indexes stored in it.
78    ///
79    /// `await` will return d[`Result<()>`].
80    #[deeplink]
81    #[options_doc(drop_coll)]
82    pub fn drop(&self) -> DropCollection<'_> {
83        DropCollection {
84            cr: CollRef::new(self),
85            options: None,
86            session: None,
87        }
88    }
89}
90
91#[cfg(feature = "sync")]
92impl<T> crate::sync::Collection<T>
93where
94    T: Send + Sync,
95{
96    /// Drops the collection, deleting all data and indexes stored in it.
97    ///
98    /// [`run`](DropCollection::run) will return d[`Result<()>`].
99    #[deeplink]
100    #[options_doc(drop_coll, "run")]
101    pub fn drop(&self) -> DropCollection<'_> {
102        self.async_collection.drop()
103    }
104}
105
106/// Drops the collection, deleting all data and indexes stored in it.  Construct with
107/// [`Collection::drop`].
108#[must_use]
109pub struct DropCollection<'a> {
110    pub(crate) cr: CollRef<'a>,
111    pub(crate) options: Option<DropCollectionOptions>,
112    pub(crate) session: Option<&'a mut ClientSession>,
113}
114
115#[option_setters(crate::coll::options::DropCollectionOptions)]
116#[export_doc(drop_coll)]
117impl<'a> DropCollection<'a> {
118    /// Runs the drop using the provided session.
119    pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
120        self.session = Some(value.into());
121        self
122    }
123}
124
125// Action impl in src/coll/action/drop.rs