|
|
@ -1,33 +1,53 @@ |
|
|
|
|
|
|
|
use sqlx::sqlite::Sqlite; |
|
|
|
use sqlx::sqlite::SqliteConnectOptions; |
|
|
|
use sqlx::sqlite::SqliteConnectOptions; |
|
|
|
use sqlx::sqlite::SqlitePoolOptions; |
|
|
|
use sqlx::sqlite::SqlitePoolOptions; |
|
|
|
use sqlx::Pool; |
|
|
|
use sqlx::Pool; |
|
|
|
// use std::env;
|
|
|
|
use std::env; |
|
|
|
|
|
|
|
|
|
|
|
struct Label { |
|
|
|
struct Label { |
|
|
|
id: u32, |
|
|
|
id: Option<u32>, |
|
|
|
description: String, |
|
|
|
description: String, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct Repository<'s, T: sqlx::Database + sqlx::Executor<'s>> { |
|
|
|
struct LabelRepository<'a> { |
|
|
|
pool: Pool<T>, |
|
|
|
pool: &'a Pool<Sqlite>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<'s, T: sqlx::Database + sqlx::Executor<'s>> Repository<'s, T> { |
|
|
|
impl<'a> LabelRepository<'a> { |
|
|
|
fn new(pool: Pool<T>) -> Self { |
|
|
|
fn new(pool: &'a Pool<Sqlite>) -> Self { |
|
|
|
Self { pool } |
|
|
|
Self { pool } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async fn save(&self, label: &Label) -> Result<(), sqlx::Error> { |
|
|
|
async fn save(&self, label: &Label) -> Result<(), sqlx::Error> { |
|
|
|
sqlx::query(r#"INSERT INTO testing (label) VALUES (?)"#) |
|
|
|
sqlx::query(r#"INSERT INTO testing (label) VALUES (?)"#) |
|
|
|
.bind(label.description) |
|
|
|
.bind(&label.description) |
|
|
|
.execute(&self.pool) |
|
|
|
.execute(self.pool) |
|
|
|
.await?; |
|
|
|
.await?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fn find_by_description(&self, label: &str) -> Result<Label, sqlx::Error> { |
|
|
|
|
|
|
|
let result: (u32, String) = |
|
|
|
|
|
|
|
sqlx::query_as(r#"SELECT id, label FROM testing WHERE label = ?"#) |
|
|
|
|
|
|
|
.bind(label) |
|
|
|
|
|
|
|
.fetch_one(self.pool) |
|
|
|
|
|
|
|
.await?; |
|
|
|
|
|
|
|
Ok(Label { |
|
|
|
|
|
|
|
id: Some(result.0), |
|
|
|
|
|
|
|
description: result.1, |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[tokio::main] |
|
|
|
async fn delete(&self, record: &Label) -> Result<(), sqlx::Error> { |
|
|
|
async fn main() -> Result<(), sqlx::Error> { |
|
|
|
sqlx::query(r#"DELETE FROM testing WHERE id = ?"#) |
|
|
|
|
|
|
|
.bind(&record.id) |
|
|
|
|
|
|
|
.execute(self.pool) |
|
|
|
|
|
|
|
.await?; |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> { |
|
|
|
println!("Open database"); |
|
|
|
println!("Open database"); |
|
|
|
let pool = SqlitePoolOptions::new() |
|
|
|
let pool = SqlitePoolOptions::new() |
|
|
|
.connect_with( |
|
|
|
.connect_with( |
|
|
@ -37,21 +57,38 @@ async fn main() -> Result<(), sqlx::Error> { |
|
|
|
) |
|
|
|
) |
|
|
|
.await?; |
|
|
|
.await?; |
|
|
|
sqlx::migrate!("db/migrations").run(&pool).await?; |
|
|
|
sqlx::migrate!("db/migrations").run(&pool).await?; |
|
|
|
|
|
|
|
Ok(pool) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::main] |
|
|
|
|
|
|
|
async fn main() -> Result<(), sqlx::Error> { |
|
|
|
|
|
|
|
let pool = connect().await?; |
|
|
|
|
|
|
|
let repo = LabelRepository::new(&pool); |
|
|
|
|
|
|
|
|
|
|
|
let repo = Repository::new(pool); |
|
|
|
let command = env::args() |
|
|
|
|
|
|
|
.nth(1) |
|
|
|
// let command = env::args().nth(1).unwrap();
|
|
|
|
.expect("Need a command: \"add\" or \"remove\""); |
|
|
|
// println!("Command: \"{}\"", command);
|
|
|
|
let value = env::args() |
|
|
|
// if command == "add" {
|
|
|
|
.nth(2) |
|
|
|
// let value = env::args().nth(2).unwrap();
|
|
|
|
.expect("Besides the command, need a label to deal with"); |
|
|
|
// println!("Should add \"{}\"", value);
|
|
|
|
|
|
|
|
// } else if command == "remove" {
|
|
|
|
println!("Command: \"{}\"", command); |
|
|
|
// let value = env::args().nth(2).unwrap();
|
|
|
|
|
|
|
|
// println!("Should remove \"{}\"", value);
|
|
|
|
if command == "add" { |
|
|
|
// sqlx::query(r#"DELETE FROM testing WHERE label = ?"#)
|
|
|
|
println!("Should add \"{}\"", value); |
|
|
|
// .bind(value)
|
|
|
|
let record = Label { |
|
|
|
// .execute(&pool)
|
|
|
|
id: None, |
|
|
|
// .await?;
|
|
|
|
description: value.into(), |
|
|
|
// }
|
|
|
|
}; |
|
|
|
|
|
|
|
repo.save(&record).await?; |
|
|
|
|
|
|
|
} else if command == "remove" { |
|
|
|
|
|
|
|
println!("Should remove \"{}\"", value); |
|
|
|
|
|
|
|
if let Ok(record) = repo.find_by_description(&value).await { |
|
|
|
|
|
|
|
repo.delete(&record).await?; |
|
|
|
|
|
|
|
println!("Removed"); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
println!("Label does not exist"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|