Disentangle table.rs from operation.rs
This commit is contained in:
parent
f9b874f302
commit
bfb5042896
2 changed files with 28 additions and 34 deletions
|
|
@ -68,25 +68,44 @@ impl State {
|
||||||
// the client, but the details of communication are hidden behind an interface
|
// the client, but the details of communication are hidden behind an interface
|
||||||
//
|
//
|
||||||
// writer: impl SqlResponseConsumer
|
// writer: impl SqlResponseConsumer
|
||||||
fn interpret(&mut self, operation: Operation) -> DbResult<Response> {
|
pub fn interpret(&mut self, operation: Operation) -> DbResult<Response> {
|
||||||
// TODO: lock stuff
|
// TODO: lock stuff
|
||||||
use Operation::*;
|
use Operation::*;
|
||||||
|
|
||||||
match operation {
|
match operation {
|
||||||
Select(table_name, column_selection, maybe_condition) => {
|
Select(table_name, column_selection, maybe_condition) => {
|
||||||
let table: &Table = self.table_from_name(&table_name)?;
|
let table: &Table = self.table_from_name(&table_name)?;
|
||||||
Ok(Response::Selected(table.select_where(column_selection, maybe_condition)?))
|
|
||||||
|
let selected_column_positions: Vec<ColumnPosition> = table.schema.column_positions_from_column_selection(&column_selection)?;
|
||||||
|
let selected_rows = match maybe_condition {
|
||||||
|
None => table.select_all_rows(&selected_column_positions),
|
||||||
|
|
||||||
|
Some(Condition::Eq(eq_column_name, value)) => {
|
||||||
|
let eq_column_position = table.schema.column_position_from_column_name(&eq_column_name)?;
|
||||||
|
table.select_rows_where_eq(&selected_column_positions, eq_column_position, value)?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Response::Selected(selected_rows))
|
||||||
},
|
},
|
||||||
Insert(table_name, values) => {
|
Insert(table_name, values) => {
|
||||||
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
||||||
|
|
||||||
let _ = table.insert(values)?;
|
let (id, row) = table.schema.row_from_insertion_values(values)?;
|
||||||
|
let _ = table.insert_row_at(id, row)?;
|
||||||
Ok(Response::Inserted)
|
Ok(Response::Inserted)
|
||||||
},
|
},
|
||||||
Delete(table_name, maybe_condition) => {
|
Delete(table_name, maybe_condition) => {
|
||||||
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
||||||
|
|
||||||
let rows_affected = table.delete_rows_where(maybe_condition)?;
|
let rows_affected = match maybe_condition {
|
||||||
|
None => table.delete_all_rows(),
|
||||||
|
Some(Condition::Eq(eq_column_name, value)) => {
|
||||||
|
let eq_column_position = table.schema.column_position_from_column_name(&eq_column_name)?;
|
||||||
|
table.delete_rows_where_eq(eq_column_position, value)?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Response::Deleted(rows_affected))
|
Ok(Response::Deleted(rows_affected))
|
||||||
},
|
},
|
||||||
CreateTable(table_name, table_schema) => {
|
CreateTable(table_name, table_schema) => {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ use std::collections::{BTreeMap, HashMap, HashSet};
|
||||||
use crate::base::{TableSchema, ColumnPosition, ColumnName, DbResult};
|
use crate::base::{TableSchema, ColumnPosition, ColumnName, DbResult};
|
||||||
use crate::type_system::{UUID, DbValue, IndexableDbValue};
|
use crate::type_system::{UUID, DbValue, IndexableDbValue};
|
||||||
use crate::column_index::ColumnIndex;
|
use crate::column_index::ColumnIndex;
|
||||||
use crate::operation::{Condition, ColumnSelection, InsertionValues};
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -66,11 +65,11 @@ impl Table {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_all_rows(&self, selected_column_positions: &Vec<ColumnPosition>) -> Vec<Row> {
|
pub fn select_all_rows(&self, selected_column_positions: &Vec<ColumnPosition>) -> Vec<Row> {
|
||||||
self.rows.values().map(|row| restrict_columns(row, &selected_column_positions)).collect()
|
self.rows.values().map(|row| restrict_columns(row, &selected_column_positions)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_rows_where_eq(&self, selected_column_positions: &Vec<ColumnPosition>, column_position: ColumnPosition, value: DbValue) -> DbResult<Vec<Row>> {
|
pub fn select_rows_where_eq(&self, selected_column_positions: &Vec<ColumnPosition>, column_position: ColumnPosition, value: DbValue) -> DbResult<Vec<Row>> {
|
||||||
match value {
|
match value {
|
||||||
DbValue::Indexable(value) => {
|
DbValue::Indexable(value) => {
|
||||||
match self.fetch_ids_from_index(column_position, &value)? {
|
match self.fetch_ids_from_index(column_position, &value)? {
|
||||||
|
|
@ -86,22 +85,8 @@ impl Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_where(&self, column_selection: ColumnSelection, condition: Option<Condition>) -> DbResult<Vec<Row>> {
|
|
||||||
let selected_column_positions: Vec<ColumnPosition> = self.schema.column_positions_from_column_selection(&column_selection)?;
|
|
||||||
match condition {
|
|
||||||
None => Ok(self.select_all_rows(&selected_column_positions)),
|
|
||||||
|
|
||||||
Some(Condition::Eq(eq_column_name, value)) => {
|
|
||||||
let eq_column_position = self.schema.column_position_from_column_name(&eq_column_name)?;
|
|
||||||
self.select_rows_where_eq(&selected_column_positions, eq_column_position, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ======Insertion======
|
// ======Insertion======
|
||||||
pub fn insert(&mut self, values: InsertionValues) -> DbResult<()> {
|
pub fn insert_row_at(&mut self, id: UUID, row: Row) -> DbResult<()> {
|
||||||
let (id, row) = self.schema.row_from_insertion_values(values)?;
|
|
||||||
|
|
||||||
if self.rows.get(&id).is_some() {
|
if self.rows.get(&id).is_some() {
|
||||||
return Err(Error::AttemptingToInsertAlreadyPresentId(self.schema.table_name.clone(), id))
|
return Err(Error::AttemptingToInsertAlreadyPresentId(self.schema.table_name.clone(), id))
|
||||||
}
|
}
|
||||||
|
|
@ -150,14 +135,14 @@ impl Table {
|
||||||
self.delete_rows_by_ids(matched_ids)
|
self.delete_rows_by_ids(matched_ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_all_rows(&mut self) -> usize {
|
pub fn delete_all_rows(&mut self) -> usize {
|
||||||
let number_of_rows = self.rows.len();
|
let number_of_rows = self.rows.len();
|
||||||
self.rows = BTreeMap::new();
|
self.rows = BTreeMap::new();
|
||||||
self.indexes = HashMap::new();
|
self.indexes = HashMap::new();
|
||||||
number_of_rows
|
number_of_rows
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_rows_where_eq(&mut self, column_position: ColumnPosition, value: DbValue) -> DbResult<usize> {
|
pub fn delete_rows_where_eq(&mut self, column_position: ColumnPosition, value: DbValue) -> DbResult<usize> {
|
||||||
match value {
|
match value {
|
||||||
DbValue::Indexable(value) => {
|
DbValue::Indexable(value) => {
|
||||||
match self.fetch_ids_from_index(column_position, &value)? {
|
match self.fetch_ids_from_index(column_position, &value)? {
|
||||||
|
|
@ -172,16 +157,6 @@ impl Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_rows_where(&mut self, maybe_condition: Option<Condition>) -> DbResult<usize> {
|
|
||||||
match maybe_condition {
|
|
||||||
None => Ok(self.delete_all_rows()),
|
|
||||||
Some(Condition::Eq(eq_column_name, value)) => {
|
|
||||||
let eq_column_position = self.schema.column_position_from_column_name(&eq_column_name)?;
|
|
||||||
self.delete_rows_where_eq(eq_column_position, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ======Indexing======
|
// ======Indexing======
|
||||||
pub fn attach_index(&mut self, column_position: ColumnPosition, column_index: ColumnIndex) {
|
pub fn attach_index(&mut self, column_position: ColumnPosition, column_index: ColumnIndex) {
|
||||||
self.indexes.insert(column_position, column_index);
|
self.indexes.insert(column_position, column_index);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue