Skip to main content

WhereCondition

Struct WhereCondition 

Source
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>

Source

pub fn new(expression: &str, parameters: Vec<&'a dyn ToSqlAny>) -> Self

Create a new condition with a SQL expression and the parameters.

Source

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.

Source

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.

Source

pub fn and_where(self, condition: WhereCondition<'a>) -> Self

Compose the condition with a AND boolean logic operator.

Source

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>

Source§

fn clone(&self) -> WhereCondition<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for WhereCondition<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for WhereCondition<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for WhereCondition<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for WhereCondition<'a>

§

impl<'a> !RefUnwindSafe for WhereCondition<'a>

§

impl<'a> Send for WhereCondition<'a>

§

impl<'a> Sync for WhereCondition<'a>

§

impl<'a> Unpin for WhereCondition<'a>

§

impl<'a> UnsafeUnpin for WhereCondition<'a>

§

impl<'a> !UnwindSafe for WhereCondition<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V