Skip to content

Commit 73dbb96

Browse files
committed
Add Opts::setup and OptsBuilder::setup
1 parent 7c6572d commit 73dbb96

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/conn/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,16 @@ impl Conn {
881881
Ok(())
882882
}
883883

884+
async fn run_setup_commands(&mut self) -> Result<()> {
885+
let mut setup = self.inner.opts.setup().to_vec();
886+
887+
while let Some(query) = setup.pop() {
888+
self.query_drop(query).await?;
889+
}
890+
891+
Ok(())
892+
}
893+
884894
/// Returns a future that resolves to [`Conn`].
885895
pub fn new<T: Into<Opts>>(opts: T) -> crate::BoxFuture<'static, Conn> {
886896
let opts = opts.into();
@@ -913,6 +923,7 @@ impl Conn {
913923
conn.read_max_allowed_packet().await?;
914924
conn.read_wait_timeout().await?;
915925
conn.run_init_commands().await?;
926+
conn.run_setup_commands().await?;
916927

917928
Ok(conn)
918929
}
@@ -1011,6 +1022,7 @@ impl Conn {
10111022
self.routine(routines::ResetRoutine).await?;
10121023
self.inner.stmt_cache.clear();
10131024
self.inner.infile_handler = None;
1025+
self.run_setup_commands().await?;
10141026
}
10151027

10161028
Ok(supports_com_reset_connection)
@@ -1052,6 +1064,7 @@ impl Conn {
10521064
self.routine(routines::ChangeUser).await?;
10531065
self.inner.stmt_cache.clear();
10541066
self.inner.infile_handler = None;
1067+
self.run_setup_commands().await?;
10551068
Ok(())
10561069
}
10571070

@@ -1548,6 +1561,30 @@ mod test {
15481561
Ok(())
15491562
}
15501563

1564+
#[tokio::test]
1565+
async fn should_execute_setup_queries_on_reset() -> super::Result<()> {
1566+
let opts = OptsBuilder::from_opts(get_opts()).setup(vec!["SET @a = 42", "SET @b = 'foo'"]);
1567+
let mut conn = Conn::new(opts).await?;
1568+
1569+
// initial run
1570+
let mut result: Vec<(u8, String)> = conn.query("SELECT @a, @b").await?;
1571+
assert_eq!(result, vec![(42, "foo".into())]);
1572+
1573+
// after reset
1574+
if conn.reset().await? {
1575+
result = conn.query("SELECT @a, @b").await?;
1576+
assert_eq!(result, vec![(42, "foo".into())]);
1577+
}
1578+
1579+
// after change user
1580+
conn.change_user(Default::default()).await?;
1581+
result = conn.query("SELECT @a, @b").await?;
1582+
assert_eq!(result, vec![(42, "foo".into())]);
1583+
1584+
conn.disconnect().await?;
1585+
Ok(())
1586+
}
1587+
15511588
#[tokio::test]
15521589
async fn should_reset_the_connection() -> super::Result<()> {
15531590
let mut conn = Conn::new(get_opts()).await?;

src/opts/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,13 @@ pub(crate) struct MysqlOpts {
403403
/// (defaults to `wait_timeout`).
404404
conn_ttl: Option<Duration>,
405405

406-
/// Commands to execute on each new database connection.
406+
/// Commands to execute once new connection is established.
407407
init: Vec<String>,
408408

409+
/// Commands to execute on new connection and every time
410+
/// [`Conn::reset`] or [`Conn::change_user`] is invoked.
411+
setup: Vec<String>,
412+
409413
/// Number of prepared statements cached on the client side (per connection). Defaults to `10`.
410414
stmt_cache_size: usize,
411415

@@ -577,11 +581,17 @@ impl Opts {
577581
self.inner.mysql_opts.db_name.as_ref().map(AsRef::as_ref)
578582
}
579583

580-
/// Commands to execute on each new database connection.
584+
/// Commands to execute once new connection is established.
581585
pub fn init(&self) -> &[String] {
582586
self.inner.mysql_opts.init.as_ref()
583587
}
584588

589+
/// Commands to execute on new connection and every time
590+
/// [`Conn::reset`] or [`Conn::change_user`] is invoked.
591+
pub fn setup(&self) -> &[String] {
592+
self.inner.mysql_opts.setup.as_ref()
593+
}
594+
585595
/// TCP keep alive timeout in milliseconds (defaults to `None`).
586596
///
587597
/// # Connection URL
@@ -871,6 +881,7 @@ impl Default for MysqlOpts {
871881
pass: None,
872882
db_name: None,
873883
init: vec![],
884+
setup: vec![],
874885
tcp_keepalive: None,
875886
tcp_nodelay: true,
876887
local_infile_handler: None,
@@ -1037,6 +1048,12 @@ impl OptsBuilder {
10371048
self
10381049
}
10391050

1051+
/// Defines setup queries. See [`Opts::setup`].
1052+
pub fn setup<T: Into<String>>(mut self, setup: Vec<T>) -> Self {
1053+
self.opts.setup = setup.into_iter().map(Into::into).collect();
1054+
self
1055+
}
1056+
10401057
/// Defines `tcp_keepalive` option. See [`Opts::tcp_keepalive`].
10411058
pub fn tcp_keepalive<T: Into<u32>>(mut self, tcp_keepalive: Option<T>) -> Self {
10421059
self.opts.tcp_keepalive = tcp_keepalive.map(Into::into);
@@ -1654,6 +1671,7 @@ mod test {
16541671
assert_eq!(url_opts.pass(), builder_opts.pass());
16551672
assert_eq!(url_opts.db_name(), builder_opts.db_name());
16561673
assert_eq!(url_opts.init(), builder_opts.init());
1674+
assert_eq!(url_opts.setup(), builder_opts.setup());
16571675
assert_eq!(url_opts.tcp_keepalive(), builder_opts.tcp_keepalive());
16581676
assert_eq!(url_opts.tcp_nodelay(), builder_opts.tcp_nodelay());
16591677
assert_eq!(url_opts.pool_opts(), builder_opts.pool_opts());

0 commit comments

Comments
 (0)