152 lines
4.6 KiB
Rust
152 lines
4.6 KiB
Rust
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ConnectionConfig {
|
|
pub id: String,
|
|
pub name: String,
|
|
pub db_type: DatabaseType,
|
|
pub host: String,
|
|
pub port: u16,
|
|
pub username: String,
|
|
pub password: String,
|
|
pub database: Option<String>,
|
|
#[serde(default)]
|
|
pub ssh_enabled: bool,
|
|
#[serde(default)]
|
|
pub ssh_host: String,
|
|
#[serde(default = "default_ssh_port")]
|
|
pub ssh_port: u16,
|
|
#[serde(default)]
|
|
pub ssh_user: String,
|
|
#[serde(default)]
|
|
pub ssh_password: String,
|
|
#[serde(default)]
|
|
pub ssh_key_path: String,
|
|
}
|
|
|
|
fn default_ssh_port() -> u16 { 22 }
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum DatabaseType {
|
|
Mysql,
|
|
Postgres,
|
|
Sqlite,
|
|
Redis,
|
|
#[serde(rename = "duckdb")]
|
|
DuckDb,
|
|
#[serde(rename = "clickhouse")]
|
|
ClickHouse,
|
|
#[serde(rename = "sqlserver")]
|
|
SqlServer,
|
|
#[serde(rename = "mongodb")]
|
|
MongoDb,
|
|
#[serde(rename = "oracle")]
|
|
Oracle,
|
|
}
|
|
|
|
impl ConnectionConfig {
|
|
pub fn connection_url(&self) -> String {
|
|
self.connection_url_with_host(&self.host, self.port)
|
|
}
|
|
|
|
pub fn connection_url_with_host(&self, host: &str, port: u16) -> String {
|
|
let db_part = self
|
|
.database
|
|
.as_deref()
|
|
.filter(|d| !d.is_empty())
|
|
.map(|d| format!("/{}", encode_url_part(d)))
|
|
.unwrap_or_default();
|
|
let username = encode_url_part(&self.username);
|
|
let password = encode_url_part(&self.password);
|
|
|
|
match self.db_type {
|
|
DatabaseType::Sqlite | DatabaseType::DuckDb => {
|
|
format!("{}?mode=rwc", self.host)
|
|
}
|
|
DatabaseType::Redis => {
|
|
if self.password.is_empty() {
|
|
format!("redis://{host}:{port}/")
|
|
} else {
|
|
format!("redis://:{password}@{host}:{port}/")
|
|
}
|
|
}
|
|
DatabaseType::Mysql => format!(
|
|
"mysql://{}:{}@{host}:{port}{db_part}?ssl-mode=preferred",
|
|
username, password
|
|
),
|
|
DatabaseType::Postgres => format!(
|
|
"postgres://{}:{}@{host}:{port}{db_part}",
|
|
username, password
|
|
),
|
|
DatabaseType::ClickHouse => format!(
|
|
"http://{host}:{port}{db_part}"
|
|
),
|
|
DatabaseType::SqlServer => format!(
|
|
"server=tcp:{host},{port};user={};password={};database={}",
|
|
self.username, self.password, self.database.as_deref().unwrap_or("master")
|
|
),
|
|
DatabaseType::MongoDb => {
|
|
if self.username.is_empty() {
|
|
format!("mongodb://{host}:{port}{db_part}")
|
|
} else {
|
|
format!("mongodb://{username}:{password}@{host}:{port}{db_part}")
|
|
}
|
|
}
|
|
DatabaseType::Oracle => format!(
|
|
"oracle://{}:{}@{host}:{port}{db_part}",
|
|
username, password
|
|
),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn encode_url_part(value: &str) -> String {
|
|
utf8_percent_encode(value, NON_ALPHANUMERIC).to_string()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::{ConnectionConfig, DatabaseType};
|
|
|
|
fn mysql_config(username: &str, password: &str, database: Option<&str>) -> ConnectionConfig {
|
|
ConnectionConfig {
|
|
id: "id".to_string(),
|
|
name: "name".to_string(),
|
|
db_type: DatabaseType::Mysql,
|
|
host: "10.1.2.3".to_string(),
|
|
port: 2883,
|
|
username: username.to_string(),
|
|
password: password.to_string(),
|
|
database: database.map(str::to_string),
|
|
ssh_enabled: false,
|
|
ssh_host: String::new(),
|
|
ssh_port: 22,
|
|
ssh_user: String::new(),
|
|
ssh_password: String::new(),
|
|
ssh_key_path: String::new(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn mysql_url_encodes_oceanbase_username() {
|
|
let config = mysql_config("user@tenant#cluster", "secret", None);
|
|
|
|
assert_eq!(
|
|
config.connection_url(),
|
|
"mysql://user%40tenant%23cluster:secret@10.1.2.3:2883?ssl-mode=preferred"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn mysql_url_encodes_password_and_database() {
|
|
let config = mysql_config("root", "p@ss:word#1", Some("db/name"));
|
|
|
|
assert_eq!(
|
|
config.connection_url(),
|
|
"mysql://root:p%40ss%3Aword%231@10.1.2.3:2883/db%2Fname?ssl-mode=preferred"
|
|
);
|
|
}
|
|
}
|