From fa690d48bd5dfe3722c492a976f3d2cb66abd88c Mon Sep 17 00:00:00 2001 From: Nicksname Date: Sun, 13 Aug 2023 01:07:33 +0200 Subject: [PATCH] exteded interactive shell --- backend/src/main_db.rs | 99 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 10 deletions(-) diff --git a/backend/src/main_db.rs b/backend/src/main_db.rs index 55c8c9d..a42d3ef 100644 --- a/backend/src/main_db.rs +++ b/backend/src/main_db.rs @@ -1,4 +1,4 @@ -use sqlx::{migrate::MigrateDatabase, FromRow, Row, Sqlite, SqlitePool}; +use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool}; use std::io; const DB_URL: &str = "sqlite://db/test.db"; @@ -36,7 +36,7 @@ async fn main() { 0 => create_db().await, 1 => init_db(), 2 => db = connect_db().await, - 3 => db = execute_sql(db).await, + 3 => execute_sql(&db).await, _ => println!("This Command does not exists") } println!(""); @@ -67,22 +67,101 @@ async fn create_db() { } fn init_db() { + // TODO println!("databases baby!"); } async fn connect_db() -> Option{ - return Some( - SqlitePool::connect(DB_URL).await.unwrap() - ); + let db = Some(SqlitePool::connect(DB_URL).await.unwrap()); + println!("Connected to DB."); + return db; } -async fn execute_sql(db: Option) -> Option { +async fn execute_sql(opt_db: &Option) { - match db { - None => println!("WTF?!"), - _ => println!("uh yeah sql."), + if opt_db.is_none() { + println!("There is no active connection to the db."); } + let db = opt_db.clone().unwrap(); - return db; + loop { + let mut choice = String::new(); + + println!("0: freely\n1: insert\n2: query\n3: delete"); + io::stdin() + .read_line(&mut choice) + .expect("Failed to read line"); + + let choice: u32 = choice + .trim() + .parse() + .expect("Please type a number!"); + + match choice { + 0 => freely(&db).await, + 1 => insert(&db).await, + 2 => query(&db).await, + 3 => delete(&db).await, + _ => { + println!("This Command does not exists"); + return; + } + } + } +} + +async fn freely(db: &SqlitePool) { + freely_with_praefix(db, "").await; +} + +async fn insert(db : &SqlitePool) { + let str = "INSERT INTO "; + freely_with_praefix(db, str).await; +} + +async fn query(db : &SqlitePool) { + let str = "SELECT "; + + let mut sql = String::new(); + + println!("{}", str); + + io::stdin() + .read_line(&mut sql) + .expect("Failed to read line"); + + sql = str.to_owned() + &sql; + + let data = sqlx::query(&sql) + .fetch_all(db) + .await + .unwrap(); + + // somehow print the information +} + +async fn delete(db : &SqlitePool) { + let str = "DELETE FROM "; + freely_with_praefix(db, str).await; +} + +async fn freely_with_praefix(db : &SqlitePool, str: &str) { + + let mut sql = String::new(); + + println!("{}", str); + + io::stdin() + .read_line(&mut sql) + .expect("Failed to read line"); + + sql = str.to_owned() + &sql; + + let result = sqlx::query(&sql) + .execute(db) + .await + .unwrap(); + + println!("result: {:?}", result); } \ No newline at end of file