Skip to main content

Db

Struct Db 

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

快捷查询入口(think-orm Db::name() 风格)

不要求定义 Model,仅靠表名 + 方言即可生成 SQL。

Implementations§

Source§

impl Db

Source

pub fn new(dialect: Box<dyn Dialect>) -> Self

创建快捷查询入口

Source

pub fn name(self, table: impl Into<String>) -> Self

指定表名(等价于 think-orm 的 Db::name('user')

Source

pub fn select(self, columns: Vec<&str>) -> Self

选择列

Source

pub fn where_cond(self, condition: impl Into<String>) -> Self

👎Deprecated since 1.3.0:

P0-2: 字符串拼接存在 SQL 注入风险,请使用 where_eq/where_ne/where_gt/where_lt/where_like 等参数化方法

WHERE 条件(AND)— 字符串拼接,存在 SQL 注入风险

⚠️ 已废弃:请使用 where_eq / where_gt 等参数化方法替代。

Source

pub fn or_where(self, condition: impl Into<String>) -> Self

👎Deprecated since 1.3.0:

P0-2: 字符串拼接存在 SQL 注入风险,请使用 or_where_eq/or_where_ne/or_where_gt 等参数化方法

WHERE 条件(OR)— 字符串拼接,存在 SQL 注入风险

⚠️ 已废弃:请使用 or_where_eq / or_where_gt 等参数化方法替代。

Source

pub fn where_eq(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化等值条件 field = ?(AND 关系)

Source

pub fn where_ne(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化不等条件 field != ?(AND 关系)

Source

pub fn where_gt(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化大于条件 field > ?(AND 关系)

Source

pub fn where_ge(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化大于等于条件 field >= ?(AND 关系)

Source

pub fn where_lt(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化小于条件 field < ?(AND 关系)

Source

pub fn where_le(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化小于等于条件 field <= ?(AND 关系)

Source

pub fn where_like(self, field: impl Into<String>, pattern: Value) -> Self

P0-2:参数化 LIKE 条件 field LIKE ?(AND 关系)

Source

pub fn or_where_eq(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化 OR 等值条件 OR field = ?

Source

pub fn or_where_ne(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化 OR 不等条件 OR field != ?

Source

pub fn or_where_gt(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化 OR 大于条件 OR field > ?

Source

pub fn or_where_ge(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化 OR 大于等于条件 OR field >= ?

Source

pub fn or_where_lt(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化 OR 小于条件 OR field < ?

Source

pub fn or_where_le(self, field: impl Into<String>, value: Value) -> Self

P0-2:参数化 OR 小于等于条件 OR field <= ?

Source

pub fn or_where_like(self, field: impl Into<String>, pattern: Value) -> Self

P0-2:参数化 OR LIKE 条件 OR field LIKE ?

Source

pub fn where_in(self, field: impl Into<String>, values: Vec<Value>) -> Self

WHERE IN

Source

pub fn where_not_in(self, field: impl Into<String>, values: Vec<Value>) -> Self

WHERE NOT IN

Source

pub fn where_between( self, field: impl Into<String>, start: Value, end: Value, ) -> Self

WHERE BETWEEN

Source

pub fn where_null(self, field: impl Into<String>) -> Self

WHERE IS NULL

Source

pub fn where_not_null(self, field: impl Into<String>) -> Self

WHERE IS NOT NULL

Source

pub fn order_by(self, field: impl Into<String>) -> Self

ORDER BY field ASC

Source

pub fn order_desc(self, field: impl Into<String>) -> Self

ORDER BY field DESC

Source

pub fn group_by(self, field: impl Into<String>) -> Self

GROUP BY

Source

pub fn having(self, condition: impl Into<String>) -> Self

HAVING

Source

pub fn limit(self, limit: usize) -> Self

LIMIT

Source

pub fn offset(self, offset: usize) -> Self

OFFSET

Source

pub fn page(self, page: usize, page_size: usize) -> Self

分页(page 从 1 开始)

Source

pub fn join_inner( self, table: impl Into<String>, on_left: impl Into<String>, on_right: impl Into<String>, ) -> Self

INNER JOIN

Source

pub fn join_left( self, table: impl Into<String>, on_left: impl Into<String>, on_right: impl Into<String>, ) -> Self

LEFT JOIN

Source

pub fn join_right( self, table: impl Into<String>, on_left: impl Into<String>, on_right: impl Into<String>, ) -> Self

RIGHT JOIN

Source

pub fn build_select(&self) -> String

构建 SELECT SQL

Source

pub fn build_insert(&self, data: &HashMap<String, Value>) -> String

构建 INSERT SQL

Source

pub fn build_update(&self, data: &HashMap<String, Value>) -> String

构建 UPDATE SQL

Source

pub fn build_delete(&self) -> String

构建 DELETE SQL

Source

pub fn build_count(&self) -> String

构建 COUNT SQL

Source

pub fn build_exists(&self) -> String

构建 EXISTS SQL

Source

pub fn build_max(&self, field: &str) -> String

构建 MAX SQL

Source

pub fn build_min(&self, field: &str) -> String

构建 MIN SQL

Source

pub fn build_sum(&self, field: &str) -> String

构建 SUM SQL

Source

pub fn build_avg(&self, field: &str) -> String

构建 AVG SQL

Auto Trait Implementations§

§

impl !RefUnwindSafe for Db

§

impl !UnwindSafe for Db

§

impl Freeze for Db

§

impl Send for Db

§

impl Sync for Db

§

impl Unpin for Db

§

impl UnsafeUnpin for Db

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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