Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add output null and time #101

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ 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)]
pub enum OutputFormat {
Table,
CSV,
TSV,
Null,
}

impl Settings {
Expand All @@ -64,9 +67,11 @@ impl Settings {
"table" => OutputFormat::Table,
"csv" => OutputFormat::CSV,
"tsv" => OutputFormat::TSV,
"null" => OutputFormat::Null,
_ => return Err(anyhow!("Unknown output format: {}", cmd_value)),
}
}
"time" => self.time = cmd_value.parse()?,
_ => return Err(anyhow!("Unknown command: {}", cmd_name)),
}
Ok(())
Expand Down Expand Up @@ -113,6 +118,7 @@ impl Default for Settings {
prompt: "{user}@{host}> ".to_string(),
output_format: OutputFormat::Table,
show_progress: false,
time: false,
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions cli/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ impl<'a> ChunkDisplay for FormatDisplay<'a> {
wtr.write_record(record)?;
}
}
OutputFormat::Null => {}
}

if self.settings.time {
eprintln!("{:.3}", self._start.elapsed().as_secs_f64());
}
Ok(())
}
Expand Down
12 changes: 7 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -248,6 +251,7 @@ pub async fn main() -> Result<()> {
} else {
config.settings.output_format = OutputFormat::TSV;
}
config.settings.time = args.time;

let mut session = session::Session::try_new(dsn, config.settings, is_repl).await?;

Expand All @@ -261,13 +265,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);
Expand Down
9 changes: 5 additions & 4 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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<R: BufRead>(&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 {
Expand All @@ -166,7 +167,7 @@ impl Session {
}
}

fn append_query(&mut self, line: &str) -> Vec<String> {
pub fn append_query(&mut self, line: &str) -> Vec<String> {
let line = line.trim();
if line.is_empty() {
return vec![];
Expand Down