diff --git a/minisql/src/interpreter.rs b/minisql/src/interpreter.rs index 699265d..2e672d1 100644 --- a/minisql/src/interpreter.rs +++ b/minisql/src/interpreter.rs @@ -2,7 +2,7 @@ use crate::internals::table::Table; use crate::operation::{ColumnSelection, Condition, Operation}; use crate::restricted_row::RestrictedRow; use crate::result::DbResult; -use crate::schema::{TableName, TablePosition, TableSchema}; +use crate::schema::{Column, TableName, TablePosition, TableSchema}; use bimap::BiMap; use serde::{Deserialize, Serialize}; @@ -212,9 +212,6 @@ mod tests { #[test] fn test_insert_select_basic1() { - use IndexableValue::*; - use Value::*; - let mut state = State::new(); let users_schema = users_schema(); let users = 0; @@ -224,9 +221,9 @@ mod tests { .unwrap(); let (id, name, age) = ( - Indexable(Uuid(0)), - Indexable(String("Plato".to_string())), - Indexable(Int(64)), + Value::Uuid(0), + Value::String("Plato".to_string()), + Value::Int(64), ); state .interpret(Operation::Insert( @@ -256,9 +253,7 @@ mod tests { #[test] fn test_insert_select_basic2() { use Condition::*; - use IndexableValue::*; use Operation::*; - use Value::*; let mut state = State::new(); let users_schema = users_schema(); @@ -270,9 +265,9 @@ mod tests { state.interpret(CreateTable(users_schema.clone())).unwrap(); let (id0, name0, age0) = ( - Indexable(Uuid(0)), - Indexable(String("Plato".to_string())), - Indexable(Int(64)), + Value::Uuid(0), + Value::String("Plato".to_string()), + Value::Int(64), ); state .interpret(Insert( @@ -282,9 +277,9 @@ mod tests { .unwrap(); let (id1, name1, age1) = ( - Indexable(Uuid(1)), - Indexable(String("Aristotle".to_string())), - Indexable(Int(20)), + Value::Uuid(1), + Value::String("Aristotle".to_string()), + Value::Int(20), ); state .interpret(Insert( @@ -366,9 +361,7 @@ mod tests { #[test] fn test_delete() { use Condition::*; - use IndexableValue::*; use Operation::*; - use Value::*; let mut state = State::new(); let users_schema = users_schema(); @@ -379,9 +372,9 @@ mod tests { state.interpret(CreateTable(users_schema.clone())).unwrap(); let (id0, name0, age0) = ( - Indexable(Uuid(0)), - Indexable(String("Plato".to_string())), - Indexable(Int(64)), + Value::Uuid(0), + Value::String("Plato".to_string()), + Value::Int(64), ); state .interpret(Insert( @@ -391,9 +384,9 @@ mod tests { .unwrap(); let (id1, name1, age1) = ( - Indexable(Uuid(1)), - Indexable(String("Aristotle".to_string())), - Indexable(Int(20)), + Value::Uuid(1), + Value::String("Aristotle".to_string()), + Value::Int(20), ); state .interpret(Insert( @@ -429,9 +422,7 @@ mod tests { #[test] fn test_index() { - use IndexableValue::*; use Operation::*; - use Value::*; let mut state = State::new(); let users_schema = users_schema(); @@ -446,9 +437,9 @@ mod tests { .unwrap(); let (id0, name0, age0) = ( - Indexable(Uuid(0)), - Indexable(String("Plato".to_string())), - Indexable(Int(64)), + Value::Uuid(0), + Value::String("Plato".to_string()), + Value::Int(64), ); state .interpret(Insert( @@ -458,9 +449,9 @@ mod tests { .unwrap(); let (id1, name1, age1) = ( - Indexable(Uuid(1)), - Indexable(String("Aristotle".to_string())), - Indexable(Int(20)), + Value::Uuid(1), + Value::String("Aristotle".to_string()), + Value::Int(20), ); state .interpret(Insert( @@ -482,7 +473,7 @@ mod tests { let aristotle_id = 1; let plato_ids = index - .get(&String("Plato".to_string())) + .get(&IndexableValue::String("Plato".to_string())) .cloned() .unwrap_or(HashSet::new()); assert!(plato_ids.contains(&plato_id)); @@ -491,103 +482,98 @@ mod tests { } } -// TODO -// pub fn example() { -// use crate::type_system::{DbType, IndexableValue, Value}; -// use Condition::*; -// use IndexableValue::*; -// use Operation::*; -// use Value::*; +pub fn example() { + use crate::type_system::{DbType, Value}; + use Condition::*; + use Operation::*; -// let id_column: Column = 0; -// let name_column: Column = 1; -// // let age_column: ColumnPosition = 2; + let id_column: Column = 0; + let name_column: Column = 1; + // let age_column: ColumnPosition = 2; -// let users_schema: TableSchema = { -// TableSchema::new( -// "users".to_string(), -// "id".to_string(), -// vec![ -// "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_schema: TableSchema = { + TableSchema::new( + "users".to_string(), + "id".to_string(), + vec![ + "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 mut state = State::new(); -// state -// .interpret(Operation::CreateTable(users_schema.clone())) -// .unwrap(); + let mut state = State::new(); + state + .interpret(Operation::CreateTable(users_schema.clone())) + .unwrap(); -// let (id0, name0, age0) = ( -// Indexable(Uuid(0)), -// Indexable(String("Plato".to_string())), -// Indexable(Int(64)), -// ); -// println!("==INSERT Plato=="); -// state -// .interpret(Insert( -// users_position, -// vec![id0.clone(), name0.clone(), age0.clone()], -// )) -// .unwrap(); + let (id0, name0, age0) = ( + Value::Uuid(0), + Value::String("Plato".to_string()), + Value::Int(64), + ); + println!("==INSERT Plato=="); + state + .interpret(Insert( + users_position, + vec![id0.clone(), name0.clone(), age0.clone()], + )) + .unwrap(); -// let (id1, name1, age1) = ( -// Indexable(Uuid(1)), -// Indexable(String("Aristotle".to_string())), -// Indexable(Int(20)), -// ); -// println!("==INSERT Aristotle=="); -// state -// .interpret(Insert( -// users_position, -// vec![id1.clone(), name1.clone(), age1.clone()], -// )) -// .unwrap(); -// println!(); + let (id1, name1, age1) = ( + Value::Uuid(1), + Value::String("Aristotle".to_string()), + Value::Int(20), + ); + println!("==INSERT Aristotle=="); + state + .interpret(Insert( + users_position, + vec![id1.clone(), name1.clone(), age1.clone()], + )) + .unwrap(); + println!(); -// { -// let response: Response = state -// .interpret(Operation::Select( -// users_position, -// users_schema.all_selection(), -// None, -// )) -// .unwrap(); -// println!("==SELECT ALL=="); -// println!("{:?}", response); -// println!(); -// } -// { -// let response: Response = state -// .interpret(Select( -// users_position, -// users_schema.all_selection(), -// Some(Eq(id_column, id0.clone())), -// )) -// .unwrap(); -// println!("==SELECT Plato=="); -// println!("{:?}", response); -// println!(); -// } + { + let response: Response = state + .interpret(Operation::Select( + users_position, + users_schema.all_selection(), + None, + )) + .unwrap(); + println!("==SELECT ALL=="); + println!("{:?}", response); + println!(); + } + { + let response: Response = state + .interpret(Select( + users_position, + users_schema.all_selection(), + Some(Eq(id_column, id0.clone())), + )) + .unwrap(); + println!("==SELECT Plato=="); + println!("{:?}", response); + println!(); + } -// { -// { -// // TODO: Why do I have to write these braces explicitely? Why doesn't Rust compiler -// // "infer" them? -// let _delete_response: Response = state -// .interpret(Delete(users_position, Some(Eq(id_column, id0.clone())))) -// .unwrap(); -// println!("==DELETE Plato=="); -// } -// let response: Response = state -// .interpret(Select(users_position, vec![name_column, id_column], None)) -// .unwrap(); -// println!("==SELECT All=="); -// println!("{:?}", response); -// println!(); -// } -// } + { + { + let _delete_response: Response = state + .interpret(Delete(users_position, Some(Eq(id_column, id0.clone())))) + .unwrap(); + println!("==DELETE Plato=="); + } + let response: Response = state + .interpret(Select(users_position, vec![name_column, id_column], None)) + .unwrap(); + println!("==SELECT All=="); + println!("{:?}", response); + println!(); + } +} diff --git a/minisql/src/type_system.rs b/minisql/src/type_system.rs index 1327be2..0d3d4b6 100644 --- a/minisql/src/type_system.rs +++ b/minisql/src/type_system.rs @@ -239,9 +239,8 @@ impl TryFrom for Value { #[cfg(test)] mod tests { - use super::{IndexableValue, Value}; + use super::Value; use crate::error::TypeConversionError::UnknownType; - use crate::type_system::Value::{Indexable, Number}; #[test] fn test_encode_number() { @@ -261,7 +260,7 @@ mod tests { #[test] fn test_encode_string() { - let value = Value::Indexable(IndexableValue::String("hello".to_string())); + let value = Value::String("hello".to_string()); let vtype = value.to_type(); let oid = vtype.type_oid(); let size = vtype.type_size(); @@ -277,7 +276,7 @@ mod tests { #[test] fn test_encode_string_utf8() { - let value = Value::Indexable(IndexableValue::String("#速度与激情9 早上好中国 现在我有冰激淋 我很喜欢冰激淋 但是《速度与激情9》比冰激淋 🍧🍦🍨".to_string())); + let value = Value::String("#速度与激情9 早上好中国 现在我有冰激淋 我很喜欢冰激淋 但是《速度与激情9》比冰激淋 🍧🍦🍨".to_string()); let vtype = value.to_type(); let oid = vtype.type_oid(); let size = vtype.type_size(); @@ -293,7 +292,7 @@ mod tests { #[test] fn test_encode_int() { - let value = Value::Indexable(IndexableValue::Int(123)); + let value = Value::Int(123); let vtype = value.to_type(); let oid = vtype.type_oid(); let size = vtype.type_size(); @@ -309,7 +308,7 @@ mod tests { #[test] fn test_encode_uuid() { - let value = Value::Indexable(IndexableValue::Uuid(123)); + let value = Value::Uuid(123); let vtype = value.to_type(); let oid = vtype.type_oid(); let size = vtype.type_size(); @@ -325,7 +324,7 @@ mod tests { #[test] fn test_mismatched_size() { - let value = Value::Indexable(IndexableValue::Uuid(123)); + let value = Value::Uuid(123); let vtype = value.to_type(); let oid = vtype.type_oid(); let size = 8; @@ -342,13 +341,13 @@ mod tests { #[test] fn test_value_stringification() { let pairs = vec![ - (Number(1.0), "Number(1)"), + (Value::Number(1.0), "Number(1)"), ( - Indexable(IndexableValue::String("hello".to_string())), + Value::String("hello".to_string()), "Indexable(String(hello))", ), - (Indexable(IndexableValue::Int(123)), "Indexable(Int(123))"), - (Indexable(IndexableValue::Uuid(123)), "Indexable(Uuid(123))"), + (Value::Int(123), "Indexable(Int(123))"), + (Value::Uuid(123), "Indexable(Uuid(123))"), ]; for (value, string) in pairs { diff --git a/parser/src/parsing/common.rs b/parser/src/parsing/common.rs index 508289e..fac2a0a 100644 --- a/parser/src/parsing/common.rs +++ b/parser/src/parsing/common.rs @@ -82,11 +82,11 @@ mod tests { #[test] fn test_parse_equality() { - use minisql::type_system::{IndexableValue, Value}; + use minisql::type_system::Value; match parse_equality("id = 1") { Ok(("", Condition::Eq(column_name, value))) => { assert!(column_name.eq("id")); - assert_eq!(value, Value::Indexable(IndexableValue::Int(1))) + assert_eq!(value, Value::Int(1)) } _ => { panic!("should parse"); diff --git a/parser/src/parsing/insert.rs b/parser/src/parsing/insert.rs index 0b62574..2dfa061 100644 --- a/parser/src/parsing/insert.rs +++ b/parser/src/parsing/insert.rs @@ -57,7 +57,7 @@ pub fn parse_values(input: &str) -> IResult<&str, Vec> { #[cfg(test)] mod tests { - use minisql::type_system::{IndexableValue, Value}; + use minisql::type_system::Value; use super::parse_insert; use crate::syntax::RawQuerySyntax; @@ -72,10 +72,10 @@ mod tests { assert_eq!( insertion_values, vec![ - ("id".to_string(), Value::Indexable(IndexableValue::Int(1))), + ("id".to_string(), Value::Int(1)), ( "data".to_string(), - Value::Indexable(IndexableValue::String("Text".to_string())) + Value::String("Text".to_string()) ) ] ); @@ -97,10 +97,10 @@ mod tests { assert_eq!( insertion_values, vec![ - ("id".to_string(), Value::Indexable(IndexableValue::Int(1))), + ("id".to_string(), Value::Int(1)), ( "data".to_string(), - Value::Indexable(IndexableValue::String("Text".to_string())) + Value::String("Text".to_string()) ) ] );