Skip to content

Commit deac269

Browse files
CreateIndex: Move Display fmt to struct (#1307)
1 parent be77ce5 commit deac269

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

src/ast/dml.rs

+43
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,49 @@ pub struct CreateIndex {
4747
pub nulls_distinct: Option<bool>,
4848
pub predicate: Option<Expr>,
4949
}
50+
51+
impl Display for CreateIndex {
52+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53+
write!(
54+
f,
55+
"CREATE {unique}INDEX {concurrently}{if_not_exists}",
56+
unique = if self.unique { "UNIQUE " } else { "" },
57+
concurrently = if self.concurrently {
58+
"CONCURRENTLY "
59+
} else {
60+
""
61+
},
62+
if_not_exists = if self.if_not_exists {
63+
"IF NOT EXISTS "
64+
} else {
65+
""
66+
},
67+
)?;
68+
if let Some(value) = &self.name {
69+
write!(f, "{value} ")?;
70+
}
71+
write!(f, "ON {}", self.table_name)?;
72+
if let Some(value) = &self.using {
73+
write!(f, " USING {value} ")?;
74+
}
75+
write!(f, "({})", display_separated(&self.columns, ","))?;
76+
if !self.include.is_empty() {
77+
write!(f, " INCLUDE ({})", display_separated(&self.include, ","))?;
78+
}
79+
if let Some(value) = self.nulls_distinct {
80+
if value {
81+
write!(f, " NULLS DISTINCT")?;
82+
} else {
83+
write!(f, " NULLS NOT DISTINCT")?;
84+
}
85+
}
86+
if let Some(predicate) = &self.predicate {
87+
write!(f, " WHERE {predicate}")?;
88+
}
89+
Ok(())
90+
}
91+
}
92+
5093
/// CREATE TABLE statement.
5194
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5295
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/ast/mod.rs

+1-42
Original file line numberDiff line numberDiff line change
@@ -3383,48 +3383,7 @@ impl fmt::Display for Statement {
33833383
}
33843384
Ok(())
33853385
}
3386-
Statement::CreateIndex(CreateIndex {
3387-
name,
3388-
table_name,
3389-
using,
3390-
columns,
3391-
unique,
3392-
concurrently,
3393-
if_not_exists,
3394-
include,
3395-
nulls_distinct,
3396-
predicate,
3397-
}) => {
3398-
write!(
3399-
f,
3400-
"CREATE {unique}INDEX {concurrently}{if_not_exists}",
3401-
unique = if *unique { "UNIQUE " } else { "" },
3402-
concurrently = if *concurrently { "CONCURRENTLY " } else { "" },
3403-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
3404-
)?;
3405-
if let Some(value) = name {
3406-
write!(f, "{value} ")?;
3407-
}
3408-
write!(f, "ON {table_name}")?;
3409-
if let Some(value) = using {
3410-
write!(f, " USING {value} ")?;
3411-
}
3412-
write!(f, "({})", display_separated(columns, ","))?;
3413-
if !include.is_empty() {
3414-
write!(f, " INCLUDE ({})", display_separated(include, ","))?;
3415-
}
3416-
if let Some(value) = nulls_distinct {
3417-
if *value {
3418-
write!(f, " NULLS DISTINCT")?;
3419-
} else {
3420-
write!(f, " NULLS NOT DISTINCT")?;
3421-
}
3422-
}
3423-
if let Some(predicate) = predicate {
3424-
write!(f, " WHERE {predicate}")?;
3425-
}
3426-
Ok(())
3427-
}
3386+
Statement::CreateIndex(create_index) => create_index.fmt(f),
34283387
Statement::CreateExtension {
34293388
name,
34303389
if_not_exists,

0 commit comments

Comments
 (0)