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
pub mod options;
use std::sync::Arc;
use bson::{Bson, Document};
use crate::{
concern::{ReadConcern, WriteConcern},
cursor::Cursor,
error::{ErrorKind, Result},
operation::{Aggregate, Create, DropDatabase, ListCollections, RunCommand},
options::{
AggregateOptions,
CollectionOptions,
CreateCollectionOptions,
DatabaseOptions,
DropDatabaseOptions,
ListCollectionsOptions,
},
selection_criteria::SelectionCriteria,
Client,
Collection,
Namespace,
};
#[derive(Clone, Debug)]
pub struct Database {
inner: Arc<DatabaseInner>,
}
#[derive(Debug)]
struct DatabaseInner {
client: Client,
name: String,
selection_criteria: Option<SelectionCriteria>,
read_concern: Option<ReadConcern>,
write_concern: Option<WriteConcern>,
}
impl Database {
pub(crate) fn new(client: Client, name: &str, options: Option<DatabaseOptions>) -> Self {
let options = options.unwrap_or_default();
let selection_criteria = options
.selection_criteria
.or_else(|| client.selection_criteria().cloned());
let read_concern = options
.read_concern
.or_else(|| client.read_concern().cloned());
let write_concern = options
.write_concern
.or_else(|| client.write_concern().cloned());
Self {
inner: Arc::new(DatabaseInner {
client,
name: name.to_string(),
selection_criteria,
read_concern,
write_concern,
}),
}
}
pub(crate) fn client(&self) -> &Client {
&self.inner.client
}
pub fn name(&self) -> &str {
&self.inner.name
}
pub fn selection_criteria(&self) -> Option<&SelectionCriteria> {
self.inner.selection_criteria.as_ref()
}
pub fn read_concern(&self) -> Option<&ReadConcern> {
self.inner.read_concern.as_ref()
}
pub fn write_concern(&self) -> Option<&WriteConcern> {
self.inner.write_concern.as_ref()
}
pub fn collection(&self, name: &str) -> Collection {
Collection::new(self.clone(), name, None)
}
pub fn collection_with_options(&self, name: &str, options: CollectionOptions) -> Collection {
Collection::new(self.clone(), name, Some(options))
}
pub fn drop(&self, options: impl Into<Option<DropDatabaseOptions>>) -> Result<()> {
let mut options = options.into();
resolve_options!(self, options, [write_concern]);
let drop_database = DropDatabase::new(self.name().to_string(), options);
self.client().execute_operation(&drop_database, None)
}
pub fn list_collections(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>,
) -> Result<Cursor> {
let list_collections = ListCollections::new(
self.name().to_string(),
filter.into(),
false,
options.into(),
);
self.client()
.execute_operation(&list_collections, None)
.map(|spec| Cursor::new(self.client().clone(), spec))
}
pub fn list_collection_names(
&self,
filter: impl Into<Option<Document>>,
) -> Result<Vec<String>> {
let list_collections =
ListCollections::new(self.name().to_string(), filter.into(), true, None);
let cursor = self
.client()
.execute_operation(&list_collections, None)
.map(|spec| Cursor::new(self.client().clone(), spec))?;
cursor
.map(|doc| {
let name = doc?
.get("name")
.and_then(Bson::as_str)
.ok_or_else(|| ErrorKind::ResponseError {
message: "Expected name field in server response, but there was none."
.to_string(),
})?
.to_string();
Ok(name)
})
.collect()
}
pub fn create_collection(
&self,
name: &str,
options: impl Into<Option<CreateCollectionOptions>>,
) -> Result<()> {
let mut options = options.into();
resolve_options!(self, options, [write_concern]);
let create = Create::new(
Namespace {
db: self.name().to_string(),
coll: name.to_string(),
},
options,
);
self.client().execute_operation(&create, None)
}
pub fn run_command(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>,
) -> Result<Document> {
let operation = RunCommand::new(self.name().into(), command, selection_criteria.into());
self.client().execute_operation(&operation, None)
}
pub fn aggregate(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>,
) -> Result<Cursor> {
let mut options = options.into();
resolve_options!(
self,
options,
[read_concern, write_concern, selection_criteria]
);
let aggregate = Aggregate::new(self.name().to_string(), pipeline, options);
let client = self.client();
client
.execute_operation(&aggregate, None)
.map(|spec| Cursor::new(client.clone(), spec))
}
}