Skip to content

Commit 2a0f181

Browse files
committed
pretty-printing for INSERT statements and VALUES clause
- Refactor DisplayCommaSeparated to accept any iterable type. - Update indented_list to work with iterators. - Improve formatting in the Insert display implementation
1 parent a6b0ce5 commit 2a0f181

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

src/ast/dml.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,28 +581,32 @@ impl Display for Insert {
581581
)?;
582582
}
583583
if !self.columns.is_empty() {
584-
write!(f, "({}) ", display_comma_separated(&self.columns))?;
584+
write!(f, "({})", display_comma_separated(&self.columns))?;
585+
SpaceOrNewline.fmt(f)?;
585586
}
586587
if let Some(ref parts) = self.partitioned {
587588
if !parts.is_empty() {
588-
write!(f, "PARTITION ({}) ", display_comma_separated(parts))?;
589+
write!(f, "PARTITION ({})", display_comma_separated(parts))?;
590+
SpaceOrNewline.fmt(f)?;
589591
}
590592
}
591593
if !self.after_columns.is_empty() {
592-
write!(f, "({}) ", display_comma_separated(&self.after_columns))?;
594+
write!(f, "({})", display_comma_separated(&self.after_columns))?;
595+
SpaceOrNewline.fmt(f)?;
593596
}
594597

595598
if let Some(settings) = &self.settings {
596-
write!(f, "SETTINGS {} ", display_comma_separated(settings))?;
599+
write!(f, "SETTINGS {}", display_comma_separated(settings))?;
600+
SpaceOrNewline.fmt(f)?;
597601
}
598602

599603
if let Some(source) = &self.source {
600-
write!(f, "{source}")?;
604+
source.fmt(f)?;
601605
} else if !self.assignments.is_empty() {
602-
write!(f, "SET ")?;
603-
write!(f, "{}", display_comma_separated(&self.assignments))?;
606+
write!(f, "SET")?;
607+
indented_list(f, &self.assignments)?;
604608
} else if let Some(format_clause) = &self.format_clause {
605-
write!(f, "{format_clause}")?;
609+
format_clause.fmt(f)?;
606610
} else if self.columns.is_empty() {
607611
write!(f, "DEFAULT VALUES")?;
608612
}
@@ -622,7 +626,9 @@ impl Display for Insert {
622626
}
623627

624628
if let Some(returning) = &self.returning {
625-
write!(f, " RETURNING {}", display_comma_separated(returning))?;
629+
SpaceOrNewline.fmt(f)?;
630+
f.write_str("RETURNING")?;
631+
indented_list(f, returning)?;
626632
}
627633
Ok(())
628634
}

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4543,7 +4543,7 @@ impl fmt::Display for Statement {
45434543
}
45444544
Ok(())
45454545
}
4546-
Statement::Insert(insert) => write!(f, "{insert}"),
4546+
Statement::Insert(insert) => insert.fmt(f),
45474547
Statement::Install {
45484548
extension_name: name,
45494549
} => write!(f, "INSTALL {name}"),

src/ast/query.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,14 +2883,26 @@ pub struct Values {
28832883

28842884
impl fmt::Display for Values {
28852885
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2886-
write!(f, "VALUES ")?;
2887-
let prefix = if self.explicit_row { "ROW" } else { "" };
2888-
let mut delim = "";
2889-
for row in &self.rows {
2890-
write!(f, "{delim}")?;
2891-
delim = ", ";
2892-
write!(f, "{prefix}({})", display_comma_separated(row))?;
2886+
struct DisplayRow<'a> {
2887+
exprs: &'a [Expr],
2888+
explicit_row: bool,
2889+
}
2890+
impl<'a> fmt::Display for DisplayRow<'a> {
2891+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2892+
let comma_separated = display_comma_separated(self.exprs);
2893+
if self.explicit_row {
2894+
write!(f, "ROW({})", comma_separated)
2895+
} else {
2896+
write!(f, "({})", comma_separated)
2897+
}
2898+
}
28932899
}
2900+
let row_display_iter = self.rows.iter().map(|row| DisplayRow {
2901+
exprs: row,
2902+
explicit_row: self.explicit_row,
2903+
});
2904+
f.write_str("VALUES")?;
2905+
indented_list(f, row_display_iter)?;
28942906
Ok(())
28952907
}
28962908
}

src/display_utils.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,17 @@ impl Display for SpaceOrNewline {
6868

6969
/// A value that displays a comma-separated list of values.
7070
/// When pretty-printed (using {:#}), it displays each value on a new line.
71-
pub(crate) struct DisplayCommaSeparated<'a, T: fmt::Display>(&'a [T]);
71+
pub(crate) struct DisplayCommaSeparated<I, T: fmt::Display>(I)
72+
where
73+
I: IntoIterator<Item = T> + Clone;
7274

73-
impl<T: fmt::Display> fmt::Display for DisplayCommaSeparated<'_, T> {
75+
impl<I, T: fmt::Display> fmt::Display for DisplayCommaSeparated<I, T>
76+
where
77+
I: IntoIterator<Item = T> + Clone,
78+
{
7479
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7580
let mut first = true;
76-
for t in self.0 {
81+
for t in self.0.clone() {
7782
if !first {
7883
f.write_char(',')?;
7984
SpaceOrNewline.fmt(f)?;
@@ -86,9 +91,12 @@ impl<T: fmt::Display> fmt::Display for DisplayCommaSeparated<'_, T> {
8691
}
8792

8893
/// Displays a whitespace, followed by a comma-separated list that is indented when pretty-printed.
89-
pub(crate) fn indented_list<T: fmt::Display>(f: &mut fmt::Formatter, slice: &[T]) -> fmt::Result {
94+
pub(crate) fn indented_list<I, T: fmt::Display>(f: &mut fmt::Formatter, iter: I) -> fmt::Result
95+
where
96+
I: IntoIterator<Item = T> + Clone,
97+
{
9098
SpaceOrNewline.fmt(f)?;
91-
Indent(DisplayCommaSeparated(slice)).fmt(f)
99+
Indent(DisplayCommaSeparated(iter)).fmt(f)
92100
}
93101

94102
#[cfg(test)]

tests/pretty_print.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ string' AS str
171171
}
172172

173173
#[test]
174-
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
175174
fn test_pretty_print_insert_values() {
176175
assert_eq!(
177176
prettify("INSERT INTO my_table (a, b, c) VALUES (1, 2, 3), (4, 5, 6)"),
@@ -186,17 +185,18 @@ VALUES
186185
}
187186

188187
#[test]
189-
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
190188
fn test_pretty_print_insert_select() {
191189
assert_eq!(
192-
prettify("INSERT INTO my_table (a, b) SELECT x, y FROM source_table"),
190+
prettify("INSERT INTO my_table (a, b) SELECT x, y FROM source_table RETURNING a AS id"),
193191
r#"
194192
INSERT INTO my_table (a, b)
195193
SELECT
196194
x,
197195
y
198196
FROM
199197
source_table
198+
RETURNING
199+
a AS id
200200
"#
201201
.trim()
202202
);

0 commit comments

Comments
 (0)