mysql_async/conn/routines/
change_user.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use mysql_common::{
    constants::{UTF8MB4_GENERAL_CI, UTF8_GENERAL_CI},
    packets::{ComChangeUser, ComChangeUserMoreData},
};
#[cfg(feature = "tracing")]
use tracing::debug_span;

use crate::Conn;

use super::Routine;

/// A routine that performs `COM_CHANGE_USER`.
#[derive(Debug, Copy, Clone)]
pub struct ChangeUser;

impl Routine<()> for ChangeUser {
    fn call<'a>(&'a mut self, conn: &'a mut Conn) -> BoxFuture<'a, crate::Result<()>> {
        #[cfg(feature = "tracing")]
        let span = debug_span!(
            "mysql_async::change_user",
            mysql_async.connection.id = conn.id()
        );

        let com_change_user = ComChangeUser::new()
            .with_user(conn.opts().user().map(|x| x.as_bytes()))
            .with_database(conn.opts().db_name().map(|x| x.as_bytes()))
            .with_auth_plugin_data(
                conn.inner
                    .auth_plugin
                    .gen_data(conn.opts().pass(), &conn.inner.nonce)
                    .as_deref(),
            )
            .with_more_data(Some(
                ComChangeUserMoreData::new(if conn.inner.version >= (5, 5, 3) {
                    UTF8MB4_GENERAL_CI
                } else {
                    UTF8_GENERAL_CI
                })
                .with_auth_plugin(Some(conn.inner.auth_plugin.clone()))
                .with_connect_attributes(None),
            ))
            .into_owned();

        let fut = async move {
            conn.write_command(&com_change_user).await?;
            conn.inner.auth_switched = false;
            conn.continue_auth().await?;
            Ok(())
        };

        #[cfg(feature = "tracing")]
        let fut = instrument_result!(fut, span);

        fut.boxed()
    }
}