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.
910use anyhow::anyhow;
1112use crate::action::{ControlFlow, State};
13use crate::parser::BuiltinCommand;
1415pub async fn run_connect(
16mut cmd: BuiltinCommand,
17 state: &mut State,
18) -> Result<ControlFlow, anyhow::Error> {
19let name = cmd.args.string("name")?;
20let url = cmd.args.string("url")?;
21// We allow the password to be specified outside of the URL
22 // in case it contains special characters
23let password = cmd.args.opt_string("password");
24 cmd.args.done()?;
2526let opts_url = mysql_async::Opts::from_url(&url)
27 .map_err(|_| anyhow!("Unable to parse MySQL URL {}", url))?;
28let opts = mysql_async::OptsBuilder::from_opts(opts_url).pass(password.clone());
29let pool = mysql_async::Pool::new(opts);
30let conn = pool
31 .get_conn()
32 .await
33.map_err(|_| anyhow!("Unable to connect to MySQL server at {}", url))?;
3435 state.mysql_clients.insert(name.clone(), conn);
36Ok(ControlFlow::Continue)
37}