Add db.has_collection

pull/39/head
Michael Jansen 7 years ago
parent 50b10ad52e
commit 4fba9e8b5a

1
.gitignore vendored

@ -2,3 +2,4 @@ target
Cargo.lock Cargo.lock
mongoc-sys/mongo-c-driver* mongoc-sys/mongo-c-driver*
ssl_env_vars ssl_env_vars
.idea/

@ -102,6 +102,7 @@ pub mod bindings {
pub fn mongoc_database_get_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char) -> *mut mongoc_collection_t; pub fn mongoc_database_get_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char) -> *mut mongoc_collection_t;
pub fn mongoc_database_get_name(database: *mut mongoc_database_t) -> *const ::libc::c_char; pub fn mongoc_database_get_name(database: *mut mongoc_database_t) -> *const ::libc::c_char;
pub fn mongoc_database_destroy(database: *mut mongoc_database_t) -> (); pub fn mongoc_database_destroy(database: *mut mongoc_database_t) -> ();
pub fn mongoc_database_has_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char, error: *mut bson_error_t) -> ::libc::boolean_t;
} }
// Client // Client

@ -189,6 +189,31 @@ impl<'a> Database<'a> {
}; };
String::from_utf8_lossy(cstr.to_bytes()) String::from_utf8_lossy(cstr.to_bytes())
} }
/// Create a new collection in this database.
pub fn has_collection<S: Into<Vec<u8>>>(
&self,
name: S
) -> Result<bool> {
let mut error = BsoncError::empty();
let name_cstring = CString::new(name).unwrap();
let has_collection = unsafe {
bindings::mongoc_database_has_collection(
self.inner,
name_cstring.as_ptr(),
error.mut_inner())
};
if error.is_empty() {
Ok(match has_collection{
0 => false,
_ => true
})
} else {
Err(error.into())
}
}
} }
impl<'a> Drop for Database<'a> { impl<'a> Drop for Database<'a> {

Loading…
Cancel
Save