Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<M>
where M: Model,
{ /* private fields */ }

Implementations§

Source§

impl<M> QueryBuilder<M>
where M: Model + for<'r> FromRow<'r, PgRow>,

Source

pub fn new() -> QueryBuilder<M>

Source

pub fn select_only(self, columns: &[&str]) -> QueryBuilder<M>

Source

pub fn distinct(self) -> QueryBuilder<M>

Source

pub fn where_eq<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn or_where_eq<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_ne<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn or_where_ne<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_gt<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn or_where_gt<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_gte<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn or_where_gte<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_lt<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn or_where_lt<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_lte<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn or_where_lte<T>(self, column: Column<M, T>, value: T) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_in<T, I>(self, column: Column<M, T>, values: I) -> QueryBuilder<M>
where T: Into<Value>, I: IntoIterator<Item = T>,

Source

pub fn or_where_in<T, I>( self, column: Column<M, T>, values: I, ) -> QueryBuilder<M>
where T: Into<Value>, I: IntoIterator<Item = T>,

Source

pub fn where_not_in<T, I>( self, column: Column<M, T>, values: I, ) -> QueryBuilder<M>
where T: Into<Value>, I: IntoIterator<Item = T>,

Source

pub fn or_where_not_in<T, I>( self, column: Column<M, T>, values: I, ) -> QueryBuilder<M>
where T: Into<Value>, I: IntoIterator<Item = T>,

Source

pub fn where_null<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn or_where_null<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn where_not_null<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn or_where_not_null<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn where_between<T>( self, column: Column<M, T>, low: T, high: T, ) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn or_where_between<T>( self, column: Column<M, T>, low: T, high: T, ) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_not_between<T>( self, column: Column<M, T>, low: T, high: T, ) -> QueryBuilder<M>
where T: Into<Value>,

Source

pub fn where_like( self, column: Column<M, String>, pattern: impl Into<String>, ) -> QueryBuilder<M>

Source

pub fn or_where_like( self, column: Column<M, String>, pattern: impl Into<String>, ) -> QueryBuilder<M>

Source

pub fn where_not_like( self, column: Column<M, String>, pattern: impl Into<String>, ) -> QueryBuilder<M>

Source

pub fn where_column<T>( self, a: Column<M, T>, b: Column<M, T>, ) -> QueryBuilder<M>

Source

pub fn where_raw(self, raw: SimpleExpr) -> QueryBuilder<M>

Source

pub fn or_where_raw(self, raw: SimpleExpr) -> QueryBuilder<M>

Source

pub fn where_sql(self, sql: impl Into<String>) -> QueryBuilder<M>

Source

pub fn or_where_sql(self, sql: impl Into<String>) -> QueryBuilder<M>

Source

pub fn join( self, table: &str, left_column: &str, right_column: &str, ) -> QueryBuilder<M>

INNER JOIN table ON left_column = right_column. Mirrors Eloquent’s join. Columns are passed as fully-qualified strings (e.g. "users.id").

Source

pub fn left_join( self, table: &str, left_column: &str, right_column: &str, ) -> QueryBuilder<M>

LEFT JOIN table ON ....

Source

pub fn right_join( self, table: &str, left_column: &str, right_column: &str, ) -> QueryBuilder<M>

RIGHT JOIN table ON ....

Source

pub fn cross_join(self, table: &str) -> QueryBuilder<M>

CROSS JOIN table (no ON clause).

Source

pub fn group_by<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn group_by_raw(self, raw: impl Into<String>) -> QueryBuilder<M>

GROUP BY raw_sql.

Source

pub fn having(self, expr: SimpleExpr) -> QueryBuilder<M>

Source

pub fn having_raw(self, sql: impl Into<String>) -> QueryBuilder<M>

Source

pub fn with_trashed(self) -> QueryBuilder<M>

Include soft-deleted rows. Eloquent’s ->withTrashed().

Source

pub fn only_trashed(self) -> QueryBuilder<M>

Only soft-deleted rows. Eloquent’s ->onlyTrashed().

Source

pub fn without_trashed(self) -> QueryBuilder<M>

