From 68e8a9cd6e40ec229dad4c834bcad548cc8d2110 Mon Sep 17 00:00:00 2001 From: Nicksname Date: Mon, 14 Aug 2023 00:04:17 +0200 Subject: [PATCH] interactive test-shell ready --- backend/src/main_db.rs | 122 ++++++++++++++++++++++++++++++----------- 1 file changed, 90 insertions(+), 32 deletions(-) diff --git a/backend/src/main_db.rs b/backend/src/main_db.rs index a42d3ef..d983046 100644 --- a/backend/src/main_db.rs +++ b/backend/src/main_db.rs @@ -1,15 +1,22 @@ -use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool}; +use sqlx::{migrate::MigrateDatabase, FromRow, Sqlite, SqlitePool}; use std::io; const DB_URL: &str = "sqlite://db/test.db"; -const COMMANDS :[(i32, &str, &str); 4] = [ +const COMMANDS :[(i32, &str, &str); 5] = [ (0, "createDB", "creates the database if not already."), (1, "initDB", "initializes the data of the db with given file"), (2, "connect", "connecting to the db."), (3, "execute sql", "execute SQL that is entered"), + (4, "disconnect", "closing connection to db."), ]; +#[derive(Clone, FromRow, Debug)] +struct User { + id: i64, + name: String, +} + #[tokio::main] async fn main() { // game loop @@ -37,6 +44,10 @@ async fn main() { 1 => init_db(), 2 => db = connect_db().await, 3 => execute_sql(&db).await, + 4 => { + db.clone().unwrap().close().await; + db = None; + } _ => println!("This Command does not exists") } println!(""); @@ -67,8 +78,7 @@ async fn create_db() { } fn init_db() { - // TODO - println!("databases baby!"); + todo!(); } async fn connect_db() -> Option{ @@ -82,13 +92,15 @@ async fn execute_sql(opt_db: &Option) { if opt_db.is_none() { println!("There is no active connection to the db."); + return; } + let db = opt_db.clone().unwrap(); loop { let mut choice = String::new(); - println!("0: freely\n1: insert\n2: query\n3: delete"); + println!("0: freely (danger!)\n1: create \n2: insert\n3: query\n4: delete\n5: drop table"); io::stdin() .read_line(&mut choice) .expect("Failed to read line"); @@ -100,9 +112,11 @@ async fn execute_sql(opt_db: &Option) { match choice { 0 => freely(&db).await, - 1 => insert(&db).await, - 2 => query(&db).await, - 3 => delete(&db).await, + 1 => create(&db).await, + 2 => insert(&db).await, + 3 => query(&db).await, + 4 => delete(&db).await, + 5 => drop_table(&db).await, _ => { println!("This Command does not exists"); return; @@ -111,57 +125,101 @@ async fn execute_sql(opt_db: &Option) { } } -async fn freely(db: &SqlitePool) { - freely_with_praefix(db, "").await; +async fn create(db : &SqlitePool) { + let result = sqlx::query( + "CREATE TABLE IF NOT EXISTS users ( + id INTEGER NOT NULL PRIMARY KEY, + name WARCHAR(250) NOT NULL + );") + .execute(db) + .await + .unwrap(); + + println!("Create user table result: {:?}", result); } 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); + let mut name = String::new(); + println!("insert what name?"); io::stdin() - .read_line(&mut sql) + .read_line(&mut name) .expect("Failed to read line"); - sql = str.to_owned() + &sql; - - let data = sqlx::query(&sql) - .fetch_all(db) + let result = sqlx::query("INSERT INTO users (name) VALUES (?)") + .bind(name) + .execute(db) .await .unwrap(); - // somehow print the information + println!("result: {:?}", result); +} + +async fn query(db : &SqlitePool) { + + let data = match sqlx::query_as::<_, User>("SELECT id, name FROM users") + .fetch_all(db) + .await { + Ok(d) => d, + Err(_) => { + println!("something went wrong."); + Vec::new() + } + }; + + for user in data { + println!("[{}] name: {}", user.id, &user.name); + } } async fn delete(db : &SqlitePool) { - let str = "DELETE FROM "; - freely_with_praefix(db, str).await; + + let mut name = String::new(); + + println!("delete what name?"); + io::stdin() + .read_line(&mut name) + .expect("Failed to read line"); + + let result = match sqlx::query("DELETE FROM users WHERE name=$1") + .bind(name) + .execute(db) + .await { + Ok(r) => Some(r), + Err(_) => { + println!("user could not be deleted."); + None + } + }; + + if result.is_some() { + println!("result: {:?}", result.unwrap()); + } + } -async fn freely_with_praefix(db : &SqlitePool, str: &str) { +async fn freely(db : &SqlitePool) { 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); +} + +async fn drop_table(db : &SqlitePool) { + let result = sqlx::query("DROP TABLE users;") + .execute(db) + .await + .unwrap(); + println!("result: {:?}", result); } \ No newline at end of file