Skip to main content

SQLInsert

Derive Macro SQLInsert 

Source
#[derive(SQLInsert)]
{
    // Attributes available to this derive:
    #[sqlx_insert]
}
Expand description

Derive macro for automatically implementing SQLInsert trait for a struct. All struct fields that are supposed to be inserted into the database need to be: sqlx::types::Type + sqlx::encode::Encode + Clone + Send + Sync. As of now, the proc macro supports only named struct fields.

Fields can be renamed using #[sqlx_insert(rename = "new_name")] attribute or ignored using #[sqlx_insert(ignore)].

Database is specified by passing type to #[sqlx_insert(database(DbType))] attribute. The DbType is a type that implements sqlx::Database trait.

Table name defaults to the type name, but can be overridden using #[sqlx_insert(table = "new_name")] attribute.

Example:

use sqlx::Postgres;
use sqlx::Sqlite;
use sqlx_insert::SQLInsert;

// If using macros feature, only a single database is supported
#[derive(SQLInsert, Clone, Debug)]
#[sqlx_insert(table = "thingy")]
#[cfg_attr(feature = "use-macros", sqlx_insert(database(Postgres)))]
#[cfg_attr(not(feature = "use-macros"), sqlx_insert(database(Postgres, Sqlite)))]
pub struct Thing {
    id: String,
    name: String,
    amount: i32,
    pear: String,
    #[sqlx_insert(ignore)]
    ignore_me: Option<String>,
    #[sqlx_insert(rename = "param_extra")]
    param: String,
}

Implements SQLInsert trait for a type.