Skip to main content

SelectQuery

Struct SelectQuery 

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

SELECT 查询构造器

Implementations§

Source§

impl SelectQuery

Source

pub fn new() -> Self

创建空的 SELECT 查询

Source

pub fn distinct(self) -> Self

设置 DISTINCT

Source

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

添加列

Source

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

添加多个列

Source

pub fn all_columns(self) -> Self

添加 *

Source

pub fn from(self, table: &str) -> Self

设置 FROM 表

Source

pub fn inner_join(self, table: &str, on: &str) -> Self

添加 INNER JOIN

§安全性(门禁 9 修复)

表名经 quote_ident() 转义。on 条件为表达式,调用方应确保不使用恶意输入构造。

Source

pub fn left_join(self, table: &str, on: &str) -> Self

添加 LEFT JOIN

§安全性(门禁 9 修复)

inner_join,表名经 quote_ident() 转义。

Source

pub fn right_join(self, table: &str, on: &str) -> Self

添加 RIGHT JOIN

§安全性(门禁 9 修复)

inner_join,表名经 quote_ident() 转义。

Source

pub fn where_clause(self, condition: &str) -> Self

添加 WHERE 条件(AND 连接)

§安全性(v0.2.2 修复 C-6)

调用 check_where_injection 检测高危模式(分号+SQL 关键字、行注释、块注释)。 复杂 WHERE 条件应使用参数化查询 API,避免直接拼接字符串。

Source

pub fn or_where(self, condition: &str) -> Self

添加 OR WHERE 条件

§安全性(v0.2.2 修复 C-6)

where_clause,调用 check_where_injection 检测高危模式。

Source

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

添加 GROUP BY

Source

pub fn having(self, condition: &str) -> Self

添加 HAVING

Source

pub fn order_by(self, column: &str, asc: bool) -> Self

添加 ORDER BY

§参数
  • column: 列名
  • asc: true=ASC, false=DESC
Source

pub fn limit(self, n: u64) -> Self

设置 LIMIT

Source

pub fn offset(self, n: u64) -> Self

设置 OFFSET

Source

pub fn paginate(self, page: u64, size: u64) -> Self

生成分页(同时设置 LIMIT 和 OFFSET)

§参数
  • page: 页码(从 1 开始)
  • size: 每页大小
Source

pub fn build(self, db_type: DbType) -> String

生成 SQL

§参数
  • db_type: 数据库类型,用于选择方言

Trait Implementations§

Source§

impl Clone for SelectQuery

Source§

fn clone(&self) -> SelectQuery

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 SelectQuery

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for SelectQuery

Source§

fn default() -> SelectQuery

Returns the “default value” for a type. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more