Explicitly exclude soft-deleted rows (this is the default for models with #[soft_deletes]). Eloquent’s ->withoutTrashed().

Source

pub fn where_has<R, F>(self, _rel: R, f: F) -> QueryBuilder<M>
where R: RelationDef<Parent = M>, <R as RelationDef>::Child: Model + for<'r> FromRow<'r, PgRow>, F: FnOnce(QueryBuilder<<R as RelationDef>::Child>) -> QueryBuilder<<R as RelationDef>::Child>,

Filter parent rows by the existence of related child rows. Mirrors Eloquent’s ->whereHas('posts', fn ($q) => $q->where(...)).

Emits WHERE EXISTS (SELECT 1 FROM child WHERE child.fk = parent.lk AND <closure conditions>).

User::query()
    .where_has(User::posts_rel(), |q| q.where_eq(Post::columns().published(), true))
    .get(pool).await?;
Source

pub fn where_doesnt_have<R, F>(self, _rel: R, f: F) -> QueryBuilder<M>
where R: RelationDef<Parent = M>, <R as RelationDef>::Child: Model + for<'r> FromRow<'r, PgRow>, F: FnOnce(QueryBuilder<<R as RelationDef>::Child>) -> QueryBuilder<<R as RelationDef>::Child>,

Negated form of where_has. Mirrors Eloquent’s ->whereDoesntHave(...).

Source

pub fn or_where_has<R, F>(self, _rel: R, f: F) -> QueryBuilder<M>
where R: RelationDef<Parent = M>, <R as RelationDef>::Child: Model + for<'r> FromRow<'r, PgRow>, F: FnOnce(QueryBuilder<<R as RelationDef>::Child>) -> QueryBuilder<<R as RelationDef>::Child>,

OR-combined where_has.

Source

pub async fn paginate( self, per_page: u64, page: u64, pool: &Pool<Postgres>, ) -> Result<Paginator<M>, Error>

Pagination. Mirrors Eloquent’s ->paginate($perPage, ['*'], 'page', $page).

Source

pub async fn with_count_of<R>( self, _rel: R, pool: &Pool<Postgres>, ) -> Result<Vec<(M, i64)>, Error>
where R: RelationDef<Parent = M>, <R as RelationDef>::Child: Model,

Fetch the parent rows along with a related-row count. Mirrors Eloquent’s ->withCount('posts'). Returns Vec<(M, i64)> instead of dynamic attributes.

let users_with_counts: Vec<(User, i64)> = User::query()
    .with_count_of(User::posts_rel(), pool)
    .await?;
Source

pub fn order_by<T>( self, column: Column<M, T>, ascending: bool, ) -> QueryBuilder<M>

Source

pub fn order_by_asc<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn order_by_desc<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn latest(self) -> QueryBuilder<M>

Source

pub fn oldest(self) -> QueryBuilder<M>

Source

pub fn latest_by<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn oldest_by<T>(self, column: Column<M, T>) -> QueryBuilder<M>

Source

pub fn in_random_order(self) -> QueryBuilder<M>

Source

pub fn reorder(self) -> QueryBuilder<M>

Source

pub fn limit(self, n: u64) -> QueryBuilder<M>

Source

pub fn take(self, n: u64) -> QueryBuilder<M>

Source

pub fn offset(self, n: u64) -> QueryBuilder<M>

Source

pub fn skip(self, n: u64) -> QueryBuilder<M>

Source

pub async fn get(self, pool: &Pool<Postgres>) -> Result<Vec<M>, Error>

Source

pub async fn first(self, pool: &Pool<Postgres>) -> Result<Option<M>, Error>

Source

pub async fn first_or_fail(self, pool: &Pool<Postgres>) -> Result<M, Error>

Source

pub async fn pluck<T>( self, column: Column<M, T>, pool: &Pool<Postgres>, ) -> Result<Vec<T>, Error>
where T: for<'r> Decode<'r, Postgres> + Type<Postgres> + Send + Unpin,

Source

pub async fn value<T>( self, column: Column<M, T>, pool: &Pool<Postgres>, ) -> Result<Option<T>, Error>
where T: for<'r> Decode<'r, Postgres> + Type<Postgres> + Send + Unpin,

Source

pub async fn count(self, pool: &Pool<Postgres>) -> Result<i64, Error>

Source

pub async fn min<T>( self, column: Column<M, T>, pool: &Pool<Postgres>, ) -> Result<Option<T>, Error>
where T: for<'r> Decode<'r, Postgres> + Type<Postgres> + Send + Unpin,

Source

pub async fn max<T>( self, column: Column<M, T>, pool: &Pool<Postgres>, ) -> Result<Option<T>, Error>
where T: for<'r> Decode<'r, Postgres> + Type<Postgres> + Send + Unpin,

Source

pub async fn sum<T>( self, column: Column<M, T>, pool: &Pool<Postgres>, ) -> Result<i64, Error>

Source

pub async fn avg<T>( self, column: Column<M, T>, pool: &Pool<Postgres>, ) -> Result<Option<f64>, Error>

Source

pub async fn exists(self, pool: &Pool<Postgres>) -> Result<bool, Error>

Source

pub async fn doesnt_exist(self, pool: &Pool<Postgres>) -> Result<bool, Error>

Trait Implementations§

Source§

impl<M> Clone for QueryBuilder<M>
where M: Model,

Source§

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

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<M> Default for QueryBuilder<M>
where M: Model,

Source§

fn default() -> QueryBuilder<M>

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

Auto Trait Implementations§

§

impl<M> Freeze for QueryBuilder<M>

§

impl<M> !RefUnwindSafe for QueryBuilder<M>

§

impl<M> Send for QueryBuilder<M>

§

impl<M> Sync for QueryBuilder<M>

§

impl<M> Unpin for QueryBuilder<M>

§

impl<M> UnsafeUnpin for QueryBuilder<M>

§

impl<M> !UnwindSafe for QueryBuilder<M>

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