pub struct SelectStatement { /* private fields */ }Expand description
SELECT statement builder
This struct provides a fluent API for constructing SELECT queries.
§Examples
use reinhardt_query::prelude::*;
let query = Query::select()
.column(Expr::col("id"))
.column(Expr::col("name"))
.from("users")
.and_where(Expr::col("active").eq(true))
.order_by("name", Order::Asc)
.limit(10);Implementations§
Source§impl SelectStatement
impl SelectStatement
Sourcepub fn take(&mut self) -> Self
pub fn take(&mut self) -> Self
Take the ownership of data in the current SelectStatement
Sourcepub fn column<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoColumnRef,
pub fn column<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoColumnRef,
Sourcepub fn columns<I, C>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = C>,
C: IntoColumnRef,
pub fn columns<I, C>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = C>,
C: IntoColumnRef,
Sourcepub fn expr<E>(&mut self, expr: E) -> &mut Selfwhere
E: Into<SimpleExpr>,
pub fn expr<E>(&mut self, expr: E) -> &mut Selfwhere
E: Into<SimpleExpr>,
Sourcepub fn from<T>(&mut self, tbl: T) -> &mut Selfwhere
T: IntoTableRef,
pub fn from<T>(&mut self, tbl: T) -> &mut Selfwhere
T: IntoTableRef,
Sourcepub fn from_as<T, A>(&mut self, tbl: T, alias: A) -> &mut Self
pub fn from_as<T, A>(&mut self, tbl: T, alias: A) -> &mut Self
Add a table with alias to the FROM clause
Equivalent to FROM table AS alias.
Sourcepub fn from_subquery(
&mut self,
query: SelectStatement,
alias: impl IntoIden,
) -> &mut Self
pub fn from_subquery( &mut self, query: SelectStatement, alias: impl IntoIden, ) -> &mut Self
Add a subquery to the FROM clause
Equivalent to FROM (SELECT ...) AS alias.
Sourcepub fn clear_selects(&mut self) -> &mut Self
pub fn clear_selects(&mut self) -> &mut Self
Clear all column selections
Sourcepub fn join<T, C>(&mut self, join: JoinType, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
pub fn join<T, C>(&mut self, join: JoinType, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
Sourcepub fn left_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
pub fn left_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
Sourcepub fn right_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
pub fn right_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
Add a RIGHT JOIN clause
Sourcepub fn full_outer_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
pub fn full_outer_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
Add a FULL OUTER JOIN clause
Sourcepub fn inner_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
pub fn inner_join<T, C>(&mut self, tbl: T, condition: C) -> &mut Selfwhere
T: IntoTableRef,
C: IntoCondition,
Add an INNER JOIN clause
Sourcepub fn cross_join<T>(&mut self, tbl: T) -> &mut Selfwhere
T: IntoTableRef,
pub fn cross_join<T>(&mut self, tbl: T) -> &mut Selfwhere
T: IntoTableRef,
Add a CROSS JOIN clause
Sourcepub fn and_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
pub fn and_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
Sourcepub fn cond_where(&mut self, condition: Condition) -> &mut Self
pub fn cond_where(&mut self, condition: Condition) -> &mut Self
Add a condition to the WHERE clause using Condition
Sourcepub fn group_by<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoColumnRef,
pub fn group_by<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoColumnRef,
Sourcepub fn group_by_col<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoColumnRef,
pub fn group_by_col<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoColumnRef,
Add a column to the GROUP BY clause (alias for group_by)
Sourcepub fn group_by_columns<I, C>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = C>,
C: IntoColumnRef,
pub fn group_by_columns<I, C>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = C>,
C: IntoColumnRef,
Add multiple GROUP BY columns
Sourcepub fn and_having<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
pub fn and_having<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
Sourcepub fn cond_having(&mut self, condition: Condition) -> &mut Self
pub fn cond_having(&mut self, condition: Condition) -> &mut Self
Add a condition to the HAVING clause using Condition
Sourcepub fn order_by<C>(&mut self, col: C, order: Order) -> &mut Selfwhere
C: IntoColumnRef,
pub fn order_by<C>(&mut self, col: C, order: Order) -> &mut Selfwhere
C: IntoColumnRef,
Sourcepub fn order_by_expr<E>(&mut self, expr: E, order: Order) -> &mut Selfwhere
E: Into<SimpleExpr>,
pub fn order_by_expr<E>(&mut self, expr: E, order: Order) -> &mut Selfwhere
E: Into<SimpleExpr>,
Add an ORDER BY clause with expression
Sourcepub fn distinct_on<I, C>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = C>,
C: IntoColumnRef,
pub fn distinct_on<I, C>(&mut self, cols: I) -> &mut Selfwhere
I: IntoIterator<Item = C>,
C: IntoColumnRef,
Set DISTINCT ON (PostgreSQL only)
Sourcepub fn union(&mut self, query: SelectStatement) -> &mut Self
pub fn union(&mut self, query: SelectStatement) -> &mut Self
Add a UNION clause
Sourcepub fn union_all(&mut self, query: SelectStatement) -> &mut Self
pub fn union_all(&mut self, query: SelectStatement) -> &mut Self
Add a UNION ALL clause
Sourcepub fn intersect(&mut self, query: SelectStatement) -> &mut Self
pub fn intersect(&mut self, query: SelectStatement) -> &mut Self
Add an INTERSECT clause
Sourcepub fn except(&mut self, query: SelectStatement) -> &mut Self
pub fn except(&mut self, query: SelectStatement) -> &mut Self
Add an EXCEPT clause
Sourcepub fn with_cte<N>(&mut self, name: N, query: SelectStatement) -> &mut Selfwhere
N: IntoIden,
pub fn with_cte<N>(&mut self, name: N, query: SelectStatement) -> &mut Selfwhere
N: IntoIden,
Add a Common Table Expression (CTE) to the WITH clause
§Examples
use reinhardt_query::prelude::*;
let cte = Query::select()
.column("id")
.column("name")
.from("users")
.and_where(Expr::col("active").eq(true));
let query = Query::select()
.with_cte("active_users", cte)
.column("*")
.from("active_users");Sourcepub fn with_recursive_cte<N>(
&mut self,
name: N,
query: SelectStatement,
) -> &mut Selfwhere
N: IntoIden,
pub fn with_recursive_cte<N>(
&mut self,
name: N,
query: SelectStatement,
) -> &mut Selfwhere
N: IntoIden,
Add a RECURSIVE Common Table Expression (CTE) to the WITH clause
§Examples
use reinhardt_query::prelude::*;
// Recursive CTE for hierarchical data
let cte = Query::select()
.column("id")
.column("parent_id")
.column("name")
.from("categories")
.and_where(Expr::col("parent_id").is_null())
.union_all(
Query::select()
.column(Expr::col(("c", "id")))
.column(Expr::col(("c", "parent_id")))
.column(Expr::col(("c", "name")))
.from_as("categories", "c")
.join(
JoinType::InnerJoin,
"category_tree",
Expr::col(("c", "parent_id")).eq(Expr::col(("category_tree", "id")))
)
);
let query = Query::select()
.with_recursive_cte("category_tree", cte)
.column("*")
.from("category_tree");Sourcepub fn window_as<T>(&mut self, name: T, window: WindowStatement) -> &mut Selfwhere
T: IntoIden,
pub fn window_as<T>(&mut self, name: T, window: WindowStatement) -> &mut Selfwhere
T: IntoIden,
Add a named window specification to the WINDOW clause
Named windows can be referenced by window functions using OVER window_name.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![Expr::col("department_id").into_simple_expr()],
order_by: vec![OrderExpr {
expr: Expr::col("salary").into_simple_expr(),
order: Order::Desc,
nulls: None,
}],
frame: None,
};
let query = Query::select()
.column("name")
.expr_as(Expr::row_number().over_named("w"), "rank")
.from("employees")
.window_as("w", window);Sourcepub fn lock_exclusive(&mut self) -> &mut Self
pub fn lock_exclusive(&mut self) -> &mut Self
Set FOR UPDATE lock
Set FOR SHARE lock
Sourcepub fn apply_if<T, F>(&mut self, val: Option<T>, func: F) -> &mut Selfwhere
F: FnOnce(&mut Self, T),
pub fn apply_if<T, F>(&mut self, val: Option<T>, func: F) -> &mut Selfwhere
F: FnOnce(&mut Self, T),
Apply a function conditionally
Sourcepub fn conditions<T, F>(
&mut self,
b: bool,
if_true: T,
if_false: F,
) -> &mut Self
pub fn conditions<T, F>( &mut self, b: bool, if_true: T, if_false: F, ) -> &mut Self
Conditional execution
Trait Implementations§
Source§impl Clone for SelectStatement
impl Clone for SelectStatement
Source§fn clone(&self) -> SelectStatement
fn clone(&self) -> SelectStatement
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more