Skip to content

Commit 2f56be9

Browse files
committed
Pass all settings from --url to client
1 parent c6e7514 commit 2f56be9

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

src/view/providers/client.rs

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@ use crate::{
55
};
66
use cursive::{Cursive, views::Dialog};
77
use percent_encoding::percent_decode;
8+
use std::collections::HashMap;
89
use std::process::Command;
9-
use std::str::FromStr;
10+
11+
/// Parse a clickhouse-rs duration string (e.g. "600s", "500ms") into microseconds.
12+
fn parse_duration_us(s: &str) -> Option<u64> {
13+
if let Some(ms) = s.strip_suffix("ms") {
14+
ms.parse::<u64>().ok().map(|v| v * 1_000)
15+
} else if let Some(secs) = s.strip_suffix('s') {
16+
secs.parse::<u64>().ok().map(|v| v * 1_000_000)
17+
} else {
18+
s.parse::<u64>().ok().map(|v| v * 1_000_000)
19+
}
20+
}
1021

1122
pub struct ClientViewProvider;
1223

@@ -63,20 +74,66 @@ impl ViewProvider for ClientViewProvider {
6374
);
6475
}
6576

66-
let pairs: std::collections::HashMap<_, _> = url.query_pairs().into_owned().collect();
67-
if let Some(skip_verify) = pairs
68-
.get("skip_verify")
69-
.and_then(|v| bool::from_str(v).ok())
70-
&& skip_verify
71-
{
72-
cmd.arg("--accept-invalid-certificate");
77+
let database = url.path().strip_prefix('/').unwrap_or_default();
78+
if !database.is_empty() {
79+
cmd.arg("--database").arg(database);
7380
}
74-
if pairs
75-
.get("secure")
76-
.and_then(|v| bool::from_str(v).ok())
77-
.unwrap_or_default()
78-
{
79-
cmd.arg("--secure");
81+
82+
let pairs: HashMap<_, _> = url.query_pairs().into_owned().collect();
83+
for (key, value) in &pairs {
84+
match key.as_str() {
85+
// clickhouse-rs internal settings, not relevant for client
86+
"compression" | "pool_min" | "pool_max" | "nodelay" | "keepalive"
87+
| "ping_before_query" | "send_retries" | "retry_timeout" | "ping_timeout"
88+
| "insert_timeout" | "execute_timeout" | "alt_hosts" | "client_name" => {}
89+
// only via client config
90+
"ca_certificate" => {}
91+
"client_certificate" => {}
92+
"client_private_key" => {}
93+
// mapped to different client flag names
94+
"skip_verify" => {
95+
if value == "true" {
96+
cmd.arg("--accept-invalid-certificate");
97+
}
98+
}
99+
"secure" => {
100+
if value == "true" {
101+
cmd.arg("--secure");
102+
} else {
103+
cmd.arg("--no-secure");
104+
}
105+
}
106+
"connection_timeout" => {
107+
if let Some(us) = parse_duration_us(value) {
108+
if !pairs.contains_key("connect_timeout") {
109+
cmd.arg(format!("--connect_timeout={}", us / 1_000_000));
110+
}
111+
if !pairs.contains_key("connect_timeout_with_failover_ms") {
112+
cmd.arg(format!(
113+
"--connect_timeout_with_failover_ms={}",
114+
us / 1_000
115+
));
116+
}
117+
if !pairs.contains_key("connect_timeout_with_failover_secure_ms") {
118+
cmd.arg(format!(
119+
"--connect_timeout_with_failover_secure_ms={}",
120+
us / 1_000
121+
));
122+
}
123+
}
124+
}
125+
"query_timeout" => {
126+
if let Some(us) = parse_duration_us(value)
127+
&& !pairs.contains_key("max_execution_time")
128+
{
129+
cmd.arg(format!("--max_execution_time={}", us / 1_000_000));
130+
}
131+
}
132+
// pass through as-is (query settings like skip_unavailable_shards, etc.)
133+
_ => {
134+
cmd.arg(format!("--{}={}", key, value));
135+
}
136+
}
80137
}
81138
}
82139

0 commit comments

Comments
 (0)