Skip to main content

QueryParams

Struct QueryParams 

Source
pub struct QueryParams {
    pub fields: Option<Vec<String>>,
    pub where_conditions: Vec<WhereCondition>,
    pub order_by: Vec<(String, SortOrder)>,
    pub page: Option<usize>,
    pub page_size: Option<usize>,
}
Expand description

查询参数结构体

包含完整的查询参数,包括字段选择、WHERE 条件、排序规则和分页参数。

§字段

  • fields:字段选择列表,None 表示选择所有字段
  • where_conditions:WHERE 条件列表
  • order_by:排序规则列表,元组格式为 (字段名, 排序方向)
  • page:当前页码,从 1 开始
  • page_size:每页大小

§示例

use yang_base::table::{QueryParams, WhereCondition, SortOrder};
use serde_json::json;

let mut params = QueryParams::new();

// 选择特定字段
params.fields = Some(vec!["id".to_string(), "name".to_string(), "email".to_string()]);

// 添加 WHERE 条件
params.where_conditions.push(WhereCondition::Eq {
    field: "status".to_string(),
    value: json!("active"),
});

params.where_conditions.push(WhereCondition::Gt {
    field: "age".to_string(),
    value: json!(18),
});

// 添加排序规则
params.order_by.push(("created_at".to_string(), SortOrder::Desc));
params.order_by.push(("name".to_string(), SortOrder::Asc));

// 设置分页参数
params.page = Some(1);
params.page_size = Some(20);

Fields§

§fields: Option<Vec<String>>

字段选择列表

  • None:选择所有字段
  • Some(vec):选择指定字段
§where_conditions: Vec<WhereCondition>

WHERE 条件列表

多个条件之间使用 AND 连接

§order_by: Vec<(String, SortOrder)>

排序规则列表

元组格式为 (字段名, 排序方向)

§page: Option<usize>

当前页码

从 1 开始,None 表示不分页

§page_size: Option<usize>

每页大小

None 表示使用默认值或不分页

Implementations§

Source§

impl QueryParams

Source

pub fn new() -> Self

创建新的查询参数

§返回值

返回空的查询参数实例

§示例
use yang_base::table::QueryParams;

let params = QueryParams::new();
assert!(params.fields.is_none());
assert!(params.where_conditions.is_empty());
assert!(params.order_by.is_empty());
assert!(params.page.is_none());
assert!(params.page_size.is_none());
Source

pub fn with_fields(self, fields: Vec<String>) -> Self

设置字段选择列表

§参数
  • fields:字段名列表
§返回值

返回 self 以支持链式调用

§示例
use yang_base::table::QueryParams;

let params = QueryParams::new()
    .with_fields(vec!["id".to_string(), "name".to_string()]);

assert_eq!(params.fields, Some(vec!["id".to_string(), "name".to_string()]));
Source

pub fn with_condition(self, condition: WhereCondition) -> Self

添加 WHERE 条件

§参数
  • condition:WHERE 条件
§返回值

返回 self 以支持链式调用

§示例
use yang_base::table::{QueryParams, WhereCondition};
use serde_json::json;

let params = QueryParams::new()
    .with_condition(WhereCondition::Eq {
        field: "status".to_string(),
        value: json!("active"),
    });

assert_eq!(params.where_conditions.len(), 1);
Source

pub fn with_order_by(self, field: String, order: SortOrder) -> Self

添加排序规则

§参数
  • field:字段名
  • order:排序方向
§返回值

返回 self 以支持链式调用

§示例
use yang_base::table::{QueryParams, SortOrder};

let params = QueryParams::new()
    .with_order_by("created_at".to_string(), SortOrder::Desc);

assert_eq!(params.order_by.len(), 1);
Source

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

设置分页参数

§参数
  • page:当前页码,从 1 开始
  • page_size:每页大小
§返回值

返回 self 以支持链式调用

§示例
use yang_base::table::QueryParams;

let params = QueryParams::new()
    .with_pagination(1, 20);

assert_eq!(params.page, Some(1));
assert_eq!(params.page_size, Some(20));
Source

pub fn normalize(&mut self)

归一化分页参数

将 page 中小于 1 的值(含 0)归一为 1,避免直接构造 QueryParams 时 page == 0 导致 LIMIT/OFFSET 计算下溢。page_size 保持原值。

§示例
use yang_base::table::QueryParams;

let mut params = QueryParams::new().with_pagination(0, 20);
params.normalize();
assert_eq!(params.page, Some(1));

Trait Implementations§

Source§

impl Clone for QueryParams

Source§

fn clone(&self) -> Self

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 QueryParams

Source§

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

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

impl Default for QueryParams

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for QueryParams

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for QueryParams

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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