Skip to content

Commit ae55d1a

Browse files
committed
raw_run_command
fix docs don't change formatting
1 parent 9b0ef52 commit ae55d1a

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/action/run_command.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::Duration;
22

3-
use bson::{Bson, Document};
3+
use bson::{Bson, Document, RawDocumentBuf};
44

55
use crate::{
66
client::session::TransactionState,
@@ -46,6 +46,26 @@ impl Database {
4646
}
4747
}
4848

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+
4969
/// Runs a database-level command and returns a cursor to the response.
5070
///
5171
/// `await` will return d[`Result<Cursor<Document>>`] or a
@@ -152,6 +172,68 @@ impl<'a> Action for RunCommand<'a> {
152172
}
153173
}
154174

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+
155237
/// Runs a database-level command and returns a cursor to the response. Create with
156238
/// [`Database::run_cursor_command`].
157239
#[must_use]

src/operation/run_command.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ impl<'conn> RunCommand<'conn> {
3333
})
3434
}
3535

36-
#[cfg(feature = "in-use-encryption")]
3736
pub(crate) fn new_raw(
3837
db: String,
3938
command: RawDocumentBuf,

0 commit comments

Comments
 (0)