Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder { /* private fields */ }
Expand description

A statement being assembled.

Implementations§

Source§

impl QueryBuilder

Source

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

Source

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

Choose columns. Without this the query selects everything.

Source

pub fn distinct(self) -> Self

Source

pub fn filter(self, column: &str, value: impl Into<Value>) -> Self

where column = value.

Source

pub fn filter_op( self, column: &str, operator: &str, value: impl Into<Value>, ) -> Self

where column <operator> value, with the operator checked against a list — an operator cannot be smuggled in from user input.

Source

pub fn or_filter(self, column: &str, value: impl Into<Value>) -> Self

Source

pub fn or_filter_op( self, column: &str, operator: &str, value: impl Into<Value>, ) -> Self

or column <operator> value.

The or_ half of filter_op, which was missing: or_filter could only ever mean equality, so a search across three columns with like had no way to say so.

Source

pub fn or_filter_like(self, column: &str, pattern: impl Into<Value>) -> Self

or column like pattern.

Source

pub fn filter_in(self, column: &str, values: Vec<Value>) -> Self

Source

pub fn filter_not_in(self, column: &str, values: Vec<Value>) -> Self

Source

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

Source

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

Source

pub fn filter_between( self, column: &str, low: impl Into<Value>, high: impl Into<Value>, ) -> Self

Source

pub fn filter_like(self, column: &str, pattern: impl Into<Value>) -> Self

where column like pattern.

Source

pub fn group_filter( self, build: impl FnOnce(QueryBuilder) -> QueryBuilder, ) -> Self

A parenthesised group of conditions.

Source

pub fn or_group_filter( self, build: impl FnOnce(QueryBuilder) -> QueryBuilder, ) -> Self

Source

pub fn join(self, table: &str, left: &str, operator: &str, right: &str) -> Self

Source

pub fn left_join( self, table: &str, left: &str, operator: &str, right: &str, ) -> Self

Source

pub fn order_by(self, column: &str, direction: Direction) -> Self

Source

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

Source

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

Source

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

Source

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

Source

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

Limit and offset for a 1-based page.

Source

pub fn to_sql(&self, dialect: &dyn Dialect) -> Result<(String, Vec<Value>)>

Build the select statement and its parameters for one database.

The dialect supplies the quoting, the placeholders and the paging syntax, so the same builder chain is correct on PostgreSQL, MySQL and SQL Server without the caller knowing which is underneath.

Source

pub fn to_count_sql( &self, dialect: &dyn Dialect, ) -> Result<(String, Vec<Value>)>

The select count(*) form of this query, ignoring order and paging.

Source

pub async fn get(&self, db: &Database) -> Result<Vec<Row>>

Run the query and return every matching row.

Source

pub async fn first(&self, db: &Database) -> Result<Option<Row>>

The first matching row, if any.

Source

pub async fn get_json(&self, db: &Database) -> Result<Json>

Rows as a JSON array, ready to return from an API handler.

Source

pub async fn count(&self, db: &Database) -> Result<i64>

Source

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

Source

pub async fn insert( &self, db: &Database, values: &[(&str, Value)], ) -> Result<i64>

Insert one row, returning its id.

Source

pub async fn insert_returning( &self, db: &Database, values: &[(&str, Value)], column: &str, ) -> Result<Value>

Insert one row and return one column of it — how a model picks up the key the database generated.

Source

pub async fn insert_without_id( &self, db: &Database, values: &[(&str, Value)], ) -> Result<u64>

Insert one row without asking for a generated key.

Source

pub async fn insert_many( &self, db: &Database, rows: &[Vec<(String, Value)>], ) -> Result<u64>

Insert many rows in one statement.

Source

pub async fn update( &self, db: &Database, values: &[(&str, Value)], ) -> Result<u64>

Update the rows this query matches. Requires a filter.

Source

pub async fn delete(&self, db: &Database) -> Result<u64>

Delete the rows this query matches. Requires a filter.

Source

pub async fn delete_all(&self, db: &Database) -> Result<u64>

Delete every row in the table. Deliberately separate from delete.

Source

pub async fn get_in(&self, tx: &mut Transaction) -> Result<Vec<Row>>

get, inside a transaction.

Source

pub async fn first_in(&self, tx: &mut Transaction) -> Result<Option<Row>>

first, inside a transaction.

Source

pub async fn count_in(&self, tx: &mut Transaction) -> Result<i64>

count, inside a transaction.

Source

pub async fn insert_in( &self, tx: &mut Transaction, values: &[(&str, Value)], ) -> Result<u64>

insert_without_id, inside a transaction.

Source

pub async fn update_in( &self, tx: &mut Transaction, values: &[(&str, Value)], ) -> Result<u64>

update, inside a transaction. The row count it returns is what a compare-and-set reads: filter("balance", …) and zero rows means somebody else moved first.

Source

pub async fn delete_in(&self, tx: &mut Transaction) -> Result<u64>

delete, inside a transaction.

Source§

impl QueryBuilder

Source

pub async fn paginate( &self, db: &Database, page: i64, per_page: i64, ) -> Result<Page>

Fetch one page, with a count query for the total.

Convenient and familiar, but the count scans the whole matching set: past a few hundred thousand rows, reach for QueryBuilder::cursor_paginate.

Source

pub async fn cursor_paginate( &self, db: &Database, column: &str, after: Option<&str>, per_page: i64, ) -> Result<CursorPage>

Fetch one page by cursor, ordered by a unique column.

No count and no offset, so the cost does not grow with the page number, and a row inserted while the reader pages through cannot cause another row to be skipped or repeated.

Trait Implementations§

Source§

impl Clone for QueryBuilder

Source§

fn clone(&self) -> QueryBuilder

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

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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> 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 = !

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.