sqlparser/
lib.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! # SQL Parser for Rust
14//!
15//! This crate provides an ANSI:SQL 2011 lexer and parser that can parse SQL
16//! into an Abstract Syntax Tree ([`AST`]). See the [sqlparser crates.io page]
17//! for more information.
18//!
19//! For more information:
20//! 1. [`Parser::parse_sql`] and [`Parser::new`] for the Parsing API
21//! 2. [`ast`] for the AST structure
22//! 3. [`Dialect`] for supported SQL dialects
23//!
24//! # Example parsing SQL text
25//!
26//! ```
27//! use sqlparser::dialect::GenericDialect;
28//! use sqlparser::parser::Parser;
29//!
30//! let dialect = GenericDialect {}; // or AnsiDialect
31//!
32//! let sql = "SELECT a, b, 123, myfunc(b) \
33//!            FROM table_1 \
34//!            WHERE a > b AND b < 100 \
35//!            ORDER BY a DESC, b";
36//!
37//! let ast = Parser::parse_sql(&dialect, sql).unwrap();
38//!
39//! println!("AST: {:?}", ast);
40//! ```
41//!
42//! # Creating SQL text from AST
43//!
44//! This crate allows users to recover the original SQL text (with comments
45//! removed, normalized whitespace and identifier capitalization), which is
46//! useful for tools that 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//!
60//! [sqlparser crates.io page]: https://crates.io/crates/sqlparser
61//! [`Parser::parse_sql`]: crate::parser::Parser::parse_sql
62//! [`Parser::new`]: crate::parser::Parser::new
63//! [`AST`]: crate::ast
64//! [`ast`]: crate::ast
65//! [`Dialect`]: crate::dialect::Dialect
66
67#![cfg_attr(not(feature = "std"), no_std)]
68#![allow(clippy::upper_case_acronyms)]
69
70// Allow proc-macros to find this crate
71extern crate self as sqlparser;
72
73#[cfg(not(feature = "std"))]
74extern crate alloc;
75
76#[macro_use]
77#[cfg(test)]
78extern crate pretty_assertions;
79
80pub mod ast;
81#[macro_use]
82pub mod dialect;
83pub mod keywords;
84pub mod parser;
85pub mod tokenizer;
86
87#[doc(hidden)]
88// This is required to make utilities accessible by both the crate-internal
89// unit-tests and by the integration tests <https://stackoverflow.com/a/44541071/1026>
90// External users are not supposed to rely on this module.
91pub mod test_utils;