pub struct SelectQuery { /* private fields */ }Expand description
SELECT 查询构造器
Implementations§
Source§impl SelectQuery
impl SelectQuery
Sourcepub fn all_columns(self) -> Self
pub fn all_columns(self) -> Self
添加 * 列
Sourcepub fn from_subquery(self, subquery_sql: &str, alias: &str) -> Self
pub fn from_subquery(self, subquery_sql: &str, alias: &str) -> Self
设置 FROM 子查询:FROM (<subquery_sql>) AS <alias>
与 from 互斥,后调用者覆盖前者。
子查询 SQL 由调用方负责构造(可由另一个 SelectQuery::build 生成),
别名经 dialect.quote() 转义,防止标识符逃逸。
§示例
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
Sourcepub fn right_join(self, table: &str, on: &str) -> Self
pub fn right_join(self, table: &str, on: &str) -> Self
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
添加 INNER JOIN,ON 条件为列对列等值连接(left_col = right_col)
§安全性
列名经 quote_column_dialect 按方言转义,防止标识符逃逸。
无参数值,纯标识符连接,最常见且最安全的 JOIN 形式。
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
添加 LEFT JOIN,ON 条件为列对列等值连接
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
添加 RIGHT JOIN,ON 条件为列对列等值连接
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
添加 INNER JOIN,ON 条件为参数化表达式(left_col op ?)
§参数
table: JOIN 的表名(支持别名orders o)left_col: 左侧列名(已转义)op_expr: 运算符 + 占位符部分(如= ?、> ?、IN (?, ?))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
添加 LEFT JOIN,ON 条件为参数化表达式
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
添加 RIGHT JOIN,ON 条件为参数化表达式
Sourcepub fn where_clause(self, condition: &str) -> Self
pub fn where_clause(self, condition: &str) -> Self
添加 WHERE 条件(AND 连接)
§安全性(v0.2.2 修复 C-6)
调用 check_where_injection 检测高危模式(分号+SQL 关键字、行注释、块注释)。
复杂 WHERE 条件应使用参数化查询 API,避免直接拼接字符串。
Sourcepub fn where_like(self, column: &str, pattern: Value) -> Self
pub fn where_like(self, column: &str, pattern: Value) -> Self
添加 column LIKE ? AND 条件
Sourcepub fn where_in(self, column: &str, values: Vec<Value>) -> Self
pub fn where_in(self, column: &str, values: Vec<Value>) -> Self
添加 column IN (?, ?, ...) AND 条件
空列表生成 1 = 0(恒假),避免生成非法的 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
添加 column NOT IN (?, ?, ...) AND 条件
空列表生成 1 = 1(恒真),避免生成非法的 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
添加 column BETWEEN ? AND ? AND 条件
Sourcepub fn where_null(self, column: &str) -> Self
pub fn where_null(self, column: &str) -> Self
添加 column IS NULL AND 条件
Sourcepub fn where_not_null(self, column: &str) -> Self
pub fn where_not_null(self, column: &str) -> Self
添加 column IS NOT NULL AND 条件
Sourcepub fn or_where_eq(self, column: &str, value: Value) -> Self
pub fn or_where_eq(self, column: &str, value: Value) -> Self
添加 column = ? OR 条件
Sourcepub fn or_where_ne(self, column: &str, value: Value) -> Self
pub fn or_where_ne(self, column: &str, value: Value) -> Self
添加 column <> ? OR 条件
Sourcepub fn or_where_gt(self, column: &str, value: Value) -> Self
pub fn or_where_gt(self, column: &str, value: Value) -> Self
添加 column > ? OR 条件
Sourcepub fn or_where_ge(self, column: &str, value: Value) -> Self
pub fn or_where_ge(self, column: &str, value: Value) -> Self
添加 column >= ? OR 条件
Sourcepub fn or_where_lt(self, column: &str, value: Value) -> Self
pub fn or_where_lt(self, column: &str, value: Value) -> Self
添加 column < ? OR 条件
Sourcepub fn or_where_le(self, column: &str, value: Value) -> Self
pub fn or_where_le(self, column: &str, value: Value) -> Self
添加 column <= ? OR 条件
Sourcepub fn or_where_like(self, column: &str, pattern: Value) -> Self
pub fn or_where_like(self, column: &str, pattern: Value) -> Self
添加 column LIKE ? OR 条件
Sourcepub fn or_where_in(self, column: &str, values: Vec<Value>) -> Self
pub fn or_where_in(self, column: &str, values: Vec<Value>) -> Self
添加 column IN (?, ?, ...) OR 条件
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
添加 column BETWEEN ? AND ? OR 条件
Sourcepub fn or_where_null(self, column: &str) -> Self
pub fn or_where_null(self, column: &str) -> Self
添加 column IS NULL OR 条件
Sourcepub fn or_where_not_null(self, column: &str) -> Self
pub fn or_where_not_null(self, column: &str) -> Self
添加 column IS NOT NULL OR 条件
Sourcepub fn with_cte(self, name: &str, subquery: &str) -> Self
pub fn with_cte(self, name: &str, subquery: &str) -> Self
添加 CTE(Common Table Expression / WITH 子句)。
生成形如 WITH name AS (subquery) SELECT ... 的 SQL。
§参数
name: CTE 名称subquery: 子查询 SQL(完整的 SELECT 语句)
Sourcepub fn with_recursive_cte(self, name: &str, subquery: &str) -> Self
pub fn with_recursive_cte(self, name: &str, subquery: &str) -> Self
Sourcepub fn window_function(self, expr: &str) -> Self
pub fn window_function(self, expr: &str) -> Self
添加窗口函数列(作为 SELECT 列表的原始表达式)。
调用方负责构造完整的窗口函数表达式,例如:
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)
§参数
expr: 完整的窗口函数表达式
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
添加 ROW_NUMBER() 窗口函数列。
§参数
partition_by: PARTITION BY 列(可为空)order_by: ORDER BY 列(如salary DESC)alias: 结果列别名(如row_num)
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
Sourcepub fn for_update(self) -> Self
pub fn for_update(self) -> Self
设置 FOR UPDATE 行锁。
在生成的 SQL 末尾追加 FOR UPDATE,用于悲观锁。
Sourcepub fn for_update_with_options(self, options: &str) -> Self
pub fn for_update_with_options(self, options: &str) -> Self
Sourcepub fn union(self, other: SelectQuery) -> SetQuery
pub fn union(self, other: SelectQuery) -> SetQuery
将当前查询与另一个查询进行 UNION 集合运算。
返回一个 SetQuery,可通过 build() 生成最终 SQL。
Sourcepub fn union_all(self, other: SelectQuery) -> SetQuery
pub fn union_all(self, other: SelectQuery) -> SetQuery
将当前查询与另一个查询进行 UNION ALL 集合运算。
Sourcepub fn intersect(self, other: SelectQuery) -> SetQuery
pub fn intersect(self, other: SelectQuery) -> SetQuery
将当前查询与另一个查询进行 INTERSECT 集合运算。
Sourcepub fn except(self, other: SelectQuery) -> SetQuery
pub fn except(self, other: SelectQuery) -> SetQuery
将当前查询与另一个查询进行 EXCEPT 集合运算。
Sourcepub fn build_with_params(self, db_type: DbType) -> BuiltQuery
pub fn build_with_params(self, db_type: DbType) -> BuiltQuery
生成带参数的 SQL(参数化查询,P0 修复:SQL 注入防护)
返回 BuiltQuery,包含带 ? 占位符的 SQL 与按序绑定的参数列表。
与 build 的区别:
- WHERE 条件可来自
where_eq/where_in/where_between等参数化 API - 用户输入作为参数绑定,而非字符串拼接到 SQL 中
§混合使用规则
当同时使用原始 where_clause(&str) 与参数化 where_eq(column, value) 时:
- 原始条件先渲染(无参数)
- 参数化条件后渲染(按顺序收集参数)
- 两者均按调用顺序保持 AND/OR 连接语义
§示例
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
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