Skip to content

Other attributes

Andrey edited this page Oct 1, 2024 · 2 revisions

#[ignore]

Ignores field from SQL rendering

#[derive(SelectDbEntity)]
pub struct WhereDto {

 #[ignore]
 pub dt_from: Option<DateTimeAsMicroseconds>,

}

#[ignore_if_none]

Ignores field from SQL rendering if the value of the field is none

#[derive(WhereDbModel)]
pub struct WhereDto {

 #[ignore_if_null]
 pub dt_from: Option<DateTimeAsMicroseconds>,

}

#[wrap_column_name]

If you column name is the same as reserved word - use this attribute to make sure it wrapped to quotes during SQL rendering

SELECT column_name FROM xxxx // without #[wrap_column_name]

SELECT 'column_name' FROM xxxx // with #[wrap_column_name]

#[json]

the text value of the table cell can be deserialized into JSON

#[derive(SelectDbEntity)]
pub struct MyDto {

 #[json]
 pub dt_from: String,

}

#[sql]

When the SQL renderer renders select columns, one of the columns can be replaced by whatever is set into #[sql] attribute

#[derive(SelectDbEntity)]
pub struct StatisticsDto {
    #[sql("count(*)::int")]
    pub count: i32,
    #[group_by]
    pub asset_id: String,
}

#[db_field_name]

If the column name is different from the struct name - db_field_name can be used to map them

#[derive(WhereDbModel)]
pub struct WhereDto<'s> {

    #[db_field_name(name = "dt")]
    pub dt_to: Option<DateTimeAsMicroseconds>,

}

#[inside_json("my_field")]

Can be used as a part WhereDbModel only to give access to a field inside json field

#[derive(WhereDbModel)]
pub struct TestWhereModel {
    #[inside_json("field_name")]
    pub my_json_field: String,
}

Generates SQL similar to

"my_json_field"->>'field_name'=$1

As well it's possible to have access to the second, third etc levels of json object

#[derive(WhereDbModel)]
pub struct TestWhereModel {
    #[inside_json("field_name.next_level")]
    pub my_json_field: String,
}

Generates SQL similar to

"my_json_field"->'field_name'->>'next_level' =$1
Clone this wiki locally