File tree Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,28 @@ This crate avoids semantic analysis because it varies drastically
59
59
between dialects and implementations. If you want to do semantic
60
60
analysis, feel free to use this project as a base.
61
61
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
+
62
84
## SQL compliance
63
85
64
86
SQL was first standardized in 1987, and revisions of the standard have been
Original file line number Diff line number Diff line change 21
21
//! 2. [`ast`] for the AST structure
22
22
//! 3. [`Dialect`] for supported SQL dialects
23
23
//!
24
- //! # Example
24
+ //! # Example parsing SQL text
25
25
//!
26
26
//! ```
27
27
//! use sqlparser::dialect::GenericDialect;
39
39
//! println!("AST: {:?}", ast);
40
40
//! ```
41
41
//!
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
+ //!
42
60
//! [sqlparser crates.io page]: https://crates.io/crates/sqlparser
43
61
//! [`Parser::parse_sql`]: crate::parser::Parser::parse_sql
44
62
//! [`Parser::new`]: crate::parser::Parser::new
Original file line number Diff line number Diff line change @@ -102,6 +102,11 @@ impl TestedDialects {
102
102
/// Ensures that `sql` parses as a single [Statement] for all tested
103
103
/// dialects.
104
104
///
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
+ ///
105
110
/// If `canonical` is non empty,this function additionally asserts
106
111
/// that:
107
112
///
You can’t perform that action at this time.
0 commit comments