-
Notifications
You must be signed in to change notification settings - Fork 1
Where Model
Andrey edited this page Sep 22, 2023
·
14 revisions
Single Model generates AND bounded where the condition
#[derive(WhereDbModel)]
pub struct GetInputParam {
pub client_id: String,
pub key: String,
}
Generates into
WHERE client_id = $1 AND key = $2
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
#[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, ...)
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
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}
For where Model
#[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}
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?;