From f1facae8a25596cb3af02ac70b077c8461298aaa Mon Sep 17 00:00:00 2001 From: Nicksname Date: Sat, 12 Aug 2023 22:26:51 +0200 Subject: [PATCH] test instance to connect to db --- backend/Cargo.toml | 11 ++++- backend/{ => src}/main.rs | 0 backend/src/main_db.rs | 88 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) rename backend/{ => src}/main.rs (100%) create mode 100644 backend/src/main_db.rs diff --git a/backend/Cargo.toml b/backend/Cargo.toml index d3f83e5..6658b43 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -6,10 +6,19 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +# rust actix-web ="4" actix-files = "*" utoipa-swagger-ui = { version = "3", features = ["actix-web"] } +# db-connection +sqlx = { version = "0.6.2", features = ["runtime-tokio-native-tls", "sqlite"]} +tokio = { version = "1.20.0", features = ["macros"]} + [[bin]] name = "webserver" -path = "main.rs" +path = "src/main.rs" + +[[bin]] +name = "db_access" +path = "src/main_db.rs" \ No newline at end of file diff --git a/backend/main.rs b/backend/src/main.rs similarity index 100% rename from backend/main.rs rename to backend/src/main.rs diff --git a/backend/src/main_db.rs b/backend/src/main_db.rs new file mode 100644 index 0000000..55c8c9d --- /dev/null +++ b/backend/src/main_db.rs @@ -0,0 +1,88 @@ +use sqlx::{migrate::MigrateDatabase, FromRow, Row, Sqlite, SqlitePool}; +use std::io; + +const DB_URL: &str = "sqlite://db/test.db"; + +const COMMANDS :[(i32, &str, &str); 4] = [ + (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"), +]; + +#[tokio::main] +async fn main() { + // game loop + + let mut db: Option = None; + + loop { + println!("What do you want to do?\n"); + print_commands(); + println!(""); + + let mut choice = String::new(); + + 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 => create_db().await, + 1 => init_db(), + 2 => db = connect_db().await, + 3 => db = execute_sql(db).await, + _ => println!("This Command does not exists") + } + println!(""); + } +} + +fn print_commands() { + + for command in COMMANDS { + let (num, name, desc) = command; + + println!("{num}. {name} \t{desc}"); + } +} + +async fn create_db() { + // TODO + + if !Sqlite::database_exists(DB_URL).await.unwrap_or(false) { + println!("Creating database {}", DB_URL); + match Sqlite::create_database(DB_URL).await { + Ok(_) => println!("Create db success"), + Err(error) => panic!("error: {}", error), + } + } else { + println!("database already exists"); + } +} + +fn init_db() { + println!("databases baby!"); +} + +async fn connect_db() -> Option{ + + return Some( + SqlitePool::connect(DB_URL).await.unwrap() + ); +} + +async fn execute_sql(db: Option) -> Option { + + match db { + None => println!("WTF?!"), + _ => println!("uh yeah sql."), + } + + return db; +} \ No newline at end of file