mz_testdrive/action/duckdb/
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};
11
12use crate::action::duckdb::get_or_create_connection;
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    cmd.args.done()?;
22
23    let conn = get_or_create_connection(state, name).await?;
24
25    mz_ore::task::spawn_blocking(
26        || "duckdb_execute".to_string(),
27        move || {
28            let conn = conn.lock().map_err(|e| anyhow!("lock poisoned: {}", e))?;
29            for query in &cmd.input {
30                println!(">> {}", query);
31                conn.execute_batch(query)
32                    .with_context(|| format!("executing DuckDB query: {}", query))?;
33            }
34            Ok::<_, anyhow::Error>(())
35        },
36    )
37    .await?;
38
39    Ok(ControlFlow::Continue)
40}