|
|
@ -58,6 +58,7 @@ impl Default for Config {
|
|
|
|
path: None,
|
|
|
|
path: None,
|
|
|
|
password: None,
|
|
|
|
password: None,
|
|
|
|
database: None,
|
|
|
|
database: None,
|
|
|
|
|
|
|
|
unix_domain_socket: None,
|
|
|
|
}],
|
|
|
|
}],
|
|
|
|
key_config: KeyConfig::default(),
|
|
|
|
key_config: KeyConfig::default(),
|
|
|
|
log_level: LogLevel::default(),
|
|
|
|
log_level: LogLevel::default(),
|
|
|
@ -74,6 +75,7 @@ pub struct Connection {
|
|
|
|
port: Option<u64>,
|
|
|
|
port: Option<u64>,
|
|
|
|
path: Option<std::path::PathBuf>,
|
|
|
|
path: Option<std::path::PathBuf>,
|
|
|
|
password: Option<String>,
|
|
|
|
password: Option<String>,
|
|
|
|
|
|
|
|
unix_domain_socket: Option<std::path::PathBuf>,
|
|
|
|
pub database: Option<String>,
|
|
|
|
pub database: Option<String>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -199,22 +201,27 @@ impl Connection {
|
|
|
|
.password
|
|
|
|
.password
|
|
|
|
.as_ref()
|
|
|
|
.as_ref()
|
|
|
|
.map_or(String::new(), |p| p.to_string());
|
|
|
|
.map_or(String::new(), |p| p.to_string());
|
|
|
|
|
|
|
|
let unix_domain_socket = self
|
|
|
|
|
|
|
|
.valid_unix_domain_socket()
|
|
|
|
|
|
|
|
.map_or(String::new(), |uds| format!("?socket={}", uds));
|
|
|
|
|
|
|
|
|
|
|
|
match self.database.as_ref() {
|
|
|
|
match self.database.as_ref() {
|
|
|
|
Some(database) => Ok(format!(
|
|
|
|
Some(database) => Ok(format!(
|
|
|
|
"mysql://{user}:{password}@{host}:{port}/{database}",
|
|
|
|
"mysql://{user}:{password}@{host}:{port}/{database}{unix_domain_socket}",
|
|
|
|
user = user,
|
|
|
|
user = user,
|
|
|
|
password = password,
|
|
|
|
password = password,
|
|
|
|
host = host,
|
|
|
|
host = host,
|
|
|
|
|
|
|
|
database = database,
|
|
|
|
port = port,
|
|
|
|
port = port,
|
|
|
|
database = database
|
|
|
|
unix_domain_socket = unix_domain_socket,
|
|
|
|
)),
|
|
|
|
)),
|
|
|
|
None => Ok(format!(
|
|
|
|
None => Ok(format!(
|
|
|
|
"mysql://{user}:{password}@{host}:{port}",
|
|
|
|
"mysql://{user}:{password}@{host}:{port}{unix_domain_socket}",
|
|
|
|
user = user,
|
|
|
|
user = user,
|
|
|
|
password = password,
|
|
|
|
password = password,
|
|
|
|
host = host,
|
|
|
|
host = host,
|
|
|
|
port = port,
|
|
|
|
port = port,
|
|
|
|
|
|
|
|
unix_domain_socket = unix_domain_socket,
|
|
|
|
)),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -236,6 +243,23 @@ impl Connection {
|
|
|
|
.as_ref()
|
|
|
|
.as_ref()
|
|
|
|
.map_or(String::new(), |p| p.to_string());
|
|
|
|
.map_or(String::new(), |p| p.to_string());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(unix_domain_socket) = self.valid_unix_domain_socket() {
|
|
|
|
|
|
|
|
match self.database.as_ref() {
|
|
|
|
|
|
|
|
Some(database) => Ok(format!(
|
|
|
|
|
|
|
|
"postgres://?dbname={database}&host={unix_domain_socket}&user={user}&password={password}",
|
|
|
|
|
|
|
|
database = database,
|
|
|
|
|
|
|
|
unix_domain_socket = unix_domain_socket,
|
|
|
|
|
|
|
|
user = user,
|
|
|
|
|
|
|
|
password = password,
|
|
|
|
|
|
|
|
)),
|
|
|
|
|
|
|
|
None => Ok(format!(
|
|
|
|
|
|
|
|
"postgres://?host={unix_domain_socket}&user={user}&password={password}",
|
|
|
|
|
|
|
|
unix_domain_socket = unix_domain_socket,
|
|
|
|
|
|
|
|
user = user,
|
|
|
|
|
|
|
|
password = password,
|
|
|
|
|
|
|
|
)),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
match self.database.as_ref() {
|
|
|
|
match self.database.as_ref() {
|
|
|
|
Some(database) => Ok(format!(
|
|
|
|
Some(database) => Ok(format!(
|
|
|
|
"postgres://{user}:{password}@{host}:{port}/{database}",
|
|
|
|
"postgres://{user}:{password}@{host}:{port}/{database}",
|
|
|
@ -243,7 +267,7 @@ impl Connection {
|
|
|
|
password = password,
|
|
|
|
password = password,
|
|
|
|
host = host,
|
|
|
|
host = host,
|
|
|
|
port = port,
|
|
|
|
port = port,
|
|
|
|
database = database
|
|
|
|
database = database,
|
|
|
|
)),
|
|
|
|
)),
|
|
|
|
None => Ok(format!(
|
|
|
|
None => Ok(format!(
|
|
|
|
"postgres://{user}:{password}@{host}:{port}",
|
|
|
|
"postgres://{user}:{password}@{host}:{port}",
|
|
|
@ -254,6 +278,7 @@ impl Connection {
|
|
|
|
)),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
DatabaseType::Sqlite => {
|
|
|
|
DatabaseType::Sqlite => {
|
|
|
|
let path = self.path.as_ref().map_or(
|
|
|
|
let path = self.path.as_ref().map_or(
|
|
|
|
Err(anyhow::anyhow!("type sqlite needs the path field")),
|
|
|
|
Err(anyhow::anyhow!("type sqlite needs the path field")),
|
|
|
@ -287,6 +312,23 @@ impl Connection {
|
|
|
|
pub fn is_postgres(&self) -> bool {
|
|
|
|
pub fn is_postgres(&self) -> bool {
|
|
|
|
matches!(self.r#type, DatabaseType::Postgres)
|
|
|
|
matches!(self.r#type, DatabaseType::Postgres)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn valid_unix_domain_socket(&self) -> Option<String> {
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
|
|
|
|
// windows also supports UDS, but `rust` does not support UDS in windows now.
|
|
|
|
|
|
|
|
// https://github.com/rust-lang/rust/issues/56533
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.unix_domain_socket.as_ref().and_then(|uds| {
|
|
|
|
|
|
|
|
let path = expand_path(uds)?;
|
|
|
|
|
|
|
|
let path_str = path.to_str()?;
|
|
|
|
|
|
|
|
if path_str.is_empty() {
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(path_str.to_owned())
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_app_config_path() -> anyhow::Result<std::path::PathBuf> {
|
|
|
|
pub fn get_app_config_path() -> anyhow::Result<std::path::PathBuf> {
|
|
|
|