Browse Source

Freaking lifetimes

master
Julio Biason 4 years ago
parent
commit
2f0eb11dec
  1. 42
      sqlxtest/src/main.rs

42
sqlxtest/src/main.rs

@ -1,32 +1,26 @@
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: u32,
description: String, description: String,
} }
struct Repository<T> struct Repository<'s, T: sqlx::Database + sqlx::Executor<'s>> {
where
T: sqlx::Database,
{
pool: Pool<T>, pool: Pool<T>,
} }
impl<T> Repository<T> impl<'s, T: sqlx::Database + sqlx::Executor<'s>> Repository<'s, T> {
where
T: sqlx::Database,
{
fn new(pool: Pool<T>) -> Self { fn new(pool: Pool<T>) -> Self {
Self { pool } Self { pool }
} }
async fn save(&mut 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(())
} }
@ -46,18 +40,18 @@ async fn main() -> Result<(), sqlx::Error> {
let repo = Repository::new(pool); let repo = Repository::new(pool);
let command = env::args().nth(1).unwrap(); // let command = env::args().nth(1).unwrap();
println!("Command: \"{}\"", command); // println!("Command: \"{}\"", command);
if command == "add" { // if command == "add" {
let value = env::args().nth(2).unwrap(); // let value = env::args().nth(2).unwrap();
println!("Should add \"{}\"", value); // println!("Should add \"{}\"", value);
} else if command == "remove" { // } else if command == "remove" {
let value = env::args().nth(2).unwrap(); // let value = env::args().nth(2).unwrap();
println!("Should remove \"{}\"", value); // println!("Should remove \"{}\"", value);
sqlx::query(r#"DELETE FROM testing WHERE label = ?"#) // sqlx::query(r#"DELETE FROM testing WHERE label = ?"#)
.bind(value) // .bind(value)
.execute(&pool) // .execute(&pool)
.await?; // .await?;
} // }
Ok(()) Ok(())
} }

Loading…
Cancel
Save