Skip to main content

Relation

Struct Relation 

Source
pub struct Relation<T: Record> { /* private fields */ }
Expand description

A chainable query builder for Record types.

Implementations§

Source§

impl<T: Record> Relation<T>

Source

pub fn new() -> Self

Creates an empty relation.

Source

pub fn where(self, conditions: HashMap<String, JsonValue>) -> Self

Adds equality conditions to the relation.

Source

pub fn order(self, column: &str, dir: OrderDirection) -> Self

Adds an order clause to the relation.

Source

pub fn group(self, column: &str) -> Self

Groups results by the specified column.

Source

pub fn having(self, condition: &str) -> Self

Adds a HAVING condition (used after group).

Source

pub fn distinct(self) -> Self

Marks the query to return distinct results only.

Source

pub fn select_columns(self, columns: &[&str]) -> Self

Restricts which columns are selected.

Source

pub fn select(self, columns: &[&str]) -> Self

Backwards-compatible alias for Self::select_columns.

Source

pub fn joins(self, association: &str) -> Self

Stores a join declaration (implementation pending full association support).

Source

pub fn includes(self, association: &str) -> Self

Stores an eager-load declaration (implementation pending).

Source

pub fn reorder(self, column: &str, dir: OrderDirection) -> Self

Replaces the current ordering.

Source

pub fn reselect(self, columns: &[&str]) -> Self

Replaces the current column selection.

Source

pub fn rewhere(self, conditions: HashMap<String, JsonValue>) -> Self

Replaces the current where scope.

Source

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

Limits the number of returned rows.

Source

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

Skips the first n rows.

Source

pub fn not_where(self, conditions: HashMap<String, JsonValue>) -> Self

Adds negated equality conditions to the relation.

Source

pub fn not(self, conditions: HashMap<String, JsonValue>) -> Self

Backwards-compatible alias for Self::not_where.

Source

pub fn or_where(self, conditions: HashMap<String, JsonValue>) -> Self

Combines an additional where scope with OR semantics.

Source

pub async fn load(&self, db: &DatabaseConnection) -> Result<Vec<T>, RecordError>

Executes the relation and returns all matching records.

Source

pub fn load_sync(&self) -> Result<Vec<T>, RecordError>

Synchronous wrapper for Self::load.

Source

pub async fn find_each<F>( &self, batch_size: u64, db: &DatabaseConnection, f: F, ) -> Result<(), RecordError>

Iterates over records in batches, calling the closure for each record.

Source

pub fn find_each_sync<F>( &self, batch_size: u64, f: F, ) -> Result<(), RecordError>

Synchronous wrapper for Self::find_each.

Source

pub async fn find_in_batches<F>( &self, batch_size: u64, db: &DatabaseConnection, f: F, ) -> Result<(), RecordError>

Iterates over matching records in batches, calling the closure for each batch.

Source

pub fn find_in_batches_sync<F>( &self, batch_size: u64, f: F, ) -> Result<(), RecordError>

Synchronous wrapper for Self::find_in_batches.

Source

pub async fn first( &self, db: &DatabaseConnection, ) -> Result<Option<T>, RecordError>

Executes the relation and returns the first matching record.

Source

pub fn first_sync(&self) -> Result<Option<T>, RecordError>

Synchronous wrapper for Self::first.

Source

pub async fn last( &self, db: &DatabaseConnection, ) -> Result<Option<T>, RecordError>

Executes the relation and returns the last matching record within the current scope.

Source

pub fn last_sync(&self) -> Result<Option<T>, RecordError>

Synchronous wrapper for Self::last.

Source

pub async fn count(&self, db: &DatabaseConnection) -> Result<u64, RecordError>

Counts matching rows.

Source

pub fn count_sync(&self) -> Result<u64, RecordError>

Synchronous wrapper for Self::count.

Source

pub async fn exists(&self, db: &DatabaseConnection) -> Result<bool, RecordError>

Returns true when the relation matches at least one row.

Source

pub fn exists_sync(&self) -> Result<bool, RecordError>

Synchronous wrapper for Self::exists.

Source

pub async fn sole(&self, db: &DatabaseConnection) -> Result<T, RecordError>

Returns the only matching record.

Source

pub fn sole_sync(&self) -> Result<T, RecordError>

