Skip to content

Where Model

Andrey edited this page Nov 8, 2023 · 14 revisions

Where Model Use cases

Single Model generates AND bounded where the condition

Plain simple Fields

#[derive(WhereDbModel)]
pub struct GetInputParam {
    pub client_id: String,
    pub key: String,
}

Generates into

WHERE client_id = $1 AND key = $2

Operators

Supported operators

  • "=" - Equal;
  • "!=" or "<>" - Checks if the values of two operands are equal or not, if values are not equal then the condition becomes true.
  • ">" - Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true.
  • "<" - Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true.
  • ">=" - Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true.
  • "<=" - Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true.
#[derive(WhereDbModel)]
pub struct GetInputParam {
    #[operator(">")]
    pub from_amount: i64,
    #[operator("<")]
    pub to_amount: i64,
}

Generates into

WHERE from_amount > $1 AND to_amount < $2

In Operator

#[derive(WhereDbModel)]
pub struct GetInputParam {
    #[operator(">")]
    pub from_amount: i64,
    #[operator("<")]
    pub to_amount: i64,
    pub status: Vec<i32>
}

Generates into

WHERE from_amount > $1 AND to_amount < $2 AND status IN ($3, $4, $5, ...)

Using the Option of Value

None value makes where statement renderer skipping the value at all;

#[derive(WhereDbModel)]
pub struct GetInputParam {
    #[operator(">")]
    pub from_amount: i64,
    #[operator("<")]
    pub to_amount: Option<i64>,

}

let where = GetInputParam { 
  from_amount: 5, 
  to_amount : None
}

Generates into

WHERE from_amount > $1

But

#[derive(WhereDbModel)]
pub struct GetInputParam {
    #[operator(">")]
    pub from_amount: i64,
    #[operator("<")]
    pub to_amount: Option<i64>,

}

let where = GetInputParam { 
  from_amount: 5, 
  to_amount : Some(6)
}

Generates into

WHERE from_amount > $1 AND to_amount < $2

Limit and Offset

Attributes #[limit] and #[offset] can be used.

Option type is applicable to skip rendering the skip or offset statement;

#[derive(WhereDbModel)]
pub struct GetInputParam {
    pub asset_id: String,

    #[limit]
    pub limit: usize,

    #[offset]
    pub offset: usize,

}

Generates into

SQL field FROM {TABLE_NAME} LIMIT {$1} OFFSET {2}

IS_NULL and IS_NOT_NULL

For where Model

#[derive(WhereDbModel)]
pub struct GetInputParam {
    pub asset_id: IsNull,
}

Generates into

SQL field FROM {TABLE_NAME} LIMIT {$1} OFFSET {2}

NoneWhereModel

If we have to proved Option::None as a Where Parameter to get_rows() case and overcome the lack of type - NoneWhereModel is introduced.

let result: Result<Vec<MyEntity>,_> = postgres.query_rows("table_name", NoneWhereModel::new()).await?;
Clone this wiki locally