zino_orm

Struct QueryBuilder

Source
pub struct QueryBuilder<E: Entity> { /* private fields */ }
Expand description

A query builder for the model entity.

§Examples

use crate::model::{User, UserColumn};
use zino_core::orm::{QueryBuilder, Schema};

let query = QueryBuilder::<User>::new()
    .and_not_in(UserColumn::Status, ["Deleted", "Locked"])
    .or(QueryBuilder::<User>::new()
        .and_eq(UserColumn::Roles, "worker")
        .and_eq(UserColumn::Visibility, "Public"))
    .or(QueryBuilder::<User>::new()
        .and_in(UserColumn::Roles, ["admin", "auditor"])
        .and_ne(UserColumn::Visibility, "Public"))
    .order_desc(UserColumn::UpdatedAt)
    .limit(10)
    .build();
let users: Vec<User> = User::find(&query).await?;

Implementations§

Source§

impl<E: Entity> QueryBuilder<E>

Source

pub fn new() -> Self

Creates a new instance.

Source

pub fn field(self, col: E::Column) -> Self

Adds a field corresponding to the column.

Source

pub fn fields<V: Into<Vec<E::Column>>>(self, cols: V) -> Self

Adds the fields corresponding to the columns.

Source

pub fn alias(self, col: E::Column, alias: &str) -> Self

Adds a field with an alias for the column.

Source

pub fn aggregate(self, aggregation: Aggregation<E>, alias: Option<&str>) -> Self

Adds a field with an optional alias for the aggregate function.

Source

pub fn window(self, window: Window<E>, alias: Option<&str>) -> Self

Adds a field with an optional alias for the window function.

Source

pub fn group_by(self, col: E::Column) -> Self

Adds a GROUP BY column.

Source

