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(command): add options cmd #407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ use anyhow::Result;
use clap::ValueEnum;
use serde::Deserialize;

use databend_driver::NumberValue;
use databend_driver::Row;
use databend_driver::Value;
use databend_driver::{Error, Result as DriverResult};

#[derive(Clone, Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
Expand Down Expand Up @@ -99,6 +104,67 @@ pub struct Settings {
pub replace_newline: bool,
}

impl TryFrom<Settings> for Vec<Row> {
type Error = Error;

fn try_from(settings: Settings) -> DriverResult<Self> {
Ok(vec![
Row::from_vec(vec![
Value::String("display_pretty_sql".to_string()),
Value::Boolean(settings.display_pretty_sql),
]),
Row::from_vec(vec![
Value::String("prompt".to_string()),
Value::String(settings.prompt.clone()),
]),
Row::from_vec(vec![
Value::String("progress_color".to_string()),
Value::String(settings.progress_color.clone()),
]),
Row::from_vec(vec![
Value::String("show_progress".to_string()),
Value::Boolean(settings.show_progress),
]),
Row::from_vec(vec![
Value::String("show_stats".to_string()),
Value::Boolean(settings.show_stats),
]),
Row::from_vec(vec![
Value::String("output_format".to_string()),
Value::String(format!("{:?}", settings.output_format)),
]),
Row::from_vec(vec![
Value::String("quote_style".to_string()),
Value::String(format!("{:?}", settings.quote_style)),
]),
Row::from_vec(vec![
Value::String("expand".to_string()),
Value::String(format!("{:?}", settings.expand)),
]),
Row::from_vec(vec![
Value::String("multi_line".to_string()),
Value::Boolean(settings.multi_line),
]),
Row::from_vec(vec![
Value::String("replace_newline".to_string()),
Value::Boolean(settings.replace_newline),
]),
Row::from_vec(vec![
Value::String("max_display_rows".to_string()),
Value::Number(NumberValue::Int64(settings.max_display_rows as i64)),
]),
Row::from_vec(vec![
Value::String("max_col_width".to_string()),
Value::Number(NumberValue::Int64(settings.max_col_width as i64)),
]),
Row::from_vec(vec![
Value::String("max_width".to_string()),
Value::Number(NumberValue::Int64(settings.max_width as i64)),
]),
])
}
}

#[derive(ValueEnum, Clone, Debug, PartialEq, Deserialize)]
pub enum OutputFormat {
Table,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ fn compute_render_widths(
}

/// Convert a series of rows into a table
fn create_table(
pub fn create_table(
schema: SchemaRef,
results: &[Row],
replace_newline: bool,
Expand Down
5 changes: 4 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ pub async fn main() -> Result<()> {
ConnectionArgs {
host: config.connection.host.clone(),
port: config.connection.port,
user: config.connection.user.clone(),
user: args
.user
.clone()
.unwrap_or_else(|| config.connection.user.clone()),
password: SensitiveString::from(""),
database: config.connection.database.clone(),
flight: args.flight,
Expand Down
37 changes: 35 additions & 2 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ use tokio_stream::StreamExt;
use crate::ast::{TokenKind, Tokenizer};
use crate::config::Settings;
use crate::config::TimeOption;
use crate::display::create_table;
use crate::display::{format_write_progress, ChunkDisplay, FormatDisplay};
use crate::helper::CliHelper;
use crate::VERSION;

use databend_driver::DataType;
use databend_driver::Field;
use databend_driver::Row;
use databend_driver::Schema;

static PROMPT_SQL: &str = "select name from system.tables union all select name from system.columns union all select name from system.databases union all select name from system.functions";

static VERSION_SHORT: Lazy<String> = Lazy::new(|| {
Expand Down Expand Up @@ -469,6 +475,30 @@ impl Session {
"!configs" => {
println!("{:#?}", self.settings);
}
"!options" => {
Copy link
Member

@sundy-li sundy-li Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems options is just different display with configs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. However, we currently do not have an [options] syntax block. Is it intended to support this content later?

let options: Vec<Row> = self.settings.clone().try_into().unwrap();
let schema = Schema::from_vec(vec![
Field {
name: "name".to_string(),
data_type: DataType::String,
},
Field {
name: "value".to_string(),
data_type: DataType::String,
},
]);
println!(
"{}",
create_table(
Arc::new(schema),
&options,
self.settings.replace_newline,
self.settings.max_display_rows,
self.settings.max_width,
self.settings.max_col_width
)?
);
}
other => {
if other.starts_with("!set") {
let query = query[4..].split_whitespace().collect::<Vec<_>>();
Expand All @@ -478,8 +508,11 @@ impl Session {
));
}
self.settings.inject_ctrl_cmd(query[0], query[1])?;
} else if other.starts_with("!source") {
let query = query[7..].trim();
} else if other.starts_with("!source") || other.starts_with("!load") {
let query = query
.strip_prefix("!source")
.or_else(|| query.strip_prefix("!load"))
.unwrap();
let path = Path::new(query);
if !path.exists() {
return Err(anyhow!("File not found: {}", query));
Expand Down
Loading