ModelQuery

Struct ModelQuery 

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

A trait for filtering queries

This allows you to filter, limit, offset, and order elements that you are querying.

§Example

User::select()
    .filter(User::id.eq(1) & User::name.like("%test%"))
    .limit(1)
    .order(User::id.desc())
    .exec(&conn).unwrap();

Implementations§

Source§

impl<M: Model> ModelQuery<M>

Source

pub fn select() -> Self

Source

pub fn count() -> ModelQuery<CountQuery>

Source§

impl<M> ModelQuery<M>

Source

pub fn combine(self, query: String, params: Vec<Box<dyn ToSql>>) -> Self

Source

pub fn filter(self, filter: impl ModelQueryFilter) -> Self

Filter the query with the given filter

§Arguments
  • filter - The filter to apply to the query
§Example
let user = User::select()
    .filter(User::id.eq(1) & User::name.like("%test%"))
    .exec(&conn).unwrap();
Source

pub fn with_id(self, id: i64) -> Self

Select element by id

§Arguments
  • id - The id of the element to select
§Example
let user = User::select()
    .with_id(1)
    .exec(&conn).unwrap();
§Note

This is equivalent to .filter(User::id.eq(id)).limit(1) and should not be combined with other filters or limits.

Source

pub fn limit(self, limit: u32) -> Self

Limit the number of elements returned

§Arguments
  • limit - The maximum number of elements to return
§Example
let users = User::select()
    .limit(10)
    .exec(&conn).unwrap();
Source

pub fn offset(self, offset: u32) -> Self

Offset selection by the given number of elements

§Arguments
  • offset - The number of elements to skip
§Example
let users = User::select()
    .offset(10)
    .exec(&conn).unwrap();
Source

pub fn order_by(self, order: ColumnQueryOrder) -> Self

Order the elements by the given order

§Arguments
  • order - The order to apply to the elements
§Example
let users = User::select()
    .order(User::id.desc())
    .exec(&conn).unwrap();
Source

pub fn join_relation(self, relation: Column<'static>) -> Self

WARNING: This is highly experimental and may not work as expected Use Relation::get() or Relation::take() instead

§Arguments
  • relation - The relation to join
§Example
let users = Post::select()
    .join_relation(Post::author)
    .exec(&conn).unwrap();
Source

pub fn columns(self, columns: &[Column<'static>]) -> Self

Select only the given columns (do not use this if you want to map to a model column which is not an Option<T>)

§Arguments
  • columns - The columns to select
§Example
let users = User::select()
    .columns(&[User::id, User::name])
    .exec(&conn).unwrap();

Trait Implementations§

Source§

impl<M: Model> ColumnInQuery for ModelQuery<M>

Source§

impl<M: Model> Debug for ModelQuery<M>

Source§

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

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

impl<M> Default for ModelQuery<M>

Source§

fn default() -> Self

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

impl<M: Model> Executable<Vec<M>> for ModelQuery<M>

Source§

fn exec(self, conn: &Connection) -> Result<Vec<M>, Error>

Source§

impl Executable<usize> for ModelQuery<CountQuery>

Source§

fn exec(self, conn: &Connection) -> Result<usize, Error>

Source§

impl<M: Model> Queryable<Vec<M>> for ModelQuery<M>

Source§

fn get_query(&mut self) -> RawQuery

Source§

fn parse_result(&mut self, rows: Rows<'_>) -> Vec<M>

Source§

fn should_execute(&self) -> bool

Source§

impl Queryable<usize> for ModelQuery<CountQuery>

Source§

fn get_query(&mut self) -> RawQuery

Source§

fn parse_result(&mut self, rows: Rows<'_>) -> usize

Source§

fn should_execute(&self) -> bool

Auto Trait Implementations§

§

impl<M> Freeze for ModelQuery<M>

§

impl<M> !RefUnwindSafe for ModelQuery<M>

§

impl<M> !Send for ModelQuery<M>

§

impl<M> !Sync for ModelQuery<M>

§

impl<M> Unpin for ModelQuery<M>
where M: Unpin,

§

impl<M> !UnwindSafe for ModelQuery<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, Q0> IntoQueryable<T> for Q0
where Q0: Queryable<T>,

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.