CreateIndex: Move Display fmt to struct (#1307)

This commit is contained in:
Philip Cristiano 2024-06-17 14:10:40 -04:00 committed by GitHub
parent be77ce50ca
commit deac269710
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 42 deletions

View file

@ -47,6 +47,49 @@ pub struct CreateIndex {
pub nulls_distinct: Option<bool>,
pub predicate: Option<Expr>,
}
impl Display for CreateIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"CREATE {unique}INDEX {concurrently}{if_not_exists}",
unique = if self.unique { "UNIQUE " } else { "" },
concurrently = if self.concurrently {
"CONCURRENTLY "
} else {
""
},
if_not_exists = if self.if_not_exists {
"IF NOT EXISTS "
} else {
""
},
)?;
if let Some(value) = &self.name {
write!(f, "{value} ")?;
}
write!(f, "ON {}", self.table_name)?;
if let Some(value) = &self.using {
write!(f, " USING {value} ")?;
}
write!(f, "({})", display_separated(&self.columns, ","))?;
if !self.include.is_empty() {
write!(f, " INCLUDE ({})", display_separated(&self.include, ","))?;
}
if let Some(value) = self.nulls_distinct {
if value {
write!(f, " NULLS DISTINCT")?;
} else {
write!(f, " NULLS NOT DISTINCT")?;
}
}
if let Some(predicate) = &self.predicate {
write!(f, " WHERE {predicate}")?;
}
Ok(())
}
}
/// CREATE TABLE statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

View file

@ -3383,48 +3383,7 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::CreateIndex(CreateIndex {
name,
table_name,
using,
columns,
unique,
concurrently,
if_not_exists,
include,
nulls_distinct,
predicate,
}) => {
write!(
f,
"CREATE {unique}INDEX {concurrently}{if_not_exists}",
unique = if *unique { "UNIQUE " } else { "" },
concurrently = if *concurrently { "CONCURRENTLY " } else { "" },
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
)?;
if let Some(value) = name {
write!(f, "{value} ")?;
}
write!(f, "ON {table_name}")?;
if let Some(value) = using {
write!(f, " USING {value} ")?;
}
write!(f, "({})", display_separated(columns, ","))?;
if !include.is_empty() {
write!(f, " INCLUDE ({})", display_separated(include, ","))?;
}
if let Some(value) = nulls_distinct {
if *value {
write!(f, " NULLS DISTINCT")?;
} else {
write!(f, " NULLS NOT DISTINCT")?;
}
}
if let Some(predicate) = predicate {
write!(f, " WHERE {predicate}")?;
}
Ok(())
}
Statement::CreateIndex(create_index) => create_index.fmt(f),
Statement::CreateExtension {
name,
if_not_exists,