pub struct QueryBuilder { /* private fields */ }Expand description
A statement being assembled.
Implementations§
Source§impl QueryBuilder
impl QueryBuilder
pub fn new(table: impl Into<String>) -> Self
Sourcepub fn select(self, columns: &[&str]) -> Self
pub fn select(self, columns: &[&str]) -> Self
Choose columns. Without this the query selects everything.
pub fn distinct(self) -> Self
Sourcepub fn filter_op(
self,
column: &str,
operator: &str,
value: impl Into<Value>,
) -> Self
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.
pub fn or_filter(self, column: &str, value: impl Into<Value>) -> Self
Sourcepub fn or_filter_op(
self,
column: &str,
operator: &str,
value: impl Into<Value>,
) -> Self
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.
Sourcepub fn or_filter_like(self, column: &str, pattern: impl Into<Value>) -> Self
pub fn or_filter_like(self, column: &str, pattern: impl Into<Value>) -> Self
or column like pattern.
pub fn filter_in(self, column: &str, values: Vec<Value>) -> Self
pub fn filter_not_in(self, column: &str, values: Vec<Value>) -> Self
pub fn filter_null(self, column: &str) -> Self
pub fn filter_not_null(self, column: &str) -> Self
pub fn filter_between( self, column: &str, low: impl Into<Value>, high: impl Into<Value>, ) -> Self
Sourcepub fn filter_like(self, column: &str, pattern: impl Into<Value>) -> Self
pub fn filter_like(self, column: &str, pattern: impl Into<Value>) -> Self
where column like pattern.
Sourcepub fn group_filter(
self,
build: impl FnOnce(QueryBuilder) -> QueryBuilder,
) -> Self
pub fn group_filter( self, build: impl FnOnce(QueryBuilder) -> QueryBuilder, ) -> Self
A parenthesised group of conditions.
pub fn or_group_filter( self, build: impl FnOnce(QueryBuilder) -> QueryBuilder, ) -> Self
pub fn join(self, table: &str, left: &str, operator: &str, right: &str) -> Self
pub fn left_join( self, table: &str, left: &str, operator: &str, right: &str, ) -> Self
pub fn order_by(self, column: &str, direction: Direction) -> Self
pub fn latest(self, column: &str) -> Self
pub fn group_by(self, columns: &[&str]) -> Self
pub fn limit(self, limit: i64) -> Self
pub fn offset(self, offset: i64) -> Self
Sourcepub fn to_sql(&self, dialect: &dyn Dialect) -> Result<(String, Vec<Value>)>
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.
Sourcepub fn to_count_sql(
&self,
dialect: &dyn Dialect,
) -> Result<(String, Vec<Value>)>
pub fn to_count_sql( &self, dialect: &dyn Dialect, ) -> Result<(String, Vec<Value>)>
The select count(*) form of this query, ignoring order and paging.
Sourcepub async fn get(&self, db: &Database) -> Result<Vec<Row>>
pub async fn get(&self, db: &Database) -> Result<Vec<Row>>
Run the query and return every matching row.
Sourcepub async fn first(&self, db: &Database) -> Result<Option<Row>>
pub async fn first(&self, db: &Database) -> Result<Option<Row>>
The first matching row, if any.
Sourcepub async fn get_json(&self, db: &Database) -> Result<Json>
pub async fn get_json(&self, db: &Database) -> Result<Json>
Rows as a JSON array, ready to return from an API handler.
pub async fn count(&self, db: &Database) -> Result<i64>
pub async fn exists(&self, db: &Database) -> Result<bool>
Sourcepub async fn insert(
&self,
db: &Database,
values: &[(&str, Value)],
) -> Result<i64>
pub async fn insert( &self, db: &Database, values: &[(&str, Value)], ) -> Result<i64>
Insert one row, returning its id.
Sourcepub async fn insert_returning(
&self,
db: &Database,
values: &[(&str, Value)],
column: &str,
) -> Result<Value>
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.
Sourcepub async fn insert_without_id(
&self,
db: &Database,
values: &[(&str, Value)],
) -> Result<u64>
pub async fn insert_without_id( &self, db: &Database, values: &[(&str, Value)], ) -> Result<u64>
Insert one row without asking for a generated key.
Sourcepub async fn insert_many(
&self,
db: &Database,
rows: &[Vec<(String, Value)>],
) -> Result<u64>
pub async fn insert_many( &self, db: &Database, rows: &[Vec<(String, Value)>], ) -> Result<u64>
Insert many rows in one statement.
Sourcepub async fn update(
&self,
db: &Database,
values: &[(&str, Value)],
) -> Result<u64>
pub async fn update( &self, db: &Database, values: &[(&str, Value)], ) -> Result<u64>
Update the rows this query matches. Requires a filter.
Sourcepub async fn delete(&self, db: &Database) -> Result<u64>
pub async fn delete(&self, db: &Database) -> Result<u64>
Delete the rows this query matches. Requires a filter.
Sourcepub async fn delete_all(&self, db: &Database) -> Result<u64>
pub async fn delete_all(&self, db: &Database) -> Result<u64>
Delete every row in the table. Deliberately separate from delete.
Sourcepub async fn get_in(&self, tx: &mut Transaction) -> Result<Vec<Row>>
pub async fn get_in(&self, tx: &mut Transaction) -> Result<Vec<Row>>
get, inside a transaction.
Sourcepub async fn first_in(&self, tx: &mut Transaction) -> Result<Option<Row>>
pub async fn first_in(&self, tx: &mut Transaction) -> Result<Option<Row>>
first, inside a transaction.
Sourcepub async fn insert_in(
&self,
tx: &mut Transaction,
values: &[(&str, Value)],
) -> Result<u64>
pub async fn insert_in( &self, tx: &mut Transaction, values: &[(&str, Value)], ) -> Result<u64>
insert_without_id, inside a transaction.
Source§impl QueryBuilder
impl QueryBuilder
Sourcepub async fn paginate(
&self,
db: &Database,
page: i64,
per_page: i64,
) -> Result<Page>
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.
Sourcepub async fn cursor_paginate(
&self,
db: &Database,
column: &str,
after: Option<&str>,
per_page: i64,
) -> Result<CursorPage>
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
impl Clone for QueryBuilder
Source§fn clone(&self) -> QueryBuilder
fn clone(&self) -> QueryBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more