|
|
@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
//! Access to a MongoDB database.
|
|
|
|
|
|
|
|
|
|
|
|
use std::ffi::{CString,CStr};
|
|
|
|
use std::ffi::{CString,CStr};
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::ptr;
|
|
|
|
use std::ptr;
|
|
|
@ -14,19 +16,25 @@ use super::collection;
|
|
|
|
use super::collection::Collection;
|
|
|
|
use super::collection::Collection;
|
|
|
|
use super::cursor;
|
|
|
|
use super::cursor;
|
|
|
|
use super::cursor::Cursor;
|
|
|
|
use super::cursor::Cursor;
|
|
|
|
|
|
|
|
use super::read_prefs::ReadPrefs;
|
|
|
|
use flags::FlagsValue;
|
|
|
|
use flags::FlagsValue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub enum CreatedBy<'a> {
|
|
|
|
pub enum CreatedBy<'a> {
|
|
|
|
BorrowedClient(&'a Client<'a>),
|
|
|
|
BorrowedClient(&'a Client<'a>),
|
|
|
|
OwnedClient(Client<'a>)
|
|
|
|
OwnedClient(Client<'a>)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Provides access to a MongoDB database.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// A database instance can be created by calling `get_database` or `take_database` on a `Client` instance.
|
|
|
|
pub struct Database<'a> {
|
|
|
|
pub struct Database<'a> {
|
|
|
|
_created_by: CreatedBy<'a>,
|
|
|
|
_created_by: CreatedBy<'a>,
|
|
|
|
inner: *mut bindings::mongoc_database_t
|
|
|
|
inner: *mut bindings::mongoc_database_t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Database<'a> {
|
|
|
|
impl<'a> Database<'a> {
|
|
|
|
|
|
|
|
#[doc(ignore)]
|
|
|
|
pub fn new(
|
|
|
|
pub fn new(
|
|
|
|
created_by: CreatedBy<'a>,
|
|
|
|
created_by: CreatedBy<'a>,
|
|
|
|
inner: *mut bindings::mongoc_database_t
|
|
|
|
inner: *mut bindings::mongoc_database_t
|
|
|
@ -38,9 +46,8 @@ impl<'a> Database<'a> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Execute a command on the database
|
|
|
|
/// Execute a command on the database.
|
|
|
|
///
|
|
|
|
/// This is performed lazily and therefore requires calling `next` on the resulting cursor.
|
|
|
|
/// See: http://api.mongodb.org/c/current/mongoc_database_command.html
|
|
|
|
|
|
|
|
pub fn command(
|
|
|
|
pub fn command(
|
|
|
|
&'a self,
|
|
|
|
&'a self,
|
|
|
|
command: Document,
|
|
|
|
command: Document,
|
|
|
@ -82,19 +89,14 @@ impl<'a> Database<'a> {
|
|
|
|
))
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Simplified version of command that returns the first document
|
|
|
|
/// Simplified version of `command` that returns the first document immediately.
|
|
|
|
///
|
|
|
|
|
|
|
|
/// See: http://api.mongodb.org/c/current/mongoc_database_command_simple.html
|
|
|
|
|
|
|
|
pub fn command_simple(
|
|
|
|
pub fn command_simple(
|
|
|
|
&'a self,
|
|
|
|
&'a self,
|
|
|
|
command: Document,
|
|
|
|
command: Document,
|
|
|
|
options: Option<&CommandAndFindOptions>
|
|
|
|
read_prefs: Option<&ReadPrefs>
|
|
|
|
) -> Result<Document> {
|
|
|
|
) -> Result<Document> {
|
|
|
|
assert!(!self.inner.is_null());
|
|
|
|
assert!(!self.inner.is_null());
|
|
|
|
|
|
|
|
|
|
|
|
let default_options = CommandAndFindOptions::default();
|
|
|
|
|
|
|
|
let options = options.unwrap_or(&default_options);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Bsonc to store the reply
|
|
|
|
// Bsonc to store the reply
|
|
|
|
let mut reply = Bsonc::new();
|
|
|
|
let mut reply = Bsonc::new();
|
|
|
|
// Empty error that might be filled
|
|
|
|
// Empty error that might be filled
|
|
|
@ -104,7 +106,7 @@ impl<'a> Database<'a> {
|
|
|
|
bindings::mongoc_database_command_simple(
|
|
|
|
bindings::mongoc_database_command_simple(
|
|
|
|
self.inner,
|
|
|
|
self.inner,
|
|
|
|
try!(Bsonc::from_document(&command)).inner(),
|
|
|
|
try!(Bsonc::from_document(&command)).inner(),
|
|
|
|
match options.read_prefs {
|
|
|
|
match read_prefs {
|
|
|
|
Some(ref prefs) => prefs.inner(),
|
|
|
|
Some(ref prefs) => prefs.inner(),
|
|
|
|
None => ptr::null()
|
|
|
|
None => ptr::null()
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -123,6 +125,7 @@ impl<'a> Database<'a> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new collection in this database.
|
|
|
|
pub fn create_collection<S: Into<Vec<u8>>>(
|
|
|
|
pub fn create_collection<S: Into<Vec<u8>>>(
|
|
|
|
&self,
|
|
|
|
&self,
|
|
|
|
name: S,
|
|
|
|
name: S,
|
|
|
@ -179,6 +182,7 @@ impl<'a> Database<'a> {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the name of this database.
|
|
|
|
pub fn get_name(&self) -> Cow<str> {
|
|
|
|
pub fn get_name(&self) -> Cow<str> {
|
|
|
|
let cstr = unsafe {
|
|
|
|
let cstr = unsafe {
|
|
|
|
CStr::from_ptr(bindings::mongoc_database_get_name(self.inner))
|
|
|
|
CStr::from_ptr(bindings::mongoc_database_get_name(self.inner))
|
|
|
|