QueryBuilder

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::new()
    .and_not_in(UserColumn::Status, ["Deleted", "Locked"])
    .or(QueryBuilder::new()
        .and_eq(UserColumn::Roles, "worker")
        .and_eq(UserColumn::Visibility, "Public"))
    .or(QueryBuilder::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 table_name(self, table_name: String) -> Self

Sets a table for the query, which should only be used to specify a dynamic table.

Source

pub fn field<C: ModelColumn<E>>(self, col: C) -> Self

Adds a field corresponding to the column.

Source

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

Adds the fields corresponding to the columns.

Source

pub fn alias<C, F>(self, col: C, alias: F) -> Self
where C: ModelColumn<E>, F: AsRef<str>,

Adds a field with an alias for the column.

Source

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

Adds a field with an alias for the aggregate expression.

Source

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

Adds a field with an alias for the window function.

Source

pub fn group_by<C, F>(self, col: C, alias: F) -> Self
where C: ModelColumn<E>, F: AsRef<str>,

Adds a GROUP BY column.

Source

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

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

Source

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

Adds a HAVING condition for equal parts.

Source

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

Adds a HAVING condition for non-equal parts.

Source

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

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

Source

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

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

Source

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

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

Source

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

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

Source

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

Adds a logical AND condition for the primary key.

Source

pub fn rand<V: IntoSqlValue>(self, value: V) -> 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<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_eq<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

Adds a logical AND condition for equal parts.

Source

pub fn and_eq_if_not_null<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_eq_if_some<C, V>(self, col: C, value: Option<V>) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_ne<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

Adds a logical AND condition for non-equal parts.

Source

pub fn and_ne_if_not_null<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_ne_if_some<C, V>(self, col: C, value: Option<V>) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_lt<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_le<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_gt<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_ge<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_in<C, T, V>(self, col: C, values: V) -> Self
where C: ModelColumn<E>, 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<C, T, V>(self, col: C, values: V) -> Self
where C: ModelColumn<E>, 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_if_nonempty<C, T, V>(self, col: C, values: V) -> Self
where C: ModelColumn<E>, T: IntoSqlValue, V: Into<Vec<T>>,

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

Source

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

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

Source

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

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

Source

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

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

Source

pub fn and_in_range<C, V>(self, col: C, min: V, max: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_between<C, V>(self, col: C, min: V, max: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn and_like<C: ModelColumn<E>>(self, col: C, value: String) -> Self

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

Source

pub fn and_ilike<C: ModelColumn<E>>(self, col: C, value: String) -> Self

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

Source

pub fn and_rlike<C: ModelColumn<E>>(self, col: C, value: String) -> Self

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

Source

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

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

Source

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

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

Source

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

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

Source

pub fn and_null<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn and_not_null<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn and_empty<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn and_nonempty<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn and_overlaps<L, R, V>(self, cols: (L, R), values: (V, V)) -> Self
where L: ModelColumn<E>, R: ModelColumn<E>, V: IntoSqlValue,

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<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_eq<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

Adds a logical OR condition for equal parts.

Source

pub fn or_eq_if_not_null<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_eq_if_some<C, V>(self, col: C, value: Option<V>) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_ne<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

Adds a logical OR condition for non-equal parts.

Source

pub fn or_ne_if_not_null<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_ne_if_some<C, V>(self, col: C, value: Option<V>) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_lt<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_le<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_gt<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_ge<C, V>(self, col: C, value: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_in<C, T, V>(self, col: C, values: V) -> Self
where C: ModelColumn<E>, 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<C, T, V>(self, col: C, values: V) -> Self
where C: ModelColumn<E>, 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_if_nonempty<C, T, V>(self, col: C, values: V) -> Self
where C: ModelColumn<E>, T: IntoSqlValue, V: Into<Vec<T>>,

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

Source

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

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

Source

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

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

Source

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

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

Source

pub fn or_in_range<C, V>(self, col: C, min: V, max: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_between<C, V>(self, col: C, min: V, max: V) -> Self
where C: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn or_like<C: ModelColumn<E>>(self, col: C, value: String) -> Self

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

Source

pub fn or_ilike<C: ModelColumn<E>>(self, col: C, value: String) -> Self

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

Source

pub fn or_rlike<C: ModelColumn<E>>(self, col: C, value: String) -> Self

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

Source

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

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

Source

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

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

Source

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

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

Source

pub fn or_null<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn or_not_null<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn or_empty<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn or_nonempty<C: ModelColumn<E>>(self, col: C) -> Self

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

Source

pub fn or_overlaps<L, R, V>(self, cols: (L, R), values: (V, V)) -> Self
where L: ModelColumn<E>, R: ModelColumn<E>, V: IntoSqlValue,

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

Source

pub fn order_by<C: ModelColumn<E>>(self, col: C, descending: bool) -> Self

Adds a query order.

Source

pub fn order_by_with_nulls<C: ModelColumn<E>>( self, col: C, 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<C: ModelColumn<E>>(self, col: C) -> Self

Adds a query order with an ascending order.

Source

pub fn order_desc<C: ModelColumn<E>>(self, col: C) -> 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 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<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, 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> 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