Merge remote-tracking branch 'origin/parsing' into parsing
This commit is contained in:
commit
f30d749962
3 changed files with 23 additions and 7 deletions
|
|
@ -1,12 +1,15 @@
|
||||||
use minisql::{operation::Operation, schema::TableSchema};
|
use minisql::{operation::Operation, schema::TableSchema};
|
||||||
use nom::{branch::alt, multi::many0, IResult};
|
use nom::{branch::alt, multi::many0, 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}};
|
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)]
|
#[derive(Debug, Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[error("parsing error: {0}")]
|
||||||
ParsingError(String),
|
ParsingError(String),
|
||||||
ValidationError(ValidationError)
|
#[error("validation error: {0}")]
|
||||||
|
ValidationError(#[from] ValidationError)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_statement<'a>(input: &'a str) -> IResult<&str, Operation> {
|
pub fn parse_statement<'a>(input: &'a str) -> IResult<&str, Operation> {
|
||||||
|
|
@ -31,7 +34,7 @@ pub fn parse_and_validate(query: String, db_metadata: &Vec<(String, &TableSchema
|
||||||
Error::ParsingError(err.to_string())
|
Error::ParsingError(err.to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
validate_operation(&op, db_metadata).map_err(|err| Error::ValidationError(err))?;
|
validate_operation(&op, db_metadata)?;
|
||||||
Ok(op)
|
Ok(op)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,12 +32,12 @@ pub fn parse_column_name(input: &str) -> IResult<&str, String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_db_type(input: &str) -> IResult<&str, DbType> {
|
pub fn parse_db_type(input: &str) -> IResult<&str, DbType> {
|
||||||
let (input, type_name) = alt((tag("STRING"), tag("INT"), tag("Float"), tag("UUID")))(input)?;
|
let (input, type_name) = alt((tag("STRING"), tag("INT"), tag("NUMBER"), tag("UUID")))(input)?;
|
||||||
let db_type = match type_name {
|
let db_type = match type_name {
|
||||||
"STRING" => DbType::String,
|
"STRING" => DbType::String,
|
||||||
"INT" => DbType::Int,
|
"INT" => DbType::Int,
|
||||||
"UUID" => DbType::Uuid,
|
"UUID" => DbType::Uuid,
|
||||||
"Float" => DbType::Number,
|
"NUMBER" => DbType::Number,
|
||||||
_ => return Err(nom::Err::Failure(make_error(input, nom::error::ErrorKind::IsNot)))
|
_ => return Err(nom::Err::Failure(make_error(input, nom::error::ErrorKind::IsNot)))
|
||||||
};
|
};
|
||||||
Ok((input, db_type))
|
Ok((input, db_type))
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,32 @@
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
use minisql::{operation::{ColumnSelection, Condition, InsertionValues, Operation}, schema::{TableSchema, ColumnName, TableName}, type_system::DbType};
|
use minisql::{operation::{ColumnSelection, Condition, InsertionValues, Operation}, schema::{TableSchema, ColumnName, TableName}, type_system::DbType};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Error)]
|
||||||
pub enum ValidationError {
|
pub enum ValidationError {
|
||||||
|
#[error("table {0} does not exist")]
|
||||||
TableDoesNotExist(TableName),
|
TableDoesNotExist(TableName),
|
||||||
|
#[error("table {0} already exists")]
|
||||||
TableAlreadyExists(TableName),
|
TableAlreadyExists(TableName),
|
||||||
|
// TODO
|
||||||
|
// #[error("columns {0} do not exist")]
|
||||||
|
#[error("columns do not exist")]
|
||||||
ColumnsDoNotExist(Vec<ColumnName>),
|
ColumnsDoNotExist(Vec<ColumnName>),
|
||||||
DuplicateColumn(String),
|
#[error("duplicate column {0}")]
|
||||||
|
DuplicateColumn(ColumnName),
|
||||||
|
// TODO: You need to actually print the error message
|
||||||
|
#[error("type mismatch")]
|
||||||
TypeMismatch(TypeMismatch),
|
TypeMismatch(TypeMismatch),
|
||||||
|
// TODO
|
||||||
|
// #[error("values for required columns {0} are missing")]
|
||||||
|
#[error("values for required columns are missing")]
|
||||||
RequiredColumnsAreMissing(Vec<ColumnName>)
|
RequiredColumnsAreMissing(Vec<ColumnName>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add derive(Error)
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TypeMismatch {
|
pub struct TypeMismatch {
|
||||||
pub column_name: ColumnName,
|
pub column_name: ColumnName,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue