use std::{borrow::Cow, fmt, str::FromStr};
use postgres_protocol::escape::{escape_identifier, escape_literal};
use super::{client::Row, Cluster, ClusterError};
trait AsSql {
fn as_sql(&self) -> Cow<'_, str>;
}
#[derive(thiserror::Error, Debug)]
pub enum ConfigError {
#[error("could not understand setting: {0}")]
SettingError(String),
}
pub fn reload(cluster: &Cluster) -> Result<(), ClusterError> {
cluster.connect(None)?.execute("SELECT pg_reload_conf()")?;
Ok(())
}
pub enum AlterSystem<'a> {
Set(&'a Parameter<'a>, &'a Value),
Reset(&'a Parameter<'a>),
ResetAll,
}
impl AlterSystem<'_> {
pub fn apply(&self, cluster: &Cluster) -> Result<(), ClusterError> {
cluster.connect(None)?.execute(&self.as_sql())?;
Ok(())
}
}
impl AsSql for AlterSystem<'_> {
fn as_sql(&self) -> Cow<'_, str> {
use AlterSystem::*;
match self {
Set(p, v) => format!("ALTER SYSTEM SET {} TO {}", p.as_sql(), v.as_sql()).into(),
Reset(p) => format!("ALTER SYSTEM RESET {}", p.as_sql()).into(),
ResetAll => "ALTER SYSTEM RESET ALL".into(),
}
}
}
#[derive(Debug, Clone)]
pub struct Setting {
pub name: String,
pub setting: String,
pub unit: Option<String>,
pub category: String,
pub short_desc: String,
pub extra_desc: Option<String>,
pub context: String,
pub vartype: String,
pub source: String,
pub min_val: Option<String>,
pub max_val: Option<String>,
pub enumvals: Option<Vec<String>>,
pub boot_val: Option<String>,
pub reset_val: Option<String>,
pub sourcefile: Option<String>,
pub sourceline: Option<i32>,
pub pending_restart: bool,
}
const ENUMVALS_SEPARATOR: char = '\x1f';
const SETTINGS_QUERY: &str = r"
SELECT
name,
setting,
unit,
category,
short_desc,
extra_desc,
context,
vartype,
source,
min_val,
max_val,
array_to_string(enumvals, chr(31)),
boot_val,
reset_val,
sourcefile,
sourceline,
pending_restart
FROM
pg_catalog.pg_settings
";
impl TryFrom<&Row> for Setting {
type Error = ConfigError;
fn try_from(row: &Row) -> Result<Self, Self::Error> {
let optional = |column: usize| row.get(column).map(str::to_owned);
let required = |column: usize| {
optional(column).ok_or_else(|| {
ConfigError::SettingError(format!("unexpected NULL in column {column}: {row:?}"))
})
};
Ok(Self {
name: required(0)?,
setting: required(1)?,
unit: optional(2),
category: required(3)?,
short_desc: required(4)?,
extra_desc: optional(5),
context: required(6)?,
vartype: required(7)?,
source: required(8)?,
min_val: optional(9),
max_val: optional(10),
enumvals: row.get(11).map(|enumvals| {
enumvals
.split(ENUMVALS_SEPARATOR)
.filter(|value| !value.is_empty())
.map(str::to_owned)
.collect()
}),
boot_val: optional(12),
reset_val: optional(13),
sourcefile: optional(14),
sourceline: row
.get(15)
.map(|line| {
line.parse().map_err(|_| {
ConfigError::SettingError(format!("invalid sourceline: {row:?}"))
})
})
.transpose()?,
pending_restart: match row.get(16) {
Some("t") => true,
Some("f") => false,
_ => Err(ConfigError::SettingError(format!(
"invalid pending_restart: {row:?}"
)))?,
},
})
}
}
impl Setting {
pub fn list(cluster: &Cluster) -> Result<Vec<Self>, ClusterError> {
let rows = cluster.connect(None)?.query(SETTINGS_QUERY)?;
Ok(rows.iter().map(Self::try_from).collect::<Result<_, _>>()?)
}
pub fn get<N: AsRef<str>>(name: N, cluster: &Cluster) -> Result<Option<Self>, ClusterError> {
let query = format!(
"{SETTINGS_QUERY} WHERE name = {}",
escape_literal(name.as_ref())
);
let rows = cluster.connect(None)?.query(&query)?;
Ok(rows.first().map(Self::try_from).transpose()?)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Parameter<'a>(pub &'a str);
impl Parameter<'_> {
pub fn get(&self, cluster: &Cluster) -> Result<Option<Value>, ClusterError> {
Ok(Setting::get(self.0, cluster)?
.map(|setting| Value::try_from(&setting).map_err(ConfigError::SettingError))
.transpose()?)
}
pub fn set<V: Into<Value>>(&self, cluster: &Cluster, value: V) -> Result<(), ClusterError> {
let value = value.into();
AlterSystem::Set(self, &value).apply(cluster)
}
pub fn reset(&self, cluster: &Cluster) -> Result<(), ClusterError> {
AlterSystem::Reset(self).apply(cluster)
}
}
impl AsSql for Parameter<'_> {
fn as_sql(&self) -> Cow<'_, str> {
escape_identifier(self.0).into()
}
}
impl fmt::Display for Parameter<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for Parameter<'_> {
fn as_ref(&self) -> &str {
self.0
}
}
impl<'a> From<&'a str> for Parameter<'a> {
fn from(name: &'a str) -> Self {
Self(name)
}
}
impl<'a> From<&'a Setting> for Parameter<'a> {
fn from(setting: &'a Setting) -> Self {
Self(&setting.name)
}
}
#[derive(Debug, PartialEq)]
pub enum Value {
Boolean(bool),
String(String), Number(String),
Memory(String, MemoryUnit),
Time(String, TimeUnit),
}
impl AsSql for Value {
fn as_sql(&self) -> Cow<'_, str> {
match self {
Value::Boolean(true) => "true".into(),
Value::Boolean(false) => "false".into(),
Value::String(value) => escape_literal(value).into(),
Value::Number(value) => value.into(),
Value::Memory(value, unit) => escape_literal(&format!("{value}{unit}")).into(),
Value::Time(value, unit) => escape_literal(&format!("{value}{unit}")).into(),
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Value::Boolean(value) => write!(f, "{value}"),
Value::String(value) => write!(f, "{value}"),
Value::Number(value) => write!(f, "{value}"),
Value::Memory(value, unit) => write!(f, "{value}{unit}"),
Value::Time(value, unit) => write!(f, "{value}{unit}"),
}
}
}
impl From<bool> for Value {
fn from(value: bool) -> Self {
Value::Boolean(value)
}
}
impl From<&str> for Value {
fn from(value: &str) -> Self {
Value::String(value.to_owned())
}
}
impl From<String> for Value {
fn from(value: String) -> Self {
Value::String(value)
}
}
impl From<&String> for Value {
fn from(value: &String) -> Self {
Value::String(value.clone())
}
}
macro_rules! value_number_from {
($($from_type:ty),*) => {
$(
impl From<$from_type> for Value {
fn from(number: $from_type) -> Self {
Value::Number(number.to_string())
}
}
)*
}
}
value_number_from!(i8, i16, i32, i64, i128);
value_number_from!(u8, u16, u32, u64, u128);
value_number_from!(f32, f64);
value_number_from!(usize, isize);
macro_rules! value_memory_from {
($($from_type:ty),*) => {
$(
impl From<($from_type, MemoryUnit)> for Value {
fn from((number, unit): ($from_type, MemoryUnit)) -> Self {
Value::Memory(number.to_string(), unit)
}
}
)*
}
}
value_memory_from!(i8, i16, i32, i64, i128);
value_memory_from!(u8, u16, u32, u64, u128);
value_memory_from!(f32, f64);
value_memory_from!(usize, isize);
macro_rules! value_time_from {
($($from_type:ty),*) => {
$(
impl From<($from_type, TimeUnit)> for Value {
fn from((number, unit): ($from_type, TimeUnit)) -> Self {
Value::Time(number.to_string(), unit)
}
}
)*
}
}
value_time_from!(i8, i16, i32, i64, i128);
value_time_from!(u8, u16, u32, u64, u128);
value_time_from!(f32, f64);
value_time_from!(usize, isize);
impl TryFrom<&Setting> for Value {
type Error = String;
fn try_from(setting: &Setting) -> Result<Self, Self::Error> {
Ok(match setting.vartype.as_ref() {
"bool" => match setting.setting.as_ref() {
"on" | "true" | "tru" | "tr" | "t" => Self::Boolean(true),
"yes" | "ye" | "y" | "1" => Self::Boolean(true),
"off" | "of" | "false" | "fals" | "fal" | "fa" | "f" => Self::Boolean(false),
"no" | "n" | "0" => Self::Boolean(false),
_ => return Err(format!("invalid boolean value: {setting:?}")),
},
"integer" | "real" => match setting.unit.as_deref() {
None => Self::Number(setting.setting.clone()),
Some("8kB" | "16MB") => Self::Number(setting.setting.clone()), Some(unit) => {
if let Ok(unit) = unit.parse::<MemoryUnit>() {
Self::Memory(setting.setting.clone(), unit)
} else if let Ok(unit) = unit.parse::<TimeUnit>() {
Self::Time(setting.setting.clone(), unit)
} else {
return Err(format!("invalid numeric value: {setting:?}"));
}
}
},
"string" => Self::String(setting.setting.clone()),
"enum" => Self::String(setting.setting.clone()),
_ => return Err(format!("unrecognised value type: {setting:?}")),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MemoryUnit {
Bytes,
Kibibytes,
Mebibytes,
Gibibytes,
Tebibytes,
}
impl fmt::Display for MemoryUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MemoryUnit::Bytes => write!(f, "B"),
MemoryUnit::Kibibytes => write!(f, "kB"),
MemoryUnit::Mebibytes => write!(f, "MB"),
MemoryUnit::Gibibytes => write!(f, "GB"),
MemoryUnit::Tebibytes => write!(f, "TB"),
}
}
}
impl FromStr for MemoryUnit {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"B" => Ok(MemoryUnit::Bytes),
"kB" => Ok(MemoryUnit::Kibibytes),
"MB" => Ok(MemoryUnit::Mebibytes),
"GB" => Ok(MemoryUnit::Gibibytes),
"TB" => Ok(MemoryUnit::Tebibytes),
_ => Err(format!("invalid memory unit: {s:?}")),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TimeUnit {
Microseconds,
Milliseconds,
Seconds,
Minutes,
Hours,
Days,
}
impl fmt::Display for TimeUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TimeUnit::Microseconds => write!(f, "us"),
TimeUnit::Milliseconds => write!(f, "ms"),
TimeUnit::Seconds => write!(f, "s"),
TimeUnit::Minutes => write!(f, "min"),
TimeUnit::Hours => write!(f, "h"),
TimeUnit::Days => write!(f, "d"),
}
}
}
impl FromStr for TimeUnit {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"us" => Ok(TimeUnit::Microseconds),
"ms" => Ok(TimeUnit::Milliseconds),
"s" => Ok(TimeUnit::Seconds),
"min" => Ok(TimeUnit::Minutes),
"h" => Ok(TimeUnit::Hours),
"d" => Ok(TimeUnit::Days),
_ => Err(format!("invalid time unit: {s:?}")),
}
}
}
#[cfg(test)]
mod tests {
use pastey::paste;
use super::{
AsSql,
MemoryUnit::{self, *},
Parameter,
TimeUnit::{self, *},
Value,
};
#[test]
fn test_parameter_as_sql() {
assert_eq!(Parameter("foo").as_sql(), "\"foo\"");
assert_eq!(Parameter("foo \\bar").as_sql(), "\"foo \\bar\"");
assert_eq!(Parameter("foo\"bar").as_sql(), "\"foo\"\"bar\"");
}
#[test]
fn test_value_as_sql_bool() {
assert_eq!(Value::Boolean(false).as_sql(), "false");
assert_eq!(Value::Boolean(true).as_sql(), "true");
}
#[test]
fn test_value_as_sql_string() {
assert_eq!(Value::from("foo").as_sql(), "'foo'");
assert_eq!(Value::from("foo \\bar").as_sql(), " E'foo \\\\bar'");
assert_eq!(Value::from("foo'\"'bar").as_sql(), "'foo''\"''bar'");
}
#[test]
fn test_value_as_sql_number() {
assert_eq!(Value::Number("123".into()).as_sql(), "123");
assert_eq!(Value::Number("123.456".into()).as_sql(), "123.456");
}
#[test]
fn test_value_as_sql_memory() {
assert_eq!(
Value::Memory("123.4".into(), Gibibytes).as_sql(),
"'123.4GB'",
);
}
#[test]
fn test_value_as_sql_time() {
assert_eq!(Value::Time("123.4".into(), Hours).as_sql(), "'123.4h'",);
}
macro_rules! test_value_number_from {
($($from_type:ty),*) => {
$(
paste! {
#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_lossless)]
fn [< test_value_number_from_ $from_type >]() {
assert_eq!(Value::from(42 as $from_type), Value::Number("42".into()));
}
}
)*
}
}
test_value_number_from!(i8, i16, i32, i64, i128);
test_value_number_from!(u8, u16, u32, u64, u128);
test_value_number_from!(f32, f64);
test_value_number_from!(usize, isize);
#[test]
fn test_memory_unit_roundtrip() {
let units = &[Bytes, Kibibytes, Mebibytes, Gibibytes, Tebibytes];
for unit in units {
assert_eq!(format!("{unit}").parse::<MemoryUnit>(), Ok(*unit));
}
}
#[test]
fn test_time_unit_roundtrip() {
let units = &[Microseconds, Milliseconds, Seconds, Minutes, Hours, Days];
for unit in units {
assert_eq!(format!("{unit}").parse::<TimeUnit>(), Ok(*unit));
}
}
}