Skip to main content

sqlglot_rust/parser/
mod.rs

1mod sql_parser;
2
3pub use sql_parser::Parser;
4
5use crate::ast::Statement;
6use crate::dialects::Dialect;
7use crate::errors::Result;
8
9/// Parse a SQL string into a [`Statement`] AST using the given dialect.
10///
11/// # Errors
12///
13/// Returns a [`SqlglotError`](crate::errors::SqlglotError) if the input
14/// is not valid SQL.
15pub fn parse(sql: &str, _dialect: Dialect) -> Result<Statement> {
16    let mut parser = Parser::new(sql)?;
17    parser.parse_statement()
18}
19
20/// Parse a SQL string into a [`Statement`] AST, preserving SQL comments.
21///
22/// Comments are attached to the nearest AST node and survive through
23/// transformation and generation.
24///
25/// # Errors
26///
27/// Returns a [`SqlglotError`](crate::errors::SqlglotError) if the input
28/// is not valid SQL.
29pub fn parse_with_comments(sql: &str, _dialect: Dialect) -> Result<Statement> {
30    let mut parser = Parser::new_with_comments(sql)?;
31    parser.parse_statement()
32}
33
34/// Parse a SQL string containing multiple statements separated by semicolons.
35///
36/// # Errors
37///
38/// Returns a [`SqlglotError`](crate::errors::SqlglotError) if parsing fails.
39pub fn parse_statements(sql: &str, _dialect: Dialect) -> Result<Vec<Statement>> {
40    let mut parser = Parser::new(sql)?;
41    parser.parse_statements()
42}
43
44/// Parse multiple semicolon-separated SQL statements, preserving comments.
45///
46/// # Errors
47///
48/// Returns a [`SqlglotError`](crate::errors::SqlglotError) if parsing fails.
49pub fn parse_statements_with_comments(sql: &str, _dialect: Dialect) -> Result<Vec<Statement>> {
50    let mut parser = Parser::new_with_comments(sql)?;
51    parser.parse_statements()
52}