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>
impl<E: Entity> QueryBuilder<E>
Sourcepub fn fields<V: Into<Vec<E::Column>>>(self, cols: V) -> Self
pub fn fields<V: Into<Vec<E::Column>>>(self, cols: V) -> Self
Adds the fields corresponding to the columns.
Sourcepub fn alias(self, col: E::Column, alias: &str) -> Self
pub fn alias(self, col: E::Column, alias: &str) -> Self
Adds a field with an alias for the column.
Sourcepub fn aggregate(self, aggregation: Aggregation<E>, alias: Option<&str>) -> Self
pub fn aggregate(self, aggregation: Aggregation<E>, alias: Option<&str>) -> Self
Adds a field with an optional alias for the aggregate function.
Sourcepub fn window(self, window: Window<E>, alias: Option<&str>) -> Self
pub fn window(self, window: Window<E>, alias: Option<&str>) -> Self
Adds a field with an optional alias for the window function.
Sourcepub fn having_filter(
self,
aggregation: Aggregation<E>,
value: impl IntoSqlValue,
) -> Self
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.
Sourcepub fn having_eq(
self,
aggregation: Aggregation<E>,
value: impl IntoSqlValue,
) -> Self
pub fn having_eq( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self
Adds a HAVING
condition for equal parts.
Sourcepub fn having_ne(
self,
aggregation: Aggregation<E>,
value: impl IntoSqlValue,
) -> Self
pub fn having_ne( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self
Adds a HAVING
condition for non-equal parts.
Sourcepub fn having_lt(
self,
aggregation: Aggregation<E>,
value: impl IntoSqlValue,
) -> Self
pub fn having_lt( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self
Adds a HAVING
condition for the column less than a value.
Sourcepub fn having_le(
self,
aggregation: Aggregation<E>,
value: impl IntoSqlValue,
) -> Self
pub fn having_le( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self
Adds a HAVING
condition for the column not greater than a value.
Sourcepub fn having_gt(
self,
aggregation: Aggregation<E>,
value: impl IntoSqlValue,
) -> Self
pub fn having_gt( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self
Adds a HAVING
condition for the column greater than a value.
Sourcepub fn having_ge(
self,
aggregation: Aggregation<E>,
value: impl IntoSqlValue,
) -> Self
pub fn having_ge( self, aggregation: Aggregation<E>, value: impl IntoSqlValue, ) -> Self
Adds a HAVING
condition for the column not less than a value.
Sourcepub fn primary_key(self, value: impl IntoSqlValue) -> Self
pub fn primary_key(self, value: impl IntoSqlValue) -> Self
Adds a logical AND
condition for the primary key.
Sourcepub fn rand(self, value: impl IntoSqlValue) -> Self
pub fn rand(self, value: impl IntoSqlValue) -> Self
Adds a logical AND
condition which selects random items by rand() < value
.
Sourcepub fn and<M: Entity>(self, other: QueryBuilder<M>) -> Self
pub fn and<M: Entity>(self, other: QueryBuilder<M>) -> Self
Adds a logical AND
condition by merging the other query builder.
Sourcepub fn and_not<M: Entity>(self, other: QueryBuilder<M>) -> Self
pub fn and_not<M: Entity>(self, other: QueryBuilder<M>) -> Self
Adds a logical AND NOT
condition by merging the other query builder.
Sourcepub fn and_filter(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn and_eq(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn and_eq(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical AND
condition for equal parts.
Sourcepub fn and_eq_if_not_null(
self,
col: E::Column,
value: impl IntoSqlValue,
) -> Self
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.
Sourcepub fn and_eq_if_some<T: IntoSqlValue>(
self,
col: E::Column,
value: Option<T>,
) -> Self
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.
Sourcepub fn and_ne(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn and_ne(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical AND
condition for non-equal parts.
Sourcepub fn and_ne_if_not_null(
self,
col: E::Column,
value: impl IntoSqlValue,
) -> Self
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.
Sourcepub fn and_ne_if_some<T: IntoSqlValue>(
self,
col: E::Column,
value: Option<T>,
) -> Self
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.
Sourcepub fn and_lt(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn and_lt(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical AND
condition for the column less than a value.
Sourcepub fn and_le(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn and_gt(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn and_gt(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical AND
condition for the column greater than a value.
Sourcepub fn and_ge(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn and_in<T, V>(self, col: E::Column, values: V) -> Self
pub fn and_in<T, V>(self, col: E::Column, values: V) -> Self
Adds a logical AND
condition for the column IN
a list of values.
Sourcepub fn and_not_in<T, V>(self, col: E::Column, values: V) -> Self
pub fn and_not_in<T, V>(self, col: E::Column, values: V) -> Self
Adds a logical AND
condition for the column NOT IN
a list of values.
Sourcepub fn and_in_subquery<C, M>(self, cols: C, subquery: QueryBuilder<M>) -> Self
pub fn and_in_subquery<C, M>(self, cols: C, subquery: QueryBuilder<M>) -> Self
Adds a logical AND
condition for the columns IN
a subquery.
Sourcepub fn and_not_in_subquery<C, M>(
self,
cols: C,
subquery: QueryBuilder<M>,
) -> Self
pub fn and_not_in_subquery<C, M>( self, cols: C, subquery: QueryBuilder<M>, ) -> Self
Adds a logical AND
condition for the columns NOT IN
a subquery.
Sourcepub fn and_in_range<T: IntoSqlValue>(
self,
col: E::Column,
min: T,
max: T,
) -> Self
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)
.
Sourcepub fn and_between<T: IntoSqlValue>(
self,
col: E::Column,
min: T,
max: T,
) -> Self
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.
Sourcepub fn and_like(self, col: E::Column, value: String) -> Self
pub fn and_like(self, col: E::Column, value: String) -> Self
Adds a logical AND
condition for the column LIKE
a string value.
Sourcepub fn and_ilike(self, col: E::Column, value: String) -> Self
pub fn and_ilike(self, col: E::Column, value: String) -> Self
Adds a logical AND
condition for the column ILIKE
a string value.
Sourcepub fn and_rlike(self, col: E::Column, value: String) -> Self
pub fn and_rlike(self, col: E::Column, value: String) -> Self
Adds a logical AND
condition for the column RLIKE
a string value.
Sourcepub fn and_contains(self, col: E::Column, value: &str) -> Self
pub fn and_contains(self, col: E::Column, value: &str) -> Self
Adds a logical AND
condition for the column which contains a string value.
Sourcepub fn and_starts_with(self, col: E::Column, value: &str) -> Self
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.
Sourcepub fn and_ends_with(self, col: E::Column, value: &str) -> Self
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.
Sourcepub fn and_null(self, col: E::Column) -> Self
pub fn and_null(self, col: E::Column) -> Self
Adds a logical AND
condition for the column which is null.
Sourcepub fn and_not_null(self, col: E::Column) -> Self
pub fn and_not_null(self, col: E::Column) -> Self
Adds a logical AND
condition for the column which is not null.
Sourcepub fn and_empty(self, col: E::Column) -> Self
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.
Sourcepub fn and_nonempty(self, col: E::Column) -> Self
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.
Sourcepub fn and_overlaps<T: IntoSqlValue>(
self,
cols: (E::Column, E::Column),
values: (T, T),
) -> Self
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.
Sourcepub fn or<M: Entity>(self, other: QueryBuilder<M>) -> Self
pub fn or<M: Entity>(self, other: QueryBuilder<M>) -> Self
Adds a logical OR
condition by merging the other query builder.
Sourcepub fn or_not<M: Entity>(self, other: QueryBuilder<M>) -> Self
pub fn or_not<M: Entity>(self, other: QueryBuilder<M>) -> Self
Adds a logical OR NOT
condition by merging the other query builder.
Sourcepub fn or_filter(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn or_eq(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn or_eq(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical OR
condition for equal parts.
Sourcepub fn or_eq_if_not_null(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn or_eq_if_some<T: IntoSqlValue>(
self,
col: E::Column,
value: Option<T>,
) -> Self
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.
Sourcepub fn or_ne(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn or_ne(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical OR
condition for non-equal parts.
Sourcepub fn or_ne_if_not_null(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn or_ne_if_some<T: IntoSqlValue>(
self,
col: E::Column,
value: Option<T>,
) -> Self
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.
Sourcepub fn or_lt(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn or_lt(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical OR
condition for the column less than a value.
Sourcepub fn or_le(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn or_gt(self, col: E::Column, value: impl IntoSqlValue) -> Self
pub fn or_gt(self, col: E::Column, value: impl IntoSqlValue) -> Self
Adds a logical OR
condition for the column greater than a value.
Sourcepub fn or_ge(self, col: E::Column, value: impl IntoSqlValue) -> Self
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.
Sourcepub fn or_in<T, V>(self, col: E::Column, values: V) -> Self
pub fn or_in<T, V>(self, col: E::Column, values: V) -> Self
Adds a logical OR
condition for the column IN
a list of values.
Sourcepub fn or_not_in<T, V>(self, col: E::Column, values: V) -> Self
pub fn or_not_in<T, V>(self, col: E::Column, values: V) -> Self
Adds a logical OR
condition for the column NOT IN
a list of values.
Sourcepub fn or_in_subquery<C, M>(self, cols: C, subquery: QueryBuilder<M>) -> Self
pub fn or_in_subquery<C, M>(self, cols: C, subquery: QueryBuilder<M>) -> Self
Adds a logical OR
condition for the columns IN
a subquery.
Sourcepub fn or_not_in_subquery<C, M>(
self,
cols: C,
subquery: QueryBuilder<M>,
) -> Self
pub fn or_not_in_subquery<C, M>( self, cols: C, subquery: QueryBuilder<M>, ) -> Self
Adds a logical OR
condition for the columns NOT IN
a subquery.
Sourcepub fn or_in_range<T: IntoSqlValue>(
self,
col: E::Column,
min: T,
max: T,
) -> Self
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)
.
Sourcepub fn or_between<T: IntoSqlValue>(self, col: E::Column, min: T, max: T) -> Self
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.
Sourcepub fn or_like(self, col: E::Column, value: String) -> Self
pub fn or_like(self, col: E::Column, value: String) -> Self
Adds a logical OR
condition for the column LIKE
a string value.
Sourcepub fn or_ilike(self, col: E::Column, value: String) -> Self
pub fn or_ilike(self, col: E::Column, value: String) -> Self
Adds a logical OR
condition for the column ILIKE
a string value.
Sourcepub fn or_rlike(self, col: E::Column, value: String) -> Self
pub fn or_rlike(self, col: E::Column, value: String) -> Self
Adds a logical OR
condition for the column RLIKE
a string value.
Sourcepub fn or_contains(self, col: E::Column, value: &str) -> Self
pub fn or_contains(self, col: E::Column, value: &str) -> Self
Adds a logical OR
condition for the column which contains a string value.
Sourcepub fn or_starts_with(self, col: E::Column, value: &str) -> Self
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.
Sourcepub fn or_ends_with(self, col: E::Column, value: &str) -> Self
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.
Sourcepub fn or_null(self, col: E::Column) -> Self
pub fn or_null(self, col: E::Column) -> Self
Adds a logical OR
condition for the column which is null.
Sourcepub fn or_not_null(self, col: E::Column) -> Self
pub fn or_not_null(self, col: E::Column) -> Self
Adds a logical OR
condition for the column which is not null.
Sourcepub fn or_empty(self, col: E::Column) -> Self
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.
Sourcepub fn or_nonempty(self, col: E::Column) -> Self
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.
Sourcepub fn or_overlaps<T: IntoSqlValue>(
self,
cols: (E::Column, E::Column),
values: (T, T),
) -> Self
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.
Sourcepub fn order_by_with_nulls(
self,
col: impl ToString,
descending: bool,
nulls_first: bool,
) -> Self
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.
Sourcepub fn order_desc(self, col: impl ToString) -> Self
pub fn order_desc(self, col: impl ToString) -> Self
Adds a query order with an descending order.
Source§impl<E: Entity + Schema> QueryBuilder<E>
impl<E: Entity + Schema> QueryBuilder<E>
Sourcepub fn build_subquery(self) -> String
pub fn build_subquery(self) -> String
Builds a subquery SQL expression.
Trait Implementations§
Source§impl<E: Clone + Entity> Clone for QueryBuilder<E>
impl<E: Clone + Entity> Clone for QueryBuilder<E>
Source§fn clone(&self) -> QueryBuilder<E>
fn clone(&self) -> QueryBuilder<E>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<E: Entity> Default for QueryBuilder<E>
impl<E: Entity> Default for QueryBuilder<E>
Source§impl<E: Entity + Schema> IntoSqlValue for QueryBuilder<E>
impl<E: Entity + Schema> IntoSqlValue for QueryBuilder<E>
Source§fn into_sql_value(self) -> JsonValue
fn into_sql_value(self) -> JsonValue
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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