From 3c57b0eb6c8afbe4d74b1f17c2c21aeef5cd521c Mon Sep 17 00:00:00 2001 From: Yuriy Dupyn <2153100+omedusyo@users.noreply.github.com> Date: Thu, 28 Dec 2023 14:39:36 +0100 Subject: [PATCH] Add an example to fn main() --- minisql/src/main.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/minisql/src/main.rs b/minisql/src/main.rs index 26d346f..3e5b530 100644 --- a/minisql/src/main.rs +++ b/minisql/src/main.rs @@ -589,6 +589,83 @@ enum Error { } fn main() { + use DbValue::*; + use IndexableDbValue::*; + use Operation::*; + use ColumnSelection::*; + use Condition::*; + + let users_schema = { + let id: ColumnPosition = 0; + let name: ColumnPosition = 1; + let age: ColumnPosition = 2; + + TableSchema { + table_name: "users".to_string(), + primary_key: id, + column_name_position_mapping: { + let mut mapping: BiMap = BiMap::new(); + mapping.insert("id".to_string(), id); + mapping.insert("name".to_string(), name); + mapping.insert("age".to_string(), age); + mapping + }, + types: vec![DbType::UUID, DbType::String, DbType::Int], + } + }; + let users = users_schema.table_name.clone(); + + + let mut state = State::new(); + state.interpret(Operation::CreateTable(users.clone(), users_schema)).unwrap(); + + let (id0, name0, age0) = ( + Indexable(UUID(0)), + Indexable(String("Plato".to_string())), + Indexable(Int(64)) + ); + println!("==INSERT Plato=="); + state.interpret(Insert(users.clone(), vec![ + ("id".to_string(), id0.clone()), + ("name".to_string(), name0.clone()), + ("age".to_string(), 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.clone(), vec![ + ("id".to_string(), id1.clone()), + ("name".to_string(), name1.clone()), + ("age".to_string(), age1.clone()), + ])).unwrap(); + println!(); + + { + let response: Response = state.interpret(Operation::Select(users.clone(), ColumnSelection::All, None)).unwrap(); + println!("==SELECT ALL=="); + println!("{:?}", response); + println!(); + } + { + let response: Response = state.interpret(Select(users.clone(), All, Some(Eq("id".to_string(), id0.clone())))).unwrap(); + println!("==SELECT Plato=="); + println!("{:?}", response); + println!(); + } + + { + let delete_response: Response = state.interpret(Delete(users.clone(), Some(Eq("id".to_string(), id0.clone())))).unwrap(); + println!("==DELETE Plato=="); + let response: Response = state.interpret(Select(users.clone(), Columns(vec!["name".to_string(), "id".to_string()]), None)).unwrap(); + println!("==SELECT All=="); + println!("{:?}", response); + println!(); + } + }