Skip to content

Commit

Permalink
z
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed Oct 9, 2023
1 parent 30ee54b commit e0240cf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
28 changes: 20 additions & 8 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ pub struct Settings {

/// Show time elapsed when executing queries.
/// only works with output format `null`.
pub time: bool,
/// Show execution time for the last query.
/// only works with output format `null`.
pub server_time: bool,
pub time: Option<TimeOption>,

/// Multi line mode, default is true.
pub multi_line: bool,
Expand All @@ -108,6 +105,23 @@ pub enum OutputFormat {
Null,
}

#[derive(ValueEnum, Clone, Debug, PartialEq)]
pub enum TimeOption {
Local,
Server,
}

impl TryFrom<&str> for TimeOption {
type Error = anyhow::Error;
fn try_from(s: &str) -> anyhow::Result<Self> {
match s.to_ascii_lowercase().as_str() {
"server" => Ok(TimeOption::Server),
"local" => Ok(TimeOption::Local),
_ => Err(anyhow!("Unknown time display option: {}", s)),
}
}
}

impl Settings {
pub fn merge_config(&mut self, cfg: SettingsConfig) {
self.display_pretty_sql = cfg.display_pretty_sql.unwrap_or(self.display_pretty_sql);
Expand Down Expand Up @@ -144,8 +158,7 @@ impl Settings {
}
}
"expand" => self.expand = cmd_value.into(),
"time" => self.time = cmd_value.parse()?,
"server_time" => self.server_time = cmd_value.parse()?,
"time" => self.time = Some(cmd_value.try_into()?),
"multi_line" => self.multi_line = cmd_value.parse()?,
"max_display_rows" => self.max_display_rows = cmd_value.parse()?,
"max_width" => self.max_width = cmd_value.parse()?,
Expand Down Expand Up @@ -210,8 +223,7 @@ impl Default for Settings {
max_col_width: 1024 * 1024,
max_width: 1024 * 1024,
show_stats: false,
time: false,
server_time: false,
time: None,
multi_line: true,
replace_newline: true,
}
Expand Down
22 changes: 8 additions & 14 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use std::{
};

use anyhow::{anyhow, Result};
use clap::{CommandFactory, Parser, ValueEnum};
use config::{Config, OutputFormat, Settings};
use clap::{ArgAction, CommandFactory, Parser, ValueEnum};
use config::{Config, OutputFormat, Settings, TimeOption};
use once_cell::sync::Lazy;

static VERSION: Lazy<String> = Lazy::new(|| {
Expand Down Expand Up @@ -163,14 +163,12 @@ struct Args {

#[clap(
long,
default_value = "local",
action = ArgAction::Set,
num_args = 0..=1, require_equals = true, default_missing_value = "local",
help = "Only show execution time without results, will implicitly set output format to `null`."
)]
time: bool,
#[clap(
long,
help = "Like --time, but will show time in the last query stats instead of client execute duration."
)]
server_time: bool,
time: Option<TimeOption>,
}

/// Parse a single key-value pair
Expand Down Expand Up @@ -323,14 +321,10 @@ pub async fn main() -> Result<()> {
if args.stats {
settings.show_stats = true;
}
if args.time {
settings.time = true;
settings.output_format = OutputFormat::Null;
}
if args.server_time {
settings.server_time = true;
if args.time.is_some() {
settings.output_format = OutputFormat::Null;
}
settings.time = args.time;

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

Expand Down
21 changes: 13 additions & 8 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use tokio_stream::StreamExt;

use crate::ast::{TokenKind, Tokenizer};
use crate::config::Settings;
use crate::config::TimeOption;
use crate::display::{format_write_progress, ChunkDisplay, FormatDisplay};
use crate::helper::CliHelper;
use crate::VERSION;
Expand Down Expand Up @@ -187,14 +188,18 @@ impl Session {
self.query.clear();
stats = self.handle_query(false, &query).await?;
}
if self.settings.time {
println!("{:.3}", start.elapsed().as_secs_f64());
} else if self.settings.server_time {
let server_time_ms = match stats {
None => 0.0,
Some(ss) => ss.running_time_ms,
};
println!("{:.3}", server_time_ms);
match self.settings.time {
None => {}
Some(TimeOption::Local) => {
println!("{:.3}", start.elapsed().as_secs_f64());
}
Some(TimeOption::Server) => {
let server_time_ms = match stats {
None => 0.0,
Some(ss) => ss.running_time_ms,
};
println!("{:.3}", server_time_ms);
}
}
Ok(())
}
Expand Down

0 comments on commit e0240cf

Please sign in to comment.