From 093c29a126510262354b52ceff2cc2a733cee740 Mon Sep 17 00:00:00 2001 From: James McGill Date: Tue, 12 Feb 2019 01:49:06 -0800 Subject: [PATCH 1/2] Add select_db(). Closes #170 --- src/conn/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/conn/mod.rs b/src/conn/mod.rs index 5bf646c..1803644 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -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>( @@ -2269,6 +2278,25 @@ 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 From 0df0e0c9798ace9c6943afaf448c21512e390dea Mon Sep 17 00:00:00 2001 From: James McGill Date: Tue, 12 Feb 2019 10:14:44 -0800 Subject: [PATCH 2/2] rustfmt --- src/conn/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/conn/mod.rs b/src/conn/mod.rs index 1803644..4eb2dfb 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -1503,8 +1503,8 @@ impl Conn { } } - /// Executes [`COM_INIT_DB`](https://dev.mysql.com/doc/internals/en/com-init-db.html) - /// on `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(), @@ -2283,7 +2283,9 @@ mod test { 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 + .query("CREATE DATABASE IF NOT EXISTS t_select_db") + .is_ok()); assert!(conn.select_db("t_select_db")); assert_eq!( conn.query("SELECT DATABASE()")