interactive test-shell ready

This commit is contained in:
Nicksname 2023-08-14 00:04:17 +02:00
parent fa690d48bd
commit 68e8a9cd6e

View file

@ -1,15 +1,22 @@
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool}; use sqlx::{migrate::MigrateDatabase, FromRow, Sqlite, SqlitePool};
use std::io; use std::io;
const DB_URL: &str = "sqlite://db/test.db"; 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."), (0, "createDB", "creates the database if not already."),
(1, "initDB", "initializes the data of the db with given file"), (1, "initDB", "initializes the data of the db with given file"),
(2, "connect", "connecting to the db."), (2, "connect", "connecting to the db."),
(3, "execute sql", "execute SQL that is entered"), (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] #[tokio::main]
async fn main() { async fn main() {
// game loop // game loop
@ -37,6 +44,10 @@ async fn main() {
1 => init_db(), 1 => init_db(),
2 => db = connect_db().await, 2 => db = connect_db().await,
3 => execute_sql(&db).await, 3 => execute_sql(&db).await,
4 => {
db.clone().unwrap().close().await;
db = None;
}
_ => println!("This Command does not exists") _ => println!("This Command does not exists")
} }
println!(""); println!("");
@ -67,8 +78,7 @@ async fn create_db() {
} }
fn init_db() { fn init_db() {
// TODO todo!();
println!("databases baby!");
} }
async fn connect_db() -> Option<SqlitePool>{ async fn connect_db() -> Option<SqlitePool>{
@ -82,13 +92,15 @@ async fn execute_sql(opt_db: &Option<SqlitePool>) {
if opt_db.is_none() { if opt_db.is_none() {
println!("There is no active connection to the db."); println!("There is no active connection to the db.");
return;
} }
let db = opt_db.clone().unwrap(); let db = opt_db.clone().unwrap();
loop { loop {
let mut choice = String::new(); 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() io::stdin()
.read_line(&mut choice) .read_line(&mut choice)
.expect("Failed to read line"); .expect("Failed to read line");
@ -100,9 +112,11 @@ async fn execute_sql(opt_db: &Option<SqlitePool>) {
match choice { match choice {
0 => freely(&db).await, 0 => freely(&db).await,
1 => insert(&db).await, 1 => create(&db).await,
2 => query(&db).await, 2 => insert(&db).await,
3 => delete(&db).await, 3 => query(&db).await,
4 => delete(&db).await,
5 => drop_table(&db).await,
_ => { _ => {
println!("This Command does not exists"); println!("This Command does not exists");
return; return;
@ -111,53 +125,88 @@ async fn execute_sql(opt_db: &Option<SqlitePool>) {
} }
} }
async fn freely(db: &SqlitePool) { async fn create(db : &SqlitePool) {
freely_with_praefix(db, "").await; let result = sqlx::query(
} "CREATE TABLE IF NOT EXISTS users (
id INTEGER NOT NULL PRIMARY KEY,
async fn insert(db : &SqlitePool) { name WARCHAR(250) NOT NULL
let str = "INSERT INTO "; );")
freely_with_praefix(db, str).await; .execute(db)
}
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 .await
.unwrap(); .unwrap();
// somehow print the information println!("Create user table result: {:?}", result);
}
async fn insert(db : &SqlitePool) {
let mut name = String::new();
println!("insert what name?");
io::stdin()
.read_line(&mut name)
.expect("Failed to read line");
let result = sqlx::query("INSERT INTO users (name) VALUES (?)")
.bind(name)
.execute(db)
.await
.unwrap();
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) { 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(); let mut sql = String::new();
println!("{}", str);
io::stdin() io::stdin()
.read_line(&mut sql) .read_line(&mut sql)
.expect("Failed to read line"); .expect("Failed to read line");
sql = str.to_owned() + &sql;
let result = sqlx::query(&sql) let result = sqlx::query(&sql)
.execute(db) .execute(db)
.await .await
@ -165,3 +214,12 @@ async fn freely_with_praefix(db : &SqlitePool, str: &str) {
println!("result: {:?}", result); println!("result: {:?}", result);
} }
async fn drop_table(db : &SqlitePool) {
let result = sqlx::query("DROP TABLE users;")
.execute(db)
.await
.unwrap();
println!("result: {:?}", result);
}