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