Skip to main content

QueryBuilder

Struct QueryBuilder 

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

查询构建器

Implementations§

Source§

impl QueryBuilder

Source

pub fn new(table: String, engine: Arc<RwLock<MemoryEngine>>) -> Self

Source

pub fn eq(self, field: &str, value: DbValue) -> Self

Source

pub fn ne(self, field: &str, value: DbValue) -> Self

Source

pub fn lt(self, field: &str, value: DbValue) -> Self

Source

pub fn le(self, field: &str, value: DbValue) -> Self

Source

pub fn gt(self, field: &str, value: DbValue) -> Self

Source

pub fn ge(self, field: &str, value: DbValue) -> Self

Source

pub fn in_list(self, field: &str, values: Vec<DbValue>) -> Self

Source

pub fn contains(self, field: &str, value: &str) -> Self

Source

pub fn is_null(self, field: &str) -> Self

IS NULL 条件:查询字段为 NULL 的行

Source

pub fn is_not_null(self, field: &str) -> Self

IS NOT NULL 条件:查询字段不为 NULL 的行

Source

pub fn and(self, other: QueryBuilder) -> Self

Source

pub fn or(self, left: FilterExpr, right: FilterExpr) -> Self

OR 条件:将两个过滤条件用 OR 连接

§示例

忽略编译的示例,完整代码请参见测试文件:

// 查询 age > 18 OR status = 'vip' 的用户
db.query("users")
    .or(
        FilterExpr::Gt { field: "age".to_string(), value: DbValue::integer(18) },
        FilterExpr::Eq { field: "status".to_string(), value: DbValue::text("vip") }
    )
    .execute()?;
Source

pub fn not(self) -> NotBuilder

NOT 条件:对一个过滤条件取反

§示例

简单条件 - 只对下一个条件生效:

// 查询 age != 18 的用户
db.query("users")
    .not()
    .eq("age", DbValue::integer(18))
    .execute()?;

复杂条件 - 使用 expr() 传入 FilterExpr:

// 查询 NOT (age > 18 AND status = 'active')
db.query("users")
    .not()
    .expr(FilterExpr::And(
        Box::new(FilterExpr::Gt { field: "age".to_string(), value: DbValue::integer(18) }),
        Box::new(FilterExpr::Eq { field: "status".to_string(), value: DbValue::text("active") })
    ))
    .execute()?;
Source

pub fn where_expr(self, expr: FilterExpr) -> Self

添加任意过滤表达式

§示例

忽略编译的示例,完整代码请参见测试文件:

// 添加 OR 条件
db.query("users")
    .where_expr(FilterExpr::Or(
        Box::new(FilterExpr::Gt { field: "age".to_string(), value: DbValue::integer(18) }),
        Box::new(FilterExpr::Eq { field: "status".to_string(), value: DbValue::text("vip") })
    ))
    .execute()?;
Source

pub fn or_simple<F1, F2>(self, left_builder: F1, right_builder: F2) -> Self

构建 OR 条件(便捷方法)

§示例

忽略编译的示例,完整代码请参见测试文件:

// 查询 age > 18 OR age < 10 的用户
db.query("users")
    .or_simple(|q| q.gt("age", DbValue::integer(18)), |q| q.lt("age", DbValue::integer(10)))
    .execute()?;
Source

pub fn not_simple<F>(self, builder: F) -> Self

构建 NOT 条件(便捷方法)

§示例

忽略编译的示例,完整代码请参见测试文件:

// 查询 status != 'deleted' 的用户
db.query("users")
    .not_simple(|q| q.eq("status", DbValue::text("deleted")))
    .execute()?;
Source

pub fn order_by(self, field: &str, order: Order) -> Self

Source

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

Source

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

Source

pub fn inner_join( self, right_table: &str, left_field: &str, right_field: &str, ) -> Self

Source

pub fn left_join( self, right_table: &str, left_field: &str, right_field: &str, ) -> Self

Source

pub fn right_join( self, right_table: &str, left_field: &str, right_field: &str, ) -> Self

Source

pub fn full_join( self, right_table: &str, left_field: &str, right_field: &str, ) -> Self

Source

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

Source

pub fn count(self) -> Self

COUNT(*) - 统计行数

Source

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

COUNT(column) - 统计非 NULL 行数

Source

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

SUM(column) - 求和

Source

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

AVG(column) - 平均值

Source

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

MAX(column) - 最大值

Source

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

MIN(column) - 最小值

Source

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

设置聚合结果别名

Source

pub fn distinct(self) -> Self

SELECT DISTINCT - 去重查询

§示例
// 查询所有不同的部门
db.query("employees")
    .select(&["department"])
    .distinct()
    .execute()?;
Source

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

GROUP BY 子句

Source

pub fn having_eq(self, value: DbValue) -> Self

HAVING 子句 - 等于

Source

pub fn having_ne(self, value: DbValue) -> Self

HAVING 子句 - 不等于

Source

pub fn having_gt(self, value: DbValue) -> Self

HAVING 子句 - 大于

Source

pub fn having_ge(self, value: DbValue) -> Self

HAVING 子句 - 大于等于

Source

pub fn having_lt(self, value: DbValue) -> Self

HAVING 子句 - 小于

Source

pub fn having_le(self, value: DbValue) -> Self

HAVING 子句 - 小于等于

Source

pub fn execute(self) -> DbResult<Vec<Row>>

执行查询

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.