mz/command/
sql.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Implementation of the `mz sql` command.
17//!
18//! Consult the user-facing documentation for details.
19
20use std::os::unix::process::CommandExt;
21
22use crate::{context::RegionContext, error::Error};
23
24/// Represents the structure containing the args to run the SQL shell.
25pub struct RunArgs {
26    /// The cluster name to use in the connection.
27    pub cluster: Option<String>,
28    /// Contains all the arguments to pass into the shell.
29    /// Take into account that they are added at the end of the statement.
30    pub psql_args: Vec<String>,
31}
32
33/// Creates an interactive SQL shell connection to the profile context environment.
34/// The SQL shell command is running `psql` behind the scenes.
35pub async fn run(cx: &RegionContext, RunArgs { cluster, psql_args }: RunArgs) -> Result<(), Error> {
36    let sql_client = cx.sql_client();
37    let claims = cx.admin_client().claims().await?;
38    let region_info = cx.get_region_info().await?;
39    let user = claims.user()?;
40
41    let _error = sql_client
42        .shell(&region_info, user, cluster)
43        .args(psql_args)
44        .exec();
45
46    Ok(())
47}