Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<T> { /* private fields */ }

Implementations§

Source§

impl<T> QueryBuilder<T>
where T: Send + Sync + Unpin + 'static,

Source

pub fn new(table: impl Into<String>, pk: impl Into<String>) -> Self

Source

pub fn with_soft_delete_col(self, col: impl Into<String>) -> Self

Устанавливает имя колонки soft-delete. Если установлено, автоматически добавляет WHERE col IS NULL.

Source

pub fn filter<F>(self, f: F) -> Self
where F: FnOnce(&T::Columns) -> FilterExpr, T: HasColumns,

Добавляет условие фильтрации (AND).

Source

pub fn filter_raw(self, sql: impl Into<String>) -> Self

Добавляет сырое WHERE условие.

Source

pub fn filter_if<F>(self, condition: bool, f: F) -> Self
where F: FnOnce(&T::Columns) -> FilterExpr, T: HasColumns,

Условный фильтр — добавляется только если condition == true.

Source

pub fn apply(self, scope: Scope<T>) -> Self

Применяет scope к текущему builder’у.

Source

pub fn with_deleted(self) -> Self

Включить удалённые записи.

Source

pub fn only_deleted(self) -> Self

Только удалённые записи.

Source

pub fn select_cols( self, cols: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Выбрать конкретные колонки.

Source

pub fn select_distinct_col(self, col: impl Into<String>) -> Self

Source

pub fn inner_join(self, table: impl Into<String>, on: impl Into<String>) -> Self

INNER JOIN таблицы с ON-выражением.

Source

pub fn left_join(self, table: impl Into<String>, on: impl Into<String>) -> Self

LEFT JOIN.

Source

pub fn group_by_col(self, col: impl Into<String>) -> Self

Source

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

Source

pub fn order_by<F>(self, f: F) -> Self
where F: FnOnce(&T::Columns) -> OrderExpr, T: HasColumns,

Source

pub fn order_by_raw(self, sql: impl Into<String>) -> Self

Source

pub fn order_by_random(self) -> Self

Source

pub fn limit(self, n: i64) -> Self

Source

pub fn offset(self, n: i64) -> Self

Source

pub fn paginate(self, page: i64, per_page: i64) -> Self

Выставляет limit/offset для страницы page (с 1), per_page записей.

Source

pub fn build_select(&self) -> (String, Vec<SqlValue>)

Строит финальный SELECT SQL и список bind-значений.

Source

pub async fn fetch_all(self, pool: &PgPool) -> OrmResult<Vec<T>>
where T: for<'r> FromRow<'r, PgRow>,

Выполняет запрос и возвращает Vec<T>.

Source

pub async fn first(self, pool: &PgPool) -> OrmResult<Option<T>>
where T: for<'r> FromRow<'r, PgRow>,

Первая запись или None.

Source

pub async fn first_or_fail(self, pool: &PgPool) -> OrmResult<T>
where T: for<'r> FromRow<'r, PgRow>,

Первая или OrmError::NotFound.

Source

pub async fn last(self, pool: &PgPool) -> OrmResult<Option<T>>
where T: for<'r> FromRow<'r, PgRow>,

Последняя запись (обратный порядок по PK).

Source

pub async fn count(self, pool: &PgPool) -> OrmResult<i64>

Количество записей, удовлетворяющих фильтру.

Source

pub async fn exists(self, pool: &PgPool) -> OrmResult<bool>

Существует ли хоть одна запись по фильтру.

Source

pub async fn fetch_page( self, page: i64, per_page: i64, pool: &PgPool, ) -> OrmResult<Page<T>>
where T: for<'r> FromRow<'r, PgRow>,

Пагинированный результат.

Source

pub async fn update_all<F>(self, f: F, pool: &PgPool) -> OrmResult<u64>
where F: FnOnce(&mut UpdateBuilder<T>), T: HasColumns,

UPDATE table SET ... WHERE <накопленные фильтры> — возвращает кол-во строк.

Source

pub async fn delete_all(self, pool: &PgPool) -> OrmResult<u64>

DELETE FROM table WHERE <накопленные фильтры> — возвращает кол-во строк.

Trait Implementations§

Source§

impl<T> Clone for QueryBuilder<T>

Source§

fn clone(&self) -> Self

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<T: Debug> Debug for QueryBuilder<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for QueryBuilder<T>

§

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

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for QueryBuilder<T>

§

impl<T> UnwindSafe for QueryBuilder<T>
where 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> 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