exteded interactive shell

This commit is contained in:
Nicksname 2023-08-13 01:07:33 +02:00
parent f1facae8a2
commit fa690d48bd

View file

@ -1,4 +1,4 @@
use sqlx::{migrate::MigrateDatabase, FromRow, Row, Sqlite, SqlitePool}; use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
use std::io; use std::io;
const DB_URL: &str = "sqlite://db/test.db"; const DB_URL: &str = "sqlite://db/test.db";
@ -36,7 +36,7 @@ async fn main() {
0 => create_db().await, 0 => create_db().await,
1 => init_db(), 1 => init_db(),
2 => db = connect_db().await, 2 => db = connect_db().await,
3 => db = execute_sql(db).await, 3 => execute_sql(&db).await,
_ => println!("This Command does not exists") _ => println!("This Command does not exists")
} }
println!(""); println!("");
@ -67,22 +67,101 @@ async fn create_db() {
} }
fn init_db() { fn init_db() {
// TODO
println!("databases baby!"); println!("databases baby!");
} }
async fn connect_db() -> Option<SqlitePool>{ async fn connect_db() -> Option<SqlitePool>{
return Some( let db = Some(SqlitePool::connect(DB_URL).await.unwrap());
SqlitePool::connect(DB_URL).await.unwrap() println!("Connected to DB.");
);
}
async fn execute_sql(db: Option<SqlitePool>) -> Option<SqlitePool> {
match db {
None => println!("WTF?!"),
_ => println!("uh yeah sql."),
}
return db; return db;
} }
async fn execute_sql(opt_db: &Option<SqlitePool>) {
if opt_db.is_none() {
println!("There is no active connection to the db.");
}
let db = opt_db.clone().unwrap();
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);
}