Introduce new simplified Operation type for Interpreter
This commit is contained in:
parent
cf76cc4d10
commit
935d9814ae
4 changed files with 110 additions and 3 deletions
|
|
@ -2,7 +2,7 @@ use crate::error::Error;
|
|||
use crate::internals::row::{ColumnPosition, Row};
|
||||
use crate::schema::{TableName, TableSchema};
|
||||
use crate::internals::table::Table;
|
||||
use crate::operation::{ColumnSelection, Condition, Operation};
|
||||
use crate::operation::{ColumnSelection, Condition, Operation, OperationForInterpreter, ConditionForInterpreter, ColumnSelectionForInterpreter};
|
||||
use crate::result::DbResult;
|
||||
use crate::type_system::{DbType, IndexableValue, Value};
|
||||
use bimap::BiMap;
|
||||
|
|
@ -65,6 +65,7 @@ impl State {
|
|||
m
|
||||
}
|
||||
|
||||
// TODO: Get rid of this
|
||||
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) => {
|
||||
|
|
@ -75,6 +76,10 @@ impl State {
|
|||
}
|
||||
}
|
||||
|
||||
fn table_at<'a>(&'a self, table_position: TablePosition) -> &'a Table {
|
||||
&self.tables[table_position]
|
||||
}
|
||||
|
||||
fn table_from_name_mut<'b: 'a, 'a>(
|
||||
&'b mut self,
|
||||
table_name: &TableName,
|
||||
|
|
@ -88,6 +93,10 @@ impl State {
|
|||
}
|
||||
}
|
||||
|
||||
fn table_at_mut<'a>(&'a mut self, table_position: TablePosition) -> &'a mut Table {
|
||||
&mut self.tables[table_position]
|
||||
}
|
||||
|
||||
fn attach_table(&mut self, table_name: TableName, table: Table) {
|
||||
let new_table_position: TablePosition = self.tables.len();
|
||||
self.table_name_position_mapping
|
||||
|
|
@ -95,7 +104,67 @@ impl State {
|
|||
self.tables.push(table);
|
||||
}
|
||||
|
||||
pub fn interpret<'a>(&'a mut self, operation: Operation) -> DbResult<Response<'a>> {
|
||||
pub fn interpret_for_interpreter<'a>(&'a mut self, operation: OperationForInterpreter) -> DbResult<Response<'a>> {
|
||||
// TODO: lock stuff
|
||||
use OperationForInterpreter::*;
|
||||
|
||||
match operation {
|
||||
Select(table_position, column_selection, maybe_condition) => {
|
||||
let table: &Table = self.table_at(table_position);
|
||||
|
||||
let selected_rows = match maybe_condition {
|
||||
None => {
|
||||
let x = table.select_all_rows(column_selection);
|
||||
Box::new(x) as Box<dyn Iterator<Item=Row> + 'a + Send>
|
||||
},
|
||||
|
||||
Some(ConditionForInterpreter::Eq(eq_column, value)) => {
|
||||
let x =
|
||||
table.select_rows_where_eq(
|
||||
column_selection,
|
||||
eq_column,
|
||||
value,
|
||||
)?;
|
||||
Box::new(x) as Box<dyn Iterator<Item=Row> + 'a + Send>
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Response::Selected(selected_rows))
|
||||
},
|
||||
Insert(table_position, values) => {
|
||||
let table: &mut Table = self.table_at_mut(table_position);
|
||||
|
||||
let (id, row) = table.schema().row_from_insertion_values_for_interpreter(values)?;
|
||||
table.insert_row_at(id, row)?;
|
||||
Ok(Response::Inserted)
|
||||
}
|
||||
Delete(table_position, maybe_condition) => {
|
||||
let table: &mut Table = self.table_at_mut(table_position);
|
||||
|
||||
let rows_affected = match maybe_condition {
|
||||
None => table.delete_all_rows(),
|
||||
Some(ConditionForInterpreter::Eq(eq_column, value)) => {
|
||||
table.delete_rows_where_eq(eq_column, value)?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Response::Deleted(rows_affected))
|
||||
}
|
||||
CreateTable(table_name, table_schema) => {
|
||||
let table = Table::new(table_schema);
|
||||
self.attach_table(table_name, table);
|
||||
|
||||
Ok(Response::TableCreated)
|
||||
}
|
||||
CreateIndex(table_position, column) => {
|
||||
let table: &mut Table = self.table_at_mut(table_position);
|
||||
table.attach_index(column)?;
|
||||
Ok(Response::IndexCreated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interpret<'a>(&'a mut self, operation: Operation) -> DbResult<Response<'a>> {
|
||||
// TODO: lock stuff
|
||||
use Operation::*;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue