Skip to main content

Predicate

Struct Predicate 

Source
pub struct Predicate<T: IEntityType> { /* private fields */ }
Expand description

A composable WHERE predicate for entity type T.

T tags the predicate so that fragments written for different entities do not silently combine or get applied to the wrong query builder.

use rust_ef::prelude::*;
use rust_ef::query::QueryBuilder;

#[derive(Debug, Clone, EntityType)]
#[table("products")]
struct Product {
    #[primary_key]
    id: i32,
    name: String,
    description: String,
    price: f64,
    is_deleted: bool,
}

fn product_filter(keyword: Option<&str>, min_price: Option<f64>) -> Predicate<Product> {
    let keyword_group = keyword.map(|keyword| {
        linq!(|p: Product| p.name.contains(keyword))
            .or(linq!(|p: Product| p.description.contains(keyword)))
    });
    let min_price = min_price.map(|min_price| linq!(|p: Product| p.price >= min_price));

    // always() 是 AND 链起点,本身不产生 SQL;
    // and_opt 让「可选条件」不需要额外 if let。
    Predicate::<Product>::always()
        .and(linq!(|p: Product| !p.is_deleted))
        .and_opt(keyword_group)
        .and_opt(min_price)
}

let predicate = product_filter(Some("rust"), Some(9.9));
let sql = QueryBuilder::<Product>::new("products")
    .filter(predicate)
    .to_sql();
// NOT (is_deleted = ?) AND ((name LIKE ?) OR (description LIKE ?)) AND (price >= ?)
assert!(sql.contains("(name LIKE ?) OR (description LIKE ?)"));

Implementations§

Source§

impl<T: IEntityType> Predicate<T>

Source

pub fn always() -> Self

A predicate that matches every row — the seed of an AND chain.

Predicate::always().and(p) is p, so seeding with it adds no SQL.

Source

pub fn never() -> Self

A predicate that matches no row — the seed of an OR chain.

Predicate::never().or(p) is p, so seeding with it adds no SQL.

Source

pub fn from_expr(expr: BoolExpr) -> Self

Wraps an expression tree as a predicate.

Prefer linq!(|b: T| ...), which is already typed and self-contained. This is the unchecked door for reusing a BoolExpr produced elsewhere (e.g. a filter read back from ModelBuilder).

Source

pub fn is_always(&self) -> bool

Whether the predicate matches every row (the AND identity).

Source

pub fn is_never(&self) -> bool

Whether the predicate matches no row.

Source

pub fn and(self, other: impl Into<Predicate<T>>) -> Self

self AND other.

always() is the identity (always().and(p) == p) and never() is absorbing (never().and(p) == never()), so identities never leak into the generated SQL.

Source

pub fn and_opt(self, other: Option<impl Into<Predicate<T>>>) -> Self

self AND other when other is Some — the one-liner for an optional filter. None leaves the predicate unchanged.

Source

pub fn or(self, other: impl Into<Predicate<T>>) -> Self

self OR other.

never() is the identity (never().or(p) == p) and always() is absorbing (always().or(p) == always()).

Source

pub fn or_opt(self, other: Option<impl Into<Predicate<T>>>) -> Self

self OR other when other is Some; None leaves the predicate unchanged.

Source

pub fn not(self) -> Self

NOT self.

Source

pub fn into_expr(self) -> BoolExpr

Converts the predicate to a BoolExpr — for APIs that take an expression directly, such as ModelBuilder::has_query_filter.

always() and never() become the literal fragments 1=1 / 1=0.

Trait Implementations§

Source§

impl<T: Clone + IEntityType> Clone for Predicate<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + IEntityType> Debug for Predicate<T>

Source§

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

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

impl<T: IEntityType> From<BoolExpr> for Predicate<T>

Source§

fn from(expr: BoolExpr) -> Self

Lets BoolExpr fragments (e.g. a stored global filter) be combined directly. This conversion is unchecked: prefer linq!(...).

Source§

impl<T: IEntityType> From<Predicate<T>> for BoolExpr

Source§

fn from(predicate: Predicate<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Forget for Predicate<T>

§

impl<T> Freeze for Predicate<T>
where PhantomData<fn() -> T>: Freeze,

§

impl<T> Move for Predicate<T>

§

impl<T> RefUnwindSafe for Predicate<T>
where PhantomData<fn() -> T>: RefUnwindSafe,

§

impl<T> Send for Predicate<T>
where PhantomData<fn() -> T>: Send,

§

impl<T> Sync for Predicate<T>
where PhantomData<fn() -> T>: Sync,

§

impl<T> Unpin for Predicate<T>
where PhantomData<fn() -> T>: Unpin,

§

impl<T> UnsafeUnpin for Predicate<T>
where PhantomData<fn() -> T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Predicate<T>
where PhantomData<fn() -> T>: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.