我正在try 创建一个 struct ,它将包含所有其他 struct 中存在的基本函数,如下所示:
pub struct GenericSelect {
table: String,
distinct: bool,
columns: Option<String>,
group: Option<String>,
order: Option<String>,
limit: Option<u32>,
offset: Option<u32>,
}
impl GenericSelect {
pub fn new<T: Into<String>>(table: T) -> GenericSelect {
GenericSelect {
table: table.into(),
distinct: false,
columns: None,
group: None,
order: None,
limit: None,
offset: None,
}
}
pub fn distinct(&mut self) -> &mut Self {
self.distinct = true;
self
}
pub fn columns<T: Into<String>>(&mut self, columns: T) -> &mut Self {
self.columns = Some(columns.into());
self
}
pub fn group<T: Into<String>>(&mut self, group: T) -> &mut Self {
self.group = Some(group.into());
self
}
pub fn order<T: Into<String>>(&mut self, order: T) -> &mut Self {
self.order = Some(order.into());
self
}
pub fn limit(&mut self, limit: u32) -> &mut Self {
self.limit = Some(limit);
self
}
pub fn offset(&mut self, offset: u32) -> &mut Self {
self.offset = Some(offset);
self
}
pub fn build(&self) -> String {
let mut statement = String::from("SELECT");
if self.distinct {
statement.push_str(" DISTINCT");
}
if let Some(columns) = &self.columns {
statement.push_str(&format!(" {}", columns));
} else {
statement.push_str(" *");
}
statement.push_str(&format!(" FROM {}", self.table));
if let Some(group) = &self.group {
statement.push_str(&format!(" GROUP BY {}", group));
}
if let Some(order) = &self.order {
statement.push_str(&format!(" ORDER BY {}", order));
}
if let Some(limit) = &self.limit {
statement.push_str(&format!(" LIMIT {}", limit));
}
if let Some(offset) = &self.offset {
statement.push_str(&format!(" OFFSET {}", offset));
}
statement.push(';');
statement
}
}
我想要一个特定的 struct 来拥有这个 struct 所拥有的一切,而不需要逐个方法地编写方法,我还希望有可能在必要时实现一个函数,比如更改特定函数的行为,等等.