tests_fuzz/translator/
common.rsuse std::fmt::Display;
use super::DslTranslator;
use crate::error::{Error, Result};
use crate::ir::alter_expr::AlterTableOperation;
use crate::ir::{AlterTableExpr, AlterTableOption};
pub(crate) struct CommonAlterTableTranslator;
impl DslTranslator<AlterTableExpr, String> for CommonAlterTableTranslator {
type Error = Error;
fn translate(&self, input: &AlterTableExpr) -> Result<String> {
Ok(match &input.alter_kinds {
AlterTableOperation::DropColumn { name } => Self::format_drop(&input.table_name, name),
AlterTableOperation::SetTableOptions { options } => {
Self::format_set_table_options(&input.table_name, options)
}
AlterTableOperation::UnsetTableOptions { keys } => {
Self::format_unset_table_options(&input.table_name, keys)
}
_ => unimplemented!(),
})
}
}
impl CommonAlterTableTranslator {
fn format_drop(name: impl Display, column: impl Display) -> String {
format!("ALTER TABLE {name} DROP COLUMN {column};")
}
fn format_set_table_options(name: impl Display, options: &[AlterTableOption]) -> String {
format!(
"ALTER TABLE {name} SET {};",
options
.iter()
.map(|option| option.to_string())
.collect::<Vec<_>>()
.join(", ")
)
}
fn format_unset_table_options(name: impl Display, keys: &[String]) -> String {
format!(
"ALTER TABLE {name} UNSET {};",
keys.iter()
.map(|key| format!("'{}'", key))
.collect::<Vec<_>>()
.join(", ")
)
}
}