use crate::cli::globals::OutputFormat;
use crate::cli::query::QueryCommand;
use crate::cli::Cli;
use crate::error::Result;
use crate::output::envelope::Target;
use crate::output::Envelope;
use std::io::Write;
#[must_use]
pub fn applies(cli: &Cli, config: &crate::config::Config) -> bool {
let Ok(shape) = crate::output_shape(cli, config) else {
return false;
};
let streamable = match (shape, &cli.command) {
(
OutputFormat::Ndjson | OutputFormat::Tsv,
crate::cli::Command::Query(QueryCommand::File(_)),
) => true,
(
OutputFormat::Ndjson | OutputFormat::Tsv | OutputFormat::Json,
crate::cli::Command::Query(QueryCommand::Run(args)),
) => only_reads(args, config),
_ => false,
};
let signed = cli.globals.output.checksum;
streamable && !signed && cli.globals.pipeline(config).is_ok_and(|p| !p.accumulates())
}
fn only_reads(args: &crate::cli::query::RunArgs, config: &crate::config::Config) -> bool {
let Some(text) = args.statement.statement.as_deref() else {
return false;
};
let Ok(limits) = crate::sql::ParseLimits::from_server(config) else {
return false;
};
crate::sql::classify::classify(text, limits)
.is_ok_and(crate::sql::classify::Classification::is_read)
}
pub async fn run<W: Write>(
cli: &Cli,
config: &crate::config::Config,
session: &mut crate::conn::session::Session,
context: &mut crate::commands::query::Context,
writer: W,
target: &Target,
) -> Result<u64> {
let tail = Envelope::new(cli.command.published_name(), serde_json::json!({}))
.from_destination(target.clone(), cli.globals.destination.source());
match &cli.command {
crate::cli::Command::Query(QueryCommand::Run(args)) => {
read_as_a_flow(cli, config, session, context, writer, &tail, args).await
}
crate::cli::Command::Query(QueryCommand::File(args)) => {
file_as_a_flow(cli, config, session, context, writer, &tail, args).await
}
_ => Err(crate::error::Failure::usage(
"usage.not_a_flow",
"this command has no rows to stream",
)),
}
}
async fn read_as_a_flow<W: Write>(
cli: &Cli,
config: &crate::config::Config,
session: &mut crate::conn::session::Session,
context: &crate::commands::query::Context,
writer: W,
tail: &Envelope,
args: &crate::cli::query::RunArgs,
) -> Result<u64> {
let text = crate::commands::query::statement_text(
&args.statement,
crate::settings::no_input(&cli.globals),
context.policy.max_input_bytes,
)?;
let gated = crate::exec::gate(&text, &args.statement.params, &context.policy)?;
crate::exec::prepare::echo(&gated);
crate::exec::read::refuse_if_not_a_read(&gated)?;
let format = crate::output_shape(cli, config)?;
let pipeline = cli.globals.pipeline(config)?;
let reach = context
.policy
.max_rows
.saturating_add(pipeline.cursor.unwrap_or(0));
let widened = crate::exec::read::with_ceiling(gated, reach, context.policy.parse)?;
crate::exec::read::stream(
session.connection(),
&widened,
crate::output::stream::Sink {
writer,
formatter: crate::output::format::for_format(format),
tail,
},
&pipeline,
&context.conversion,
reach,
&context.token,
)
.await
}
async fn file_as_a_flow<W: Write>(
cli: &Cli,
config: &crate::config::Config,
session: &mut crate::conn::session::Session,
context: &mut crate::commands::query::Context,
writer: W,
tail: &Envelope,
args: &crate::cli::query::FileArgs,
) -> Result<u64> {
let (text, delimiter) = crate::commands::query_change::script_to_run(args, &context.policy)?;
let statements = crate::exec::script::split(&text, &delimiter)?;
let format = crate::output_shape(cli, config)?;
let pipeline = cli.globals.pipeline(config)?;
let server_read_only = session.facts().read_only;
let mut run = crate::exec::script::Run {
stop_on_failure: crate::commands::query_change::stops_at_the_first_failure(args),
conn: session.connection(),
policy: &context.policy,
write_state: &mut context.write_state,
server_read_only,
conversion: &context.conversion,
};
crate::exec::script::stream_file(
statements,
&mut run,
crate::output::stream::Sink {
writer,
formatter: crate::output::format::for_format(format),
tail,
},
&pipeline,
&context.token,
)
.await
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
fn parse(args: &[&str]) -> Cli {
let command_line: Vec<String> = std::iter::once("mariadb-cli")
.chain(args.iter().copied())
.map(ToOwned::to_owned)
.collect();
Cli::try_parse_from(command_line).expect("the sample invocation parses")
}
fn empty() -> (tempfile::TempDir, crate::config::Config) {
let scratch = tempfile::tempdir().expect("scratch directory");
let config =
crate::config::Config::load(Some(scratch.path())).expect("an empty location loads");
(scratch, config)
}
#[test]
fn a_line_oriented_read_streams() {
assert!(applies(
&parse(&["--output-format", "ndjson", "query", "run", "SELECT 1"]),
&empty().1
));
assert!(applies(
&parse(&["--output-format", "tsv", "query", "run", "SELECT 1"]),
&empty().1
));
}
#[test]
fn the_single_document_streams_a_read() {
assert!(applies(&parse(&["query", "run", "SELECT 1"]), &empty().1));
assert!(applies(
&parse(&["--output-format", "json", "query", "run", "SELECT 1"]),
&empty().1
));
}
#[test]
fn the_single_document_gathers_a_run_of_a_file() {
assert!(!applies(
&parse(&["--output-format", "json", "query", "file", "script.sql"]),
&empty().1
));
assert!(
applies(
&parse(&["--output-format", "ndjson", "query", "file", "script.sql"]),
&empty().1
),
"a shape that carries one record per line still streams it"
);
}
#[test]
fn a_statement_that_changes_the_server_is_gathered_and_never_refused() {
for shape in ["json", "ndjson", "tsv"] {
for statement in ["DELETE FROM t WHERE a = 1", "CREATE TABLE t (a INT)"] {
assert!(
!applies(
&parse(&["--output-format", shape, "query", "run", statement]),
&empty().1
),
"`{shape}` streamed `{statement}`, which has no rows to stream"
);
}
}
}
#[test]
fn a_statement_read_from_the_input_is_gathered() {
assert!(!applies(
&parse(&["--output-format", "ndjson", "query", "run", "--stdin"]),
&empty().1
));
}
#[test]
fn a_statement_that_cannot_be_parsed_is_never_streamed() {
assert!(!applies(
&parse(&["--output-format", "ndjson", "query", "run", "SELECT FROM"]),
&empty().1
));
}
#[test]
fn the_shape_meant_for_a_person_never_streams() {
assert!(!applies(
&parse(&["--output-format", "text", "query", "run", "SELECT 1"]),
&empty().1
));
}
#[test]
fn a_command_without_rows_never_streams() {
assert!(!applies(
&parse(&["--output-format", "ndjson", "schema", "tables"]),
&empty().1
));
assert!(!applies(
&parse(&["--output-format", "ndjson", "meta", "version"]),
&empty().1
));
}
#[test]
fn a_per_row_operation_still_streams() {
for operation in [
vec!["--limit", "10"],
vec!["--filter", "id=1"],
vec!["--select", "id"],
vec!["--dedupe-by", "id"],
vec!["--truncate-cell", "20"],
vec!["--max-output-bytes", "4096"],
] {
let argv: Vec<&str> = vec!["--output-format", "ndjson"]
.into_iter()
.chain(operation.iter().copied())
.chain(["query", "run", "SELECT 1"])
.collect();
assert!(
applies(&parse(&argv), &empty().1),
"`{argv:?}` must still stream"
);
}
}
#[test]
fn an_answer_over_the_whole_set_takes_the_whole_answer_path() {
for operation in [
vec!["--sort", "id"],
vec!["--count-only"],
vec!["--checksum"],
] {
let argv: Vec<&str> = vec!["--output-format", "ndjson"]
.into_iter()
.chain(operation.iter().copied())
.chain(["query", "run", "SELECT 1"])
.collect();
assert!(
!applies(&parse(&argv), &empty().1),
"`{argv:?}` must be gathered"
);
}
}
#[test]
fn one_gathering_operation_is_enough_to_gather() {
assert!(!applies(
&parse(&[
"--output-format",
"ndjson",
"--limit",
"10",
"--sort",
"id",
"query",
"run",
"SELECT 1"
]),
&empty().1
));
}
}