Skip to main content

dsl_parse/backend/sqlparser/
mod.rs

1//! sqlparser backend.
2//!
3//! Slices the source on top-level semicolons (via [`crate::split`]) and
4//! parses each chunk independently so one bad statement doesn't poison
5//! the rest of the file. The resulting upstream AST is then mapped to our
6//! internal [`Statement`](crate::ast::Statement) by [`convert`].
7
8mod convert;
9mod dialect_pick;
10
11use crate::ast::{Statement, StatementKind};
12use crate::dialect::Dialect;
13use crate::error::ParseError;
14use crate::parsed_file::ParsedFile;
15use crate::split::split_statements;
16use sqlparser::parser::Parser;
17
18/// Entry point. Identical signature to the crate-level `parse`; the
19/// feature flag in `lib.rs` decides whether to call this.
20pub fn parse(source: &str, dialect: Dialect) -> ParsedFile {
21  let d = dialect_pick::pick(dialect);
22  let mut statements = Vec::new();
23  let mut errors = Vec::new();
24
25  for (chunk, range) in split_statements(source) {
26    match Parser::parse_sql(&*d, &chunk) {
27      Ok(parsed) => {
28        for stmt in parsed {
29          statements.push(Statement { range, kind: convert::statement(stmt, &chunk) });
30        }
31      },
32      Err(e) => {
33        errors.push(ParseError { range, message: e.to_string() });
34        statements.push(Statement { range, kind: StatementKind::Unknown { text: chunk } });
35      },
36    }
37  }
38
39  ParsedFile { statements, errors }
40}