40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
use minisql::{operation::Operation, interpreter::DbSchema};
|
|
use crate::syntax::RawQuerySyntax;
|
|
use nom::{branch::alt, IResult};
|
|
use thiserror::Error;
|
|
|
|
use crate::{parsing::{create::parse_create, delete::parse_delete, index::parse_create_index, insert::parse_insert, select::parse_select}, validation::{validate_operation, ValidationError}};
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum Error {
|
|
#[error("parsing error: {0}")]
|
|
ParsingError(String),
|
|
#[error("validation error: {0}")]
|
|
ValidationError(#[from] ValidationError)
|
|
}
|
|
|
|
fn parse_statement(input: &str) -> IResult<&str, RawQuerySyntax> {
|
|
alt((
|
|
parse_insert,
|
|
parse_create,
|
|
parse_delete,
|
|
//parse_drop,
|
|
parse_select,
|
|
// parse_update,
|
|
parse_create_index
|
|
))(input)
|
|
}
|
|
|
|
pub fn parse_and_validate(str_query: String, db_schema: &DbSchema) -> Result<Operation, Error> {
|
|
let (_, op) = parse_statement(str_query.as_str())
|
|
.map_err(|err| {
|
|
Error::ParsingError(err.to_string())
|
|
})?;
|
|
|
|
Ok(validate_operation(op, db_schema)?)
|
|
}
|
|
|
|
// #[test]
|
|
// fn test_select() {
|
|
// parse_and_validate("SELECT * FROM users;".to_string(), &Vec::new()).unwrap();
|
|
// }
|