Skip to main content

cqlsh_rs/
lib.rs

1//! cqlsh-rs library — exposes modules for benchmarks and integration tests.
2
3pub mod cli;
4pub mod colorizer;
5pub mod completer;
6pub mod config;
7pub mod copy;
8pub mod cql_lexer;
9pub mod describe;
10pub mod driver;
11pub mod error;
12pub mod executor;
13pub mod formatter;
14pub mod pager;
15pub mod parser;
16pub mod repl;
17pub mod schema_cache;
18pub mod session;
19pub mod shell_completions;
20
21use std::io::Write;
22
23use anyhow::Result;
24
25/// Execute a CQL string against a remote host and return the captured stdout output.
26///
27/// Connects to the given host/port, optionally uses `keyspace`, then runs `cql`
28/// as if it were passed to `-e`. This runs in-process so `cargo tarpaulin` can
29/// measure coverage for all code paths exercised.
30pub async fn run_cql_in_process(
31    host: &str,
32    port: u16,
33    keyspace: Option<&str>,
34    cql: &str,
35) -> Result<String> {
36    use config::{ColorMode, CqlshrcConfig, MergedConfig};
37    use std::path::PathBuf;
38
39    let mut config = MergedConfig {
40        host: host.to_string(),
41        port,
42        username: None,
43        password: None,
44        keyspace: keyspace.map(str::to_string),
45        ssl: false,
46        color: ColorMode::Off,
47        debug: false,
48        tty: false,
49        no_file_io: false,
50        no_compact: false,
51        disable_history: true,
52        execute: None,
53        file: None,
54        connect_timeout: 10,
55        request_timeout: 30,
56        encoding: "utf-8".to_string(),
57        cqlversion: None,
58        protocol_version: None,
59        consistency_level: None,
60        serial_consistency_level: None,
61        browser: None,
62        secure_connect_bundle: None,
63        fetch_size: 100,
64        cqlshrc_path: PathBuf::from("/dev/null"),
65        cqlshrc: CqlshrcConfig::default(),
66    };
67
68    let mut session = session::CqlSession::connect(&config).await?;
69
70    if let Some(ks) = keyspace {
71        session.use_keyspace(ks).await?;
72        config.keyspace = Some(ks.to_string());
73    }
74
75    let colorizer = colorizer::CqlColorizer::new(false);
76    let mut output: Vec<u8> = Vec::new();
77    executor::execute_cql_string(&mut session, &config, &colorizer, cql, &mut output).await;
78
79    let _ = output.flush();
80    Ok(String::from_utf8_lossy(&output).into_owned())
81}