mz_testdrive/action/mysql/
connect.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use anyhow::anyhow;
11
12use crate::action::{ControlFlow, State};
13use crate::parser::BuiltinCommand;
14
15pub async fn run_connect(
16    mut cmd: BuiltinCommand,
17    state: &mut State,
18) -> Result<ControlFlow, anyhow::Error> {
19    let name = cmd.args.string("name")?;
20    let url = cmd.args.string("url")?;
21    // We allow the password to be specified outside of the URL
22    // in case it contains special characters
23    let password = cmd.args.opt_string("password");
24    cmd.args.done()?;
25
26    let opts_url = mysql_async::Opts::from_url(&url)
27        .map_err(|_| anyhow!("Unable to parse MySQL URL {}", url))?;
28    let opts = mysql_async::OptsBuilder::from_opts(opts_url).pass(password.clone());
29    let pool = mysql_async::Pool::new(opts);
30    let conn = pool
31        .get_conn()
32        .await
33        .map_err(|_| anyhow!("Unable to connect to MySQL server at {}", url))?;
34
35    state.mysql_clients.insert(name.clone(), conn);
36    Ok(ControlFlow::Continue)
37}