|
1 | 1 | use std::time::Duration; |
2 | 2 |
|
3 | | -use bson::{Bson, Document}; |
| 3 | +use bson::{Bson, Document, RawDocumentBuf}; |
4 | 4 |
|
5 | 5 | use crate::{ |
6 | 6 | client::session::TransactionState, |
@@ -46,6 +46,26 @@ impl Database { |
46 | 46 | } |
47 | 47 | } |
48 | 48 |
|
| 49 | + /// Runs a database-level command. |
| 50 | + /// |
| 51 | + /// Note that no inspection is done on `doc`, so the command will not use the database's default |
| 52 | + /// read concern or write concern. If specific read concern or write concern is desired, it must |
| 53 | + /// be specified manually. |
| 54 | + /// Please note that run_command doesn't validate WriteConcerns passed into the body of the |
| 55 | + /// command document. |
| 56 | + /// |
| 57 | + /// `await` will return d[`Result<Document>`]. |
| 58 | + #[deeplink] |
| 59 | + #[options_doc(run_command)] |
| 60 | + pub fn raw_run_command(&self, command: RawDocumentBuf) -> RawRunCommand { |
| 61 | + RawRunCommand { |
| 62 | + db: self, |
| 63 | + command, |
| 64 | + options: None, |
| 65 | + session: None, |
| 66 | + } |
| 67 | + } |
| 68 | + |
49 | 69 | /// Runs a database-level command and returns a cursor to the response. |
50 | 70 | /// |
51 | 71 | /// `await` will return d[`Result<Cursor<Document>>`] or a |
@@ -152,6 +172,68 @@ impl<'a> Action for RunCommand<'a> { |
152 | 172 | } |
153 | 173 | } |
154 | 174 |
|
| 175 | +/// Run a database-level command. Create with [`Database::raw_run_command`]. |
| 176 | +#[must_use] |
| 177 | +pub struct RawRunCommand<'a> { |
| 178 | + db: &'a Database, |
| 179 | + command: RawDocumentBuf, |
| 180 | + options: Option<RunCommandOptions>, |
| 181 | + session: Option<&'a mut ClientSession>, |
| 182 | +} |
| 183 | + |
| 184 | +#[option_setters(crate::db::options::RunCommandOptions)] |
| 185 | +#[export_doc(run_raw_command)] |
| 186 | +impl<'a> RawRunCommand<'a> { |
| 187 | + /// Run the command using the provided [`ClientSession`]. |
| 188 | + pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self { |
| 189 | + self.session = Some(value.into()); |
| 190 | + self |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +#[action_impl] |
| 195 | +impl<'a> Action for RawRunCommand<'a> { |
| 196 | + type Future = RunRawCommandFuture; |
| 197 | + |
| 198 | + async fn execute(self) -> Result<Document> { |
| 199 | + let mut selection_criteria = self.options.and_then(|o| o.selection_criteria); |
| 200 | + if let Some(session) = &self.session { |
| 201 | + match session.transaction.state { |
| 202 | + TransactionState::Starting | TransactionState::InProgress => { |
| 203 | + if self.command.get("readConcern").is_ok() { |
| 204 | + return Err(ErrorKind::InvalidArgument { |
| 205 | + message: "Cannot set read concern after starting a transaction".into(), |
| 206 | + } |
| 207 | + .into()); |
| 208 | + } |
| 209 | + selection_criteria = match selection_criteria { |
| 210 | + Some(selection_criteria) => Some(selection_criteria), |
| 211 | + None => { |
| 212 | + if let Some(ref options) = session.transaction.options { |
| 213 | + options.selection_criteria.clone() |
| 214 | + } else { |
| 215 | + None |
| 216 | + } |
| 217 | + } |
| 218 | + }; |
| 219 | + } |
| 220 | + _ => {} |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + let operation = run_command::RunCommand::new_raw( |
| 225 | + self.db.name().into(), |
| 226 | + self.command, |
| 227 | + selection_criteria, |
| 228 | + None, |
| 229 | + )?; |
| 230 | + self.db |
| 231 | + .client() |
| 232 | + .execute_operation(operation, self.session) |
| 233 | + .await |
| 234 | + } |
| 235 | +} |
| 236 | + |
155 | 237 | /// Runs a database-level command and returns a cursor to the response. Create with |
156 | 238 | /// [`Database::run_cursor_command`]. |
157 | 239 | #[must_use] |
|
0 commit comments