From 13e7aba7542c85d06b485c065e37004d97398e19 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 1 Jun 2021 12:47:04 -0300 Subject: [PATCH] Creating a repository is harder than you think --- sqlxtest/src/main.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/sqlxtest/src/main.rs b/sqlxtest/src/main.rs index 47a7696..d5905fe 100644 --- a/sqlxtest/src/main.rs +++ b/sqlxtest/src/main.rs @@ -1,7 +1,37 @@ use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqlitePoolOptions; +use sqlx::Pool; use std::env; +struct Label { + id: u32, + description: String, +} + +struct Repository +where + T: sqlx::Database, +{ + pool: Pool, +} + +impl Repository +where + T: sqlx::Database, +{ + fn new(pool: Pool) -> Self { + Self { pool } + } + + async fn save(&mut self, label: &Label) -> Result<(), sqlx::Error> { + sqlx::query(r#"INSERT INTO testing (label) VALUES (?)"#) + .bind(label.description) + .execute(self.pool) + .await?; + Ok(()) + } +} + #[tokio::main] async fn main() -> Result<(), sqlx::Error> { println!("Open database"); @@ -14,15 +44,13 @@ async fn main() -> Result<(), sqlx::Error> { .await?; sqlx::migrate!("db/migrations").run(&pool).await?; + let repo = Repository::new(pool); + let command = env::args().nth(1).unwrap(); println!("Command: \"{}\"", command); if command == "add" { let value = env::args().nth(2).unwrap(); println!("Should add \"{}\"", value); - sqlx::query(r#"INSERT INTO testing (label) VALUES (?)"#) - .bind(value) - .execute(&pool) - .await?; } else if command == "remove" { let value = env::args().nth(2).unwrap(); println!("Should remove \"{}\"", value);