Skip to content

Commit 5aeb83c

Browse files
committed
document the new feature
1 parent b8e9de9 commit 5aeb83c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/ast/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,6 +4207,28 @@ impl fmt::Display for RaisErrorOption {
42074207
}
42084208

42094209
impl fmt::Display for Statement {
4210+
/// Formats a SQL statement with support for pretty printing.
4211+
///
4212+
/// When using the alternate flag (`{:#}`), the statement will be formatted with proper
4213+
/// indentation and line breaks. For example:
4214+
///
4215+
/// ```
4216+
/// # use sqlparser::dialect::GenericDialect;
4217+
/// # use sqlparser::parser::Parser;
4218+
/// let sql = "SELECT a, b FROM table_1";
4219+
/// let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
4220+
///
4221+
/// // Regular formatting
4222+
/// assert_eq!(format!("{}", ast[0]), "SELECT a, b FROM table_1");
4223+
///
4224+
/// // Pretty printing
4225+
/// assert_eq!(format!("{:#}", ast[0]),
4226+
/// r#"SELECT
4227+
/// a,
4228+
/// b
4229+
/// FROM
4230+
/// table_1"#);
4231+
/// ```
42104232
// Clippy thinks this function is too complicated, but it is painful to
42114233
// split up without extracting structs for each `Statement` variant.
42124234
#[allow(clippy::cognitive_complexity)]

src/display_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
//! Utilities for formatting SQL AST nodes with pretty printing support.
2+
//!
3+
//! The module provides formatters that implement the `Display` trait with support
4+
//! for both regular (`{}`) and pretty (`{:#}`) formatting modes. Pretty printing
5+
//! adds proper indentation and line breaks to make SQL statements more readable.
6+
17
use core::fmt::{self, Display, Write};
28

39
/// A wrapper around a value that adds an indent to the value when displayed with {:#}.

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@
6464
//! // The original SQL text can be generated from the AST
6565
//! assert_eq!(ast[0].to_string(), sql);
6666
//! ```
67+
//!
68+
//! # Pretty Printing
69+
//!
70+
//! SQL statements can be pretty-printed with proper indentation and line breaks using the alternate flag (`{:#}`):
71+
//!
72+
//! ```
73+
//! # use sqlparser::dialect::GenericDialect;
74+
//! # use sqlparser::parser::Parser;
75+
//! let sql = "SELECT a, b FROM table_1";
76+
//! let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
77+
//!
78+
//! // Pretty print with indentation and line breaks
79+
//! let pretty_sql = format!("{:#}", ast[0]);
80+
//! assert_eq!(pretty_sql, r#"
81+
//! SELECT
82+
//! a,
83+
//! b
84+
//! FROM
85+
//! table_1
86+
//! "#.trim());
87+
//! ```
6788
//! [sqlparser crates.io page]: https://crates.io/crates/sqlparser
6889
//! [`Parser::parse_sql`]: crate::parser::Parser::parse_sql
6990
//! [`Parser::new`]: crate::parser::Parser::new

0 commit comments

Comments
 (0)