Add parsing (incl. validation)

Ensure identifiers start with alphabetical character

Rename parse_variable_name -> parse_column_name

Add DB value parsers and condition parser placeholder

Fix number parser, basic condition parser

Move select parser to select module

Add create statement parser

Move condition parser to common; add delete statement parser

Add drop statement parser

Add insert parser

Add update parser, combine operation parsers into one

Add initial validation, fix compiler warnings

Validation WIP

Allow more spaces in create statement, update TableSchema struct

Add create index parser and validator

Add todo in parse_identifier

Rework the new structure, many other changes
This commit is contained in:
Maxim Svistunov 2024-01-26 18:20:45 +01:00
parent 143dc0e5ce
commit 61c0a34253
20 changed files with 1138 additions and 39 deletions

View file

@ -20,7 +20,7 @@ pub struct State {
// #[derive(Debug)]
pub enum Response<'a> {
Selected(Box<dyn Iterator<Item=Row> + 'a>),
Selected(Box<dyn Iterator<Item=Row> + 'a + Send>),
Inserted,
Deleted(usize), // how many were deleted
TableCreated,
@ -48,13 +48,23 @@ impl std::fmt::Debug for Response<'_> {
}
impl State {
fn new() -> Self {
pub fn new() -> Self {
Self {
table_name_position_mapping: BiMap::new(),
tables: vec![],
}
}
/// TODO: return a reference to avoid allocations
pub fn metadata<'a>(&'a self) -> Vec<(String, &'a TableSchema)> {
let mut m = Vec::new();
for (name, pos) in &self.table_name_position_mapping {
let table_schema = self.tables.get(*pos).unwrap().schema();
m.push((name.clone(), table_schema));
}
m
}
fn table_from_name<'a>(&'a self, table_name: &TableName) -> DbResult<&'a Table> {
match self.table_name_position_mapping.get_by_left(table_name) {
Some(table_position) => {
@ -99,7 +109,7 @@ impl State {
let selected_rows = match maybe_condition {
None => {
let x = table.select_all_rows(selected_column_positions);
Box::new(x) as Box<dyn Iterator<Item=Row> + 'a>
Box::new(x) as Box<dyn Iterator<Item=Row> + 'a + Send>
},
Some(Condition::Eq(eq_column_name, value)) => {
@ -112,7 +122,7 @@ impl State {
eq_column_position,
value,
)?;
Box::new(x) as Box<dyn Iterator<Item=Row> + 'a>
Box::new(x) as Box<dyn Iterator<Item=Row> + 'a + Send>
}
};

View file

@ -12,15 +12,15 @@ use std::collections::HashMap;
pub struct TableSchema {
table_name: TableName, // used for descriptive errors
primary_key: ColumnPosition,
column_name_position_mapping: BiMap<ColumnName, ColumnPosition>,
types: Vec<DbType>,
pub column_name_position_mapping: BiMap<ColumnName, ColumnPosition>,
pub types: Vec<DbType>,
}
pub type TableName = String;
pub type ColumnName = String;
impl TableSchema {
pub(crate) fn new(table_name: TableName, primary_key: ColumnPosition, column_name_position_map: Vec<(ColumnName, ColumnPosition)>, types: Vec<DbType>) -> Self {
pub fn new(table_name: TableName, primary_key: ColumnPosition, column_name_position_map: Vec<(ColumnName, ColumnPosition)>, types: Vec<DbType>) -> Self {
let mut column_name_position_mapping: BiMap<ColumnName, ColumnPosition> = BiMap::new();
for (column_name, column_position) in column_name_position_map {
column_name_position_mapping.insert(column_name, column_position);
@ -113,7 +113,7 @@ impl TableSchema {
}
}
fn number_of_columns(&self) -> usize {
pub fn number_of_columns(&self) -> usize {
self.column_name_position_mapping.len()
}

View file

@ -1,5 +1,5 @@
// ==============Types================
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DbType {
String,
Int,