mz_testdrive/action/sql_server/
execute.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::{Context, anyhow};
11use mz_ore::str::StrExt;
12
13use crate::action::{ControlFlow, State};
14use crate::parser::BuiltinCommand;
15
16pub async fn run_execute(
17    mut cmd: BuiltinCommand,
18    state: &mut State,
19) -> Result<ControlFlow, anyhow::Error> {
20    let name = cmd.args.string("name")?;
21    let split_lines = cmd.args.opt_bool("split-lines")?.unwrap_or(true);
22    cmd.args.done()?;
23
24    let client = state
25        .sql_server_clients
26        .get_mut(&name)
27        .ok_or_else(|| anyhow!("connection {} not found", name.quoted()))?;
28
29    if split_lines {
30        for query in cmd.input {
31            println!(">> {}", query);
32            client
33                .simple_query(query)
34                .await
35                .context("executing SQL Server query")?;
36        }
37    } else {
38        let query = cmd.input.join("\n");
39        println!(">> {}", query);
40        // execute uses prepared statements, which will fail for CREATE FUNCTION/PROCEDURE etc, see
41        // https://github.com/prisma/tiberius/issues/236, so using simple_query instead
42        client
43            .simple_query(query)
44            .await
45            .context("executing SQL Server query")?;
46    }
47
48    Ok(ControlFlow::Continue)
49}