pub fn having_filter( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self

Adds a HAVING condition using the value as a filter for the column.

Source

pub fn having_eq( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self

Adds a HAVING condition for equal parts.

Source

pub fn having_ne( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self

Adds a HAVING condition for non-equal parts.

Source

pub fn having_lt( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self

Adds a HAVING condition for the column less than a value.

Source

pub fn having_le( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self

Adds a HAVING condition for the column not greater than a value.

Source

pub fn having_gt( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self

Adds a HAVING condition for the column greater than a value.

Source

pub fn having_ge( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self

Adds a HAVING condition for the column not less than a value.

Source

pub fn primary_key(self, value: impl IntoSqlValue) -> Self

Adds a logical AND condition for the primary key.

Source

pub fn rand(self, value: impl IntoSqlValue) -> Self

Adds a logical AND condition which selects random items by rand() < value.

Source

pub fn and<M: Entity>(self, other: QueryBuilder<M>) -> Self

Adds a logical AND condition by merging the other query builder.

Source

pub fn and_not<M: Entity>(self, other: QueryBuilder<M>) -> Self

Adds a logical AND NOT condition by merging the other query builder.

Source

pub fn and_filter(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical AND condition using the value as a filter for the column.

Source

pub fn and_eq(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical AND condition for equal parts.

Source

pub fn and_eq_if_not_null( self, col: E::Column, value: impl IntoSqlValue, ) -> Self

Adds a logical AND condition for equal parts if the value is not null.

Source

pub fn and_eq_if_some<T: IntoSqlValue>( self, col: E::Column, value: Option<T>, ) -> Self

Adds a logical AND condition for equal parts if the value is not none.

Source

pub fn and_ne(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical AND condition for non-equal parts.

Source

pub fn and_ne_if_not_null( self, col: E::Column, value: impl IntoSqlValue, ) -> Self

Adds a logical AND condition for non-equal parts if the value is not null.

Source

pub fn and_ne_if_some<T: IntoSqlValue>( self, col: E::Column, value: Option<T>, ) -> Self

Adds a logical AND condition for non-equal parts if the value is not none.

Source

pub fn and_lt(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical AND condition for the column less than a value.

Source

pub fn and_le(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical AND condition for the column not greater than a value.

Source

pub fn and_gt(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical AND condition for the column greater than a value.

Source

pub fn and_ge(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical AND condition for the column not less than a value.

Source

pub fn and_in<T, V>(self, col: E::Column, values: V) -> Self
where T: IntoSqlValue, V: Into<Vec<T>>,

Adds a logical AND condition for the column IN a list of values.

Source

pub fn and_not_in<T, V>(self, col: E::Column, values: V) -> Self
where T: IntoSqlValue, V: Into<Vec<T>>,

Adds a logical AND condition for the column NOT IN a list of values.

Source

pub fn and_in_subquery<C, M>(self, cols: C, subquery: QueryBuilder<M>) -> Self
where C: Into<Vec<E::Column>>, M: Entity + Schema,

Adds a logical AND condition for the columns IN a subquery.

Source

pub fn and_not_in_subquery<C, M>( self, cols: C, subquery: QueryBuilder<M>, ) -> Self
where C: Into<Vec<E::Column>>, M: Entity + Schema,

Adds a logical AND condition for the columns NOT IN a subquery.

Source

pub fn and_in_range<T: IntoSqlValue>( self, col: E::Column, min: T, max: T, ) -> Self

Adds a logical AND condition for the column in a range [min, max).

Source

pub fn and_between<T: IntoSqlValue>( self, col: E::Column, min: T, max: T, ) -> Self

Adds a logical AND condition for the column BETWEEN two values.

Source

pub fn and_like(self, col: E::Column, value: String) -> Self

Adds a logical AND condition for the column LIKE a string value.

Source

pub fn and_ilike(self, col: E::Column, value: String) -> Self

Adds a logical AND condition for the column ILIKE a string value.

Source

pub fn and_rlike(self, col: E::Column, value: String) -> Self

Adds a logical AND condition for the column RLIKE a string value.

Source

pub fn and_contains(self, col: E::Column, value: &str) -> Self

Adds a logical AND condition for the column which contains a string value.

Source

pub fn and_starts_with(self, col: E::Column, value: &str) -> Self

Adds a logical AND condition for the column which starts with a string value.

Source

pub fn and_ends_with(self, col: E::Column, value: &str) -> Self

Adds a logical AND condition for the column which ends with a string value.

Source

pub fn and_null(self, col: E::Column) -> Self

Adds a logical AND condition for the column which is null.

Source

pub fn and_not_null(self, col: E::Column) -> Self

Adds a logical AND condition for the column which is not null.

Source

pub fn and_empty(self, col: E::Column) -> Self

Adds a logical AND condition for the column which is an empty string or a null.

Source

pub fn and_nonempty(self, col: E::Column) -> Self

Adds a logical AND condition for the column which is not an empty string or a null.

Source

pub fn and_overlaps<T: IntoSqlValue>( self, cols: (E::Column, E::Column), values: (T, T), ) -> Self

Adds a logical AND condition for the two ranges which overlaps with each other.

Source

pub fn or<M: Entity>(self, other: QueryBuilder<M>) -> Self

Adds a logical OR condition by merging the other query builder.

Source

pub fn or_not<M: Entity>(self, other: QueryBuilder<M>) -> Self

Adds a logical OR NOT condition by merging the other query builder.

Source

pub fn or_filter(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition using the value as a filter for the column.

Source

pub fn or_eq(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for equal parts.

Source

pub fn or_eq_if_not_null(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for equal parts if the value is not null.

Source

pub fn or_eq_if_some<T: IntoSqlValue>( self, col: E::Column, value: Option<T>, ) -> Self

Adds a logical OR condition for equal parts if the value is not none.

Source

pub fn or_ne(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for non-equal parts.

Source

pub fn or_ne_if_not_null(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for non-equal parts if the value is not none.

Source

pub fn or_ne_if_some<T: IntoSqlValue>( self, col: E::Column, value: Option<T>, ) -> Self

Adds a logical OR condition for non-equal parts if the value is not none.

Source

pub fn or_lt(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for the column less than a value.

Source

pub fn or_le(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for the column not greater than a value.

Source

pub fn or_gt(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for the column greater than a value.

Source

pub fn or_ge(self, col: E::Column, value: impl IntoSqlValue) -> Self

Adds a logical OR condition for the column not less than a value.

Source

pub fn or_in<T, V>(self, col: E::Column, values: V) -> Self
where T: IntoSqlValue, V: Into<Vec<T>>,

Adds a logical OR condition for the column IN a list of values.

Source

pub fn or_not_in<T, V>(self, col: E::Column, values: V) -> Self
where T: IntoSqlValue, V: Into<Vec<T>>,

Adds a logical OR condition for the column NOT IN a list of values.

Source

pub fn or_in_subquery<C, M>(self, cols: C, subquery: QueryBuilder<M>) -> Self
where C: Into<Vec<E::Column>>, M: Entity + Schema,

Adds a logical OR condition for the columns IN a subquery.

Source

pub fn or_not_in_subquery<C, M>( self, cols: C, subquery: QueryBuilder<M>, ) -> Self
where C: Into<Vec<E::Column>>, M: Entity + Schema,

Adds a logical OR condition for the columns NOT IN a subquery.

Source

pub fn or_in_range<T: IntoSqlValue>( self, col: E::Column, min: T, max: T, ) -> Self

Adds a logical OR condition for the column is in a range [min, max).

Source

pub fn or_between<T: IntoSqlValue>(self, col: E::Column, min: T, max: T) -> Self

Adds a logical OR condition for the column BETWEEN two values.

Source

pub fn or_like(self, col: E::Column, value: String) -> Self

Adds a logical OR condition for the column LIKE a string value.

Source

pub fn or_ilike(self, col: E::Column, value: String) -> Self

Adds a logical OR condition for the column ILIKE a string value.

Source

pub fn or_rlike(self, col: E::Column, value: String) -> Self

Adds a logical OR condition for the column RLIKE a string value.

Source

pub fn or_contains(self, col: E::Column, value: &str) -> Self

Adds a logical OR condition for the column which contains a string value.

Source

pub fn or_starts_with(self, col: E::Column, value: &str) -> Self

Adds a logical OR condition for the column which starts with a string value.

Source

pub fn or_ends_with(self, col: E::Column, value: &str) -> Self

Adds a logical OR condition for the column which ends with a string value.

Source

pub fn or_null(self, col: E::Column) -> Self

Adds a logical OR condition for the column which is null.

Source

pub fn or_not_null(self, col: E::Column) -> Self

Adds a logical OR condition for the column which is not null.

Source

pub fn or_empty(self, col: E::Column) -> Self

Adds a logical OR condition for the column which is an empty string or a null.

Source

pub fn or_nonempty(self, col: E::Column) -> Self

Adds a logical OR condition for the column which is not an empty string or a null.

Source

pub fn or_overlaps<T: IntoSqlValue>( self, cols: (E::Column, E::Column), values: (T, T), ) -> Self

Adds a logical OR condition for the two ranges which overlaps with each other.

Source

pub fn order_by(self, col: impl ToString, descending: bool) -> Self

Adds a query order.

Source

pub fn order_by_with_nulls( self, col: impl ToString, descending: bool, nulls_first: bool, ) -> Self

Adds a query order with an extra flag to indicate whether the nulls appear first or last.

Source

pub fn order_asc(self, col: impl ToString) -> Self

Adds a query order with an ascending order.

Source

pub fn order_desc(self, col: impl ToString) -> Self

Adds a query order with an descending order.

Source

pub fn offset(self, offset: usize) -> Self

Sets the offset.

Source

pub fn limit(self, limit: usize) -> Self

Sets the limit.

Source

pub fn build(self) -> Query

Builds the model query.

Source§

impl<E: Entity + Schema> QueryBuilder<E>

Source

pub fn build_subquery(self) -> String

Builds a subquery SQL expression.

Trait Implementations§

Source§

impl<E: Clone + Entity> Clone for QueryBuilder<E>

Source§

fn clone(&self) -> QueryBuilder<E>

Returns a copy 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<E: Debug + Entity> Debug for QueryBuilder<E>

Source§

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

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

impl<E: Entity> Default for QueryBuilder<E>

Source§

fn default() -> Self

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

impl<E: Entity + Schema> IntoSqlValue for QueryBuilder<E>

Source§

fn into_sql_value(self) -> JsonValue

Converts self to a SQL value.

Auto Trait Implementations§

§

impl<E> Freeze for QueryBuilder<E>

§

impl<E> RefUnwindSafe for QueryBuilder<E>
where E: RefUnwindSafe,

§

impl<E> Send for QueryBuilder<E>
where E: Send,

§

impl<E> Sync for QueryBuilder<E>
where E: Sync,

§

impl<E> Unpin for QueryBuilder<E>
where E: Unpin,

§

impl<E> UnwindSafe for QueryBuilder<E>
where E: 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, dst: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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, 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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T