Synchronous wrapper for Self::sole.

Source

pub async fn explain( &self, db: &DatabaseConnection, ) -> Result<String, RecordError>

Returns the database query plan as a string.

Source

pub fn explain_sync(&self) -> Result<String, RecordError>

Synchronous wrapper for Self::explain.

Source

pub async fn sum( &self, column: &str, db: &DatabaseConnection, ) -> Result<f64, RecordError>

Returns the sum of the specified column.

Source

pub fn sum_sync(&self, column: &str) -> Result<f64, RecordError>

Synchronous wrapper for Self::sum.

Source

pub async fn average( &self, column: &str, db: &DatabaseConnection, ) -> Result<f64, RecordError>

Returns the average of the specified column.

Source

pub fn average_sync(&self, column: &str) -> Result<f64, RecordError>

Synchronous wrapper for Self::average.

Source

pub async fn minimum( &self, column: &str, db: &DatabaseConnection, ) -> Result<Option<JsonValue>, RecordError>

Returns the minimum value of the specified column.

Source

pub fn minimum_sync( &self, column: &str, ) -> Result<Option<JsonValue>, RecordError>

Synchronous wrapper for Self::minimum.

Source

pub async fn maximum( &self, column: &str, db: &DatabaseConnection, ) -> Result<Option<JsonValue>, RecordError>

Returns the maximum value of the specified column.

Source

pub fn maximum_sync( &self, column: &str, ) -> Result<Option<JsonValue>, RecordError>

Synchronous wrapper for Self::maximum.

Source

pub async fn group_count( &self, db: &DatabaseConnection, ) -> Result<HashMap<JsonValue, JsonValue>, RecordError>

Returns grouped counts for the first configured group column.

Source

pub async fn group_sum( &self, column: &str, db: &DatabaseConnection, ) -> Result<HashMap<JsonValue, JsonValue>, RecordError>

Returns grouped sums for the first configured group column.

Source

pub async fn group_average( &self, column: &str, db: &DatabaseConnection, ) -> Result<HashMap<JsonValue, JsonValue>, RecordError>

Returns grouped averages for the first configured group column.

Source

pub async fn group_minimum( &self, column: &str, db: &DatabaseConnection, ) -> Result<HashMap<JsonValue, JsonValue>, RecordError>

Returns grouped minimum values for the first configured group column.

Source

pub async fn group_maximum( &self, column: &str, db: &DatabaseConnection, ) -> Result<HashMap<JsonValue, JsonValue>, RecordError>

Returns grouped maximum values for the first configured group column.

Source

pub async fn pluck_columns( &self, columns: &[&str], db: &DatabaseConnection, ) -> Result<Vec<Vec<JsonValue>>, RecordError>

Plucks the values of the specified columns into row vectors.

Source

pub fn pluck_columns_sync( &self, columns: &[&str], ) -> Result<Vec<Vec<JsonValue>>, RecordError>

Synchronous wrapper for Self::pluck_columns.

Source

pub async fn pluck( &self, column: &str, db: &DatabaseConnection, ) -> Result<Vec<JsonValue>, RecordError>

Plucks the values of the specified column into a Vec.

Source

pub fn pluck_sync(&self, column: &str) -> Result<Vec<JsonValue>, RecordError>

Synchronous wrapper for Self::pluck.

Source

pub async fn pick( &self, column: &str, db: &DatabaseConnection, ) -> Result<Option<JsonValue>, RecordError>

Returns the first value of the specified column.

Source

pub fn pick_sync(&self, column: &str) -> Result<Option<JsonValue>, RecordError>

Synchronous wrapper for Self::pick.

Source

pub async fn ids( &self, db: &DatabaseConnection, ) -> Result<Vec<i64>, RecordError>

Returns all primary key values.

Source

pub fn ids_sync(&self) -> Result<Vec<i64>, RecordError>

Synchronous wrapper for Self::ids.

Trait Implementations§

Source§

impl<T: Record> Clone for Relation<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 + Record> Debug for Relation<T>

Source§

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

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

impl<T: Record> Default for Relation<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for Relation<T>

§

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

§

impl<T> Send for Relation<T>

§

impl<T> Sync for Relation<T>

§

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

§

impl<T> UnsafeUnpin for Relation<T>

§

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