Skip to main content

QueryAs

Struct QueryAs 

Source
pub struct QueryAs<T> { /* private fields */ }
Expand description

类型化裸 SQL 查询对象(SQLx query_as! 的 sz-orm 等效物)。

Query 的区别:QueryAs<T> 将行映射为 T: FromQueryResult, 提供类型安全的查询结果;Query 返回 Vec<HashMap<String, Value>>

§用法

use sz_orm_core::queryable::QueryAs;

#[derive(FromQueryResult)]
struct User { id: i64, name: String }

let q = QueryAs::<User>::new("SELECT id, name FROM users WHERE id = ?");
let users: Vec<User> = q.fetch_all(&mut conn).await?;
let one: User = QueryAs::<User>::new("SELECT id, name FROM users LIMIT 1")
    .fetch_one(&mut conn)
    .await?;

Implementations§

Source§

impl<T> QueryAs<T>

Source

pub fn new(sql: impl Into<String>) -> Self

从 SQL 字符串构造类型化查询

Source

pub fn sql(&self) -> &str

获取底层 SQL(用于日志/调试)

Source§

impl<T: FromQueryResult> QueryAs<T>

Source

pub async fn fetch_all( &self, conn: &mut dyn Connection, ) -> Result<Vec<T>, DbError>

执行查询,返回所有行映射为 Vec<T>

运行时列名验证:比对 DB 返回的列名与 T::row_desc()(由 #[derive(FromQueryResult)] 自动生成)。若 SQL SELECT 列不在 struct 字段中, 返回 DbError::QueryError,避免静默忽略多余列或类型不匹配。

运行时列类型验证(P0-2):比对 DB 返回值的实际类型与 T::column_types() 期望类型。若不兼容(如 DB 返回 TEXT 但 struct 期望 i64),返回 DbError::QueryError,防止静默类型截断。

Source

pub async fn fetch_one(&self, conn: &mut dyn Connection) -> Result<T, DbError>

执行查询,期望恰好一行;0 行或多行均返回错误

Source

pub async fn fetch_optional( &self, conn: &mut dyn Connection, ) -> Result<Option<T>, DbError>

执行查询,返回 0 或 1 行;0 行返回 Ok(None)

Trait Implementations§

Source§

impl<T> Display for QueryAs<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for QueryAs<T>

§

impl<T> RefUnwindSafe for QueryAs<T>
where T: RefUnwindSafe,

§

impl<T> Send for QueryAs<T>
where T: Send,

§

impl<T> Sync for QueryAs<T>
where T: Sync,

§

impl<T> Unpin for QueryAs<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for QueryAs<T>

§

impl<T> UnwindSafe for QueryAs<T>
where T: UnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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