Skip to content

Commit 64eccdb

Browse files
authored
Document round trip ability (#1052)
1 parent 86aa044 commit 64eccdb

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ This crate avoids semantic analysis because it varies drastically
5959
between dialects and implementations. If you want to do semantic
6060
analysis, feel free to use this project as a base.
6161

62+
## Preserves Syntax Round Trip
63+
64+
This crate allows users to recover the original SQL text (with normalized
65+
whitespace and keyword capitalization), which is useful for tools that
66+
analyze and manipulate SQL.
67+
68+
This means that other than whitespace and the capitalization of keywords, the
69+
following should hold true for all SQL:
70+
71+
```rust
72+
// Parse SQL
73+
let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
74+
75+
// The original SQL text can be generated from the AST
76+
assert_eq!(ast[0].to_string(), sql);
77+
```
78+
79+
There are still some cases in this crate where different SQL with seemingly
80+
similar semantics are represented with the same AST. We welcome PRs to fix such
81+
issues and distinguish different syntaxes in the AST.
82+
83+
6284
## SQL compliance
6385

6486
SQL was first standardized in 1987, and revisions of the standard have been

src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! 2. [`ast`] for the AST structure
2222
//! 3. [`Dialect`] for supported SQL dialects
2323
//!
24-
//! # Example
24+
//! # Example parsing SQL text
2525
//!
2626
//! ```
2727
//! use sqlparser::dialect::GenericDialect;
@@ -39,6 +39,24 @@
3939
//! println!("AST: {:?}", ast);
4040
//! ```
4141
//!
42+
//! # Creating SQL text from AST
43+
//!
44+
//! This crate allows users to recover the original SQL text (with normalized
45+
//! whitespace and identifier capitalization), which is useful for tools that
46+
//! analyze and manipulate SQL.
47+
//!
48+
//! ```
49+
//! # use sqlparser::dialect::GenericDialect;
50+
//! # use sqlparser::parser::Parser;
51+
//! let sql = "SELECT a FROM table_1";
52+
//!
53+
//! // parse to a Vec<Statement>
54+
//! let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
55+
//!
56+
//! // The original SQL text can be generated from the AST
57+
//! assert_eq!(ast[0].to_string(), sql);
58+
//! ```
59+
//!
4260
//! [sqlparser crates.io page]: https://crates.io/crates/sqlparser
4361
//! [`Parser::parse_sql`]: crate::parser::Parser::parse_sql
4462
//! [`Parser::new`]: crate::parser::Parser::new

src/test_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ impl TestedDialects {
102102
/// Ensures that `sql` parses as a single [Statement] for all tested
103103
/// dialects.
104104
///
105+
/// In general, the canonical SQL should be the same (see crate
106+
/// documentation for rationale) and you should prefer the `verified_`
107+
/// variants in testing, such as [`verified_statement`] or
108+
/// [`verified_query`].
109+
///
105110
/// If `canonical` is non empty,this function additionally asserts
106111
/// that:
107112
///

0 commit comments

Comments
 (0)