interactive test-shell ready
This commit is contained in:
parent
fa690d48bd
commit
68e8a9cd6e
1 changed files with 90 additions and 32 deletions
|
@ -1,15 +1,22 @@
|
|||
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
||||
use sqlx::{migrate::MigrateDatabase, FromRow, Sqlite, SqlitePool};
|
||||
use std::io;
|
||||
|
||||
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."),
|
||||
(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"),
|
||||
(4, "disconnect", "closing connection to db."),
|
||||
];
|
||||
|
||||
#[derive(Clone, FromRow, Debug)]
|
||||
struct User {
|
||||
id: i64,
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// game loop
|
||||
|
@ -37,6 +44,10 @@ async fn main() {
|
|||
1 => init_db(),
|
||||
2 => db = connect_db().await,
|
||||
3 => execute_sql(&db).await,
|
||||
4 => {
|
||||
db.clone().unwrap().close().await;
|
||||
db = None;
|
||||
}
|
||||
_ => println!("This Command does not exists")
|
||||
}
|
||||
println!("");
|
||||
|
@ -67,8 +78,7 @@ async fn create_db() {
|
|||
}
|
||||
|
||||
fn init_db() {
|
||||
// TODO
|
||||
println!("databases baby!");
|
||||
todo!();
|
||||
}
|
||||
|
||||
async fn connect_db() -> Option<SqlitePool>{
|
||||
|
@ -82,13 +92,15 @@ async fn execute_sql(opt_db: &Option<SqlitePool>) {
|
|||
|
||||
if opt_db.is_none() {
|
||||
println!("There is no active connection to the db.");
|
||||
return;
|
||||
}
|
||||
|
||||
let db = opt_db.clone().unwrap();
|
||||
|
||||
loop {
|
||||
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()
|
||||
.read_line(&mut choice)
|
||||
.expect("Failed to read line");
|
||||
|
@ -100,9 +112,11 @@ async fn execute_sql(opt_db: &Option<SqlitePool>) {
|
|||
|
||||
match choice {
|
||||
0 => freely(&db).await,
|
||||
1 => insert(&db).await,
|
||||
2 => query(&db).await,
|
||||
3 => delete(&db).await,
|
||||
1 => create(&db).await,
|
||||
2 => insert(&db).await,
|
||||
3 => query(&db).await,
|
||||
4 => delete(&db).await,
|
||||
5 => drop_table(&db).await,
|
||||
_ => {
|
||||
println!("This Command does not exists");
|
||||
return;
|
||||
|
@ -111,53 +125,88 @@ async fn execute_sql(opt_db: &Option<SqlitePool>) {
|
|||
}
|
||||
}
|
||||
|
||||
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)
|
||||
async fn create(db : &SqlitePool) {
|
||||
let result = sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name WARCHAR(250) NOT NULL
|
||||
);")
|
||||
.execute(db)
|
||||
.await
|
||||
.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) {
|
||||
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();
|
||||
|
||||
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
|
||||
|
@ -165,3 +214,12 @@ async fn freely_with_praefix(db : &SqlitePool, str: &str) {
|
|||
|
||||
println!("result: {:?}", result);
|
||||
}
|
||||
|
||||
async fn drop_table(db : &SqlitePool) {
|
||||
let result = sqlx::query("DROP TABLE users;")
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("result: {:?}", result);
|
||||
}
|
Loading…
Reference in a new issue