Improve Create Table

This commit is contained in:
Yuriy Dupyn 2024-01-28 17:14:02 +01:00
parent 4a099468b2
commit 7b5b2bf9f3
6 changed files with 197 additions and 107 deletions

View file

@ -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) = (