mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-04 06:18:17 +00:00
pretty-print CREATE TABLE statements (#1854)
This commit is contained in:
parent
525ed81fde
commit
a496f78803
3 changed files with 11 additions and 7 deletions
|
@ -29,7 +29,7 @@ use serde::{Deserialize, Serialize};
|
||||||
#[cfg(feature = "visitor")]
|
#[cfg(feature = "visitor")]
|
||||||
use sqlparser_derive::{Visit, VisitMut};
|
use sqlparser_derive::{Visit, VisitMut};
|
||||||
|
|
||||||
use crate::display_utils::{indented_list, Indent, SpaceOrNewline};
|
use crate::display_utils::{indented_list, DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
|
||||||
|
|
||||||
pub use super::ddl::{ColumnDef, TableConstraint};
|
pub use super::ddl::{ColumnDef, TableConstraint};
|
||||||
|
|
||||||
|
@ -267,14 +267,19 @@ impl Display for CreateTable {
|
||||||
write!(f, " ON CLUSTER {}", on_cluster)?;
|
write!(f, " ON CLUSTER {}", on_cluster)?;
|
||||||
}
|
}
|
||||||
if !self.columns.is_empty() || !self.constraints.is_empty() {
|
if !self.columns.is_empty() || !self.constraints.is_empty() {
|
||||||
write!(f, " ({}", display_comma_separated(&self.columns))?;
|
f.write_str(" (")?;
|
||||||
|
NewLine.fmt(f)?;
|
||||||
|
Indent(DisplayCommaSeparated(&self.columns)).fmt(f)?;
|
||||||
if !self.columns.is_empty() && !self.constraints.is_empty() {
|
if !self.columns.is_empty() && !self.constraints.is_empty() {
|
||||||
write!(f, ", ")?;
|
f.write_str(",")?;
|
||||||
|
SpaceOrNewline.fmt(f)?;
|
||||||
}
|
}
|
||||||
write!(f, "{})", display_comma_separated(&self.constraints))?;
|
Indent(DisplayCommaSeparated(&self.constraints)).fmt(f)?;
|
||||||
|
NewLine.fmt(f)?;
|
||||||
|
f.write_str(")")?;
|
||||||
} else if self.query.is_none() && self.like.is_none() && self.clone.is_none() {
|
} else if self.query.is_none() && self.like.is_none() && self.clone.is_none() {
|
||||||
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
|
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
|
||||||
write!(f, " ()")?;
|
f.write_str(" ()")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hive table comment should be after column definitions, please refer to:
|
// Hive table comment should be after column definitions, please refer to:
|
||||||
|
|
|
@ -68,7 +68,7 @@ impl Display for SpaceOrNewline {
|
||||||
|
|
||||||
/// A value that displays a comma-separated list of values.
|
/// A value that displays a comma-separated list of values.
|
||||||
/// When pretty-printed (using {:#}), it displays each value on a new line.
|
/// When pretty-printed (using {:#}), it displays each value on a new line.
|
||||||
pub(crate) struct DisplayCommaSeparated<'a, T: fmt::Display>(&'a [T]);
|
pub(crate) struct DisplayCommaSeparated<'a, T: fmt::Display>(pub(crate) &'a [T]);
|
||||||
|
|
||||||
impl<T: fmt::Display> fmt::Display for DisplayCommaSeparated<'_, T> {
|
impl<T: fmt::Display> fmt::Display for DisplayCommaSeparated<'_, T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
|
|
@ -249,7 +249,6 @@ DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore = "https://github.com/apache/datafusion-sqlparser-rs/issues/1850"]
|
|
||||||
fn test_pretty_print_create_table() {
|
fn test_pretty_print_create_table() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
prettify("CREATE TABLE my_table (id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, CONSTRAINT fk_other FOREIGN KEY (id) REFERENCES other_table(id))"),
|
prettify("CREATE TABLE my_table (id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, CONSTRAINT fk_other FOREIGN KEY (id) REFERENCES other_table(id))"),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue