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