Skip to content

Commit

Permalink
feat!: ping and select_db return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Aug 3, 2023
1 parent 0a40252 commit 76e78b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
30 changes: 13 additions & 17 deletions src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,22 +1084,18 @@ impl Conn {
self.handle_result_set()
}

/// Executes [`COM_PING`](http://dev.mysql.com/doc/internals/en/com-ping.html)
/// Executes [`COM_PING`](https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_ping.html)
/// on `Conn`. Return `true` on success or `false` on error.
pub fn ping(&mut self) -> bool {
match self.write_command(Command::COM_PING, &[]) {
Ok(_) => self.drop_packet().is_ok(),
_ => false,
}
pub fn ping(&mut self) -> Result<(), Error> {
self.write_command(Command::COM_PING, &[])?;
self.drop_packet()
}

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

/// Starts new transaction with provided options.
Expand Down Expand Up @@ -1358,7 +1354,7 @@ mod test {
.unwrap()
.unwrap();
assert!(mode.contains("TRADITIONAL"));
assert!(conn.ping());
assert!(conn.ping().is_ok());

if crate::test_misc::test_compression() {
assert!(format!("{:?}", conn.0.stream).contains("Compression"));
Expand Down Expand Up @@ -1522,7 +1518,7 @@ mod test {
fn should_connect_by_hostname() {
let opts = OptsBuilder::from_opts(get_opts()).ip_or_hostname(Some("localhost"));
let mut conn = Conn::new(opts).unwrap();
assert!(conn.ping());
assert!(conn.ping().is_ok());
}

#[test]
Expand All @@ -1532,7 +1528,7 @@ mod test {
let mut conn = Conn::new(get_opts()).unwrap();
conn.query_drop(format!("CREATE DATABASE IF NOT EXISTS {}", DB_NAME))
.unwrap();
assert!(conn.select_db(DB_NAME));
assert!(conn.select_db(DB_NAME).is_ok());

let db_name: String = conn.query_first("SELECT DATABASE()").unwrap().unwrap();
assert_eq!(db_name, DB_NAME);
Expand Down Expand Up @@ -2181,7 +2177,7 @@ mod test {
let opts = OptsBuilder::from_opts(get_opts())
.prefer_socket(false)
.tcp_connect_timeout(Some(::std::time::Duration::from_millis(1000)));
assert!(Conn::new(opts).unwrap().ping());
assert!(Conn::new(opts).unwrap().ping().is_ok());

let opts = OptsBuilder::from_opts(get_opts())
.prefer_socket(false)
Expand Down Expand Up @@ -2238,7 +2234,7 @@ mod test {
.bind_address(Some(([127, 0, 0, 1], port)))
.tcp_connect_timeout(Some(::std::time::Duration::from_millis(1000)));
let mut conn = Conn::new(opts).unwrap();
assert!(conn.ping());
assert!(conn.ping().is_ok());
let debug_format: String = format!("{:?}", conn);
let expected_1 = format!("addr: V4(127.0.0.1:{})", port);
let expected_2 = format!("addr: 127.0.0.1:{}", port);
Expand Down
2 changes: 1 addition & 1 deletion src/conn/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Pool {
}
};

if call_ping && self.inner.opts().check_health() && !conn.ping() {
if call_ping && self.inner.opts().check_health() && conn.ping().is_err() {
// existing connection seem to be dead, retrying..
self.inner.decrease();
return self._get_conn(stmt, timeout, call_ping);
Expand Down

0 comments on commit 76e78b1

Please sign in to comment.