sqly

Derive Macro Update

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

Applied to types which are defined to update rows in a table.

Implements Update.


§Attribute Definition (see Attribute Notation and Attribute Documentation)

§Struct Attributes:

#[sqly((table)! (= Path)!)] // required
#[sqly((rename)? (= String)!)]

#[sqly((unchecked)?)]
#[sqly((print)?)]
#[sqly((debug)?)]

§Field Attributes:

#[sqly((column)? (= String)!)]
#[sqly((rename)? (= String)!)]

#[sqly((value)? (= Expr)!)]
#[sqly((infer)?)]

#[sqly((skip)?)]
#[sqly((key)?)]


§Example

use sqly::*; // traits
 
#[derive(Table)]
#[sqly(table = "books")]
struct Book {
    // ...
    // these fields are ignored
}
 
#[derive(Update)]
#[sqly(table = Book)]
struct UpdateBook {
    #[sqly(key)] // update keys must be specified
    id: i32,
    title: String, // other fields are values
    #[sqly(skip)]
    info: &'static str, // except when skipped
}
 
async fn update_book(book_id: i32, title: String, db: &sqlx::PgPool) -> Result<()> {
    Book::update(&UpdateBook {
        id: book_id, // key
        title: title, // value
        info: "..." // ignored
    }) // returns `sqlx::query::Query`
    .execute(db)
    .await?;
    Ok(())
}