Skip to content

Commit

Permalink
Merge pull request #171 from jbmcgill/170-select-db
Browse files Browse the repository at this point in the history
Add select_db(). Closes #170
  • Loading branch information
blackbeam authored Feb 12, 2019
2 parents f9dd8d7 + 0df0e0c commit 8e26bfb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,15 @@ impl Conn {
}
}

/// Executes [`COM_INIT_DB`](https://dev.mysql.com/doc/internals/en/com-init-db.html)
/// on `Conn`.
pub fn select_db(&mut self, schema: &str) -> bool {
match self.write_command_data(Command::COM_INIT_DB, schema.as_bytes()) {
Ok(_) => self.drop_packet().is_ok(),
_ => false,
}
}

/// Starts new transaction with provided options.
/// `readonly` is only available since MySQL 5.6.5.
pub fn start_transaction<'a>(
Expand Down Expand Up @@ -2269,6 +2278,27 @@ mod test {
);
}
#[test]
fn should_select_db() {
let mut opts = OptsBuilder::from_opts(get_opts());
opts.db_name(Some("mysql"));
opts.ip_or_hostname(Some("localhost"));
let mut conn = Conn::new(opts).unwrap();
assert!(conn
.query("CREATE DATABASE IF NOT EXISTS t_select_db")
.is_ok());
assert!(conn.select_db("t_select_db"));
assert_eq!(
conn.query("SELECT DATABASE()")
.unwrap()
.next()
.unwrap()
.unwrap()
.unwrap(),
vec![Bytes(b"t_select_db".to_vec())]
);
assert!(conn.query("DROP DATABASE t_select_db").is_ok());
}
#[test]
fn should_execute_queryes_and_parse_results() {
let mut conn = Conn::new(get_opts()).unwrap();
assert!(conn
Expand Down

0 comments on commit 8e26bfb

Please sign in to comment.