Skip to main content

Column

Struct Column 

Source
pub struct Column<M> { /* private fields */ }
Expand description

A column of model M.

#[derive(Model)] generates one associated constant per field, named after the field in SCREAMING_SNAKE_CASE (User::EMAIL, User::ID, …). Columns are tied to their model, so a Post column can’t accidentally be used to filter Users.

Implementations§

Source§

impl<M> Column<M>

Source

pub const fn new(name: &'static str) -> Column<M>

Create a column handle. Normally generated by #[derive(Model)].

Source

pub const fn name(&self) -> &'static str

The column name in the database.

Source§

impl<M> Column<M>
where M: Model,

Source

pub fn eq(self, value: impl Into<Value>) -> Expr<M>

column = value

Source

pub fn ne(self, value: impl Into<Value>) -> Expr<M>

column <> value

Source

pub fn gt(self, value: impl Into<Value>) -> Expr<M>

column > value

Source

pub fn gte(self, value: impl Into<Value>) -> Expr<M>

column >= value

Source

pub fn lt(self, value: impl Into<Value>) -> Expr<M>

column < value

Source

pub fn lte(self, value: impl Into<Value>) -> Expr<M>

column <= value

Source

pub fn like(self, pattern: impl Into<Value>) -> Expr<M>

column LIKE pattern

Source

pub fn not_like(self, pattern: impl Into<Value>) -> Expr<M>

column NOT LIKE pattern

Source

pub fn ilike(self, pattern: impl Into<Value>) -> Expr<M>

Case-insensitive column ILIKE pattern

Source

pub fn contains(self, text: &str) -> Expr<M>

column LIKE '%text%', escaping %, _ and \ in text.

Source

pub fn starts_with(self, text: &str) -> Expr<M>

column LIKE 'text%', escaping %, _ and \ in text.

Source

pub fn ends_with(self, text: &str) -> Expr<M>

column LIKE '%text', escaping %, _ and \ in text.

Source

pub fn is_in<V>(self, values: impl IntoIterator<Item = V>) -> Expr<M>
where V: Into<Value>,

column IN (…). An empty list matches nothing.

Source

pub fn not_in<V>(self, values: impl IntoIterator<Item = V>) -> Expr<M>
where V: Into<Value>,

column NOT IN (…). An empty list matches everything.

Source

pub fn in_subquery<N>(self, subquery: Projected<N>) -> Expr<M>
where N: Model,

column IN (subquery), where the subquery selects one column.

ⓘ
// Users who wrote a post with more than 100 views.
User::filter(User::ID.in_subquery(
    Post::filter(Post::VIEWS.gt(100)).select(Post::AUTHOR_ID),
))
Source

pub fn not_in_subquery<N>(self, subquery: Projected<N>) -> Expr<M>
where N: Model,

column NOT IN (subquery). Beware that NOT IN matches nothing if the subquery returns any NULL; prefer Expr::not_exists then.

Source

pub fn eq_outer<O>(self, outer: Column<O>) -> Expr<M>
where O: Model, M: Model,

Compare this column to a column of the enclosing query’s model, for correlated subqueries: "posts"."author_id" = "users"."id".

ⓘ
// Users with at least one post.
User::filter(Expr::exists(Post::filter(Post::AUTHOR_ID.eq_outer(User::ID))))
Source

pub fn between(self, low: impl Into<Value>, high: impl Into<Value>) -> Expr<M>

column BETWEEN low AND high

Source

pub fn is_null(self) -> Expr<M>

column IS NULL

Source

pub fn is_not_null(self) -> Expr<M>

column IS NOT NULL

Source

pub fn eq_any<T>(self, values: impl IntoIterator<Item = T>) -> Expr<M>
where Vec<T>: Into<Value>,

column = ANY($1) with a single array parameter — like is_in, but one parameter however many values, so it suits very large lists.

Source

pub fn array_contains(self, values: impl Into<Value>) -> Expr<M>

Array column contains every element of values (column @> $1).

Source

pub fn array_overlaps(self, values: impl Into<Value>) -> Expr<M>

Array column shares at least one element with values (column && $1).

Source

pub fn array_has(self, value: impl Into<Value>) -> Expr<M>

Array column contains value ($1 = ANY(column)).

Source

pub fn json_has_key(self, key: &str) -> Expr<M>

JSONB column has the top-level key key (column ? $1).

Source

pub fn json_has_any_key<K>(self, keys: impl IntoIterator<Item = K>) -> Expr<M>
where K: Into<String>,

JSONB column has any of keys (column ?| $1).

Source

pub fn json_has_all_keys<K>(self, keys: impl IntoIterator<Item = K>) -> Expr<M>
where K: Into<String>,

JSONB column has all of keys (column ?& $1).

Source

pub fn json_contains(self, json: Value) -> Expr<M>

JSONB column contains json (column @> $1), e.g. User::PREFS.json_contains(json!({"theme": "dark"})).

Source

pub fn json_text(self, key: &str) -> Projection<M>

The text value at key of a JSON/JSONB column (column ->> $1), to filter, select or order by: User::PREFS.json_text("theme").eq("dark").

Source

pub fn json_path_text<K>( self, path: impl IntoIterator<Item = K>, ) -> Projection<M>
where K: Into<String>,

The text value at a nested path (column #>> $1): Event::DATA.json_path_text(["user", "id"]).

Source

pub fn search(self, query: &str) -> Expr<M>

Full-text search on a text column with the server’s default text search configuration: to_tsvector(column) @@ websearch_to_tsquery($1).

query uses web-search syntax: rust -java "query builder" or orm. For speed, index to_tsvector(column) or use a generated tsvector column with ts_matches.

Source

pub fn search_in(self, config: &str, query: &str) -> Expr<M>

search with an explicit configuration such as "english" (enables stemming and stop words).

Source

pub fn ts_matches(self, query: &str) -> Expr<M>

Match a tsvector column: column @@ websearch_to_tsquery($1).

Source

pub fn search_rank(self, query: &str) -> Projection<M>

Relevance of a text column for query, to order results: .order_by(Post::BODY.search_rank("rust orm").desc()).

Source

pub fn count(self) -> Projection<M>

COUNT(column) — number of non-NULL values.

Source

pub fn count_distinct(self) -> Projection<M>

COUNT(DISTINCT column)

Source

pub fn sum(self) -> Projection<M>

SUM(column). PostgreSQL returns BIGINT for SMALLINT/INTEGER columns and NUMERIC for BIGINT columns; use Projection::cast to pick the Rust type you decode into.

Source

pub fn avg(self) -> Projection<M>

AVG(column), returned as NUMERIC for integer columns; see Projection::cast.

Source

pub fn min(self) -> Projection<M>

MIN(column)

Source

pub fn max(self) -> Projection<M>

MAX(column)

Source

pub fn asc(self) -> Order<M>

Ascending ordering on this column.

Source

pub fn desc(self) -> Order<M>

Descending ordering on this column.

Source§

impl<To> Column<To>
where To: Model,

Source

pub fn on<From>(self, other: Column<From>) -> JoinOn<From, To>
where From: Model,

An ad-hoc join condition: join To where this column equals other (a column of a model already in the query).

ⓘ
Post::query().join(Category::ID.on(Post::CATEGORY_ID))

Trait Implementations§

Source§

impl<M> Clone for Column<M>

Source§

fn clone(&self) -> Column<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> Copy for Column<M>

Source§

impl<M> Debug for Column<M>

Source§

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

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

impl<M> Display for Column<M>

Source§

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

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

impl<M> From<Column<M>> for Projection<M>
where M: Model,

Source§

fn from(column: Column<M>) -> Projection<M>

Converts to this type from the input type.
Source§

impl<M> From<Column<M>> for Order<M>
where M: Model,

Source§

fn from(column: Column<M>) -> Order<M>

Converts to this type from the input type.
Source§

impl<M> IntoProjections<M> for Column<M>
where M: Model,

Source§

fn into_projections(self) -> Vec<Projection<M>>

Convert into the list of selected expressions.
Source§

impl<X> ScopedItem for Column<X>
where X: Model,

Source§

type Model = X

The model the item belongs to.

Auto Trait Implementations§

§

impl<M> Freeze for Column<M>
where PhantomData<fn() -> M>: Freeze,

§

impl<M> RefUnwindSafe for Column<M>
where PhantomData<fn() -> M>: RefUnwindSafe,

§

impl<M> Send for Column<M>
where PhantomData<fn() -> M>: Send,

§

impl<M> Sync for Column<M>
where PhantomData<fn() -> M>: Sync,

§

impl<M> Unpin for Column<M>
where PhantomData<fn() -> M>: Unpin,

§

impl<M> UnsafeUnpin for Column<M>
where PhantomData<fn() -> M>: UnsafeUnpin,

§

impl<M> UnwindSafe for Column<M>
where PhantomData<fn() -> M>: 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<M> InScope<(M, ()), Here> for M

Source§

impl<M, Rest, N> InScope<(M, (Rest, N)), Last> for N

Source§

impl<X, M, Rest, N, I> InScope<(M, (Rest, N)), There<I>> for X
where X: InScope<(M, Rest), I>,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<S, I, A> ScopedProjections<S, (I,)> for A
where A: ScopedItem, <A as ScopedItem>::Model: InScope<S, I>,

Source§

fn into_items<R>(self) -> Vec<Projection<R>>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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