pub struct SelectQuery { /* private fields */ }Expand description
SELECT query builder
Implementations§
Source§impl SelectQuery
impl SelectQuery
Sourcepub fn all_columns(self) -> Self
pub fn all_columns(self) -> Self
Add a * column
Sourcepub fn from_subquery(self, subquery_sql: &str, alias: &str) -> Self
pub fn from_subquery(self, subquery_sql: &str, alias: &str) -> Self
Set FROM subquery: FROM (<subquery_sql>) AS <alias>
Mutually exclusive with from; later caller overrides earlier.
The subquery SQL is constructed by the caller (may be generated by another
SelectQuery::build), and the alias is escaped via dialect.quote() to
prevent identifier escape.
§Example
use sz_orm_core::DbType;
use sz_orm_query_builder::Query;
let inner = Query::select()
.column("id")
.column("amount")
.from("orders")
.build(DbType::MySQL);
let sql = Query::select()
.column("id")
.from_subquery(&inner, "t")
.build(DbType::MySQL);
assert!(sql.contains("FROM (SELECT `id`, `amount` FROM `orders`) AS `t`"));Sourcepub fn inner_join(self, table: &str, on: &str) -> Self
pub fn inner_join(self, table: &str, on: &str) -> Self
Add an INNER JOIN
§Security (gate 9 fix)
The table name is escaped via quote_ident(). The on condition is an
expression; the caller should ensure it is not constructed with malicious input.
Sourcepub fn left_join(self, table: &str, on: &str) -> Self
pub fn left_join(self, table: &str, on: &str) -> Self
Add a LEFT JOIN
§Security (gate 9 fix)
Same as inner_join; the table name is escaped via quote_ident().
Sourcepub fn right_join(self, table: &str, on: &str) -> Self
pub fn right_join(self, table: &str, on: &str) -> Self
Add a RIGHT JOIN
§Security (gate 9 fix)
Same as inner_join; the table name is escaped via quote_ident().
Sourcepub fn inner_join_on(self, table: &str, left_col: &str, right_col: &str) -> Self
pub fn inner_join_on(self, table: &str, left_col: &str, right_col: &str) -> Self
Add an INNER JOIN with a column-to-column equality ON condition (left_col = right_col)
§Security
Column names are escaped per dialect via quote_column_dialect, preventing identifier escape.
No parameter values; pure identifier join, the most common and safest JOIN form.
Sourcepub fn left_join_on(self, table: &str, left_col: &str, right_col: &str) -> Self
pub fn left_join_on(self, table: &str, left_col: &str, right_col: &str) -> Self
Add a LEFT JOIN with a column-to-column equality ON condition
Sourcepub fn right_join_on(self, table: &str, left_col: &str, right_col: &str) -> Self
pub fn right_join_on(self, table: &str, left_col: &str, right_col: &str) -> Self
Add a RIGHT JOIN with a column-to-column equality ON condition
Sourcepub fn inner_join_param(
self,
table: &str,
left_col: &str,
op_expr: &str,
value: Value,
) -> Self
pub fn inner_join_param( self, table: &str, left_col: &str, op_expr: &str, value: Value, ) -> Self
Add an INNER JOIN with a parameterized expression ON condition (left_col op ?)
§Parameters
table: JOIN table name (supports aliasorders o)left_col: left column name (already escaped)op_expr: operator + placeholder part (e.g.,= ?,> ?,IN (?, ?))value: single parameter value
Sourcepub fn left_join_param(
self,
table: &str,
left_col: &str,
op_expr: &str,
value: Value,
) -> Self
pub fn left_join_param( self, table: &str, left_col: &str, op_expr: &str, value: Value, ) -> Self
Add a LEFT JOIN with a parameterized expression ON condition
Sourcepub fn right_join_param(
self,
table: &str,
left_col: &str,
op_expr: &str,
value: Value,
) -> Self
pub fn right_join_param( self, table: &str, left_col: &str, op_expr: &str, value: Value, ) -> Self
Add a RIGHT JOIN with a parameterized expression ON condition
Sourcepub fn where_clause(self, condition: &str) -> Self
pub fn where_clause(self, condition: &str) -> Self
Add a WHERE condition (AND joined)
§Security (v0.2.2 fix C-6)
Calls check_where_injection to detect high-risk patterns (semicolon + SQL keyword,
line comments, block comments). Complex WHERE conditions should use the parameterized
query API to avoid direct string concatenation.
Sourcepub fn where_like(self, column: &str, pattern: Value) -> Self
pub fn where_like(self, column: &str, pattern: Value) -> Self
Add a column LIKE ? AND condition
Sourcepub fn where_in(self, column: &str, values: Vec<Value>) -> Self
pub fn where_in(self, column: &str, values: Vec<Value>) -> Self
Add a column IN (?, ?, ...) AND condition
An empty list produces 1 = 0 (always false), avoiding an invalid IN ().
Sourcepub fn where_not_in(self, column: &str, values: Vec<Value>) -> Self
pub fn where_not_in(self, column: &str, values: Vec<Value>) -> Self
Add a column NOT IN (?, ?, ...) AND condition
An empty list produces 1 = 1 (always true), avoiding an invalid NOT IN ().
Sourcepub fn where_between(self, column: &str, low: Value, high: Value) -> Self
pub fn where_between(self, column: &str, low: Value, high: Value) -> Self
Add a column BETWEEN ? AND ? AND condition
Sourcepub fn where_null(self, column: &str) -> Self
pub fn where_null(self, column: &str) -> Self
Add a column IS NULL AND condition
Sourcepub fn where_not_null(self, column: &str) -> Self
pub fn where_not_null(self, column: &str) -> Self
Add a column IS NOT NULL AND condition
Sourcepub fn or_where_eq(self, column: &str, value: Value) -> Self
pub fn or_where_eq(self, column: &str, value: Value) -> Self
Add a column = ? OR condition
Sourcepub fn or_where_ne(self, column: &str, value: Value) -> Self
pub fn or_where_ne(self, column: &str, value: Value) -> Self
Add a column <> ? OR condition
Sourcepub fn or_where_gt(self, column: &str, value: Value) -> Self
pub fn or_where_gt(self, column: &str, value: Value) -> Self
Add a column > ? OR condition
Sourcepub fn or_where_ge(self, column: &str, value: Value) -> Self
pub fn or_where_ge(self, column: &str, value: Value) -> Self
Add a column >= ? OR condition
Sourcepub fn or_where_lt(self, column: &str, value: Value) -> Self
pub fn or_where_lt(self, column: &str, value: Value) -> Self
Add a column < ? OR condition
Sourcepub fn or_where_le(self, column: &str, value: Value) -> Self
pub fn or_where_le(self, column: &str, value: Value) -> Self
Add a column <= ? OR condition
Sourcepub fn or_where_like(self, column: &str, pattern: Value) -> Self
pub fn or_where_like(self, column: &str, pattern: Value) -> Self
Add a column LIKE ? OR condition
Sourcepub fn or_where_in(self, column: &str, values: Vec<Value>) -> Self
pub fn or_where_in(self, column: &str, values: Vec<Value>) -> Self
Add a column IN (?, ?, ...) OR condition
Sourcepub fn or_where_between(self, column: &str, low: Value, high: Value) -> Self
pub fn or_where_between(self, column: &str, low: Value, high: Value) -> Self
Add a column BETWEEN ? AND ? OR condition
Sourcepub fn or_where_null(self, column: &str) -> Self
pub fn or_where_null(self, column: &str) -> Self
Add a column IS NULL OR condition
Sourcepub fn or_where_not_null(self, column: &str) -> Self
pub fn or_where_not_null(self, column: &str) -> Self
Add a column IS NOT NULL OR condition
Sourcepub fn paginate(self, page: u64, size: u64) -> Self
pub fn paginate(self, page: u64, size: u64) -> Self
Generate pagination (sets both LIMIT and OFFSET)
§Parameters
page: page number (1-based)size: page size
Sourcepub fn with_cte(self, name: &str, subquery: &str) -> Self
pub fn with_cte(self, name: &str, subquery: &str) -> Self
Add a CTE (Common Table Expression / WITH clause).
Generates SQL of the form WITH name AS (subquery) SELECT ....
§Parameters
name: CTE namesubquery: subquery SQL (a complete SELECT statement)
Sourcepub fn with_recursive_cte(self, name: &str, subquery: &str) -> Self
pub fn with_recursive_cte(self, name: &str, subquery: &str) -> Self
Add a recursive CTE (WITH RECURSIVE name AS (...) SELECT ...).
§Parameters
name: CTE namesubquery: recursive subquery SQL
Sourcepub fn window_function(self, expr: &str) -> Self
pub fn window_function(self, expr: &str) -> Self
Add a window function column (as a raw expression in the SELECT list).
The caller is responsible for constructing the complete window function expression, e.g.:
ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC)RANK() OVER (ORDER BY score DESC)SUM(amount) OVER (PARTITION BY user_id ORDER BY created_at)
§Parameters
expr: complete window function expression
Sourcepub fn row_number(self, partition_by: &str, order_by: &str, alias: &str) -> Self
pub fn row_number(self, partition_by: &str, order_by: &str, alias: &str) -> Self
Add a ROW_NUMBER() window function column.
§Parameters
partition_by: PARTITION BY column (may be empty)order_by: ORDER BY column (e.g.,salary DESC)alias: result column alias (e.g.,row_num)
Sourcepub fn rank(self, partition_by: &str, order_by: &str, alias: &str) -> Self
pub fn rank(self, partition_by: &str, order_by: &str, alias: &str) -> Self
Add a RANK() window function column.
§Parameters
partition_by: PARTITION BY column (may be empty)order_by: ORDER BY columnalias: result column alias
Sourcepub fn dense_rank(self, partition_by: &str, order_by: &str, alias: &str) -> Self
pub fn dense_rank(self, partition_by: &str, order_by: &str, alias: &str) -> Self
Add a DENSE_RANK() window function column.
§Parameters
partition_by: PARTITION BY column (may be empty)order_by: ORDER BY columnalias: result column alias
Sourcepub fn for_update(self) -> Self
pub fn for_update(self) -> Self
Set FOR UPDATE row lock.
Appends FOR UPDATE to the end of the generated SQL, used for pessimistic lock.
Sourcepub fn for_update_with_options(self, options: &str) -> Self
pub fn for_update_with_options(self, options: &str) -> Self
Set FOR UPDATE with options (e.g., NOWAIT, SKIP LOCKED).
§Parameters
options: options string, e.g.,"NOWAIT"or"SKIP LOCKED"
Sourcepub fn union(self, other: SelectQuery) -> SetQuery
pub fn union(self, other: SelectQuery) -> SetQuery
Combine the current query with another query using UNION set operation.
Returns a SetQuery, which can be turned into the final SQL via build().
Sourcepub fn union_all(self, other: SelectQuery) -> SetQuery
pub fn union_all(self, other: SelectQuery) -> SetQuery
Combine the current query with another query using UNION ALL set operation.
Sourcepub fn intersect(self, other: SelectQuery) -> SetQuery
pub fn intersect(self, other: SelectQuery) -> SetQuery
Combine the current query with another query using INTERSECT set operation.
Sourcepub fn except(self, other: SelectQuery) -> SetQuery
pub fn except(self, other: SelectQuery) -> SetQuery
Combine the current query with another query using EXCEPT set operation.
Sourcepub fn build_with_params(self, db_type: DbType) -> BuiltQuery
pub fn build_with_params(self, db_type: DbType) -> BuiltQuery
Generate parameterized SQL (parameterized query, P0 fix: SQL injection prevention)
Returns a BuiltQuery, containing a SQL string with ? placeholders and a
list of parameters bound in order. Differences from build:
- WHERE conditions can come from parameterized APIs like
where_eq/where_in/where_between - User input is bound as parameters rather than concatenated into the SQL string
§Mixed usage rules
When using both raw where_clause(&str) and parameterized where_eq(column, value):
- Raw conditions render first (no parameters)
- Parameterized conditions render after (parameters collected in order)
- Both preserve AND/OR conjunction semantics in call order
§Example
use sz_orm_core::{DbType, Value};
use sz_orm_query_builder::Query;
let built = Query::select()
.column("id")
.from("users")
.where_eq("age", Value::I32(18))
.or_where_eq("role", Value::String("admin".into()))
.build_with_params(DbType::MySQL);
assert!(built.sql.contains("WHERE `age` = ? OR `role` = ?"));
assert_eq!(built.params.len(), 2);Trait Implementations§
Source§impl Clone for SelectQuery
impl Clone for SelectQuery
Source§fn clone(&self) -> SelectQuery
fn clone(&self) -> SelectQuery
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SelectQuery
impl Debug for SelectQuery
Source§impl Default for SelectQuery
impl Default for SelectQuery
Source§fn default() -> SelectQuery
fn default() -> SelectQuery
Auto Trait Implementations§
impl Freeze for SelectQuery
impl RefUnwindSafe for SelectQuery
impl Send for SelectQuery
impl Sync for SelectQuery
impl Unpin for SelectQuery
impl UnsafeUnpin for SelectQuery
impl UnwindSafe for SelectQuery
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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