Introduce comma_separated_string() to reduce boilerplate in ToString impls

This commit is contained in:
Nickolay Ponomarev 2019-04-20 19:54:12 +03:00
parent ef76baa7fd
commit c8e7c3b343
2 changed files with 17 additions and 56 deletions

View file

@ -30,6 +30,14 @@ pub use self::value::Value;
pub use self::sql_operator::SQLOperator; pub use self::sql_operator::SQLOperator;
/// Like `vec.join(", ")`, but for any types implementing ToString.
fn comma_separated_string<T: ToString>(vec: &Vec<T>) -> String {
vec.iter()
.map(|a| a.to_string())
.collect::<Vec<String>>()
.join(", ")
}
/// Identifier name, in the originally quoted form (e.g. `"id"`) /// Identifier name, in the originally quoted form (e.g. `"id"`)
pub type SQLIdent = String; pub type SQLIdent = String;
@ -123,10 +131,7 @@ impl ToString for ASTNode {
"{} {}IN ({})", "{} {}IN ({})",
expr.as_ref().to_string(), expr.as_ref().to_string(),
if *negated { "NOT " } else { "" }, if *negated { "NOT " } else { "" },
list.iter() comma_separated_string(list)
.map(|a| a.to_string())
.collect::<Vec<String>>()
.join(", ")
), ),
ASTNode::SQLInSubquery { ASTNode::SQLInSubquery {
expr, expr,
@ -279,11 +284,7 @@ impl ToString for SQLStatement {
" VALUES({})", " VALUES({})",
values values
.iter() .iter()
.map(|row| row .map(|row| comma_separated_string(row))
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", "))
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", ") .join(", ")
); );
@ -297,14 +298,7 @@ impl ToString for SQLStatement {
} => { } => {
let mut s = format!("COPY {}", table_name.to_string()); let mut s = format!("COPY {}", table_name.to_string());
if columns.len() > 0 { if columns.len() > 0 {
s += &format!( s += &format!(" ({})", comma_separated_string(columns));
" ({})",
columns
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", ")
);
} }
s += " FROM stdin; "; s += " FROM stdin; ";
if values.len() > 0 { if values.len() > 0 {
@ -327,14 +321,7 @@ impl ToString for SQLStatement {
} => { } => {
let mut s = format!("UPDATE {}", table_name.to_string()); let mut s = format!("UPDATE {}", table_name.to_string());
if assignments.len() > 0 { if assignments.len() > 0 {
s += &format!( s += &format!("{}", comma_separated_string(assignments));
"{}",
assignments
.iter()
.map(|ass| ass.to_string())
.collect::<Vec<String>>()
.join(", ")
);
} }
if let Some(selection) = selection { if let Some(selection) = selection {
s += &format!(" WHERE {}", selection.to_string()); s += &format!(" WHERE {}", selection.to_string());
@ -373,11 +360,7 @@ impl ToString for SQLStatement {
} if *external => format!( } if *external => format!(
"CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'", "CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'",
name.to_string(), name.to_string(),
columns comma_separated_string(columns),
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", "),
file_format.as_ref().map(|f| f.to_string()).unwrap(), file_format.as_ref().map(|f| f.to_string()).unwrap(),
location.as_ref().unwrap() location.as_ref().unwrap()
), ),
@ -390,11 +373,7 @@ impl ToString for SQLStatement {
} => format!( } => format!(
"CREATE TABLE {} ({})", "CREATE TABLE {} ({})",
name.to_string(), name.to_string(),
columns comma_separated_string(columns)
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", ")
), ),
SQLStatement::SQLAlterTable { name, operation } => { SQLStatement::SQLAlterTable { name, operation } => {
format!("ALTER TABLE {} {}", name.to_string(), operation.to_string()) format!("ALTER TABLE {} {}", name.to_string(), operation.to_string())

View file

@ -29,14 +29,7 @@ impl ToString for SQLQuery {
} }
s += &self.body.to_string(); s += &self.body.to_string();
if let Some(ref order_by) = self.order_by { if let Some(ref order_by) = self.order_by {
s += &format!( s += &format!(" ORDER BY {}", comma_separated_string(order_by));
" ORDER BY {}",
order_by
.iter()
.map(|o| o.to_string())
.collect::<Vec<String>>()
.join(", ")
);
} }
if let Some(ref limit) = self.limit { if let Some(ref limit) = self.limit {
s += &format!(" LIMIT {}", limit.to_string()); s += &format!(" LIMIT {}", limit.to_string());
@ -130,11 +123,7 @@ impl ToString for SQLSelect {
let mut s = format!( let mut s = format!(
"SELECT{} {}", "SELECT{} {}",
if self.distinct { " DISTINCT" } else { "" }, if self.distinct { " DISTINCT" } else { "" },
self.projection comma_separated_string(&self.projection)
.iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(", ")
); );
if let Some(ref relation) = self.relation { if let Some(ref relation) = self.relation {
s += &format!(" FROM {}", relation.to_string()); s += &format!(" FROM {}", relation.to_string());
@ -146,14 +135,7 @@ impl ToString for SQLSelect {
s += &format!(" WHERE {}", selection.to_string()); s += &format!(" WHERE {}", selection.to_string());
} }
if let Some(ref group_by) = self.group_by { if let Some(ref group_by) = self.group_by {
s += &format!( s += &format!(" GROUP BY {}", comma_separated_string(group_by));
" GROUP BY {}",
group_by
.iter()
.map(|g| g.to_string())
.collect::<Vec<String>>()
.join(", ")
);
} }
if let Some(ref having) = self.having { if let Some(ref having) = self.having {
s += &format!(" HAVING {}", having.to_string()); s += &format!(" HAVING {}", having.to_string());