From 3d363bc1a9215e9f915d2daf829c12a19e6e921c Mon Sep 17 00:00:00 2001 From: sundy-li <543950155@qq.com> Date: Thu, 27 Apr 2023 16:49:05 +0800 Subject: [PATCH 1/3] feat: add output null and time --- cli/src/config.rs | 4 ++++ cli/src/display.rs | 4 ++++ cli/src/main.rs | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/cli/src/config.rs b/cli/src/config.rs index b0ddc817b..04c290a67 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -50,6 +50,8 @@ pub enum OutputFormat { Table, CSV, TSV, + Time, + Null, } impl Settings { @@ -64,6 +66,8 @@ impl Settings { "table" => OutputFormat::Table, "csv" => OutputFormat::CSV, "tsv" => OutputFormat::TSV, + "null" => OutputFormat::Null, + "time" => OutputFormat::Time, _ => return Err(anyhow!("Unknown output format: {}", cmd_value)), } } diff --git a/cli/src/display.rs b/cli/src/display.rs index c3e031ff6..240545cdf 100644 --- a/cli/src/display.rs +++ b/cli/src/display.rs @@ -199,6 +199,10 @@ impl<'a> ChunkDisplay for FormatDisplay<'a> { wtr.write_record(record)?; } } + OutputFormat::Time => { + println!("{:.3}", self._start.elapsed().as_secs_f64()); + } + OutputFormat::Null => {} } Ok(()) } diff --git a/cli/src/main.rs b/cli/src/main.rs index af467160e..01e22df4a 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -136,6 +136,9 @@ struct Args { #[clap(long, help = "Show progress for data loading in stderr")] progress: bool, + + #[clap(long, help = "Only show execution time without results")] + time: bool, } /// Parse a single key-value pair @@ -249,6 +252,10 @@ pub async fn main() -> Result<()> { config.settings.output_format = OutputFormat::TSV; } + if args.time { + config.settings.output_format = OutputFormat::Time; + } + let mut session = session::Session::try_new(dsn, config.settings, is_repl).await?; if is_repl { From f08854324d629bcd3092a5c4389e9dbfce68d3bf Mon Sep 17 00:00:00 2001 From: sundy-li <543950155@qq.com> Date: Thu, 27 Apr 2023 17:13:41 +0800 Subject: [PATCH 2/3] support multi statments --- cli/src/main.rs | 8 +++----- cli/src/session.rs | 9 +++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 01e22df4a..ddd3ed96b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -20,7 +20,7 @@ mod display; mod helper; mod session; -use std::collections::BTreeMap; +use std::{collections::BTreeMap, io::stdin}; use anyhow::{anyhow, Result}; use clap::{CommandFactory, Parser, ValueEnum}; @@ -268,13 +268,11 @@ pub async fn main() -> Result<()> { if args.non_interactive { return Err(anyhow!("no query specified")); } - session.handle_stdin().await + session.handle_reader(stdin().lock()).await } Some(query) => match args.data { None => { - if let Err(e) = session.handle_query(false, &query).await { - eprintln!("{}", e); - } + session.handle_reader(std::io::Cursor::new(query)).await; } Some(data) => { let options = args.format.get_options(&args.format_opt); diff --git a/cli/src/session.rs b/cli/src/session.rs index 26d01c52f..8b916349f 100644 --- a/cli/src/session.rs +++ b/cli/src/session.rs @@ -13,6 +13,7 @@ // limitations under the License. use std::collections::BTreeMap; +use std::io::stdin; use std::io::BufRead; use std::path::Path; use std::sync::Arc; @@ -71,7 +72,7 @@ impl Session { if self.is_repl { self.handle_repl().await; } else { - self.handle_stdin().await; + self.handle_reader(stdin().lock()).await; } } @@ -144,8 +145,8 @@ impl Session { let _ = rl.save_history(&get_history_path()); } - pub async fn handle_stdin(&mut self) { - let mut lines = std::io::stdin().lock().lines(); + pub async fn handle_reader(&mut self, r: R) { + let mut lines = r.lines(); while let Some(Ok(line)) = lines.next() { let queries = self.append_query(&line); for query in queries { @@ -166,7 +167,7 @@ impl Session { } } - fn append_query(&mut self, line: &str) -> Vec { + pub fn append_query(&mut self, line: &str) -> Vec { let line = line.trim(); if line.is_empty() { return vec![]; From 0ccd291ff49ab25815f66aede34184c99aa39705 Mon Sep 17 00:00:00 2001 From: sundy-li <543950155@qq.com> Date: Thu, 27 Apr 2023 17:20:43 +0800 Subject: [PATCH 3/3] make time as settings --- cli/src/config.rs | 6 ++++-- cli/src/display.rs | 7 ++++--- cli/src/main.rs | 5 +---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index 04c290a67..32dca1527 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -43,6 +43,8 @@ pub struct Settings { /// Output format is set by the flag. #[serde(skip)] pub output_format: OutputFormat, + #[serde(skip)] + pub time: bool, } #[derive(ValueEnum, Clone, Debug, PartialEq, Deserialize)] @@ -50,7 +52,6 @@ pub enum OutputFormat { Table, CSV, TSV, - Time, Null, } @@ -67,10 +68,10 @@ impl Settings { "csv" => OutputFormat::CSV, "tsv" => OutputFormat::TSV, "null" => OutputFormat::Null, - "time" => OutputFormat::Time, _ => return Err(anyhow!("Unknown output format: {}", cmd_value)), } } + "time" => self.time = cmd_value.parse()?, _ => return Err(anyhow!("Unknown command: {}", cmd_name)), } Ok(()) @@ -117,6 +118,7 @@ impl Default for Settings { prompt: "{user}@{host}> ".to_string(), output_format: OutputFormat::Table, show_progress: false, + time: false, } } } diff --git a/cli/src/display.rs b/cli/src/display.rs index 240545cdf..4b4ea56cd 100644 --- a/cli/src/display.rs +++ b/cli/src/display.rs @@ -199,11 +199,12 @@ impl<'a> ChunkDisplay for FormatDisplay<'a> { wtr.write_record(record)?; } } - OutputFormat::Time => { - println!("{:.3}", self._start.elapsed().as_secs_f64()); - } OutputFormat::Null => {} } + + if self.settings.time { + eprintln!("{:.3}", self._start.elapsed().as_secs_f64()); + } Ok(()) } diff --git a/cli/src/main.rs b/cli/src/main.rs index ddd3ed96b..a4b0762f9 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -251,10 +251,7 @@ pub async fn main() -> Result<()> { } else { config.settings.output_format = OutputFormat::TSV; } - - if args.time { - config.settings.output_format = OutputFormat::Time; - } + config.settings.time = args.time; let mut session = session::Session::try_new(dsn, config.settings, is_repl).await?;