diff --git a/cli/Cargo.toml b/cli/Cargo.toml index fab0b1408..305b39601 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bendsql" -version = "0.3.1" +version = "0.3.2" edition = "2021" license = "Apache-2.0" description = "Databend Native Command Line Tool" diff --git a/cli/src/config.rs b/cli/src/config.rs index 967c1b2af..d8104fd9e 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -33,10 +33,14 @@ pub struct Settings { pub display_pretty_sql: bool, pub prompt: String, pub progress_color: String, - pub output_format: OutputFormat, + /// Show progress [bar] when executing queries. /// Only works in non-interactive mode. pub show_progress: bool, + + /// Output format is set by the flag. + #[serde(skip)] + pub output_format: OutputFormat, } #[derive(ValueEnum, Clone, Debug, PartialEq, Deserialize)] diff --git a/cli/src/main.rs b/cli/src/main.rs index 5ee06dca4..8975b4ae6 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -125,8 +125,8 @@ struct Args { #[clap(long, value_parser = parse_key_val::, help = "Data format options")] format_opt: Vec<(String, String)>, - #[clap(short = 'o', long, default_value = "table", help = "Output format")] - output: OutputFormat, + #[clap(short = 'o', long, help = "Output format")] + output: Option, #[clap(long, help = "Show progress for data loading in stderr")] progress: bool, @@ -233,10 +233,16 @@ pub async fn main() -> Result<()> { conn_args.get_dsn()? } }; - config.settings.output_format = args.output; + let is_repl = atty::is(atty::Stream::Stdin) && !args.non_interactive && args.query.is_none(); config.settings.show_progress = args.progress; + if let Some(output) = args.output { + config.settings.output_format = output; + } else if is_repl { + config.settings.output_format = OutputFormat::Table; + } else { + config.settings.output_format = OutputFormat::TSV; + } - let is_repl = atty::is(atty::Stream::Stdin) && !args.non_interactive && args.query.is_none(); let mut session = session::Session::try_new(dsn, config.settings, is_repl).await?; if is_repl { diff --git a/cli/src/session.rs b/cli/src/session.rs index 880819fec..2b5083368 100644 --- a/cli/src/session.rs +++ b/cli/src/session.rs @@ -156,6 +156,13 @@ impl Session { } } } + + // if the last query is not finished with `;`, we need to execute it. + if let Some(query) = self.query.take() { + if let Err(e) = self.handle_query(false, &query).await { + eprintln!("{}", e); + } + } } fn append_query(&mut self, line: &str) -> Option {