Derive Macro wb_sqlite::Update

source ·
#[derive(Update)]
{
    // Attributes available to this derive:
    #[sql]
}
Expand description

fn update(&self, exec: impl sqlx::SqliteExecutor<’_ >) -> Result<bool, sqlx::Error>

Generate fn for UPDATE with sqlx, if there is a PRIMARY KEY.
PRIMARY KEY detection works only if constraint is in all caps, lowercase serves as escape hatch.

UPDATE {tab_name} SET {cols} WHERE {pk}=

#[derive(CreateTableSql,Insert,Update)]
struct Cat {
   #[sql(constraint = "PRIMARY KEY")]
   id: i64,
   name: String,
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), sqlx::Error> {
   use sqlx::{Connection, Executor};
   let mut conn = sqlx::SqliteConnection::connect(":memory:").await?;
   conn.execute(Cat::CREATE_TABLE_SQL).await?;

   let c = Cat {
      id: 0,
      name: "miau".to_owned(),
   };
   let id = c.insert(&mut conn).await?;
   assert!(id == 1);

   let c2 = Cat {
      id: 1,
      name: "meouw".to_owned(),
   };
   let ok = c2.update(&mut conn).await?;
   assert!(ok);

   Ok(())
}