Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit 17c7a10

Browse files
authored
Merge pull request #171 from kyeah/db-version
Add `db.version()` function
2 parents 8c2429a + b2d5bca commit 17c7a10

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rand = "0.3.14"
2020
rust-crypto = "0.2.31"
2121
rustc-serialize = "0.3.19"
2222
scan_fmt = "0.1.0"
23+
semver = "0.5.0"
2324
separator = "0.3.1"
2425
textnonce = { version = "0.4.1", default-features = false }
2526
time = "0.1.35"

src/command_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#[derive(PartialEq, Eq, Clone)]
55
pub enum CommandType {
66
Aggregate,
7+
BuildInfo,
78
Count,
89
CreateCollection,
910
CreateIndexes,
@@ -37,6 +38,7 @@ impl CommandType {
3738
pub fn to_str(&self) -> &str {
3839
match *self {
3940
CommandType::Aggregate => "aggregate",
41+
CommandType::BuildInfo => "buildinfo",
4042
CommandType::Count => "count",
4143
CommandType::CreateCollection => "create_collection",
4244
CommandType::CreateIndexes => "create_indexes",
@@ -87,6 +89,7 @@ impl CommandType {
8789
CommandType::UpdateMany |
8890
CommandType::UpdateOne => true,
8991
CommandType::Aggregate |
92+
CommandType::BuildInfo |
9093
CommandType::Count |
9194
CommandType::Distinct |
9295
CommandType::Find |

src/db/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ use auth::Authenticator;
6262
use bson;
6363
use bson::Bson;
6464
use {Client, CommandType, ThreadedClient, Result};
65-
use Error::{CursorNotFoundError, OperationError};
65+
use Error::{CursorNotFoundError, OperationError, ResponseError};
6666
use coll::Collection;
6767
use coll::options::FindOptions;
6868
use common::{ReadPreference, WriteConcern};
6969
use cursor::{Cursor, DEFAULT_BATCH_SIZE};
7070
use self::options::{CreateCollectionOptions, CreateUserOptions, UserInfoOptions};
7171
use self::roles::Role;
72+
use semver::Version;
73+
use std::error::Error;
7274
use std::sync::Arc;
7375

7476
/// Interfaces with a MongoDB database.
@@ -93,6 +95,8 @@ pub trait ThreadedDatabase {
9395
read_preference: Option<ReadPreference>,
9496
write_concern: Option<WriteConcern>)
9597
-> Database;
98+
// Returns the version of the MongoDB instance.
99+
fn version(&self) -> Result<Version>;
96100
/// Logs in a user using the SCRAM-SHA-1 mechanism.
97101
fn auth(&self, user: &str, password: &str) -> Result<()>;
98102
/// Creates a collection representation with inherited read and write controls.
@@ -275,6 +279,21 @@ impl ThreadedDatabase for Database {
275279
}
276280
}
277281

282+
fn version(&self) -> Result<Version> {
283+
let doc = doc! { "buildinfo" => 1 };
284+
let out = try!(self.command(doc,
285+
CommandType::BuildInfo,
286+
None));
287+
288+
match out.get("version") {
289+
Some(&Bson::String(ref s)) => match Version::parse(s) {
290+
Ok(v) => Ok(v),
291+
Err(e) => Err(ResponseError(String::from(e.description()))),
292+
},
293+
_ => Err(ResponseError(String::from("No version received from server"))),
294+
}
295+
}
296+
278297
fn create_collection(&self,
279298
name: &str,
280299
options: Option<CreateCollectionOptions>)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ extern crate rand;
9797
extern crate rustc_serialize;
9898
#[macro_use]
9999
extern crate scan_fmt;
100+
extern crate semver;
100101
extern crate separator;
101102
extern crate textnonce;
102103
extern crate time;

tests/client/db.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,10 @@ fn create_and_get_users() {
167167
_ => panic!("User isn't named 'val' but should be"),
168168
};
169169
}
170+
171+
#[test]
172+
fn get_version() {
173+
let client = Client::connect("localhost", 27017).unwrap();
174+
let db = client.db("get_version");
175+
let version = db.version().unwrap();
176+
}

0 commit comments

Comments
 (0)