Improve Create Table
This commit is contained in:
parent
4a099468b2
commit
7b5b2bf9f3
6 changed files with 197 additions and 107 deletions
|
|
@ -73,10 +73,10 @@ impl State {
|
|||
&mut self.tables[table_position]
|
||||
}
|
||||
|
||||
fn attach_table(&mut self, table_name: TableName, table: Table) {
|
||||
fn attach_table(&mut self, table: Table) {
|
||||
let new_table_position: TablePosition = self.tables.len();
|
||||
self.table_name_position_mapping
|
||||
.insert(table_name, new_table_position);
|
||||
.insert(table.schema().table_name().clone(), new_table_position);
|
||||
self.tables.push(table);
|
||||
}
|
||||
|
||||
|
|
@ -126,9 +126,9 @@ impl State {
|
|||
|
||||
Ok(Response::Deleted(rows_affected))
|
||||
}
|
||||
CreateTable(table_name, table_schema) => {
|
||||
CreateTable(table_schema) => {
|
||||
let table = Table::new(table_schema);
|
||||
self.attach_table(table_name, table);
|
||||
self.attach_table(table);
|
||||
|
||||
Ok(Response::TableCreated)
|
||||
}
|
||||
|
|
@ -150,17 +150,13 @@ mod tests {
|
|||
use crate::operation::Operation;
|
||||
|
||||
fn users_schema() -> TableSchema {
|
||||
let id: ColumnPosition = 0;
|
||||
let name: ColumnPosition = 1;
|
||||
let age: ColumnPosition = 2;
|
||||
|
||||
TableSchema::new(
|
||||
"users".to_string(),
|
||||
id,
|
||||
"id".to_string(),
|
||||
vec!(
|
||||
("id".to_string(), id),
|
||||
("name".to_string(), name),
|
||||
("age".to_string(), age),
|
||||
"id".to_string(),
|
||||
"name".to_string(),
|
||||
"age".to_string(),
|
||||
),
|
||||
vec![DbType::Uuid, DbType::String, DbType::Int],
|
||||
)
|
||||
|
|
@ -173,7 +169,7 @@ mod tests {
|
|||
let users = users_schema.table_name().clone();
|
||||
|
||||
state
|
||||
.interpret(Operation::CreateTable(users.clone(), users_schema))
|
||||
.interpret(Operation::CreateTable(users_schema))
|
||||
.unwrap();
|
||||
|
||||
assert!(state.tables.len() == 1);
|
||||
|
|
@ -187,11 +183,10 @@ mod tests {
|
|||
fn test_select_empty() {
|
||||
let mut state = State::new();
|
||||
let users_schema = users_schema();
|
||||
let users = users_schema.table_name().clone();
|
||||
let users_position = 0;
|
||||
|
||||
state
|
||||
.interpret(Operation::CreateTable(users, users_schema.clone()))
|
||||
.interpret(Operation::CreateTable(users_schema.clone()))
|
||||
.unwrap();
|
||||
let response: Response = state
|
||||
.interpret(Operation::Select(users_position, users_schema.all_selection(), None))
|
||||
|
|
@ -215,7 +210,7 @@ mod tests {
|
|||
|
||||
|
||||
state
|
||||
.interpret(Operation::CreateTable("users".to_string(), users_schema.clone()))
|
||||
.interpret(Operation::CreateTable(users_schema.clone()))
|
||||
.unwrap();
|
||||
|
||||
let (id, name, age) = (
|
||||
|
|
@ -267,7 +262,7 @@ mod tests {
|
|||
let name_column: ColumnPosition = 1;
|
||||
|
||||
state
|
||||
.interpret(CreateTable(users_schema.table_name().clone(), users_schema.clone()))
|
||||
.interpret(CreateTable(users_schema.clone()))
|
||||
.unwrap();
|
||||
|
||||
let (id0, name0, age0) = (
|
||||
|
|
@ -384,7 +379,7 @@ mod tests {
|
|||
let id_column: ColumnPosition = 0;
|
||||
|
||||
state
|
||||
.interpret(CreateTable(users_schema.table_name().clone(), users_schema.clone()))
|
||||
.interpret(CreateTable(users_schema.clone()))
|
||||
.unwrap();
|
||||
|
||||
let (id0, name0, age0) = (
|
||||
|
|
@ -458,7 +453,7 @@ mod tests {
|
|||
let name_column: ColumnPosition = 1;
|
||||
|
||||
state
|
||||
.interpret(CreateTable(users_schema.table_name().clone(), users_schema.clone()))
|
||||
.interpret(CreateTable(users_schema.clone()))
|
||||
.unwrap();
|
||||
|
||||
state
|
||||
|
|
@ -525,26 +520,25 @@ pub fn example() {
|
|||
|
||||
let id_column: ColumnPosition = 0;
|
||||
let name_column: ColumnPosition = 1;
|
||||
let age_column: ColumnPosition = 2;
|
||||
// let age_column: ColumnPosition = 2;
|
||||
|
||||
let users_schema: TableSchema = {
|
||||
TableSchema::new(
|
||||
"users".to_string(),
|
||||
id_column,
|
||||
"id".to_string(),
|
||||
vec!(
|
||||
("id".to_string(), id_column),
|
||||
("name".to_string(), name_column),
|
||||
("age".to_string(), age_column),
|
||||
"id".to_string(), // 0
|
||||
"name".to_string(), // 1
|
||||
"age".to_string(), // 2
|
||||
),
|
||||
vec![DbType::Uuid, DbType::String, DbType::Int],
|
||||
)
|
||||
};
|
||||
let users_position: TablePosition = 0;
|
||||
let users = users_schema.table_name().clone();
|
||||
|
||||
let mut state = State::new();
|
||||
state
|
||||
.interpret(Operation::CreateTable(users, users_schema.clone()))
|
||||
.interpret(Operation::CreateTable(users_schema.clone()))
|
||||
.unwrap();
|
||||
|
||||
let (id0, name0, age0) = (
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::schema::{TableName, TableSchema};
|
||||
use crate::schema::TableSchema;
|
||||
use crate::type_system::Value;
|
||||
use crate::internals::row::ColumnPosition;
|
||||
use crate::interpreter::TablePosition;
|
||||
|
|
@ -9,7 +9,7 @@ pub enum Operation {
|
|||
Select(TablePosition, ColumnSelection, Option<Condition>),
|
||||
Insert(TablePosition, InsertionValues),
|
||||
Delete(TablePosition, Option<Condition>),
|
||||
CreateTable(TableName, TableSchema),
|
||||
CreateTable(TableSchema),
|
||||
CreateIndex(TablePosition, ColumnPosition),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,11 +18,15 @@ pub type TableName = String;
|
|||
pub type ColumnName = String;
|
||||
|
||||
impl TableSchema {
|
||||
pub 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_column_name: ColumnName, columns: Vec<ColumnName>, types: Vec<DbType>) -> Self {
|
||||
let mut column_name_position_mapping: BiMap<ColumnName, ColumnPosition> = BiMap::new();
|
||||
for (column_name, column) in column_name_position_map {
|
||||
for (column, column_name) in columns.into_iter().enumerate() {
|
||||
column_name_position_mapping.insert(column_name, column);
|
||||
}
|
||||
let primary_key: ColumnPosition = match column_name_position_mapping.get_by_left(&primary_column_name).copied() {
|
||||
Some(primary_key) => primary_key,
|
||||
None => unreachable!() // SAFETY: Existence of unique primary key is ensured in validation.
|
||||
};
|
||||
Self { table_name, primary_key, column_name_position_mapping, types }
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue