forked from mgl_crew/Mitgliederladen
test instance to connect to db
This commit is contained in:
parent
a2536696ce
commit
f1facae8a2
3 changed files with 98 additions and 1 deletions
|
@ -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"
|
88
backend/src/main_db.rs
Normal file
88
backend/src/main_db.rs
Normal file
|
@ -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<SqlitePool> = 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<SqlitePool>{
|
||||
|
||||
return Some(
|
||||
SqlitePool::connect(DB_URL).await.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
async fn execute_sql(db: Option<SqlitePool>) -> Option<SqlitePool> {
|
||||
|
||||
match db {
|
||||
None => println!("WTF?!"),
|
||||
_ => println!("uh yeah sql."),
|
||||
}
|
||||
|
||||
return db;
|
||||
}
|
Loading…
Reference in a new issue