Macro diesel::impl_Insertable [] [src]

macro_rules! impl_Insertable {
    (
        ($table_name:ident)
        $(#[$ignore:meta])*
        $(pub)* struct $($body:tt)*
    ) => { ... };
    (
        ($table_name:ident)
        $struct_name:ident <$($lifetime:tt),*>
        $body:tt $(;)*
    ) => { ... };
    (
        ($table_name:ident)
        $struct_name:ident
        $body:tt $(;)*
    ) => { ... };
    (
        (
            struct_name = $struct_name:ident,
            $($headers:tt)*
        ),
        fields = [$({
            column_name: $column_name:ident,
            field_ty: $field_ty:ty,
            field_kind: $field_kind:ident,
            $($rest:tt)*
        })+],
    ) => { ... };
    (
        (
            struct_name = $struct_name:ident,
            $($headers:tt)*
        ),
        fields = [$({
            field_name: $field_name:ident,
            column_name: $column_name:ident,
            field_ty: $field_ty:ty,
            field_kind: $field_kind:ident,
            $($rest:tt)*
        })+],
    ) => { ... };
    (
        table_name = $table_name:ident,
        struct_ty = $struct_ty:ty,
        lifetimes = ($($lifetime:tt),*),
        self_to_columns = $self_to_columns:pat,
        columns = ($($column_name:ident, $field_ty:ty, $field_kind:ident),+),
    ) => { ... };
}

Implements the Insertable trait for a given struct. This macro should be called with the name of the table you wish to use the struct with, followed by the entire struct body.

Example

struct NewUser<'a> {
    name: &'a str,
    hair_color: &'a str,
}

impl_Insertable! {
    (users)
    struct NewUser<'a> {
        name: &'a str,
        hair_color: &'a str,
    }
}

To avoid copying your struct definition, you can use the custom_derive crate.

custom_derive! {
    #[derive(Insertable(users))]
    struct NewUser<'a> {
        name: &'a str,
        hair_color: &'a str,
    }
}

You can also use this macro with tuple structs, but all fields must be annotated with #[column_name(name)]. Additionally, a trailing comma after the last field is required.

struct NewUser<'a>(&'a str, Option<&'a str>);

impl_Insertable! {
    (users)
    struct NewUser<'a>(
        #[column_name(name)]
        &'a str,
        #[column_name(hair_color)]
        Option<&'a str>,
    );
}