1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use std::time::Duration;
use crate::bson::{Bson, Document, RawDocumentBuf};
use crate::{
bson_compat::RawResult,
client::session::TransactionState,
coll::options::CursorType,
db::options::{RunCommandOptions, RunCursorCommandOptions},
error::{ErrorKind, Result},
operation::{run_command, run_cursor_command},
selection_criteria::SelectionCriteria,
ClientSession,
Cursor,
Database,
SessionCursor,
};
use super::{
action_impl,
deeplink,
export_doc,
option_setters,
options_doc,
ExplicitSession,
ImplicitSession,
};
impl Database {
/// Runs a database-level command.
///
/// Note that no inspection is done on `doc`, so the command will not use the database's default
/// read concern or write concern. If specific read concern or write concern is desired, it must
/// be specified manually.
/// Please note that run_command doesn't validate WriteConcerns passed into the body of the
/// command document.
///
/// `await` will return d[`Result<Document>`].
#[deeplink]
#[options_doc(run_command)]
pub fn run_command(&self, command: Document) -> RunCommand {
RunCommand {
db: self,
command: RawDocumentBuf::try_from(&command),
options: None,
session: None,
}
}
/// Runs a database-level command.
///
/// Note that no inspection is done on `doc`, so the command will not use the database's default
/// read concern or write concern. If specific read concern or write concern is desired, it must
/// be specified manually.
/// Please note that run_raw_command doesn't validate WriteConcerns passed into the body of the
/// command document.
///
/// `await` will return d[`Result<Document>`].
#[deeplink]
#[options_doc(run_command)]
pub fn run_raw_command(&self, command: RawDocumentBuf) -> RunCommand {
RunCommand {
db: self,
command: Ok(command),
options: None,
session: None,
}
}
/// Runs a database-level command and returns a cursor to the response.
///
/// `await` will return d[`Result<Cursor<Document>>`] or a
/// d[`Result<SessionCursor<Document>>`] if a [`ClientSession`] is provided.
#[deeplink]
#[options_doc(run_cursor_command)]
pub fn run_cursor_command(&self, command: Document) -> RunCursorCommand {
RunCursorCommand {
db: self,
command: RawDocumentBuf::try_from(&command),
options: None,
session: ImplicitSession,
}
}
/// Runs a database-level command and returns a cursor to the response.
///
/// `await` will return d[`Result<Cursor<Document>>`] or a
/// d[`Result<SessionCursor<Document>>`] if a [`ClientSession`] is provided.
#[deeplink]
#[options_doc(run_cursor_command)]
pub fn run_raw_cursor_command(&self, command: RawDocumentBuf) -> RunCursorCommand {
RunCursorCommand {
db: self,
command: Ok(command),
options: None,
session: ImplicitSession,
}
}
}
#[cfg(feature = "sync")]
impl crate::sync::Database {
/// Runs a database-level command.
///
/// Note that no inspection is done on `doc`, so the command will not use the database's default
/// read concern or write concern. If specific read concern or write concern is desired, it must
/// be specified manually.
/// Please note that run_command doesn't validate WriteConcerns passed into the body of the
/// command document.
///
/// [`run`](RunCommand::run) will return d[`Result<Document>`].
#[deeplink]
#[options_doc(run_command, sync)]
pub fn run_command(&self, command: Document) -> RunCommand {
self.async_database.run_command(command)
}
/// Runs a database-level command.
///
/// Note that no inspection is done on `doc`, so the command will not use the database's default
/// read concern or write concern. If specific read concern or write concern is desired, it must
/// be specified manually.
/// Please note that run_raw_command doesn't validate WriteConcerns passed into the body of the
/// command document.
///
/// [`run`](RunCommand::run) will return d[`Result<Document>`].
#[deeplink]
#[options_doc(run_command, sync)]
pub fn run_raw_command(&self, command: RawDocumentBuf) -> RunCommand {
self.async_database.run_raw_command(command)
}
/// Runs a database-level command and returns a cursor to the response.
///
/// [`run`](RunCursorCommand::run) will return d[`Result<crate::sync::Cursor<Document>>`] or a
/// d[`Result<crate::sync::SessionCursor<Document>>`] if a [`ClientSession`] is provided.
#[deeplink]
#[options_doc(run_cursor_command, sync)]
pub fn run_cursor_command(&self, command: Document) -> RunCursorCommand {
self.async_database.run_cursor_command(command)
}
/// Runs a database-level command and returns a cursor to the response.
///
/// [`run`](RunCursorCommand::run) will return d[`Result<crate::sync::Cursor<Document>>`] or a
/// d[`Result<crate::sync::SessionCursor<Document>>`] if a [`ClientSession`] is provided.
#[deeplink]
#[options_doc(run_cursor_command, sync)]
pub fn run_raw_cursor_command(&self, command: RawDocumentBuf) -> RunCursorCommand {
self.async_database.run_raw_cursor_command(command)
}
}
/// Run a database-level command. Create with [`Database::run_command`].
#[must_use]
pub struct RunCommand<'a> {
db: &'a Database,
command: RawResult<RawDocumentBuf>,
options: Option<RunCommandOptions>,
session: Option<&'a mut ClientSession>,
}
#[option_setters(crate::db::options::RunCommandOptions)]
#[export_doc(run_command)]
impl<'a> RunCommand<'a> {
/// Run the command using the provided [`ClientSession`].
pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
self.session = Some(value.into());
self
}
}
#[action_impl]
impl<'a> Action for RunCommand<'a> {
type Future = RunCommandFuture;
async fn execute(self) -> Result<Document> {
let mut selection_criteria = self.options.and_then(|o| o.selection_criteria);
let command = self.command?;
if let Some(session) = &self.session {
match session.transaction.state {
TransactionState::Starting | TransactionState::InProgress => {
if command.get("readConcern").is_ok_and(|rc| rc.is_some()) {
return Err(ErrorKind::InvalidArgument {
message: "Cannot set read concern after starting a transaction".into(),
}
.into());
}
selection_criteria = match selection_criteria {
Some(selection_criteria) => Some(selection_criteria),
None => {
if let Some(ref options) = session.transaction.options {
options.selection_criteria.clone()
} else {
None
}
}
};
}
_ => {}
}
}
let operation =
run_command::RunCommand::new(self.db.name().into(), command, selection_criteria, None);
self.db
.client()
.execute_operation(operation, self.session)
.await
}
}
/// Runs a database-level command and returns a cursor to the response. Create with
/// [`Database::run_cursor_command`].
#[must_use]
pub struct RunCursorCommand<'a, Session = ImplicitSession> {
db: &'a Database,
command: RawResult<RawDocumentBuf>,
options: Option<RunCursorCommandOptions>,
session: Session,
}
#[option_setters(crate::db::options::RunCursorCommandOptions)]
#[export_doc(run_cursor_command, extra = [session])]
impl<Session> RunCursorCommand<'_, Session> {}
impl<'a> RunCursorCommand<'a, ImplicitSession> {
/// Run the command using the provided [`ClientSession`].
pub fn session(
self,
value: impl Into<&'a mut ClientSession>,
) -> RunCursorCommand<'a, ExplicitSession<'a>> {
RunCursorCommand {
db: self.db,
command: self.command,
options: self.options,
session: ExplicitSession(value.into()),
}
}
}
#[action_impl(sync = crate::sync::Cursor<Document>)]
impl<'a> Action for RunCursorCommand<'a, ImplicitSession> {
type Future = RunCursorCommandFuture;
async fn execute(self) -> Result<Cursor<Document>> {
let selection_criteria = self
.options
.as_ref()
.and_then(|options| options.selection_criteria.clone());
let rcc = run_command::RunCommand::new(
self.db.name().to_string(),
self.command?,
selection_criteria,
None,
);
let rc_command = run_cursor_command::RunCursorCommand::new(rcc, self.options)?;
let client = self.db.client();
client.execute_cursor_operation(rc_command).await
}
}
#[action_impl(sync = crate::sync::SessionCursor<Document>)]
impl<'a> Action for RunCursorCommand<'a, ExplicitSession<'a>> {
type Future = RunCursorCommandSessionFuture;
async fn execute(mut self) -> Result<SessionCursor<Document>> {
resolve_selection_criteria_with_session!(
self.db,
self.options,
Some(&mut *self.session.0)
)?;
let selection_criteria = self
.options
.as_ref()
.and_then(|options| options.selection_criteria.clone());
let rcc = run_command::RunCommand::new(
self.db.name().to_string(),
self.command?,
selection_criteria,
None,
);
let rc_command = run_cursor_command::RunCursorCommand::new(rcc, self.options)?;
let client = self.db.client();
client
.execute_session_cursor_operation(rc_command, self.session.0)
.await
}
}