pub struct WhereCondition<'a> { /* private fields */ }Expand description
A structure to hold the where condition of the queries.
It is designed to be a composable structure to build the where condition of
the queries alongside the according parameters, preserving the order of the
parameters.
The conditions are composed using the and_where and or_where methods
with a default representation of true if no condition is provided.
§Examples
§Default condition
The default condition is true which is useful when the conditions are
passed as parameters. This way, when no condition is provided, the
query will be like select * from table where true. The condition will
be ignored by the database planner and will return all the records.
use agrum::{WhereCondition, params};
let condition = WhereCondition::default();
assert_eq!("true", condition.to_string());§Building a condition
The WhereCondition structure can be built using the new method to
create a new condition with a SQL expression and the parameters.
The and_where and or_where methods can be used to compose the conditions
with the boolean logic operators.
The expand method can be used to get the SQL expression and the parameters
(consuming the instance).
use agrum::{WhereCondition, params};
let condition = WhereCondition::new("A = $?", params![1_i32]);
let condition = condition.and_where(WhereCondition::new("B = $?", params![2_i32]));
let condition = condition.or_where(WhereCondition::new("C = $?", params![3_i32]));
assert_eq!("A = $? and B = $? or C = $?", condition.to_string());Implementations§
Source§impl<'a> WhereCondition<'a>
impl<'a> WhereCondition<'a>
Sourcepub fn new(expression: &str, parameters: Vec<&'a dyn ToSqlAny>) -> Self
pub fn new(expression: &str, parameters: Vec<&'a dyn ToSqlAny>) -> Self
Create a new condition with a SQL expression and the parameters.
Sourcepub fn expand(self) -> (String, Vec<&'a dyn ToSqlAny>)
pub fn expand(self) -> (String, Vec<&'a dyn ToSqlAny>)
Expand the condition to a SQL expression and the parameters (consuming the instance). This is normally used to get the SQL expression and the parameters.
Sourcepub fn where_in(field: &str, parameters: Vec<&'a dyn ToSqlAny>) -> Self
pub fn where_in(field: &str, parameters: Vec<&'a dyn ToSqlAny>) -> Self
Create a new condition with a IN SQL expression and the parameters.
It creates as many $? placeholders as the number of parameters.
Sourcepub fn and_where(self, condition: WhereCondition<'a>) -> Self
pub fn and_where(self, condition: WhereCondition<'a>) -> Self
Compose the condition with a AND boolean logic operator.
Sourcepub fn or_where(self, condition: WhereCondition<'a>) -> Self
pub fn or_where(self, condition: WhereCondition<'a>) -> Self
Compose the condition with a OR boolean logic operator.
Trait Implementations§
Source§impl<'a> Clone for WhereCondition<'a>
impl<'a> Clone for WhereCondition<'a>
Source§fn clone(&self) -> WhereCondition<'a>
fn clone(&self) -> WhereCondition<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more