1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Top Level of the sql parsing engine

mod common;
mod create;
mod insert;
mod select;

use self::select::parse_select;

use super::objects::ParseTree;
use create::parse_create_table;
use insert::parse_insert;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::combinator::{all_consuming, complete, opt};
use nom::error::{convert_error, ContextError, ParseError, VerboseError};
use nom::sequence::tuple;
use nom::Finish;
use nom::IResult;
use thiserror::Error;

pub struct SqlParser {}

impl SqlParser {
    pub fn parse(input: &str) -> Result<ParseTree, SqlParserError> {
        match SqlParser::nom_parse::<VerboseError<&str>>(input).finish() {
            Ok((_, cmd)) => Ok(cmd),
            Err(e) => Err(SqlParserError::ParseError(convert_error(input, e))),
        }
    }

    fn nom_parse<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
        input: &'a str,
    ) -> IResult<&'a str, ParseTree, E> {
        //TODO Had to remove all consuming since it was throwing EOF issues
        let (input, (result, _)) = complete(tuple((
            alt((parse_create_table, parse_insert, parse_select)),
            opt(tag(";")),
        )))(input)?;
        Ok((input, result))
    }
}

#[derive(Debug, Error)]
pub enum SqlParserError {
    #[error("SQL Parse Error {0}")]
    ParseError(String),
    #[error("Got an incomplete on {0} which shouldn't be possible")]
    Incomplete(String),
}